From fa0e7df4e3d6550f0391bb3b81b2b6ac5165439d Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Sun, 16 Oct 2011 21:01:46 -0400 Subject: [PATCH] switch_root: Add --subroot option This is useful for parallel installing multiple operating system roots. See: http://git.gnome.org/browse/hacktree --- sys-utils/switch_root.8 | 4 ++++ sys-utils/switch_root.c | 31 +++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/sys-utils/switch_root.8 b/sys-utils/switch_root.8 index 34ab0d0..6981852 100644 --- a/sys-utils/switch_root.8 +++ b/sys-utils/switch_root.8 @@ -27,6 +27,10 @@ process. show help and exit .IP "\fB\-V, \-\-version\fP" show version number and exit +.IP "\-\-subroot\fP" +Instead of calling chroot into the / of the new root, instead use the +given argument. This helps parallel install multiple operating systems in one +root filesystem. .SH RETURN VALUE .B switch_root diff --git a/sys-utils/switch_root.c b/sys-utils/switch_root.c index 2dfed71..20ae5d6 100644 --- a/sys-utils/switch_root.c +++ b/sys-utils/switch_root.c @@ -108,7 +108,7 @@ done: return rc; } -static int switchroot(const char *newroot) +static int switchroot(const char *newroot, const char *subroot) { /* Don't try to unmount the old "/", there's no way to do it. */ const char *umounts[] = { "/dev", "/proc", "/sys", NULL }; @@ -141,7 +141,7 @@ static int switchroot(const char *newroot) return -1; } - if (chroot(".")) { + if (chroot(subroot ? subroot : ".")) { warn("failed to change root"); return -1; } @@ -160,7 +160,7 @@ static int switchroot(const char *newroot) static void usage(FILE *output) { - fprintf(output, "usage: %s \n", + fprintf(output, "usage: %s [--subroot DIR] \n", program_invocation_short_name); exit(output == stderr ? EXIT_FAILURE : EXIT_SUCCESS); } @@ -175,22 +175,33 @@ static void version(void) int main(int argc, char *argv[]) { char *newroot, *init, **initargs; + char *subroot = NULL; + int argi; - if (argv[1] && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) + if (argc < 2) + usage(stderr); + + argi = 1; + if ((!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) usage(stdout); - if (argv[1] && (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V"))) + if ((!strcmp(argv[1], "--version") || !strcmp(argv[1], "-V"))) version(); - if (argc < 3) + if (argc > 3 && argv[1] && (!strcmp(argv[1], "--subroot"))) { + subroot = argv[2]; + argi = 3; + } + if (argc <= argi+1) { usage(stderr); + } - newroot = argv[1]; - init = argv[2]; - initargs = &argv[2]; + newroot = argv[argi]; + init = argv[argi+1]; + initargs = &argv[argi+1]; if (!*newroot || !*init) usage(stderr); - if (switchroot(newroot)) + if (switchroot(newroot, subroot)) errx(EXIT_FAILURE, "failed. Sorry."); if (access(init, X_OK)) -- 1.7.6.4