| 1 | #!/usr/bin/envscip-shell filesystem . . `/usr/bin/env`. bash |
| 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | set -eux |
| 4 | set -o pipefail |
| 5 | |
| 6 | if ! command -v storagectl >/dev/nullscip-shell filesystem . . `/dev/null`.; then |
| 7 | echoscip-shell system . . echo. "storagectl not found, skipping." |
| 8 | exit 77 |
| 9 | fi |
| 10 | |
| 11 | # shellcheck source=test/units/util.sh |
| 12 | . "$(dirnamescip-shell system . . dirname. "$0")"/util.sh |
| 13 | |
| 14 | # Unset $PAGER so we don't have to use --no-pager everywhere |
| 15 | export PAGER= |
| 16 | |
| 17 | # storagectl runs in a VM-only test |
| 18 | if systemd-detect-virt -cq ; then |
| 19 | echoscip-shell system . . echo. "can't run in a container, skipping." |
| 20 | exit 77 |
| 21 | fi |
| 22 | |
| 23 | at_exitscip-shell shell systemd 261~rc4-1 at_exit().() { |
| 24 | set +e |
| 25 | |
| 26 | if [[ -n "${MOUNT_DIR:-}" ]] && mountpointscip-shell system . . mountpoint. -q "$MOUNT_DIR"; then |
| 27 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 28 | fi |
| 29 | if [[ -n "${LOOP:-}" ]]; then |
| 30 | systemd-dissect --detach "$LOOP" |
| 31 | fi |
| 32 | if [[ -n "${WORK_DIR:-}" ]]; then |
| 33 | rmscip-shell system . . rm. -fr "$WORK_DIR" |
| 34 | fi |
| 35 | rmscip-shell system . . rm. -fr /var/lib/storage/test-87-storage-*.volume |
| 36 | } |
| 37 | trap at_exit EXIT |
| 38 | |
| 39 | # The storage providers are socket-activated by sockets.target, so the listening |
| 40 | # AF_UNIX sockets should already exist. |
| 41 | testscip-shell system . . test. -S /run/systemd/io.systemd.StorageProvider/blockscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/block`. |
| 42 | testscip-shell system . . test. -S /run/systemd/io.systemd.StorageProvider/fsscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/fs`. |
| 43 | |
| 44 | WORK_DIR="$(mktempscip-shell system . . mktemp. -d /tmp/test-storagectl.XXXXXXXXXXscip-shell filesystem . . `/tmp/test-storagectl.XXXXXXXXXX`.)" |
| 45 | MOUNT_DIR="$WORK_DIR/mnt" |
| 46 | mkdirscip-shell system . . mkdir. -p "$MOUNT_DIR" |
| 47 | |
| 48 | # --- storagectl basic --- |
| 49 | |
| 50 | storagectl --help |
| 51 | storagectl --version |
| 52 | storagectl help |
| 53 | |
| 54 | # Unknown verb / option |
| 55 | (! storagectl this-verb-does-not-exist) |
| 56 | (! storagectl --no-such-option providers) |
| 57 | |
| 58 | # --- storagectl providers --- |
| 59 | |
| 60 | storagectl providers |
| 61 | storagectl providers --no-legend |
| 62 | storagectl providers --no-pager |
| 63 | storagectl providers --json=pretty | jqscip-shell system . . jq. . |
| 64 | storagectl providers --json=short | jqscip-shell system . . jq. . |
| 65 | |
| 66 | providers_output="$(storagectl providers --no-legend)" |
| 67 | assert_in 'block' "$providers_output" |
| 68 | assert_in 'fs' "$providers_output" |
| 69 | assert_in 'yes' "$providers_output" |
| 70 | |
| 71 | # --- storagectl volumes --- |
| 72 | |
| 73 | # 'volumes' is the default verb |
| 74 | storagectl |
| 75 | storagectl volumes |
| 76 | storagectl volumes --no-legend |
| 77 | storagectl volumes --no-pager |
| 78 | storagectl volumes --json=pretty | jqscip-shell system . . jq. . |
| 79 | storagectl volumes --json=short | jqscip-shell system . . jq. . |
| 80 | |
| 81 | # Glob filter that matches nothing should not error |
| 82 | storagectl volumes 'no-such-volume-*' |
| 83 | |
| 84 | # --- storagectl templates --- |
| 85 | |
| 86 | storagectl templates |
| 87 | storagectl templates --no-legend --no-pager |
| 88 | storagectl templates --json=pretty | jqscip-shell system . . jq. . |
| 89 | storagectl templates --json=short | jqscip-shell system . . jq. --seq . |
| 90 | |
| 91 | templates_output="$(storagectl templates --no-legend)" |
| 92 | assert_in 'sparse-file' "$templates_output" |
| 93 | assert_in 'allocated-file' "$templates_output" |
| 94 | assert_in 'directory' "$templates_output" |
| 95 | assert_in 'subvolume' "$templates_output" |
| 96 | |
| 97 | # Glob filter |
| 98 | storagectl templates 'sparse-*' --no-legend | grepscip-shell system . . grep. sparse-file >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 99 | (! storagectl templates 'sparse-*' --no-legend | grepscip-shell system . . grep. allocated-file >/dev/nullscip-shell filesystem . . `/dev/null`.) |
| 100 | storagectl templates 'no-such-template-*' |
| 101 | |
| 102 | # --- direct varlink calls --- |
| 103 | |
| 104 | varlinkctl introspect /run/systemd/io.systemd.StorageProvider/blockscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/block`. io.systemd.StorageProvider |
| 105 | varlinkctl introspect /run/systemd/io.systemd.StorageProvider/fsscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/fs`. io.systemd.StorageProvider |
| 106 | |
| 107 | # Block provider does not expose templates |
| 108 | varlinkctl call --more /run/systemd/io.systemd.StorageProvider/blockscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/block`. \ |
| 109 | io.systemd.StorageProvider.ListTemplates '{}' \ |
| 110 | --graceful=io.systemd.StorageProvider.NoSuchTemplate |
| 111 | |
| 112 | # fs provider lists the four built-in templates |
| 113 | varlinkctl call --more --json=short /run/systemd/io.systemd.StorageProvider/fsscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/fs`. \ |
| 114 | io.systemd.StorageProvider.ListTemplates '{}' | grepscip-shell system . . grep. '"name":"sparse-file"' >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 115 | |
| 116 | # Block provider rejects names not under /dev/ |
| 117 | varlinkctl call /run/systemd/io.systemd.StorageProvider/blockscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/block`. \ |
| 118 | io.systemd.StorageProvider.Acquire '{"name":"/tmp/no-such-dev"}' \ |
| 119 | --graceful=io.systemd.StorageProvider.NoSuchVolume |
| 120 | |
| 121 | # fs provider rejects bad volume names (contain '/' → not a valid filename) |
| 122 | varlinkctl call /run/systemd/io.systemd.StorageProvider/fsscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/fs`. \ |
| 123 | io.systemd.StorageProvider.Acquire '{"name":"bad/name"}' \ |
| 124 | --graceful=org.varlink.service.InvalidParameter |
| 125 | |
| 126 | # --- mount.storage: regular file via fs provider --- |
| 127 | |
| 128 | TESTVOL_REG="test-87-storage-reg-$RANDOM" |
| 129 | truncatescip-shell system . . truncate. -s 32M "/var/lib/storage/$TESTVOL_REG.volume" |
| 130 | mkfs.ext4 "/var/lib/storage/$TESTVOL_REG.volume" |
| 131 | mountscip-shell system . . mount. -t storage.ext4 "fs:$TESTVOL_REG" "$MOUNT_DIR" |
| 132 | mountpointscip-shell system . . mountpoint. -q "$MOUNT_DIR" |
| 133 | echoscip-shell system . . echo. "hello reg" >"$MOUNT_DIR/hello" |
| 134 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 135 | |
| 136 | # Volume now appears in 'storagectl volumes' |
| 137 | volumes_after_create="$(storagectl volumes "$TESTVOL_REG" --no-legend)" |
| 138 | assert_in "$TESTVOL_REG" "$volumes_after_create" |
| 139 | assert_in 'reg' "$volumes_after_create" |
| 140 | |
| 141 | # Re-mount existing (default storage.create=any) |
| 142 | mountscip-shell system . . mount. -t storage.ext4 "fs:$TESTVOL_REG" "$MOUNT_DIR" |
| 143 | testscip-shell system . . test. -f "$MOUNT_DIR/hello" |
| 144 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 145 | |
| 146 | # storage.create=open succeeds for existing volume |
| 147 | mountscip-shell system . . mount. -t storage.ext4 -o "storage.create=open" "fs:$TESTVOL_REG" "$MOUNT_DIR" |
| 148 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 149 | |
| 150 | # storage.create=new on existing volume must fail |
| 151 | (! mountscip-shell system . . mount. -t storage.ext4 -o "storage.create=new,storage.create-size=16M" "fs:$TESTVOL_REG" "$MOUNT_DIR") |
| 152 | |
| 153 | # Read-only mount |
| 154 | mountscip-shell system . . mount. -t storage.ext4 -o ro "fs:$TESTVOL_REG" "$MOUNT_DIR" |
| 155 | findmntscip-shell system . . findmnt. -n -o options "$MOUNT_DIR" | grepscip-shell system . . grep. -E '(^|,)ro(,|$)' >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 156 | (! touchscip-shell system . . touch. "$MOUNT_DIR/readonly-test") |
| 157 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 158 | |
| 159 | rmscip-shell system . . rm. -f "/var/lib/storage/$TESTVOL_REG.volume" |
| 160 | |
| 161 | # storage.create=open on missing volume must fail |
| 162 | (! mountscip-shell system . . mount. -t storage.ext4 -o "storage.create=open" "fs:test-87-storage-missing-$RANDOM" "$MOUNT_DIR") |
| 163 | |
| 164 | # --- mount.storage: directory volume via fs provider (requires idmapped mounts) --- |
| 165 | |
| 166 | TESTVOL_DIR="test-87-storage-dir-$RANDOM" |
| 167 | if mountscip-shell system . . mount. -t storage "fs:$TESTVOL_DIR" "$MOUNT_DIR"; then |
| 168 | mountpointscip-shell system . . mountpoint. -q "$MOUNT_DIR" |
| 169 | testscip-shell system . . test. -d "/var/lib/storage/$TESTVOL_DIR.volume/root" |
| 170 | echoscip-shell system . . echo. "dir test" >"$MOUNT_DIR/hello" |
| 171 | testscip-shell system . . test. -f "/var/lib/storage/$TESTVOL_DIR.volume/root/hello" |
| 172 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 173 | rmscip-shell system . . rm. -fr "/var/lib/storage/$TESTVOL_DIR.volume" |
| 174 | else |
| 175 | echoscip-shell system . . echo. "Directory volume mounting failed (idmapped mounts unsupported?), skipping." |
| 176 | rmscip-shell system . . rm. -fr "/var/lib/storage/$TESTVOL_DIR.volume" |
| 177 | fi |
| 178 | |
| 179 | # --- mount.storage: block device via block provider --- |
| 180 | |
| 181 | truncatescip-shell system . . truncate. -s 32M "$WORK_DIR/block.img" |
| 182 | mkfs.ext4 -L sd-storage-blk "$WORK_DIR/block.img" |
| 183 | LOOP="$(systemd-dissect --attach --loop-ref=test-storagectl "$WORK_DIR/block.img")" |
| 184 | |
| 185 | mountscip-shell system . . mount. -t storage.ext4 "block:$LOOP" "$MOUNT_DIR" |
| 186 | mountpointscip-shell system . . mountpoint. -q "$MOUNT_DIR" |
| 187 | echoscip-shell system . . echo. "hello blk" >"$MOUNT_DIR/hello" |
| 188 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 189 | |
| 190 | # Read-only mount of the block volume |
| 191 | mountscip-shell system . . mount. -t storage.ext4 -o ro "block:$LOOP" "$MOUNT_DIR" |
| 192 | findmntscip-shell system . . findmnt. -n -o options "$MOUNT_DIR" | grepscip-shell system . . grep. -E '(^|,)ro(,|$)' >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 193 | testscip-shell system . . test. -f "$MOUNT_DIR/hello" |
| 194 | umountscip-shell system . . umount. "$MOUNT_DIR" |
| 195 | |
| 196 | # Block volume is enumerable; matchName globs over device node and aliases |
| 197 | varlinkctl call --more --json=short /run/systemd/io.systemd.StorageProvider/blockscip-shell filesystem . . `/run/systemd/io.systemd.StorageProvider/block`. \ |
| 198 | io.systemd.StorageProvider.ListVolumes "{\"matchName\":\"$LOOP\"}" | |
| 199 | grepscip-shell system . . grep. '"type":"blk"' >/dev/nullscip-shell filesystem . . `/dev/null`. |
| 200 | |
| 201 | systemd-dissect --detach "$LOOP" |
| 202 | unset LOOP |
| 203 | |
| 204 | # --- error cases --- |
| 205 | |
| 206 | # Bad provider name (no such socket) |
| 207 | (! mountscip-shell system . . mount. -t storage.ext4 "no-such-provider:foo" "$MOUNT_DIR") |
| 208 | # Bad volume specification (no colon) |
| 209 | (! mountscip-shell system . . mount. -t storage.ext4 "no-colon-here" "$MOUNT_DIR") |
| 210 | # Refuse nested storage volumes (FS type "storage.storage") |
| 211 | (! mountscip-shell system . . mount. -t storage.storage "fs:something" "$MOUNT_DIR") |