|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set -eux |
| 4 | + |
| 5 | +if [ "$(id -u)" != '0' -o -z "${1:-}" ]; then |
| 6 | + echo "*** run as: unshare -Umr $0 /path/to/tmpdir" |
| 7 | + false |
| 8 | +fi |
| 9 | + |
| 10 | +top="$1" |
| 11 | +test -d "${top}" |
| 12 | +test -w "${top}" |
| 13 | + |
| 14 | +mkd() { |
| 15 | + mkdir -p "$1" |
| 16 | + echo "$1" |
| 17 | +} |
| 18 | + |
| 19 | +assert_fail() { |
| 20 | + if "$@"; then |
| 21 | + echo "*** unexpectedly passed: $*" |
| 22 | + false |
| 23 | + fi |
| 24 | +} |
| 25 | + |
| 26 | +imageid='0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef' |
| 27 | +null='' |
| 28 | + |
| 29 | +# this is present in the initramfs itself |
| 30 | +config="${top}/config" |
| 31 | +tee "${config}" <<EOF |
| 32 | +EOF |
| 33 | + |
| 34 | +# this is the filesystem directly on the block device |
| 35 | +blkdev="$(mkd "${top}/blkdev")" |
| 36 | +repo="$(mkd "${blkdev}/composefs")" |
| 37 | +state="$(mkd "${blkdev}/state")" |
| 38 | +deployment="$(mkd "${state}/${imageid}")" |
| 39 | +dply_etc="$(mkd "${deployment}/etc")" |
| 40 | +dply_etc_u="$(mkd "${dply_etc}/upper")" |
| 41 | +dply_etc_w="$(mkd "${dply_etc}/work")" |
| 42 | +dply_var="$(mkd "${deployment}/var")" |
| 43 | + |
| 44 | +# fake composefs content (because we can't mount erofs) |
| 45 | +root="$(mkd "${top}/root-fs")" |
| 46 | +root_etc="$(mkd "${root}/etc")" |
| 47 | +root_var="$(mkd "${root}/var")" |
| 48 | +root_sysroot="$(mkd "${root}/sysroot")" |
| 49 | +mount -o bind,ro "${root}" "${root}" # see open_root_fs() |
| 50 | + |
| 51 | +# this emulates the initramfs initially mounting the block device on /sysroot |
| 52 | +sysroot="$(mkd "${top}/sysroot")" |
| 53 | +mount -o bind "${blkdev}" "${sysroot}" |
| 54 | + |
| 55 | +composefs-setup-root \ |
| 56 | + --config "${config}" \ |
| 57 | + --cmdline "composefs=${imageid}" \ |
| 58 | + --root-fs "${root}" \ |
| 59 | + --sysroot "${sysroot}" \ |
| 60 | + ${null} |
| 61 | + |
| 62 | +grep "${top}" /proc/mounts |
| 63 | + |
| 64 | +# these are in the newly mounted root filesystem now |
| 65 | +var="${sysroot}/var" |
| 66 | +etc="${sysroot}/etc" |
| 67 | + |
| 68 | +assert_fail touch "${sysroot}/a" # read-only |
| 69 | +touch "${etc}/file.conf" |
| 70 | +touch "${var}/db" |
| 71 | + |
| 72 | +# make sure those went into the expected places |
| 73 | +test -f "${dply_etc}/upper/file.conf" |
| 74 | +test -f "${dply_var}/db" |
| 75 | + |
| 76 | +find "${top}" |
| 77 | + |
| 78 | +umount -R "${sysroot}" # this should entirely reverse the impact of the pivot |
| 79 | +umount -R "${root}" # reverse the ro-bindmount above |
| 80 | +assert_fail grep "${top}" /proc/mounts # make sure nothing else is mounted |
| 81 | + |
| 82 | +find "${top}" |
| 83 | + |
| 84 | +# from here we can inspect the after-effects |
0 commit comments