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.
This commit is contained in:
Vito Caputo
2015-03-23 19:08:20 -07:00
parent 0219d440c7
commit fdcd649471
9 changed files with 346 additions and 22 deletions
+11
View File
@@ -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}'"} \
+9
View File
@@ -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
+34 -19
View File
@@ -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.
+11 -3
View File
@@ -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)
+1
View File
@@ -1,2 +1,3 @@
mkdir -p "$ROOT"
cp -a ../usr_from_coreos/rootfs/. "$ROOT"
ln -sf "coreos" "$ROOT/flavor"
+49
View File
@@ -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
@@ -0,0 +1,29 @@
From 1eba7c03e8689ae5d96e0f258bedf6ed96b1741f Mon Sep 17 00:00:00 2001
From: Vito Caputo <vito.caputo@coreos.com>
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
@@ -0,0 +1,112 @@
From 3cd7128052c0c0e691b936523ac211ca8acd9b85 Mon Sep 17 00:00:00 2001
From: Vito Caputo <vito.caputo@coreos.com>
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
@@ -0,0 +1,90 @@
From b7c75246350bd7af55f80dd9ac89c23682765aaf Mon Sep 17 00:00:00 2001
From: Vito Caputo <vito.caputo@coreos.com>
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