X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnspawn%2Fnspawn.c;h=2a1f37bffcd50dca1a40bc3763e4f0827dc230e7;hb=2c21044f05e32ec483b6ab13e175278779e9ebe3;hp=50f2c5911100acfd70c83fd4b45c649e397aea09;hpb=3a74cea5e4cea6d6f852a2a7118efe0e339b78c7;p=elogind.git diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 50f2c5911..2a1f37bff 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -55,7 +55,10 @@ static char *arg_directory = NULL; static char *arg_user = NULL; static char **arg_controllers = NULL; +static char *arg_uuid = NULL; static bool arg_private_network = false; +static bool arg_read_only = false; +static bool arg_boot = false; static int help(void) { @@ -63,9 +66,12 @@ static int help(void) { "Spawn a minimal namespace container for debugging, testing and building.\n\n" " -h --help Show this help\n" " -D --directory=NAME Root directory for the container\n" + " -b --boot Boot up full system (i.e. invoke init)\n" " -u --user=USER Run the command under specified user or uid\n" " -C --controllers=LIST Put the container in specified comma-separated cgroup hierarchies\n" - " --private-network Disable network in container\n", + " --uuid=UUID Set a specific machine UUID for the container\n" + " --private-network Disable network in container\n" + " --read-only Mount the root directory read-only\n", program_invocation_short_name); return 0; @@ -74,7 +80,9 @@ static int help(void) { static int parse_argv(int argc, char *argv[]) { enum { - ARG_PRIVATE_NETWORK = 0x100 + ARG_PRIVATE_NETWORK = 0x100, + ARG_UUID, + ARG_READ_ONLY }; static const struct option options[] = { @@ -83,6 +91,9 @@ static int parse_argv(int argc, char *argv[]) { { "user", required_argument, NULL, 'u' }, { "controllers", required_argument, NULL, 'C' }, { "private-network", no_argument, NULL, ARG_PRIVATE_NETWORK }, + { "boot", no_argument, NULL, 'b' }, + { "uuid", required_argument, NULL, ARG_UUID }, + { "read-only", no_argument, NULL, ARG_READ_ONLY }, { NULL, 0, NULL, 0 } }; @@ -91,7 +102,7 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); - while ((c = getopt_long(argc, argv, "+hD:u:C:", options, NULL)) >= 0) { + while ((c = getopt_long(argc, argv, "+hD:u:C:b", options, NULL)) >= 0) { switch (c) { @@ -133,6 +144,18 @@ static int parse_argv(int argc, char *argv[]) { arg_private_network = true; break; + case 'b': + arg_boot = true; + break; + + case ARG_UUID: + arg_uuid = optarg; + break; + + case ARG_READ_ONLY: + arg_read_only = true; + break; + case '?': return -EINVAL; @@ -248,6 +271,28 @@ static int setup_timezone(const char *dest) { return 0; } +static int setup_resolv_conf(const char *dest) { + char *where; + + assert(dest); + + if (arg_private_network) + return 0; + + /* Fix resolv.conf, if possible */ + if (asprintf(&where, "%s/etc/resolv.conf", dest) < 0) { + log_error("Out of memory"); + return -ENOMEM; + } + + if (mount("/etc/resolv.conf", where, "bind", MS_BIND, NULL) >= 0) + mount("/etc/resolv.conf", where, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL); + + free(where); + + return 0; +} + static int copy_devnodes(const char *dest) { static const char devnodes[] = @@ -905,6 +950,7 @@ int main(int argc, char *argv[]) { NULL, /* HOME */ NULL, /* USER */ NULL, /* LOGNAME */ + NULL, /* container_uuid */ NULL }; @@ -933,6 +979,18 @@ int main(int argc, char *argv[]) { if (mount(NULL, "/", NULL, MS_PRIVATE|MS_REC, NULL) < 0) goto child_fail; + /* Turn directory into bind mount */ + if (mount(arg_directory, arg_directory, "bind", MS_BIND, NULL) < 0) { + log_error("Failed to make bind mount."); + goto child_fail; + } + + if (arg_read_only) + if (mount(arg_directory, arg_directory, "bind", MS_BIND|MS_REMOUNT|MS_RDONLY, NULL) < 0) { + log_error("Failed to make read-only."); + goto child_fail; + } + if (mount_all(arg_directory) < 0) goto child_fail; @@ -950,6 +1008,9 @@ int main(int argc, char *argv[]) { if (setup_timezone(arg_directory) < 0) goto child_fail; + if (setup_resolv_conf(arg_directory) < 0) + goto child_fail; + if (chdir(arg_directory) < 0) { log_error("chdir(%s) failed: %m", arg_directory); goto child_fail; @@ -960,8 +1021,8 @@ int main(int argc, char *argv[]) { dup2(STDIN_FILENO, STDERR_FILENO) != STDERR_FILENO) goto child_fail; - if (mount(arg_directory, "/", "bind", MS_BIND, NULL) < 0) { - log_error("mount(MS_MOVE) failed: %m"); + if (mount(arg_directory, "/", "bind", MS_MOVE, NULL) < 0) { + log_error("mount(MS_BIND) failed: %m"); goto child_fail; } @@ -1015,16 +1076,41 @@ int main(int argc, char *argv[]) { } } - if ((asprintf((char**)(envp + 3), "HOME=%s", home? home: "/root") < 0) || - (asprintf((char**)(envp + 4), "USER=%s", arg_user? arg_user : "root") < 0) || - (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user? arg_user : "root") < 0)) { + if ((asprintf((char**)(envp + 3), "HOME=%s", home ? home: "/root") < 0) || + (asprintf((char**)(envp + 4), "USER=%s", arg_user ? arg_user : "root") < 0) || + (asprintf((char**)(envp + 5), "LOGNAME=%s", arg_user ? arg_user : "root") < 0)) { log_error("Out of memory"); goto child_fail; } + if (arg_uuid) { + if (asprintf((char**)(envp + 6), "container_uuid=%s", arg_uuid) < 0) { + log_error("Out of memory"); + goto child_fail; + } + } + setup_hostname(); - if (argc > optind) + if (arg_boot) { + char **a; + size_t l; + + /* Automatically search for the init system */ + + l = 1 + argc - optind; + a = newa(char*, l + 1); + memcpy(a + 1, argv + optind, l * sizeof(char*)); + + a[0] = (char*) "/usr/lib/systemd/systemd"; + execve(a[0], a, (char**) envp); + + a[0] = (char*) "/lib/systemd/systemd"; + execve(a[0], a, (char**) envp); + + a[0] = (char*) "/sbin/init"; + execve(a[0], a, (char**) envp); + } else if (argc > optind) execvpe(argv[optind], argv + optind, (char**) envp); else { chdir(home ? home : "/root");