chname(8) allows to run specified command with new hostname, e.g. for running something in chroot and distinguishing it later in syslog. Implemented by using unshare(CLONE_NEWUTS) and hence requires Linux 2.6.19. Signed-off-by: Mikhail Gusarov <dottedmag@xxxxxxxxxxxxx> --- sys-utils/Makefile.am | 4 +- sys-utils/chname.8 | 28 ++++++++++++++++++++++++ sys-utils/chname.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 sys-utils/chname.8 create mode 100644 sys-utils/chname.c diff --git a/sys-utils/Makefile.am b/sys-utils/Makefile.am index b4c6ac6..b90dd86 100644 --- a/sys-utils/Makefile.am +++ b/sys-utils/Makefile.am @@ -12,10 +12,10 @@ if LINUX bin_PROGRAMS += dmesg sbin_PROGRAMS += ctrlaltdel usrbinexec_PROGRAMS += cytune setarch lscpu -usrsbinexec_PROGRAMS += ldattach tunelp rtcwake +usrsbinexec_PROGRAMS += ldattach tunelp rtcwake chname dist_man_MANS += dmesg.1 ctrlaltdel.8 cytune.8 setarch.8 \ - ldattach.8 lscpu.1 tunelp.8 rtcwake.8 + ldattach.8 lscpu.1 tunelp.8 rtcwake.8 chname.8 endif cytune_SOURCES = cytune.c cyclades.h diff --git a/sys-utils/chname.8 b/sys-utils/chname.8 new file mode 100644 index 0000000..d9bcc1e --- /dev/null +++ b/sys-utils/chname.8 @@ -0,0 +1,28 @@ +.TH CHNAME 8 2009-05-01 "chname" "Linux Programmer's Manual" +.SH NAME +chname \- run command with new hostname +.SH SYNOPSIS +.B chname +.I hostname +.I command +.B [ +.I args ... +.B ] +.SH DESCRIPTION +Runs given program with specified system hostname. + +Implemented by creating a new utsname namespace, setting hostname and executing +\fIcommand\fP in this namespace. This is particularly useful for running +commands in a chroot. + +\fBchname\fP requires Linux 2.6.19 or later with CONFIG_UTS_NS=y. +.SH EXAMPLE +.B chname +.I my.chroot.host.name +.B chroot +.I /chroot/dir /bin/bash +.SH AUTHOR +Mikhail Gusarov <dottedmag@xxxxxxxxxxxxx> +.SH AVAILABILITY +The chname command is part of util-linux-ng package and is available from +ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/. diff --git a/sys-utils/chname.c b/sys-utils/chname.c new file mode 100644 index 0000000..045b406 --- /dev/null +++ b/sys-utils/chname.c @@ -0,0 +1,56 @@ +/* + * chname.c - run process with changed hostname + * + * © 2009 Mikhail Gusarov <dottedmag@xxxxxxxxxxxxx> + * + * This program is free software; you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 675 Mass + * Ave, Cambridge, MA 02139, USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +/* For unshare() and CLONE_NEWUTS */ +#define _USE_MISC +#include <sched.h> + +int main(int argc, char **argv) +{ + const char *prgname = argv[0]; + + if (argc < 3) { + fprintf(stderr, "Usage: %s <hostname> <program> [<program args>]\n", + prgname); + exit(1); + } + + if (unshare(CLONE_NEWUTS)) { + fprintf(stderr, "%s: Unable to create new utsname namespace: %s\n", + prgname, strerror(errno)); + exit(1); + } + + if (sethostname(argv[1], strlen(argv[1]))) { + fprintf(stderr, "%s: Unable to set hostname: %s\n", + prgname, strerror(errno)); + exit(1); + } + + execvp(argv[2], &argv[2]); + fprintf(stderr, "%s: exec failed: %s\n", prgname, strerror(errno)); + exit(1); +} -- 1.6.2.1 -- To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html