Skip to content

Commit 2544bae

Browse files
lucabHuijingHei
authored andcommitted
core/dracut/ignition-ostree: add a bwrap-in-sysroot helper
This introduces a new `coreos-sysroot-bwrap` helper in initramfs, for binaries that need to be executed with the final sysroot as a target, but before the pivot-root happens.
1 parent 6bfd01c commit 2544bae

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Needed to work around the initrd `rootfs` / filesystem not being a valid
4+
# mount to pivot out of. For reference, see:
5+
# - https://github.com/torvalds/linux/blob/26bc672134241a080a83b2ab9aa8abede8d30e1c/fs/namespace.c#L3605
6+
# - https://gist.github.com/jlebon/fb6e7c6dcc3ce17d3e2a86f5938ec033
7+
set -euo pipefail
8+
9+
TMP_CHROOT_DIR=""
10+
11+
main() {
12+
setup_chroot_tmpdir
13+
run_chrooted_bwrap "$@"
14+
}
15+
16+
setup_chroot_tmpdir() {
17+
TMP_CHROOT_DIR=$(mktemp --directory --tmpdir=/mnt '.coreos-sysroot-bwrap.tmp.XXXXXXXXXX')
18+
mount --bind / "${TMP_CHROOT_DIR}"
19+
mount --make-private "${TMP_CHROOT_DIR}"
20+
mount --bind "${TMP_CHROOT_DIR}" "${TMP_CHROOT_DIR}"
21+
for mnt in proc sys dev; do
22+
mount --bind /$mnt "${TMP_CHROOT_DIR}"/$mnt
23+
done
24+
touch "${TMP_CHROOT_DIR}"/run/ostree-booted
25+
mount --bind /sysroot "${TMP_CHROOT_DIR}"/sysroot
26+
}
27+
28+
run_chrooted_bwrap() {
29+
chroot "${TMP_CHROOT_DIR}" \
30+
/usr/bin/env --chdir /sysroot \
31+
bwrap \
32+
--unshare-pid --unshare-uts --unshare-ipc --unshare-net \
33+
--unshare-cgroup-try --dev /dev --proc /proc --chdir / \
34+
--ro-bind usr /usr --bind etc /etc --dir /tmp --tmpfs /var/tmp \
35+
--tmpfs /run --ro-bind /run/ostree-booted /run/ostree-booted \
36+
--symlink usr/lib /lib \
37+
--symlink usr/lib64 /lib64 \
38+
--symlink usr/bin /bin \
39+
--symlink usr/sbin /sbin -- "$@"
40+
}
41+
42+
cleanup() {
43+
if test -z "${TMP_CHROOT_DIR}"; then
44+
return
45+
fi
46+
47+
umount --lazy --recursive "${TMP_CHROOT_DIR}"
48+
umount --recursive "${TMP_CHROOT_DIR}"
49+
rmdir "${TMP_CHROOT_DIR}"
50+
}
51+
52+
trap cleanup EXIT
53+
main "$@"

overlay.d/05core/usr/lib/dracut/modules.d/40ignition-ostree/module-setup.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ installkernel() {
2222

2323
install() {
2424
inst_multiple \
25+
bwrap \
2526
realpath \
27+
rmdir \
2628
setfiles \
2729
chcon \
2830
systemd-sysusers \
@@ -103,4 +105,5 @@ install() {
103105
/usr/libexec/coreos-check-rootfs-size
104106

105107
inst_script "$moddir/coreos-relabel" /usr/bin/coreos-relabel
108+
inst_script "$moddir/coreos-sysroot-bwrap" /usr/bin/coreos-sysroot-bwrap
106109
}

0 commit comments

Comments
 (0)