From 39f9230780dc4a69d61a957e8344812a63c4ecca Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 12 Mar 2015 17:06:23 -0700 Subject: [PATCH 1/4] stage1: replace exit-watcher's sleep with small utility Moving towards fewer external dependencies, aiming for only systemd being external so it's the only complicated thing we need to do things like try pulling from the host or building from source when not deriving stage1 from a CoreOS image. --- stage1/.gitignore | 1 + stage1/rootfs/Makefile | 2 +- stage1/rootfs/aggregate/units/exit-watcher.service | 2 +- stage1/rootfs/usr/manifest.d/sleep | 5 ----- stage1/rootfs/waiter/Makefile | 13 +++++++++++++ stage1/rootfs/waiter/install | 1 + stage1/rootfs/waiter/waiter.c | 9 +++++++++ 7 files changed, 26 insertions(+), 7 deletions(-) delete mode 100644 stage1/rootfs/usr/manifest.d/sleep create mode 100644 stage1/rootfs/waiter/Makefile create mode 100644 stage1/rootfs/waiter/install create mode 100644 stage1/rootfs/waiter/waiter.c diff --git a/stage1/.gitignore b/stage1/.gitignore index 3e6248a..2749af8 100644 --- a/stage1/.gitignore +++ b/stage1/.gitignore @@ -11,3 +11,4 @@ rootfs/usr/manifest.txt rootfs/usr/rootfs/ rootfs/usr/usr.done rootfs/usr/usr.squashfs +rootfs/waiter/waiter diff --git a/stage1/rootfs/Makefile b/stage1/rootfs/Makefile index 342ec75..907f456 100644 --- a/stage1/rootfs/Makefile +++ b/stage1/rootfs/Makefile @@ -1,4 +1,4 @@ -_SUBDIRS=usr shim diagexec prepare-app enter net-plugins net init +_SUBDIRS=usr waiter shim diagexec prepare-app enter net-plugins net init SUBDIRS=$(_SUBDIRS) aggregate export CFLAGS=-Wall -Os diff --git a/stage1/rootfs/aggregate/units/exit-watcher.service b/stage1/rootfs/aggregate/units/exit-watcher.service index 81950d7..9911396 100644 --- a/stage1/rootfs/aggregate/units/exit-watcher.service +++ b/stage1/rootfs/aggregate/units/exit-watcher.service @@ -4,5 +4,5 @@ StopWhenUnneeded=true DefaultDependencies=false [Service] -ExecStart=/usr/bin/sleep 9999999999d +ExecStart=/waiter ExecStopPost=/usr/bin/systemctl isolate reaper.service diff --git a/stage1/rootfs/usr/manifest.d/sleep b/stage1/rootfs/usr/manifest.d/sleep deleted file mode 100644 index 835caa0..0000000 --- a/stage1/rootfs/usr/manifest.d/sleep +++ /dev/null @@ -1,5 +0,0 @@ -bin/sleep -lib64/ld-2.17.so -lib64/ld-linux-x86-64.so.2 -lib64/libc-2.17.so -lib64/libc.so.6 diff --git a/stage1/rootfs/waiter/Makefile b/stage1/rootfs/waiter/Makefile new file mode 100644 index 0000000..3879667 --- /dev/null +++ b/stage1/rootfs/waiter/Makefile @@ -0,0 +1,13 @@ +BIN=waiter +SRC=waiter.c + +$(BIN): $(SRC) Makefile install + $(CC) $(CFLAGS) -o $@ $(SRC) -static -s + @cp install ../aggregate/install.d/10waiter + +.PHONY: clean +clean: + rm -f $(BIN) + +test: + echo TODO diff --git a/stage1/rootfs/waiter/install b/stage1/rootfs/waiter/install new file mode 100644 index 0000000..2e542a9 --- /dev/null +++ b/stage1/rootfs/waiter/install @@ -0,0 +1 @@ +install -m 0755 ../waiter/waiter "$ROOT" diff --git a/stage1/rootfs/waiter/waiter.c b/stage1/rootfs/waiter/waiter.c new file mode 100644 index 0000000..e46daff --- /dev/null +++ b/stage1/rootfs/waiter/waiter.c @@ -0,0 +1,9 @@ +#include +#include + +int main(int argc, const char *argv[]) +{ + for(;;) pause(); + + return EXIT_SUCCESS; +} From 310d3ba4c26cf9fd54207b68e09b9c3016303f73 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Fri, 13 Mar 2015 10:31:55 -0700 Subject: [PATCH 2/4] stage1: rename rootfs/usr -> rootfs/usr_from_coreos --- stage1/.gitignore | 10 +++++----- stage1/rootfs/Makefile | 3 ++- stage1/rootfs/usr/install | 2 -- stage1/rootfs/{usr => usr_from_coreos}/Makefile | 2 +- stage1/rootfs/{usr => usr_from_coreos}/cache.sh | 0 stage1/rootfs/usr_from_coreos/install | 2 ++ stage1/rootfs/{usr => usr_from_coreos}/manifest.d/bash | 0 .../rootfs/{usr => usr_from_coreos}/manifest.d/systemd | 0 stage1/rootfs/{usr => usr_from_coreos}/mkbase.sh | 0 9 files changed, 10 insertions(+), 9 deletions(-) delete mode 100644 stage1/rootfs/usr/install rename stage1/rootfs/{usr => usr_from_coreos}/Makefile (88%) rename stage1/rootfs/{usr => usr_from_coreos}/cache.sh (100%) create mode 100644 stage1/rootfs/usr_from_coreos/install rename stage1/rootfs/{usr => usr_from_coreos}/manifest.d/bash (100%) rename stage1/rootfs/{usr => usr_from_coreos}/manifest.d/systemd (100%) rename stage1/rootfs/{usr => usr_from_coreos}/mkbase.sh (100%) diff --git a/stage1/.gitignore b/stage1/.gitignore index 2749af8..838d2e9 100644 --- a/stage1/.gitignore +++ b/stage1/.gitignore @@ -6,9 +6,9 @@ rootfs/diagexec/diagexec rootfs/enter/enter rootfs/prepare-app/prepare-app rootfs/shim/shim.so -rootfs/usr/cache/ -rootfs/usr/manifest.txt -rootfs/usr/rootfs/ -rootfs/usr/usr.done -rootfs/usr/usr.squashfs +rootfs/usr_from_coreos/cache/ +rootfs/usr_from_coreos/manifest.txt +rootfs/usr_from_coreos/rootfs/ +rootfs/usr_from_coreos/usr.done +rootfs/usr_from_coreos/usr.squashfs rootfs/waiter/waiter diff --git a/stage1/rootfs/Makefile b/stage1/rootfs/Makefile index 907f456..4865644 100644 --- a/stage1/rootfs/Makefile +++ b/stage1/rootfs/Makefile @@ -1,4 +1,5 @@ -_SUBDIRS=usr waiter shim diagexec prepare-app enter net-plugins net init +USR=usr_from_coreos +_SUBDIRS=$(USR) waiter shim diagexec prepare-app enter net-plugins net init SUBDIRS=$(_SUBDIRS) aggregate export CFLAGS=-Wall -Os diff --git a/stage1/rootfs/usr/install b/stage1/rootfs/usr/install deleted file mode 100644 index ee740ad..0000000 --- a/stage1/rootfs/usr/install +++ /dev/null @@ -1,2 +0,0 @@ -mkdir -p "$ROOT" -cp -a ../usr/rootfs/. "$ROOT" diff --git a/stage1/rootfs/usr/Makefile b/stage1/rootfs/usr_from_coreos/Makefile similarity index 88% rename from stage1/rootfs/usr/Makefile rename to stage1/rootfs/usr_from_coreos/Makefile index 610672c..9066cfe 100644 --- a/stage1/rootfs/usr/Makefile +++ b/stage1/rootfs/usr_from_coreos/Makefile @@ -3,7 +3,7 @@ usr.done: Makefile mkbase.sh cache/pxe.img manifest.d/* install @./mkbase.sh && touch usr.done - @cp install ../aggregate/install.d/00usr + @cp -f install ../aggregate/install.d/00usr cache/pxe.img: Makefile cache.sh @./cache.sh diff --git a/stage1/rootfs/usr/cache.sh b/stage1/rootfs/usr_from_coreos/cache.sh similarity index 100% rename from stage1/rootfs/usr/cache.sh rename to stage1/rootfs/usr_from_coreos/cache.sh diff --git a/stage1/rootfs/usr_from_coreos/install b/stage1/rootfs/usr_from_coreos/install new file mode 100644 index 0000000..564a223 --- /dev/null +++ b/stage1/rootfs/usr_from_coreos/install @@ -0,0 +1,2 @@ +mkdir -p "$ROOT" +cp -a ../usr_from_coreos/rootfs/. "$ROOT" diff --git a/stage1/rootfs/usr/manifest.d/bash b/stage1/rootfs/usr_from_coreos/manifest.d/bash similarity index 100% rename from stage1/rootfs/usr/manifest.d/bash rename to stage1/rootfs/usr_from_coreos/manifest.d/bash diff --git a/stage1/rootfs/usr/manifest.d/systemd b/stage1/rootfs/usr_from_coreos/manifest.d/systemd similarity index 100% rename from stage1/rootfs/usr/manifest.d/systemd rename to stage1/rootfs/usr_from_coreos/manifest.d/systemd diff --git a/stage1/rootfs/usr/mkbase.sh b/stage1/rootfs/usr_from_coreos/mkbase.sh similarity index 100% rename from stage1/rootfs/usr/mkbase.sh rename to stage1/rootfs/usr_from_coreos/mkbase.sh From 0219d440c7ca14d6c7c9005d8405d7dba9eb3619 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 16 Mar 2015 17:13:00 -0700 Subject: [PATCH 3/4] build: parallelize stage1 by number of cpus reported by lscpu This becomes significant when we build systemd from source. --- build | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/build b/build index 3bd178a..cd993df 100755 --- a/build +++ b/build @@ -50,6 +50,8 @@ if [[ "$OSTYPE" == "linux-gnu" ]]; then mkdir -p stage1/rootfs/init/bin ln -sf ${GOBIN}/init stage1/rootfs/init/bin - echo "Building rootfs (stage1)..." - make -C stage1/rootfs + JOBS=1 + which lscpu >&/dev/null && JOBS=$(lscpu --parse=CPU | grep -c ^[^#]) + echo "Building rootfs (stage1) using ${JOBS} CPUs..." + make -C stage1/rootfs -j ${JOBS} fi From fdcd649471f8a3cafc24c8b79d0328f24d3ea213 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Tue, 17 Mar 2015 17:33:12 -0700 Subject: [PATCH 4/4] stage1: add support for building stage1's usr from src Currently only systemd is being built from source, toggled from the top-level build script by setting RKT_STAGE1_USR_FROM=src, e.g: RKT_STAGE1_USR_FROM=src ./build The default stage1 continues to be coreos-derived, but may be explicitly specified via: RKT_STAGE1_USR_FROM=coreos ./build The stage1 init program has been modified to introspect the "flavor" of stage1 at runtime, which influences how it invokes systemd-nspawn. A small series of patches are included to give systemd-nspawn the functionalities enabled via LD_PRELOAD of the shim in the coreos-derived stage1. The systemd source is cloned at build time the first time an RKT_STAGE1_USR_FROM=src build is performed, at this time the same version as the coreos image provided is being used, but this can be bumped in the future. --- build | 11 ++ stage1/.gitignore | 9 ++ stage1/init/init.go | 53 ++++++--- stage1/rootfs/Makefile | 14 ++- stage1/rootfs/usr_from_coreos/install | 1 + stage1/rootfs/usr_from_src/Makefile | 49 ++++++++ ...sable-registration-on-non-sysd-hosts.patch | 29 +++++ .../0002-nspawn-add-keep-fd-support.patch | 112 ++++++++++++++++++ .../patches/0003-nspawn-add-pid-file.patch | 90 ++++++++++++++ 9 files changed, 346 insertions(+), 22 deletions(-) create mode 100644 stage1/rootfs/usr_from_src/Makefile create mode 100644 stage1/rootfs/usr_from_src/patches/0001-nspawn-just-disable-registration-on-non-sysd-hosts.patch create mode 100644 stage1/rootfs/usr_from_src/patches/0002-nspawn-add-keep-fd-support.patch create mode 100644 stage1/rootfs/usr_from_src/patches/0003-nspawn-add-pid-file.patch diff --git a/build b/build index cd993df..f6a847e 100755 --- a/build +++ b/build @@ -13,6 +13,17 @@ export GOPATH=${PWD}/gopath eval $(go env) +# set default stage1 usr style and validate potential override +RKT_STAGE1_USR_FROM="${RKT_STAGE1_USR_FROM:-coreos}" +case "${RKT_STAGE1_USR_FROM}" in + coreos|src) + ;; + *) + echo "RKT_STAGE1_USR_FROM=${RKT_STAGE1_USR_FROM} unsupported" + exit 255 +esac +export RKT_STAGE1_USR_FROM + echo "Building rkt (stage0)..." go build -o ${GOBIN}/rkt \ ${RKT_STAGE1_IMAGE:+-ldflags "-X main.defaultStage1Image '${RKT_STAGE1_IMAGE}'"} \ diff --git a/stage1/.gitignore b/stage1/.gitignore index 838d2e9..d1f7b34 100644 --- a/stage1/.gitignore +++ b/stage1/.gitignore @@ -11,4 +11,13 @@ rootfs/usr_from_coreos/manifest.txt rootfs/usr_from_coreos/rootfs/ rootfs/usr_from_coreos/usr.done rootfs/usr_from_coreos/usr.squashfs +rootfs/usr_from_src/bash.done +rootfs/usr_from_src/host_deps.txt +rootfs/usr_from_src/install +rootfs/usr_from_src/systemd.done +rootfs/usr_from_src/systemd.src +rootfs/usr_from_src/systemd/ +rootfs/usr_from_src/systemd_build/ +rootfs/usr_from_src/usr.done rootfs/waiter/waiter +rootfs/.usr diff --git a/stage1/init/init.go b/stage1/init/init.go index d144843..8c5cd36 100644 --- a/stage1/init/init.go +++ b/stage1/init/init.go @@ -93,12 +93,35 @@ func init() { runtime.LockOSThread() } -func computeArgs(c *Container, debug bool) ([]string, error) { - args := []string{ - filepath.Join(common.Stage1RootfsPath(c.Root), interpBin), - filepath.Join(common.Stage1RootfsPath(c.Root), nspawnBin), - "--boot", // Launch systemd in the container - "--register", "false", // We cannot assume the host system is running systemd +// getArgsEnv returns the nspawn args and env according to the usr used +func getArgsEnv(c *Container, debug bool) ([]string, []string, error) { + args := []string{} + env := os.Environ() + + flavor, err := os.Readlink(filepath.Join(common.Stage1RootfsPath(c.Root), "flavor")) + if err != nil { + return nil, nil, fmt.Errorf("unable to determine stage1 flavor: %v", err) + } + + switch flavor { + case "coreos": + // when running the coreos-derived stage1 with unpatched systemd-nspawn we need some ld-linux hackery + args = append(args, filepath.Join(common.Stage1RootfsPath(c.Root), interpBin)) + args = append(args, filepath.Join(common.Stage1RootfsPath(c.Root), nspawnBin)) + args = append(args, "--boot") // Launch systemd in the container + args = append(args, "--register", "false") // We cannot assume the host system is running systemd + // TODO(vc): we should leave registration enabled if systemd is running on the host, + // but it needs to be sufficiently new systemd or registration will fail. + + env = append(env, "LD_PRELOAD="+filepath.Join(common.Stage1RootfsPath(c.Root), "fakesdboot.so")) + env = append(env, "LD_LIBRARY_PATH="+filepath.Join(common.Stage1RootfsPath(c.Root), "usr/lib")) + + case "src": + args = append(args, filepath.Join(common.Stage1RootfsPath(c.Root), nspawnBin)) + args = append(args, "--boot") // Launch systemd in the container + + default: + return nil, nil, fmt.Errorf("unrecognized stage1 flavor: %q", flavor) } if !debug { @@ -107,7 +130,7 @@ func computeArgs(c *Container, debug bool) ([]string, error) { nsargs, err := c.ContainerToNspawnArgs() if err != nil { - return nil, fmt.Errorf("Failed to generate nspawn args: %v", err) + return nil, nil, fmt.Errorf("Failed to generate nspawn args: %v", err) } args = append(args, nsargs...) @@ -119,15 +142,7 @@ func computeArgs(c *Container, debug bool) ([]string, error) { args = append(args, "--show-status=0") // silence systemd initialization status output } - return args, nil -} - -func computeEnv(c *Container) []string { - env := os.Environ() - env = append(env, "LD_PRELOAD="+filepath.Join(common.Stage1RootfsPath(c.Root), "fakesdboot.so")) - env = append(env, "LD_LIBRARY_PATH="+filepath.Join(common.Stage1RootfsPath(c.Root), "usr/lib")) - - return env + return args, env, nil } func withClearedCloExec(lfd int, f func() error) error { @@ -169,11 +184,11 @@ func stage1() int { return 2 } - args, err := computeArgs(c, debug) + args, env, err := getArgsEnv(c, debug) if err != nil { - fmt.Fprintln(os.Stderr, err) + fmt.Fprintf(os.Stderr, "Failed to get execution parameters: %v\n", err) + return 3 } - env := computeEnv(c) if privNet { // careful not to make another local err variable. diff --git a/stage1/rootfs/Makefile b/stage1/rootfs/Makefile index 4865644..cca5c3e 100644 --- a/stage1/rootfs/Makefile +++ b/stage1/rootfs/Makefile @@ -1,13 +1,21 @@ -USR=usr_from_coreos +USR=usr_from_$(RKT_STAGE1_USR_FROM) _SUBDIRS=$(USR) waiter shim diagexec prepare-app enter net-plugins net init SUBDIRS=$(_SUBDIRS) aggregate export CFLAGS=-Wall -Os -.PHONY: test clean subdirs $(SUBDIRS) +.PHONY: usr_switch test clean subdirs $(SUBDIRS) subdirs: $(SUBDIRS) -$(SUBDIRS): +# cache the usr flavor and detect when it's changed so we can force the new flavor to at least install itself +usr_switch: + @[ -e .usr ] && LAST_USR=$$(cat .usr); \ + if [ "$(USR)" != "$$LAST_USR" ]; then \ + touch "$(USR)/install"; \ + echo -n "$(USR)" > .usr; \ + fi + +$(SUBDIRS): usr_switch $(MAKE) -C $@ $(MAKECMDGOALS) clean: $(SUBDIRS) diff --git a/stage1/rootfs/usr_from_coreos/install b/stage1/rootfs/usr_from_coreos/install index 564a223..e062b5b 100644 --- a/stage1/rootfs/usr_from_coreos/install +++ b/stage1/rootfs/usr_from_coreos/install @@ -1,2 +1,3 @@ mkdir -p "$ROOT" cp -a ../usr_from_coreos/rootfs/. "$ROOT" +ln -sf "coreos" "$ROOT/flavor" diff --git a/stage1/rootfs/usr_from_src/Makefile b/stage1/rootfs/usr_from_src/Makefile new file mode 100644 index 0000000..f97c71d --- /dev/null +++ b/stage1/rootfs/usr_from_src/Makefile @@ -0,0 +1,49 @@ +# build systemd from source, produce an install hook selecting the needed files and host dependencies + +usr.done: Makefile install + cp -f install ../aggregate/install.d/00usr + touch usr.done + +# prepare the install file +install: Makefile host_deps.txt + echo mkdir -p "\$${ROOT}" > install.tmp + echo cp -af ../usr_from_src/systemd_build/installed/. \"\$${ROOT}\" >> install.tmp + cat host_deps.txt | while read dep; do echo install -D \"$${dep}\" \"\$${ROOT}/$${dep}\"; done >> install.tmp + echo ln -sf src \"\$${ROOT}/flavor\" >> install.tmp + mv install.tmp install + +# discover host library dependencies for all the ELF executables in systemd_build/installed, note the LD_LIBRARY_PATH= to find systemd-produced libraries. +host_deps.txt: Makefile systemd.done bash.done + cd systemd_build/installed && find -type f | xargs file | grep ELF | cut -f1 -d: | LD_LIBRARY_PATH=usr/lib xargs ldd | grep -v ^\\. | grep '/' | sed -e 's/^[[:space:]]*//' -e 's/.*=> //' -e 's/ (0x[0-9a-f]*)$$//' | grep -v ^[^/] | sort -u > ../../host_deps.txt.tmp + mv host_deps.txt.tmp host_deps.txt + +# grab bash from the host +bash.done: Makefile systemd.done + cp -af `which bash` systemd_build/installed/usr/bin/bash + touch bash.done + +# configure, build, and install systemd +systemd.done: Makefile systemd.src + { [ ! -e systemd_build ] || rm -Rf systemd_build; } + mkdir systemd_build + cd systemd_build && ../systemd/configure --disable-python-devel --disable-dbus --disable-kmod --disable-blkid --disable-chkconfig --disable-pam --disable-acl --disable-smack --disable-gcrypt --disable-elfutils --disable-libcryptsetup --disable-qrencode --disable-microhttpd --disable-gnutls --disable-binfmt --disable-vconsole --disable-readahead --disable-bootchart --disable-quotacheck --disable-tmpfiles --disable-sysusers --disable-randomseed --disable-backlight --disable-rfkill --disable-logind --disable-machined --disable-timedated --disable-timesyncd --disable-localed --disable-coredump --disable-polkit --disable-resolved --disable-networkd --disable-efi --disable-multi-seat-x --disable-myhostname --disable-gudev --disable-manpages --disable-tests --disable-blkid --disable-hibernate --disable-terminal --disable-hwdb --disable-importd && $(MAKE) && DESTDIR=$${PWD}/installed make install-strip + touch systemd.done + +# TODO(vc): it may make more sense to have the systemd source be a git submodule? +systemd.src: Makefile patches/* + { [ ! -e systemd ] || rm -Rf systemd; } + mkdir systemd + git clone --branch v215 --depth 1 git://anongit.freedesktop.org/systemd/systemd + cd systemd && for p in ../patches/*; do patch -p1 <"$$p"; done + cd systemd && ./autogen.sh + touch systemd.src + +.PHONY: clean distclean +clean: + rm -Rf systemd_build systemd.done bash.done host_deps.txt rootfs usr.done install + +distclean: clean + rm -Rf systemd systemd.src + +test: + echo TODO diff --git a/stage1/rootfs/usr_from_src/patches/0001-nspawn-just-disable-registration-on-non-sysd-hosts.patch b/stage1/rootfs/usr_from_src/patches/0001-nspawn-just-disable-registration-on-non-sysd-hosts.patch new file mode 100644 index 0000000..05e88a3 --- /dev/null +++ b/stage1/rootfs/usr_from_src/patches/0001-nspawn-just-disable-registration-on-non-sysd-hosts.patch @@ -0,0 +1,29 @@ +From 1eba7c03e8689ae5d96e0f258bedf6ed96b1741f Mon Sep 17 00:00:00 2001 +From: Vito Caputo +Date: Mon, 16 Mar 2015 13:58:52 -0700 +Subject: [PATCH 1/3] nspawn: just disable registration on non-sysd hosts + +--- + src/nspawn/nspawn.c | 5 +++-- + 1 file changed, 3 insertions(+), 2 deletions(-) + +diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c +index 0d538c2..f1e5c0d 100644 +--- a/src/nspawn/nspawn.c ++++ b/src/nspawn/nspawn.c +@@ -2808,9 +2808,10 @@ int main(int argc, char *argv[]) { + goto finish; + } + ++ /* we're going to run nspawn on non-systemd hosts, we'll just disable registration when non-sysd */ + if (sd_booted() <= 0) { +- log_error("Not running on a systemd system."); +- goto finish; ++ /* TODO(vc): there are other things which won't work on non-systemd hosts, but we don't activate them yet via rkt */ ++ arg_register = false; + } + + log_close(); +-- +2.1.4 + diff --git a/stage1/rootfs/usr_from_src/patches/0002-nspawn-add-keep-fd-support.patch b/stage1/rootfs/usr_from_src/patches/0002-nspawn-add-keep-fd-support.patch new file mode 100644 index 0000000..df65a1e --- /dev/null +++ b/stage1/rootfs/usr_from_src/patches/0002-nspawn-add-keep-fd-support.patch @@ -0,0 +1,112 @@ +From 3cd7128052c0c0e691b936523ac211ca8acd9b85 Mon Sep 17 00:00:00 2001 +From: Vito Caputo +Date: Mon, 16 Mar 2015 15:10:15 -0700 +Subject: [PATCH 2/3] nspawn: add --keep-fd support + +This enables inheriting a file from the executing process which persists +for the duration of the nspawn parent process. Useful in implementing a +robust lifecycle reference tied to the nspawn container, which when +combined with advisory locking can also provide a simple event mechanism +triggered at container exit. +--- + src/nspawn/nspawn.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- + 1 file changed, 45 insertions(+), 1 deletion(-) + +diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c +index f1e5c0d..0277a12 100644 +--- a/src/nspawn/nspawn.c ++++ b/src/nspawn/nspawn.c +@@ -153,6 +153,7 @@ static bool arg_quiet = false; + static bool arg_share_system = false; + static bool arg_register = true; + static bool arg_keep_unit = false; ++static int arg_keep_fd = -1; + static char **arg_network_interfaces = NULL; + static char **arg_network_macvlan = NULL; + static bool arg_network_veth = false; +@@ -207,7 +208,8 @@ 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" ++ " --keep-fd=FDNUM Do not close the specified file descriptor\n", + program_invocation_short_name); + + return 0; +@@ -230,6 +232,7 @@ static int parse_argv(int argc, char *argv[]) { + ARG_SHARE_SYSTEM, + ARG_REGISTER, + ARG_KEEP_UNIT, ++ ARG_KEEP_FD, + ARG_NETWORK_INTERFACE, + ARG_NETWORK_MACVLAN, + ARG_NETWORK_VETH, +@@ -261,6 +264,7 @@ static int parse_argv(int argc, char *argv[]) { + { "share-system", no_argument, NULL, ARG_SHARE_SYSTEM }, + { "register", required_argument, NULL, ARG_REGISTER }, + { "keep-unit", no_argument, NULL, ARG_KEEP_UNIT }, ++ { "keep-fd", required_argument, NULL, ARG_KEEP_FD }, + { "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE }, + { "network-macvlan", required_argument, NULL, ARG_NETWORK_MACVLAN }, + { "network-veth", no_argument, NULL, ARG_NETWORK_VETH }, +@@ -549,6 +553,14 @@ static int parse_argv(int argc, char *argv[]) { + arg_keep_unit = true; + break; + ++ case ARG_KEEP_FD: ++ r = safe_atoi(optarg, &arg_keep_fd); ++ if (r < 0) { ++ log_error("Failed to parse --keep-fd= argument: %s", optarg); ++ return r; ++ } ++ break; ++ + case ARG_PERSONALITY: + + arg_personality = personality_from_string(optarg); +@@ -2823,9 +2835,41 @@ int main(int argc, char *argv[]) { + goto finish; + } + } ++ ++ /* if we're keeping an fd open add it to the listen fds set temporarily across fdset_close_others() */ ++ if (arg_keep_fd >= 0) { ++ if (!fds) { ++ fds = fdset_new(); ++ if (!fds) { ++ r = log_oom(); ++ goto finish; ++ } ++ } ++ k = fdset_put(fds, arg_keep_fd); ++ if (k < 0) { ++ log_error("Failed to add kept fd: %s", strerror(-k)); ++ goto finish; ++ } ++ } ++ + fdset_close_others(fds); + log_open(); + ++ if (arg_keep_fd >= 0) { ++ k = fdset_remove(fds, arg_keep_fd); ++ if (k < 0) { ++ log_error("Failed to remove kept fd: %s", strerror(-k)); ++ goto finish; ++ } ++ ++ /* ensure the kept fd is closed in the child, only the parent keeps it around. */ ++ k = fd_cloexec(arg_keep_fd, true); ++ if (k < 0) { ++ log_error("Failed to set cloexec on kept fd: %s", strerror(-k)); ++ goto finish; ++ } ++ } ++ + if (arg_directory) { + if (path_equal(arg_directory, "/")) { + log_error("Spawning container on root directory not supported."); +-- +2.1.4 + diff --git a/stage1/rootfs/usr_from_src/patches/0003-nspawn-add-pid-file.patch b/stage1/rootfs/usr_from_src/patches/0003-nspawn-add-pid-file.patch new file mode 100644 index 0000000..f753e35 --- /dev/null +++ b/stage1/rootfs/usr_from_src/patches/0003-nspawn-add-pid-file.patch @@ -0,0 +1,90 @@ +From b7c75246350bd7af55f80dd9ac89c23682765aaf Mon Sep 17 00:00:00 2001 +From: Vito Caputo +Date: Mon, 16 Mar 2015 15:13:49 -0700 +Subject: [PATCH 3/3] nspawn: add --pid-file + +Simple mechanism for recording the container's pid independent of any external +daemons/systemd services. +--- + src/nspawn/nspawn.c | 23 ++++++++++++++++++++++- + 1 file changed, 22 insertions(+), 1 deletion(-) + +diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c +index 0277a12..6650629 100644 +--- a/src/nspawn/nspawn.c ++++ b/src/nspawn/nspawn.c +@@ -154,6 +154,7 @@ static bool arg_share_system = false; + static bool arg_register = true; + static bool arg_keep_unit = false; + static int arg_keep_fd = -1; ++static const char *arg_pid_file = NULL; + static char **arg_network_interfaces = NULL; + static char **arg_network_macvlan = NULL; + static bool arg_network_veth = false; +@@ -209,7 +210,8 @@ static int help(void) { + " --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" +- " --keep-fd=FDNUM Do not close the specified file descriptor\n", ++ " --keep-fd=FDNUM Do not close the specified file descriptor\n" ++ " --pid-file=FILE Write child pid to FILE\n", + program_invocation_short_name); + + return 0; +@@ -233,6 +235,7 @@ static int parse_argv(int argc, char *argv[]) { + ARG_REGISTER, + ARG_KEEP_UNIT, + ARG_KEEP_FD, ++ ARG_PID_FILE, + ARG_NETWORK_INTERFACE, + ARG_NETWORK_MACVLAN, + ARG_NETWORK_VETH, +@@ -265,6 +268,7 @@ static int parse_argv(int argc, char *argv[]) { + { "register", required_argument, NULL, ARG_REGISTER }, + { "keep-unit", no_argument, NULL, ARG_KEEP_UNIT }, + { "keep-fd", required_argument, NULL, ARG_KEEP_FD }, ++ { "pid-file", required_argument, NULL, ARG_PID_FILE }, + { "network-interface", required_argument, NULL, ARG_NETWORK_INTERFACE }, + { "network-macvlan", required_argument, NULL, ARG_NETWORK_MACVLAN }, + { "network-veth", no_argument, NULL, ARG_NETWORK_VETH }, +@@ -561,6 +565,10 @@ static int parse_argv(int argc, char *argv[]) { + } + break; + ++ case ARG_PID_FILE: ++ arg_pid_file = optarg; ++ break; ++ + case ARG_PERSONALITY: + + arg_personality = personality_from_string(optarg); +@@ -2757,6 +2765,13 @@ static int wait_for_container(pid_t pid, ContainerStatus *container) { + return r; + } + ++static int write_pid(const char *f, int pid) { ++ char pid_str[13]; /* -2147483646\n\0 */ ++ ++ snprintf(pid_str, sizeof(pid_str), "%i\n", pid); ++ return write_string_file_atomic(f, pid_str); ++} ++ + static void nop_handler(int sig) {} + + int main(int argc, char *argv[]) { +@@ -3317,6 +3332,12 @@ int main(int argc, char *argv[]) { + eventfds[1] = safe_close(eventfds[1]); + + if (r >= 0) { ++ if (arg_pid_file) { ++ r = write_pid(arg_pid_file, pid); ++ if (r < 0) ++ goto finish; ++ } ++ + r = register_machine(pid); + if (r < 0) + goto finish; +-- +2.1.4 +