LINK_GUEST
} LinkJournal;
+typedef enum Volatile {
+ VOLATILE_NO,
+ VOLATILE_YES,
+ VOLATILE_STATE,
+} Volatile;
+
static char *arg_directory = NULL;
static char *arg_user = NULL;
static sd_id128_t arg_uuid = {};
static const char *arg_network_bridge = NULL;
static unsigned long arg_personality = 0xffffffffLU;
static const char *arg_image = NULL;
+static Volatile arg_volatile = VOLATILE_NO;
static int help(void) {
" --share-system Share system namespaces with host\n"
" --register=BOOLEAN Register container as machine\n"
" --keep-unit Do not register a scope for the machine, reuse\n"
- " the service unit nspawn is running in\n",
+ " the service unit nspawn is running in\n"
+ " --volatile[=MODE] Run the system in volatile mode\n",
program_invocation_short_name);
return 0;
ARG_NETWORK_VETH,
ARG_NETWORK_BRIDGE,
ARG_PERSONALITY,
+ ARG_VOLATILE,
};
static const struct option options[] = {
{ "network-bridge", required_argument, NULL, ARG_NETWORK_BRIDGE },
{ "personality", required_argument, NULL, ARG_PERSONALITY },
{ "image", required_argument, NULL, 'i' },
+ { "volatile", optional_argument, NULL, ARG_VOLATILE },
{}
};
break;
+ case ARG_VOLATILE:
+
+ if (!optarg)
+ arg_volatile = VOLATILE_YES;
+ else {
+ r = parse_boolean(optarg);
+ if (r < 0) {
+ if (streq(optarg, "state"))
+ arg_volatile = VOLATILE_STATE;
+ else {
+ log_error("Failed to parse --volatile= argument: %s", optarg);
+ return r;
+ }
+ } else
+ arg_volatile = r ? VOLATILE_YES : VOLATILE_NO;
+ }
+
+ break;
+
case '?':
return -EINVAL;
return -EINVAL;
}
+ if (arg_volatile != VOLATILE_NO && arg_read_only) {
+ log_error("Cannot combine --read-only with --volatile. Note that --volatile already implies a read-only base hierarchy.");
+ return -EINVAL;
+ }
+
arg_retain = (arg_retain | plus | (arg_private_network ? 1ULL << CAP_NET_ADMIN : 0)) & ~minus;
return 1;
}
/* Create the mount point, but be conservative -- refuse to create block
- * and char devices. */
+ * and char devices. */
if (S_ISDIR(source_st.st_mode))
mkdir_label(where, 0755);
else if (S_ISFIFO(source_st.st_mode))
if (!y)
y = path_startswith(q, "/usr/share/zoneinfo/");
-
/* Already pointing to the right place? Then do nothing .. */
if (y && streq(y, z))
return 0;
if (!what)
return log_oom();
+ mkdir_parents(where, 0755);
unlink(where);
+
if (symlink(what, where) < 0) {
log_error("Failed to correct timezone of container: %m");
return 0;
/* We don't really care for the results of this really. If it
* fails, it fails, but meh... */
+ mkdir_parents(where, 0755);
copy_file("/etc/resolv.conf", where, O_TRUNC|O_NOFOLLOW, 0644);
return 0;
}
+static int setup_volatile_state(const char *directory) {
+ const char *p;
+ int r;
+
+ assert(directory);
+
+ if (arg_volatile != VOLATILE_STATE)
+ return 0;
+
+ /* --volatile=state means we simply overmount /var
+ with a tmpfs, and the rest read-only. */
+
+ r = bind_remount_recursive(directory, true);
+ if (r < 0) {
+ log_error("Failed to remount %s read-only: %s", directory, strerror(-r));
+ return r;
+ }
+
+ p = strappenda(directory, "/var");
+ mkdir(p, 0755);
+
+ if (mount("tmpfs", p, "tmpfs", MS_STRICTATIME, "mode=755") < 0) {
+ log_error("Failed to mount tmpfs to /var: %m");
+ return -errno;
+ }
+
+ return 0;
+}
+
+static int setup_volatile(const char *directory) {
+ bool tmpfs_mounted = false, bind_mounted = false;
+ char template[] = "/tmp/nspawn-volatile-XXXXXX";
+ const char *f, *t;
+ int r;
+
+ assert(directory);
+
+ if (arg_volatile != VOLATILE_YES)
+ return 0;
+
+ /* --volatile=yes means we mount a tmpfs to the root dir, and
+ the original /usr to use inside it, and that read-only. */
+
+ if (!mkdtemp(template)) {
+ log_error("Failed to create temporary directory: %m");
+ return -errno;
+ }
+
+ if (mount("tmpfs", template, "tmpfs", MS_STRICTATIME, "mode=755") < 0) {
+ log_error("Failed to mount tmpfs for root directory: %m");
+ r = -errno;
+ goto fail;
+ }
+
+ tmpfs_mounted = true;
+
+ f = strappenda(directory, "/usr");
+ t = strappenda(template, "/usr");
+
+ mkdir(t, 0755);
+ if (mount(f, t, "bind", MS_BIND|MS_REC, NULL) < 0) {
+ log_error("Failed to create /usr bind mount: %m");
+ r = -errno;
+ goto fail;
+ }
+
+ bind_mounted = true;
+
+ r = bind_remount_recursive(t, true);
+ if (r < 0) {
+ log_error("Failed to remount %s read-only: %s", t, strerror(-r));
+ goto fail;
+ }
+
+ if (mount(template, directory, NULL, MS_MOVE, NULL) < 0) {
+ log_error("Failed to move root mount: %m");
+ r = -errno;
+ goto fail;
+ }
+
+ rmdir(template);
+
+ return 0;
+
+fail:
+ if (bind_mounted)
+ umount(t);
+ if (tmpfs_mounted)
+ umount(template);
+ rmdir(template);
+ return r;
+}
+
static char* id128_format_as_uuid(sd_id128_t id, char s[37]) {
snprintf(s, 37,
return 0;
}
-static int audit_still_doesnt_work_in_containers(void) {
+static int setup_seccomp(void) {
#ifdef HAVE_SECCOMP
+ static const int blacklist[] = {
+ SCMP_SYS(kexec_load),
+ SCMP_SYS(open_by_handle_at),
+ SCMP_SYS(init_module),
+ SCMP_SYS(finit_module),
+ SCMP_SYS(delete_module),
+ SCMP_SYS(iopl),
+ SCMP_SYS(ioperm),
+ SCMP_SYS(swapon),
+ SCMP_SYS(swapoff),
+ };
+
scmp_filter_ctx seccomp;
+ unsigned i;
int r;
- /*
- Audit is broken in containers, much of the userspace audit
- hookup will fail if running inside a container. We don't
- care and just turn off creation of audit sockets.
-
- This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail
- with EAFNOSUPPORT which audit userspace uses as indication
- that audit is disabled in the kernel.
- */
-
seccomp = seccomp_init(SCMP_ACT_ALLOW);
if (!seccomp)
return log_oom();
goto finish;
}
+ for (i = 0; i < ELEMENTSOF(blacklist); i++) {
+ r = seccomp_rule_add(seccomp, SCMP_ACT_ERRNO(EPERM), blacklist[i], 0);
+ if (r == -EFAULT)
+ continue; /* unknown syscall */
+ if (r < 0) {
+ log_error("Failed to block syscall: %s", strerror(-r));
+ goto finish;
+ }
+ }
+
+ /*
+ Audit is broken in containers, much of the userspace audit
+ hookup will fail if running inside a container. We don't
+ care and just turn off creation of audit sockets.
+
+ This will make socket(AF_NETLINK, *, NETLINK_AUDIT) fail
+ with EAFNOSUPPORT which audit userspace uses as indication
+ that audit is disabled in the kernel.
+ */
+
r = seccomp_rule_add(
seccomp,
SCMP_ACT_ERRNO(EAFNOSUPPORT),
siginfo_t status;
r = wait_for_terminate(pid, &status);
- if (r < 0)
+ if (r < 0) {
+ log_warning("Failed to wait for container: %s", strerror(-r));
return r;
+ }
switch (status.si_code) {
case CLD_EXITED:
goto finish;
}
- r = dissect_image(image_fd, &root_device, &root_device_rw, &home_device, &home_device_rw, &srv_device, &srv_device_rw, &secondary);
+ r = dissect_image(image_fd,
+ &root_device, &root_device_rw,
+ &home_device, &home_device_rw,
+ &srv_device, &srv_device_rw,
+ &secondary);
if (r < 0)
goto finish;
}
srv_device, srv_device_rw) < 0)
goto child_fail;
- r = base_filesystem_create(arg_directory);
- if (r < 0) {
- log_error("Failed to create the base filesystem: %s", strerror(-r));
- goto child_fail;
- }
-
/* Turn directory into bind mount */
if (mount(arg_directory, arg_directory, "bind", MS_BIND|MS_REC, NULL) < 0) {
log_error("Failed to make bind mount: %m");
goto child_fail;
}
+ r = setup_volatile(arg_directory);
+ if (r < 0)
+ goto child_fail;
+
+ if (setup_volatile_state(arg_directory) < 0)
+ goto child_fail;
+
+ r = base_filesystem_create(arg_directory);
+ if (r < 0)
+ goto child_fail;
+
if (arg_read_only) {
k = bind_remount_recursive(arg_directory, true);
if (k < 0) {
dev_setup(arg_directory);
- if (audit_still_doesnt_work_in_containers() < 0)
+ if (setup_seccomp() < 0)
goto child_fail;
if (setup_dev_console(arg_directory, console) < 0)
* join its cgroup which might limit what it can do */
r = eventfd_child_succeeded(eventfds[1]);
eventfds[1] = safe_close(eventfds[1]);
- if (r < 0)
- goto check_container_status;
- r = register_machine(pid);
- if (r < 0)
- goto finish;
+ if (r >= 0) {
+ r = register_machine(pid);
+ if (r < 0)
+ goto finish;
- r = move_network_interfaces(pid);
- if (r < 0)
- goto finish;
+ r = move_network_interfaces(pid);
+ if (r < 0)
+ goto finish;
- r = setup_veth(pid, veth_name);
- if (r < 0)
- goto finish;
+ r = setup_veth(pid, veth_name);
+ if (r < 0)
+ goto finish;
- r = setup_bridge(veth_name);
- if (r < 0)
- goto finish;
+ r = setup_bridge(veth_name);
+ if (r < 0)
+ goto finish;
- r = setup_macvlan(pid);
- if (r < 0)
- goto finish;
+ r = setup_macvlan(pid);
+ if (r < 0)
+ goto finish;
- /* Block SIGCHLD here, before notifying child.
- * process_pty() will handle it with the other signals. */
- r = sigprocmask(SIG_BLOCK, &mask_chld, NULL);
- if (r < 0)
- goto finish;
+ /* Block SIGCHLD here, before notifying child.
+ * process_pty() will handle it with the other signals. */
+ r = sigprocmask(SIG_BLOCK, &mask_chld, NULL);
+ if (r < 0)
+ goto finish;
- /* Reset signal to default */
- r = default_signals(SIGCHLD, -1);
- if (r < 0)
- goto finish;
+ /* Reset signal to default */
+ r = default_signals(SIGCHLD, -1);
+ if (r < 0)
+ goto finish;
- /* Notify the child that the parent is ready with all
- * its setup, and that the child can now hand over
- * control to the code to run inside the container. */
- r = eventfd_send_state(eventfds[0],
- EVENTFD_PARENT_SUCCEEDED);
- eventfds[0] = safe_close(eventfds[0]);
- if (r < 0)
- goto finish;
+ /* Notify the child that the parent is ready with all
+ * its setup, and that the child can now hand over
+ * control to the code to run inside the container. */
+ r = eventfd_send_state(eventfds[0], EVENTFD_PARENT_SUCCEEDED);
+ eventfds[0] = safe_close(eventfds[0]);
+ if (r < 0)
+ goto finish;
- k = process_pty(master, &mask, arg_boot ? pid : 0, SIGRTMIN+3);
- if (k < 0) {
- r = EXIT_FAILURE;
- break;
- }
+ k = process_pty(master, &mask, arg_boot ? pid : 0, SIGRTMIN+3);
+ if (k < 0) {
+ r = EXIT_FAILURE;
+ break;
+ }
- if (!arg_quiet)
- putc('\n', stdout);
+ if (!arg_quiet)
+ putc('\n', stdout);
- /* Kill if it is not dead yet anyway */
- terminate_machine(pid);
+ /* Kill if it is not dead yet anyway */
+ terminate_machine(pid);
+ }
-check_container_status:
- /* Redundant, but better safe than sorry */
+ /* Normally redundant, but better safe than sorry */
kill(pid, SIGKILL);
r = wait_for_container(pid, &container_status);
pid = 0;
- if (r != 0) {
- /* If r < 0, explicitly set to EXIT_FAILURE,
- * otherwise return the exit code of the
- * containered process. */
- if (r < 0)
- r = EXIT_FAILURE;
+ if (r < 0) {
+ /* We failed to wait for the container, or the
+ * container exited abnormally */
+ r = EXIT_FAILURE;
break;
- } else if (container_status == CONTAINER_TERMINATED)
+ } else if (r > 0 || container_status == CONTAINER_TERMINATED)
+ /* The container exited with a non-zero
+ * status, or with zero status and no reboot
+ * was requested. */
break;
/* CONTAINER_REBOOTED, loop again */
+
+ if (arg_keep_unit) {
+ /* Special handling if we are running as a
+ * service: instead of simply restarting the
+ * machine we want to restart the entire
+ * service, so let's inform systemd about this
+ * with the special exit code 133. The service
+ * file uses RestartForceExitStatus=133 so
+ * that this results in a full nspawn
+ * restart. This is necessary since we might
+ * have cgroup parameters set we want to have
+ * flushed out. */
+ r = 133;
+ break;
+ }
}
finish: