HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

test/units/TEST-50-DISSECT.dissect.sh

67,676 bytesGit mode 100755Download original fileLGPL-2.1-or-later

Git blob: ad6c064b92f8d6184e62f772ac4c9f628a6654b4

Lines 1001–1438 of 1,438

VBASE="vtest$RANDOM"
VDIR="/tmp/$VBASE.v"
mkdir "$VDIR"
 
ln -s "$MINIMAL_IMAGE.raw" "$VDIR/${VBASE}_33.raw"
ln -s "$MINIMAL_IMAGE.raw" "$VDIR/${VBASE}_34.raw"
ln -s "$MINIMAL_IMAGE.raw" "$VDIR/${VBASE}_35.raw"
 
systemd-run -P -p RootImage="$VDIR" cat /usr/lib/os-release | grep -F "MARKER=1" >/dev/null
 
rm "$VDIR/${VBASE}_33.raw" "$VDIR/${VBASE}_34.raw" "$VDIR/${VBASE}_35.raw"
rmdir "$VDIR"
 
mkdir -p /run/machines /run/portables /run/extensions
touch /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
 
systemd-dissect --discover --json=short >/tmp/discover.json
grep -q -F '{"name":"a","type":"raw","class":"machine","ro":false,"path":"/run/machines/a.raw"' /tmp/discover.json
grep -q -F '{"name":"b","type":"raw","class":"portable","ro":false,"path":"/run/portables/b.raw"' /tmp/discover.json
grep -q -F '{"name":"c","type":"raw","class":"sysext","ro":false,"path":"/run/extensions/c.raw"' /tmp/discover.json
rm /tmp/discover.json /run/machines/a.raw /run/portables/b.raw /run/extensions/c.raw
 
systemd-dissect --discover --system
systemd-dissect --discover --user
systemd-dissect --discover --system --user
 
LOOP="$(systemd-dissect --attach --loop-ref=waldo "$MINIMAL_IMAGE.raw")"
 
# Wait until the symlinks we want to test are established
udevadm trigger -w "$LOOP"
 
# Check if the /dev/loop/* symlinks really reference the right device
test /dev/disk/by-loop-ref/waldo -ef "$LOOP"
 
if [ "$(stat -c '%Hd:%Ld' "$MINIMAL_IMAGE.raw")" != '?d:?d' ] ; then
   # Old stat didn't know the %Hd and %Ld specifiers and turned them into ?d
   # instead. Let's simply skip the test on such old systems.
   test "$(stat -c '/dev/disk/by-loop-inode/%Hd:%Ld-%i' "$MINIMAL_IMAGE.raw")" -ef "$LOOP"
fi
 
# Detach by loopback device
systemd-dissect --detach "$LOOP"
 
# Test long reference name.
# Note, sizeof_field(struct loop_info64, lo_file_name) == 64,
# and --loop-ref accepts upto 63 characters, and udev creates symlink
# based on the name when it has upto _62_ characters.
name="$(for _ in {1..62}; do echo -n 'x'; done)"
LOOP="$(systemd-dissect --attach --loop-ref="$name" "$MINIMAL_IMAGE.raw")"
udevadm trigger -w "$LOOP"
 
# Check if the /dev/disk/by-loop-ref/$name symlink really references the right device
test "/dev/disk/by-loop-ref/$name" -ef "$LOOP"
 
# Detach by the /dev/disk/by-loop-ref symlink
systemd-dissect --detach "/dev/disk/by-loop-ref/$name"
 
name="$(for _ in {1..63}; do echo -n 'x'; done)"
LOOP="$(systemd-dissect --attach --loop-ref="$name" "$MINIMAL_IMAGE.raw")"
udevadm trigger -w "$LOOP"
 
# Check if the /dev/disk/by-loop-ref/$name symlink does not exist
test ! -e "/dev/disk/by-loop-ref/$name"
 
# Detach by backing inode
systemd-dissect --detach "$MINIMAL_IMAGE.raw"
(! systemd-dissect --detach "$MINIMAL_IMAGE.raw")
 
# check for confext functionality
mkdir -p /run/confexts/test/etc/extension-release.d
echo "ID=_any" >/run/confexts/test/etc/extension-release.d/extension-release.test
echo "ARCHITECTURE=_any" >>/run/confexts/test/etc/extension-release.d/extension-release.test
echo "MARKER_CONFEXT_123" >/run/confexts/test/etc/testfile
cat <<EOF >/run/confexts/test/etc/testscript
#!/usr/bin/env bash
echo "This should not happen"
EOF
chmod +x /run/confexts/test/etc/testscript
systemd-confext merge
grep -q -F "MARKER_CONFEXT_123" /etc/testfile
(! /etc/testscript)
systemd-confext status
systemd-confext status --json=pretty | jq -e 'any(.[]; .hierarchy == "/etc" and .extensions == ["test"] and .since != null)' >/dev/null
systemd-confext unmerge
systemd-confext status --json=pretty | jq -e 'any(.[]; .hierarchy == "/etc" and .extensions == [] and .since == null)' >/dev/null
rm -rf /run/confexts/
 
cleanup_sysupdate_notify_confext_mutable() {
    local mutable_marker="$1"
    local notify_socket_was_active="$2"
 
    if [[ "$notify_socket_was_active" == "yes" ]]; then
        systemctl restart systemd-sysupdate-notify-sysext.socket || :
    else
        systemctl stop systemd-sysupdate-notify-sysext.socket || :
    fi
 
    rm -f "$mutable_marker"
    rm -f "/var/lib/extensions.mutable/etc/${mutable_marker#/etc/}"
    systemd-confext unmerge || :
    rm -f /run/systemd/confext.conf.d/50-test-mutable.conf
    rm -rf /run/confexts/
    rmdir /var/lib/extensions.mutable/etc /var/lib/extensions.mutable 2>/dev/null || :
}
 
test_sysupdate_notify_confext_mutable() {
    local mutable_marker="/etc/test-confext-mutable-$RANDOM"
    local notify_socket_was_active=no
 
    if systemctl is-active --quiet systemd-sysupdate-notify-sysext.socket; then
        notify_socket_was_active=yes
    fi
 
    trap 'cleanup_sysupdate_notify_confext_mutable "$mutable_marker" "$notify_socket_was_active"' RETURN ERR
 
    # Check that the sysupdate notification refresh honors confext-specific configuration.
    mkdir -p /run/systemd/confext.conf.d
    cat >/run/systemd/confext.conf.d/50-test-mutable.conf <<EOF
[ConfExt]
Mutable=yes
EOF
    mkdir -p /run/confexts/test/etc/extension-release.d
    echo "ID=_any" >/run/confexts/test/etc/extension-release.d/extension-release.test
    echo "ARCHITECTURE=_any" >>/run/confexts/test/etc/extension-release.d/extension-release.test
    echo "MARKER_CONFEXT_123" >/run/confexts/test/etc/testfile
    systemd-confext merge
    echo "MARKER_CONFEXT_MUTABLE" >"$mutable_marker"
    systemctl start systemd-sysupdate-notify-sysext.socket
    varlinkctl call /run/systemd/sysupdate/notify/io.systemd.sysext io.systemd.SysUpdate.Notify.OnCompletedUpdate '{}'
    grep -F "MARKER_CONFEXT_MUTABLE" "$mutable_marker" >/dev/null
    trap - RETURN ERR
    cleanup_sysupdate_notify_confext_mutable "$mutable_marker" "$notify_socket_was_active"
}
 
test_sysupdate_notify_confext_mutable
 
cleanup_sysupdate_notify_confext_before_sysext() {
    local notify_socket_was_active="$1"
 
    if [[ "$notify_socket_was_active" == "yes" ]]; then
        systemctl restart systemd-sysupdate-notify-sysext.socket || :
    else
        systemctl stop systemd-sysupdate-notify-sysext.socket || :
    fi
 
    systemctl stop test-order-foo.service || :
    systemd-sysext unmerge || :
    systemd-confext unmerge || :
    rm -rf /run/extensions/test-order /run/confexts/test-order
    rm -f /run/test-order-marker
}
 
test_sysupdate_notify_confext_before_sysext() {
    local notify_socket_was_active=no
 
    if systemctl is-active --quiet systemd-sysupdate-notify-sysext.socket; then
        notify_socket_was_active=yes
    fi
 
    trap 'cleanup_sysupdate_notify_confext_before_sysext "$notify_socket_was_active"' RETURN ERR
 
    # Check that the sysupdate notification refresh merges confexts before sysexts, same order as for unit
    # startup at boot. The confext ships a drop-in for a whole prefix of units, meaning it can't know what
    # units to restart through EXTENSION_RESTART_UNITS= if it would be merged after the sysext that defines
    # the unit affected by the drop-in. Only if the confext is merged first the unit picks up the drop-in.
    # This is just one example the general scenario when a confext has to create state before a sysext's
    # service is started and the confext does not know what sysext's services would be affected.
    mkdir -p /run/confexts/test-order/etc/extension-release.d /run/confexts/test-order/etc/systemd/system/test-order-.service.d
    cat >/run/confexts/test-order/etc/extension-release.d/extension-release.test-order <<EOF
ID=_any
ARCHITECTURE=_any
EXTENSION_RELOAD_MANAGER=1
EOF
    cat >/run/confexts/test-order/etc/systemd/system/test-order-.service.d/10-marker.conf <<EOF
[Service]
Environment=MARKER=from-confext
EOF
 
    mkdir -p /run/extensions/test-order/usr/lib/extension-release.d /run/extensions/test-order/usr/lib/systemd/system
    cat >/run/extensions/test-order/usr/lib/extension-release.d/extension-release.test-order <<EOF
ID=_any
ARCHITECTURE=_any
EXTENSION_RELOAD_MANAGER=1
EXTENSION_RESTART_UNITS=test-order-foo.service
EOF
    cat >/run/extensions/test-order/usr/lib/systemd/system/test-order-foo.service <<'EOF'
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=sh -c 'echo "${MARKER:-unset}" >/run/test-order-marker'
EOF
 
    systemctl start systemd-sysupdate-notify-sysext.socket
    varlinkctl call /run/systemd/sysupdate/notify/io.systemd.sysext io.systemd.SysUpdate.Notify.OnCompletedUpdate '{}'
 
    test -f /etc/systemd/system/test-order-.service.d/10-marker.conf
    test -f /usr/lib/systemd/system/test-order-foo.service
    systemctl --quiet is-active test-order-foo.service
    grep -F "from-confext" /run/test-order-marker >/dev/null
 
    trap - RETURN ERR
    cleanup_sysupdate_notify_confext_before_sysext "$notify_socket_was_active"
}
 
test_sysupdate_notify_confext_before_sysext
 
unsquashfs -force -no-xattrs -d /tmp/img "$MINIMAL_IMAGE.raw"
systemd-run --unit=test-root-ephemeral \
    -p RootDirectory=/tmp/img \
    -p RootEphemeral=yes \
    -p Type=exec \
    bash -c "touch /abc && sleep infinity"
test -n "$(ls -A /var/lib/systemd/ephemeral-trees)"
systemctl stop test-root-ephemeral
# shellcheck disable=SC2016
timeout 10 bash -c 'until test -z "$(ls -A /var/lib/systemd/ephemeral-trees)"; do sleep .5; done'
test ! -f /tmp/img/abc
 
# Test RootDirectoryFileDescriptor=
systemd-run --wait --pipe --root-directory=/tmp/img -- grep -q 'MARKER=1' /usr/lib/os-release
# Make sure the same root file descriptor can be reused multiple times.
systemd-run --wait --pipe --same-root-dir -p ExecStartPre=true true
 
systemd-dissect --mtree /tmp/img >/dev/null
systemd-dissect --list /tmp/img >/dev/null
 
read -r SHA256SUM1 _ < <(systemd-dissect --copy-from /tmp/img etc/os-release | sha256sum)
test "$SHA256SUM1" != ""
 
echo abc >abc
systemd-dissect --copy-to /tmp/img abc /abc
test -f /tmp/img/abc
 
# Test --copy-ownership= option
rm -rf /tmp/copychown-test
mkdir -p /tmp/copychown-test/srcdir
echo "test file" >/tmp/copychown-test/srcdir/testfile
chown 1234:5678 /tmp/copychown-test/srcdir/testfile
chown 1234:5678 /tmp/copychown-test/srcdir
 
# Test --copy-ownership=yes preserves ownership for regular files
systemd-dissect --copy-from /tmp/img etc/os-release /tmp/copychown-test/os-release-chown-yes --copy-ownership=yes
test "$(stat -c %u:%g /tmp/copychown-test/os-release-chown-yes)" = "0:0"
 
# Test --copy-ownership=no uses current user for regular files
systemd-dissect --copy-from /tmp/img etc/os-release /tmp/copychown-test/os-release-chown-no --copy-ownership=no
test "$(stat -c %u:%g /tmp/copychown-test/os-release-chown-no)" = "0:0"
 
# Test --copy-ownership=auto (default) does not preserve ownership for regular files
systemd-dissect --copy-from /tmp/img etc/os-release /tmp/copychown-test/os-release-chown-auto
test "$(stat -c %u:%g /tmp/copychown-test/os-release-chown-auto)" = "0:0"
 
# Test --copy-ownership=yes preserves ownership for directories
systemd-dissect --copy-to /tmp/img /tmp/copychown-test/srcdir /copychown-dir-yes --copy-ownership=yes
test "$(stat -c %u:%g /tmp/img/copychown-dir-yes)" = "1234:5678"
test "$(stat -c %u:%g /tmp/img/copychown-dir-yes/testfile)" = "1234:5678"
rm -rf /tmp/img/copychown-dir-yes
 
# Test --copy-ownership=no overrides ownership for directories
systemd-dissect --copy-to /tmp/img /tmp/copychown-test/srcdir /copychown-dir-no --copy-ownership=no
test "$(stat -c %u:%g /tmp/img/copychown-dir-no)" = "0:0"
test "$(stat -c %u:%g /tmp/img/copychown-dir-no/testfile)" = "0:0"
rm -rf /tmp/img/copychown-dir-no
 
# Test --copy-ownership=auto (default) preserves ownership for directories
systemd-dissect --copy-to /tmp/img /tmp/copychown-test/srcdir /copychown-dir-auto
test "$(stat -c %u:%g /tmp/img/copychown-dir-auto)" = "1234:5678"
test "$(stat -c %u:%g /tmp/img/copychown-dir-auto/testfile)" = "1234:5678"
rm -rf /tmp/img/copychown-dir-auto
 
rm -rf /tmp/copychown-test
 
# Test for dissect tool support with systemd-sysext
mkdir -p /run/extensions/ testkit/usr/lib/extension-release.d/
echo "ID=_any" >testkit/usr/lib/extension-release.d/extension-release.testkit
echo "ARCHITECTURE=_any" >>testkit/usr/lib/extension-release.d/extension-release.testkit
echo "MARKER_SYSEXT_123" >testkit/usr/lib/testfile
mksquashfs testkit/ testkit.raw -noappend
cp testkit.raw /run/extensions/
unsquashfs -force -l /run/extensions/testkit.raw
systemd-dissect --no-pager /run/extensions/testkit.raw | grep '✓ sysext for portable service' >/dev/null
systemd-dissect --no-pager /run/extensions/testkit.raw | grep '✓ sysext for system' >/dev/null
systemd-sysext merge
systemd-sysext status
grep -q -F "MARKER_SYSEXT_123" /usr/lib/testfile
systemd-sysext unmerge
rm -rf /run/extensions/ testkit/
 
# Test for dissect tool support with systemd-confext
mkdir -p /run/confexts/ testjob/etc/extension-release.d/
echo "ID=_any" >testjob/etc/extension-release.d/extension-release.testjob
echo "ARCHITECTURE=_any" >>testjob/etc/extension-release.d/extension-release.testjob
echo "MARKER_CONFEXT_123" >testjob/etc/testfile
mksquashfs testjob/ testjob.raw -noappend
cp testjob.raw /run/confexts/
unsquashfs -force -l /run/confexts/testjob.raw
systemd-dissect --no-pager /run/confexts/testjob.raw | grep '✓ confext for system' >/dev/null
systemd-dissect --no-pager /run/confexts/testjob.raw | grep '✓ confext for portable service' >/dev/null
systemd-confext merge
systemd-confext status
grep -q -F "MARKER_CONFEXT_123" /etc/testfile
systemd-confext unmerge
rm -rf /run/confexts/ testjob/
 
systemd-run -P -p RootImage="$MINIMAL_IMAGE.raw" cat /run/host/os-release | cmp "$OS_RELEASE"
 
# Test that systemd-sysext reloads the daemon.
mkdir -p /var/lib/extensions/
ln -s /tmp/app-reload.raw /var/lib/extensions/app-reload.raw
systemd-sysext merge --no-reload
# the service should not be running
(! systemctl --quiet is-active foo.service)
systemd-sysext unmerge --no-reload
systemd-sysext merge
journalctl --sync
# "journalctl -u foo.service" may not work as expected, especially entries for _TRANSPORT=stdout,
# for short-living services or when the service manager generates debugging logs.
# Instead, SYSLOG_IDENTIFIER= should be reliable for stdout. Let's use it.
timeout -v 30s bash -c "journalctl -b SYSLOG_IDENTIFIER=echo _TRANSPORT=stdout -o cat -n all --follow | grep -m 1 -q '^foo$'"
systemd-sysext unmerge --no-reload
# Grep on the Warning to find the warning helper mentioning the daemon reload.
systemctl status foo.service 2>&1 | grep -F "Warning" >/dev/null
systemd-sysext merge
systemd-sysext unmerge
systemctl status foo.service 2>&1 | grep -v -F "Warning" >/dev/null
rm /var/lib/extensions/app-reload.raw
 
# Test that GPT images with an ISO9660 El Torito boot catalog are dissected correctly. The blkid
# superblock filter must prevent the iso9660 superblock from being detected, so dissection proceeds via
# the GPT partition table rather than treating the whole image as a single iso9660 filesystem.
defs="$(mktemp --directory "$IMAGE_DIR/test-50-dissect-eltorito.defs.XXXXXXXXXX")"
imgs="$(mktemp --directory "$IMAGE_DIR/test-50-dissect-eltorito.imgs.XXXXXXXXXX")"
 
tee "$defs/00-esp.conf" <<EOF
[Partition]
Type=esp
Format=vfat
SizeMinBytes=100M
SizeMaxBytes=100M
EOF
 
tee "$defs/10-root.conf" <<EOF
[Partition]
Type=root
Format=ext4
SizeMinBytes=100M
SizeMaxBytes=100M
EOF
 
systemd-repart --pretty=yes \
               --definitions="$defs" \
               --empty=create \
               --size=auto \
               --dry-run=no \
               --offline=yes \
               --el-torito=yes \
               "$imgs/eltorito.img"
 
# Verify the image has both iso9660 superblock and GPT partition table
blkid -o value -s PTTYPE "$imgs/eltorito.img" | grep -x gpt
blkid -o value -s TYPE "$imgs/eltorito.img" | grep -x iso9660
 
# systemd-dissect must process the GPT partitions, not the whole-device iso9660 superblock
systemd-dissect --json=short "$imgs/eltorito.img" | grep -F '"designator":"root"' | grep -F '"fstype":"ext4"'
systemd-dissect --json=short "$imgs/eltorito.img" | grep -F '"designator":"esp"' | grep -F '"fstype":"vfat"'
 
rm -rf "$defs" "$imgs"
 
# Sneak in a couple of expected-to-fail invocations to cover
# https://github.com/systemd/systemd/issues/29610
(! systemd-run -P -p MountImages="/this/should/definitely/not/exist.img:/run/img2\:3:nosuid" false)
(! systemd-run -P -p ExtensionImages="/this/should/definitely/not/exist.img" false)
(! systemd-run -P -p RootImage="/this/should/definitely/not/exist.img" false)
(! systemd-run -P -p ExtensionDirectories="/foo/bar /foo/baz" false)
 
# Ensure a multi-device btrfs doesn't fail to mount due to loopdev
# https://github.com/systemd/systemd/issues/42520:
if [[ -f "${BTRFS_MEMBER1:-}" ]]; then
    img="" mnt="" loop=""
 
    # This block runs under 'set -e'; register cleanup up front so a failure in between can't leak the
    # attached loop device or the image and perturb later device enumeration / udevadm settle.
    # shellcheck disable=SC2317
    cleanup_btrfs_mountimages() {
        if [[ -n "$mnt" ]]; then
            umount -R "$mnt" || true
        fi
        if [[ -n "$loop" ]]; then
            # Drop the members from the kernel's global, boot-wide btrfs device cache before detaching;
            # otherwise the cached entries dangle at recycled loop minors and trip multi-device detection in
            # later tests reusing the same /dev/loopN.
            btrfs device scan --forget "${loop}p1" "${loop}p2" || true
            losetup -d "$loop" || true
            # Pair the detach with a settle, like every other losetup -d in this test, so teardown isn't
            # still in flight when control returns to the broader TEST-50 run.
            udevadm settle --timeout=60 || true
        fi
        rm -f "$img"
        # Only remove the mountpoint once it is confirmed unmounted, so a failed unmount above doesn't make
        # rm -rf recurse through the mountpoint into the still-mounted filesystem.
        if [[ -n "$mnt" ]] && ! mountpoint -q "$mnt"; then
            rm -rf "$mnt"
        fi
    }
    trap cleanup_btrfs_mountimages EXIT
 
    img="$(mktemp /var/tmp/test-50-mountimages-btrfs.img.XXXXXXXXXX)"
    mnt="$(mktemp -d "$IMAGE_DIR/test-50-mountimages-btrfs.mnt.XXXXXXXXXX")"
    truncate -s 600M "$img"
    echo -e 'label: gpt\nsize=280MiB, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, name=data1\ntype=0FC63DAF-8483-4772-8E79-3D69D8477DE4, name=data2' | sfdisk "$img"
    loop="$(losetup --show -P -f "$img")"
    udevadm wait --timeout=60 --settle --initialized=no "${loop}p1" "${loop}p2"
    udevadm lock --timeout=60 --device="$loop" dd if="$BTRFS_MEMBER1" of="${loop}p1" bs=4M
    udevadm lock --timeout=60 --device="$loop" dd if="$BTRFS_MEMBER2" of="${loop}p2" bs=4M
    udevadm settle --timeout=60
    btrfs device scan "${loop}p1" "${loop}p2"
 
    mount -t btrfs "${loop}p1" "$mnt"
    btrfs subvolume create "$mnt/@demo"
    echo "MARKER=1" >"$mnt/@demo/os-release"
    btrfs subvolume create "$mnt/@"
    btrfs subvolume set-default "$mnt/@"
    umount "$mnt"
    mount -t btrfs "${loop}p1" "$mnt"
 
    systemd-run -P \
                -p MountImages="${loop}p1:/run/img-btrfs:subvol=@demo" \
                cat /run/img-btrfs/os-release | grep -F "MARKER=1" >/dev/null
    # Double check that there's no loopdev
    src="$(systemd-run -P \
                    -p MountImages="${loop}p1:/run/img-btrfs:subvol=@demo" \
                    findmnt -n -o SOURCE /run/img-btrfs)"
    assert_eq "${src%%\[*}" "${loop}p1"
 
    trap - EXIT
    cleanup_btrfs_mountimages
fi