Debian Codegraph @

kpartx/test-kpartx

raw
1 #! /bin/bash
2
3 # Run shellcheck with "-e SC2016", this is causing too many annotations
4 # (for each push_cleanup invocation).
5
6 # This is a unit test program for kpartx, in particular for deleting partitions.
7 #
8 # The rationale is the following:
9 #
10 # 1) kpartx should delete all mappings it created beforehand.
11 # 2) kpartx should handle partitions on dm devices and other devices
12 # (e.g. loop devices) equally well.
13 # 3) kpartx should only delete "partitions", which are single-target
14 # linear mappings into a block device. Other maps should not be touched.
15 # 4) kpartx should only delete mappings it created itself beforehand.
16 # In particular, it shouldn't delete LVM LVs, even if they are fully
17 # contained in the block device at hand and thus look like partitions
18 # in the first place. (For historical compatibility reasons, we allow
19 # such mappings to be deleted with the -f/--force flag).
20 # 5) DM map names may be changed, thus kpartx shouldn't rely on them to
21 # check whether a mapping is a partition of a particular device. It is
22 # legal for a partition of /dev/loop0 to be named "loop0".
23
24 # Note: This program tries hard to clean up, but if tests fail,
25 # stale DM or loop devices may keep lurking around.
26
27 # Set WORKDIR in environment to existing dir to for persistence
28 # WARNING: existing files will be truncated.
29 # If empty, test will be done in temporary dir
30 : "${WORKDIR:=}"
31 # Set this environment variable to test an alternative kpartx executable
32 : "${KPARTX:=}"
33 # Options to pass to kpartx always
34 : "${KPARTX_OPTS:=-s}"
35 # Set non-empty to enable some debug messages
36 : "${KPARTX_DEBUG:=}"
37 # Time to wait for device nodes to appear (microseconds)
38 # Waiting is only needed if "s" is not in $KPARTX_OPTS
39 : "${WAIT_US:=0}"
40
41 # IMPORTANT: The ERR trap is essential for this program to work correctly!
42 trap 'LINE=$LINENO
43 trap - ERR
44 eval echo "$0: == error in \\\"\$BASH_COMMAND\\\" \(\\\"$BASH_COMMAND\\\"\) on line $LINE ==" >&2
45 exit 1' ERR INT TERM
46 trap 'cleanup' 0
47
48 CLEANUP=:
49 cleanup() {
50 trap - ERR
51 trap - 0
52 if [[ "$OK" ]]; then
53 echo "== all tests completed successfully ==" >&2
54 else
55 echo "== step $STEP failed ==" >&2
56 fi
57 if [[ "$KPARTX_DEBUG" ]]; then
58 eval "echo \"== BEGIN CLEANUP ==
59 $CLEANUP
60 == END CLEANUP ==\""
61 fi
62 eval "$CLEANUP" &>/dev/null
63 }
64
65 push_cleanup() {
66 CLEANUP="$*
67 $CLEANUP"
68 }
69
70 pop_cleanup() {
71 # CAUTION: simplistic
72 CLEANUP=${CLEANUP#*;}
73 }
74
75 step() {
76 STEP=$*
77 echo "== Test step: $STEP ==" >&2
78 }
79
80 mk_partitions() {
81 # prefer sfdisk, as parted will try to create
82 # partition mappings by itself
83 if command -v sfdisk &>/dev/null; then
84 printf ",+,\n" | sfdisk -f "$1" &>/dev/null
85 else
86 parted -s "$1" mklabel msdos
87 parted -s -- "$1" mkpart prim ext2 1MiB -1s
88 fi
89 }
90
91 wipe_ptable() {
92 dd if=/dev/zero of="$1" bs=1b count=1 &>/dev/null
93 }
94
95 current_state() {
96 [[ "$KPARTX_DEBUG" ]] || return 0
97 echo "--------------------"
98 dmsetup ls
99 }
100
101
102 # Default name of partition device
103 partname() {
104 local base
105 base=${1##*/}
106
107 # Is the last character a digit?
108 if [[ "${base:$((${#base}-1))}" =~ [0-9] ]]; then
109 printf "/dev/mapper/%sp%s" "$base" "$2"
110 else
111 printf "/dev/mapper/%s%s" "$base" "$2"
112 fi
113 }
114
115 wait_a_moment() {
116 sleep "$(printf %d.%06d "$((WAIT_USEC / 1000000))" "$((WAIT_USEC % 1000000))")"
117 }
118
119 step preparation
120
121 [[ "$UID" -eq 0 ]]
122 [[ "$KPARTX" ]] || {
123 if [[ -f "$PWD/kpartx" && -x "$PWD/kpartx" ]]; then
124 KPARTX=$PWD/kpartx
125 elif [[ -f "$PWD/kpartx/kpartx" && -x "$PWD/kpartx/kpartx" ]]; then
126 KPARTX=$PWD/kpartx/kpartx
127 else
128 KPARTX=$(command -v kpartx)
129 fi
130 }
131 [[ "$KPARTX" ]]
132 echo "== Using kpartx = $KPARTX ==" >&2
133
134 # Try to use system kpartx for cleanup
135 # Fall back to ours if not found
136 # shellcheck disable=SC2034
137 CLEANUP_KPARTX=$(command -v kpartx) || CLEANUP_KPARTX=$KPARTX
138
139 FILE1=kpartx1
140 FILE2=kpartx2
141 FILE3=kpartx3
142 FILE4=kpartx4
143 SIZE=$((1024*1024*1024)) # use bytes as units here
144 SECTSIZ=512
145 OFFS=32 # offset of linear mapping into dev, sectors
146 VG=__kpvg__ # volume group name
147 LV=__kplv__ # logical vol name
148 OK=
149
150 [[ "$WORKDIR" ]] || {
151 WORKDIR=$(mktemp -d /tmp/kpartx-XXXXXX)
152 # shellcheck disable=SC2016
153 push_cleanup 'rm -rf "$WORKDIR"'
154 }
155
156 if [[ "$( (lvmconfig --typeconfig full 2>/dev/null || true) | \
157 sed -n 's/.*use_devicesfile=//p' )" = 1 ]]; then
158 LVMCONF=
159 # This test may modify the devices file. Be sure to restore it
160 if [[ -f /etc/lvm/devices/system.devices ]]; then
161 cp -a /etc/lvm/devices/system.devices "$WORKDIR"
162 push_cleanup 'cp -a $WORKDIR/system.devices /etc/lvm/devices'
163 else
164 push_cleanup 'rm -f /etc/lvm/devices/system.devices'
165 fi
166 else
167 # This isn't shell code, it's actually a single lvm command argument.
168 # shellcheck disable=SC2089
169 LVMCONF='devices { filter = [ "a|/dev/loop.*|", r".*" ] }'
170 fi
171
172 push_cleanup "cd $PWD"
173 # If cd fails, the script will terminate
174 # shellcheck disable=SC2164
175 cd "$WORKDIR"
176
177 step "create loop devices"
178 truncate -s "$SIZE" "$FILE1"
179 truncate -s "$SIZE" "$FILE2"
180 truncate -s "$SIZE" "$FILE3"
181 truncate -s "$SIZE" "$FILE4"
182
183 LO1=$(losetup -f "$FILE1" --show)
184 push_cleanup 'losetup -d "$LO1"'
185 LO2=$(losetup -f "$FILE2" --show)
186 push_cleanup 'losetup -d "$LO2"'
187 LO3=$(losetup -f "$FILE3" --show)
188 push_cleanup 'losetup -d "$LO3"'
189 LO4=$(losetup -f "$FILE4" --show)
190 push_cleanup 'losetup -d "$LO4"'
191
192 [[ "$LO1" && "$LO2" && "$LO3" && "$LO4" && -b "$LO1" && -b "$LO2" && -b "$LO3" && -b "$LO4" ]]
193 DEV1=$(stat -c "%t:%T" "$LO1")
194 DEV2=$(stat -c "%t:%T" "$LO2")
195
196 wait_a_moment
197
198 SPAN1=__kpt__
199 SPAN2=$(basename "$LO2")
200 # This is a non-kpartx pseudo "partition" mapping
201 USER1=user1
202 step "create DM devices => $SPAN1, $SPAN2, $USER1"
203 # Create two linear mappings spanning two loopdevs.
204 # One of them gets a pathological name colliding with
205 # the loop device name.
206 # These mappings must not be removed by kpartx.
207 # They also serve as DM devices to test partition removal on those.
208
209 TABLE="\
210 0 $((SIZE/SECTSIZ-OFFS)) linear $DEV1 $OFFS
211 $((SIZE/SECTSIZ-OFFS)) $((SIZE/SECTSIZ-OFFS)) linear $DEV2 $OFFS"
212
213 dmsetup create "$SPAN1" <<<"$TABLE"
214 push_cleanup 'dmsetup remove -f "$SPAN1"'
215
216 dmsetup create "$SPAN2" <<<"$TABLE"
217 push_cleanup 'dmsetup remove -f "$SPAN2"'
218
219 push_cleanup 'dmsetup remove -f "$USER1"'
220 dmsetup create "$USER1" <<EOF
221 0 $((SIZE/SECTSIZ-OFFS)) linear $DEV1 $OFFS
222 EOF
223
224 wait_a_moment
225 [[ -b "/dev/mapper/$SPAN1" ]]
226 [[ -b "/dev/mapper/$SPAN2" ]]
227 [[ -b "/dev/mapper/$USER1" ]]
228
229 step "create vg and lv on $LO3 => $VG-$LV, $USER1"
230 # On the 3rd loop device, we create a VG and an LV
231 # The LV should not be removed by kpartx.
232 # shellcheck disable=SC2090
233 pvcreate ${LVMCONF:+--config "$LVMCONF"} -f "$LO3"
234 # shellcheck disable=SC2090
235 vgcreate ${LVMCONF:+--config "$LVMCONF"} "$VG" "$LO3"
236 push_cleanup 'vgremove ${LVMCONF:+--config "$LVMCONF"} -f "$VG"'
237 # shellcheck disable=SC2090
238 lvcreate ${LVMCONF:+--config "$LVMCONF"} -L "$((SIZE/2))B" -n "$LV" "$VG"
239 push_cleanup 'lvremove ${LVMCONF:+--config "$LVMCONF"} -f "$VG/$LV"'
240 wait_a_moment
241
242 current_state
243 [[ -b "/dev/mapper/$VG-$LV" ]]
244
245 # dmsetup table /dev/mapper/$VG-$LV
246 # dmsetup info /dev/mapper/$VG-$LV
247
248 step "create partitions on loop devices => $LO1 $LO2 $LO4"
249
250 mk_partitions "$LO1"
251 mk_partitions "$LO2"
252 mk_partitions "$LO4"
253
254
255 # Test invocation of kpartx with regular file here
256 LO2P1=/dev/mapper/$(basename "$LO2")-foo1
257 push_cleanup 'dmsetup remove -f "$(basename $LO2P1)"'
258 step "run kpartx on regular file $FILE2 => $LO2P1"
259 # shellcheck disable=2086
260 $KPARTX $KPARTX_OPTS -a -p -foo "$FILE2"
261 current_state
262 [[ -b "$LO2P1" ]]
263
264 step "deleting partition table on $LO2 => -$LO2P1"
265 wipe_ptable "$LO2"
266 # shellcheck disable=2086
267 $KPARTX $KPARTX_OPTS -d "$LO2"
268 current_state
269 [[ ! -b "$LO2P1" ]]
270
271 step "re-add just removed partions on $LO2 => $LO2P1"
272 mk_partitions "$LO2"
273 # shellcheck disable=2086
274 $KPARTX $KPARTX_OPTS -a -p -foo "$FILE2"
275 current_state
276 [[ -b "$LO2P1" ]]
277
278 LO1P1=/dev/mapper/$(basename "$LO1")-eggs1
279 step "run kpartx on loop device $LO1 => $LO1P1"
280 push_cleanup 'dmsetup remove -f $(basename "$LO1P1")'
281 # shellcheck disable=2086
282 $KPARTX $KPARTX_OPTS -a -p -eggs "$LO1"
283
284 wait_a_moment
285 current_state
286 [[ -b "$LO1P1" ]]
287 [[ -b "$LO2P1" ]]
288
289 # dmsetup info $LO2P1
290
291 step "rename $(basename "$LO1P1") -> $(basename "$LO1")"
292 # Set pathological name for partition on $LO1 (same as loop device itself)
293 dmsetup rename "$(basename "$LO1P1")" "$(basename "$LO1")"
294 LO1P1=/dev/mapper/$(basename "$LO1")
295 pop_cleanup
296 push_cleanup 'dmsetup remove -f "$(basename "$LO1P1")"'
297
298 current_state
299 [[ -b "$LO1P1" ]]
300
301 mk_partitions "/dev/mapper/$SPAN2"
302 SPAN2P1=/dev/mapper/${SPAN2}-bar1
303 step "create partitions on DM device $SPAN2 => $SPAN2P1"
304 # shellcheck disable=2086
305 $KPARTX $KPARTX_OPTS -a -p -bar "/dev/mapper/$SPAN2"
306 wait_a_moment
307 current_state
308 [[ -b "$SPAN2P1" ]]
309
310 # udev rules may have created partition mappings without UUIDs
311 # which aren't removed by default (if system standard kpartx doesn't
312 # set the UUID). Remove them using -f
313 push_cleanup '$CLEANUP_KPARTX $KPARTX_OPTS -f -d "/dev/mapper/$SPAN2"'
314 push_cleanup 'dmsetup remove -f "$(basename "$SPAN2P1")"'
315
316 step "create partitions on DM device $SPAN1 => $SPAN1P1"
317 # shellcheck disable=2086
318 $KPARTX $KPARTX_OPTS -a -p -spam "/dev/mapper/$SPAN1"
319 SPAN1P1=/dev/mapper/${SPAN1}-spam1
320 # see above
321 push_cleanup '$CLEANUP_KPARTX $KPARTX_OPTS -f -d "/dev/mapper/$SPAN1"'
322 push_cleanup 'dmsetup remove -f "$(basename "$SPAN1P1")"'
323
324 wait_a_moment
325 current_state
326 [[ -b "$SPAN2P1" ]]
327 [[ -b "$SPAN1P1" ]]
328
329 step "rename partitions on $SPAN1 with default delimiter => $SPAN1P1 -> $(partname "$SPAN1" 1)"
330 # shellcheck disable=2086
331 $KPARTX $KPARTX_OPTS -u "/dev/mapper/$SPAN1"
332 current_state
333 [[ ! -b "$SPAN1P1" ]]
334 # This assumes that $SPAN1 ends in a non-digit
335 [[ -b "$(partname "$SPAN1" 1)" ]]
336
337 step "rename partitions on DM device with delimiter -spam => $(partname "$SPAN1" 1) -> $SPAN1P1"
338 # shellcheck disable=2086
339 $KPARTX $KPARTX_OPTS -u -p -spam "/dev/mapper/$SPAN1"
340 current_state
341 [[ -b "$SPAN1P1" ]]
342 [[ ! -b "$(partname "$SPAN1" 1)" ]]
343
344 step "delete partitions on DM device $SPAN1 => -$SPAN1P1"
345 # shellcheck disable=2086
346 $KPARTX $KPARTX_OPTS -d "/dev/mapper/$SPAN1" >&2
347 wait_a_moment
348
349 current_state
350 [[ -b "$SPAN2P1" ]]
351 [[ -b "$LO1P1" ]]
352 [[ -b "$LO2P1" ]]
353 [[ ! -b "$SPAN1P1" ]]
354
355 step "delete partitions on DM device $SPAN2 => -$SPAN2P1"
356 # shellcheck disable=2086
357 $KPARTX $KPARTX_OPTS -d "/dev/mapper/$SPAN2"
358 wait_a_moment
359 current_state
360 [[ -b "$LO1P1" ]]
361 [[ -b "$LO2P1" ]]
362 [[ ! -b "$SPAN2P1" ]]
363
364 step "rename on $LO2 with delimiter -spam => $LO2P1 -> ${LO2P1//-foo/-spam}"
365 # shellcheck disable=2086
366 $KPARTX $KPARTX_OPTS -u -p -spam "$LO2"
367 wait_a_moment
368 current_state
369 [[ ! -b "$LO2P1" ]]
370 [[ -b "${LO2P1//-foo/-spam}" ]]
371
372 step "rename partitions on $LO2 with delimiter -foo => ${LO2P1//-foo/-spam} -> $LO2P1"
373 # shellcheck disable=2086
374 $KPARTX $KPARTX_OPTS -u -p -foo "$LO2"
375 wait_a_moment
376 current_state
377 [[ -b "$LO2P1" ]]
378 [[ ! -b "${LO2P1//-foo/-spam}" ]]
379
380 step "rename partitions on $LO2 with default delimiter => $LO2P1 -> $(partname "$LO2" 1)"
381 # shellcheck disable=2086
382 $KPARTX $KPARTX_OPTS -u "$LO2"
383 wait_a_moment
384 current_state
385 [[ ! -b "$LO2P1" ]]
386 [[ -b "$(partname "$LO2" 1)" ]]
387
388 step "rename partitions on $LO2 with delimiter -foo => $(partname "$LO2" 1) -> $LO2P1"
389 # shellcheck disable=2086
390 $KPARTX $KPARTX_OPTS -u -p -foo "$LO2"
391 current_state
392 [[ -b "$LO2P1" ]]
393 [[ ! -b "$(partname "$LO2" 1)" ]]
394
395 step "rename partitions on $LO2 with delimiter spam => $LO2P1 -> ${LO2P1//-foo/spam} "
396 # shellcheck disable=2086
397 $KPARTX $KPARTX_OPTS -u -p spam "$LO2"
398 wait_a_moment
399 current_state
400 [[ ! -b "$LO2P1" ]]
401 [[ -b "${LO2P1//-foo/spam}" ]]
402
403 step "delete partitions on loop device $LO3"
404 # shellcheck disable=2086
405 $KPARTX $KPARTX_OPTS -d "$LO3"
406 wait_a_moment
407 current_state
408
409 step "delete partitions on file $FILE2 / $LO2"
410 # This will also delete the loop device
411 # shellcheck disable=2086
412 $KPARTX $KPARTX_OPTS -d "$FILE2"
413 wait_a_moment
414 current_state
415
416 step "delete partitions on file $LO1"
417 # shellcheck disable=2086
418 $KPARTX $KPARTX_OPTS -d "$LO1"
419 wait_a_moment
420 current_state
421 [[ ! -b "$LO1P1" ]]
422
423 pop_cleanup # 'dmsetup remove -f $(basename "$SPAN1P1")'
424 [[ ! -b "$LO2P1" ]]
425 pop_cleanup # '$CLEANUP_KPARTX $KPARTX_OPTS -f -d /dev/mapper/$SPAN1'
426
427 # spans should not have been removed
428 current_state
429 [[ -b "/dev/mapper/$SPAN1" ]]
430 [[ -b "/dev/mapper/$SPAN2" ]]
431 [[ -b "/dev/mapper/$USER1" ]]
432 # LVs neither
433 [[ -b "/dev/mapper/$VG-$LV" ]]
434
435 step "delete partitions on $LO3 with -f"
436
437 # shellcheck disable=2086
438 $KPARTX $KPARTX_OPTS -f -d "$LO3"
439 # -d -f should delete the LV, too
440 [[ ! -b "/dev/mapper/$VG-$LV" ]]
441 [[ -b "/dev/mapper/$SPAN1" ]]
442 [[ -b "/dev/mapper/$SPAN2" ]]
443
444 step "test kpartx creation/deletion on an image file with no existing loopdev"
445 losetup -d "$LO4"
446
447 # shellcheck disable=2086
448 OUTPUT=$($KPARTX $KPARTX_OPTS -v -a "$FILE4" 2>&1)
449 read -r loop dm < \
450 <(sed -n 's/^add map \(loop[0-9]*\)p1 ([0-9]*:\([0-9]*\)).*$/\1 dm-\2/p' \
451 <<<"$OUTPUT")
452 [[ "$dm" && "$loop" ]]
453 push_cleanup 'dmsetup remove -f "/dev/$dm"'
454 push_cleanup 'losetup -d "/dev/$loop"'
455
456 [[ -b "/dev/mapper/${loop}p1" ]]
457 # shellcheck disable=2086
458 $KPARTX -d $KPARTX_OPTS "$FILE4"
459 [[ ! -b "/dev/mapper/${loop}p1" ]]
460 # /dev/$loop is _not_ automatically deleted
461 [[ -b "/dev/${loop}" ]]
462
463 OK=yes