From: Motiejus Jakštys Date: Mon, 4 May 2020 16:57:40 +0000 (+0300) Subject: nspawn: mount custom paths before writing to /etc X-Git-Tag: v246-rc1~450 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5c4deb9a5c29cf7b4f53009a1fb439b816cb3a34;p=thirdparty%2Fsystemd.git nspawn: mount custom paths before writing to /etc Consider such configuration: $ systemd-nspawn --read-only --timezone=copy --resolv-conf=copy-host \ --overlay="+/etc::/etc" <...> Assuming one wants `/` to be read-only, DNS and `/etc/localtime` to work. One way to do it is to create an overlay filesystem in `/etc/`. However, systemd-nspawn tries to create `/etc/resolv.conf` and `/etc/localtime` before mounting the custom paths, while `/` (and, by extension, `/etc`) is read-only. Thus it fails to create those files. Mounting custom paths before modifying anything in `/etc/` makes this possible. Full example: ``` $ debootstrap buster /var/lib/machines/t1 http://deb.debian.org/debian $ systemd-nspawn --private-users=false --timezone=copy --resolv-conf=copy-host --read-only --tmpfs=/var --tmpfs=/run --overlay="+/etc::/etc" -D /var/lib/machines/t1 ping -c 1 example.com Spawning container t1 on /var/lib/machines/t1. Press ^] three times within 1s to kill container. ping: example.com: Temporary failure in name resolution Container t1 failed with error code 130. ``` With the patch: ``` $ sudo ./build/systemd-nspawn --private-users=false --timezone=copy --resolv-conf=copy-host --read-only --tmpfs=/var --tmpfs=/run --overlay="+/etc::/etc" -D /var/lib/machines/t1 ping -qc 1 example.com Spawning container t1 on /var/lib/machines/t1. Press ^] three times within 1s to kill container. PING example.com (93.184.216.34) 56(84) bytes of data. --- example.org ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 110.912/110.912/110.912/0.000 ms Container t1 exited successfully. ``` --- diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index c2148596b79..43c6b6845ed 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -3486,6 +3486,16 @@ static int outer_child( if (r < 0) return r; + r = mount_custom( + directory, + arg_custom_mounts, + arg_n_custom_mounts, + arg_uid_shift, + arg_selinux_apifs_context, + MOUNT_NON_ROOT_ONLY); + if (r < 0) + return r; + r = setup_timezone(directory); if (r < 0) return r; @@ -3502,16 +3512,6 @@ static int outer_child( if (r < 0) return r; - r = mount_custom( - directory, - arg_custom_mounts, - arg_n_custom_mounts, - arg_uid_shift, - arg_selinux_apifs_context, - MOUNT_NON_ROOT_ONLY); - if (r < 0) - return r; - if (!arg_use_cgns) { r = mount_cgroups( directory,