3 # This is a unit test program for kpartx, in particular for deleting partitions.
5 # The rationale is the following:
7 # 1) kpartx should delete all mappings it created beforehand.
8 # 2) kpartx should handle partitions on dm devices and other devices
9 # (e.g. loop devices) equally well.
10 # 3) kpartx should only delete "partitions", which are single-target
11 # linear mappings into a block device. Other maps should not be touched.
12 # 4) kpartx should only delete mappings it created itself beforehand.
13 # In particular, it shouldn't delete LVM LVs, even if they are fully
14 # contained in the block device at hand and thus look like partitions
15 # in the first place. (For historical compatibility reasons, we allow
16 # such mappings to be deleted with the -f/--force flag).
17 # 5) DM map names may be changed, thus kpartx shouldn't rely on them to
18 # check whether a mapping is a partition of a particular device. It is
19 # legal for a partition of /dev/loop0 to be named "loop0".
21 # Note: This program tries hard to clean up, but if tests fail,
22 # stale DM or loop devices may keep lurking around.
24 # Set WORKDIR in environment to existing dir to for persistence
25 # WARNING: exisiting files will be truncated.
26 # If empty, test will be done in temporary dir
28 # Set this environment variable to test an alternative kpartx executable
30 # Options to pass to kpartx always
32 # Time to wait for device nodes to appear (microseconds)
33 # Waiting is only needed if "s" is not in $KPARTX_OPTS
36 # IMPORTANT: The ERR trap is essential for this program to work correctly!
37 trap 'LINE=$LINENO; trap - ERR; echo "== error in $BASH_COMMAND on line $LINE ==" >&2; exit 1' ERR
45 echo == all tests completed successfully == >&2
47 echo == step $STEP failed == >&2
49 eval "$CLEANUP" &>/dev/null
63 echo == Test step: $STEP == >&2
67 parted -s $1 mklabel msdos
68 parted -s -- $1 mkpart prim ext2 1MiB -1s
72 dd if=/dev/zero of=$1 bs=1b count=1
79 if [[ -x $PWD/kpartx/kpartx ]]; then
80 KPARTX=$PWD/kpartx/kpartx
82 KPARTX=$(which kpartx)
90 SIZE=$((1024*1024*1024)) # use bytes as units here
92 OFFS=32 # offset of linear mapping into dev, sectors
93 VG=kpvg # volume group name
94 LV=kplv # logical vol name
95 LVMCONF='devices { filter = [ "a|/dev/loop.*|", r".*" ] }'
100 WORKDIR=$(mktemp -d /tmp/kpartx-XXXXXX)
101 push_cleanup 'rm -rf $WORKDIR'
104 push_cleanup "cd $PWD"
107 step "create loop devices"
108 truncate -s $SIZE $FILE1
109 truncate -s $SIZE $FILE2
110 truncate -s $SIZE $FILE3
112 LO1=$(losetup -f $FILE1 --show)
113 push_cleanup 'losetup -d $LO1'
114 LO2=$(losetup -f $FILE2 --show)
115 push_cleanup 'losetup -d $LO2'
116 LO3=$(losetup -f $FILE3 --show)
117 push_cleanup 'losetup -d $LO3'
119 [[ $LO1 && $LO2 && $LO3 && -b $LO1 && -b $LO2 && -b $LO3 ]]
120 DEV1=$(stat -c "%t:%T" $LO1)
121 DEV2=$(stat -c "%t:%T" $LO2)
122 DEV3=$(stat -c "%t:%T" $LO3)
126 step "create DM devices (spans)"
127 # Create two linear mappings spanning two loopdevs.
128 # One of them gets a pathological name colliding with
129 # the loop device name.
130 # These mappings must not be removed by kpartx.
131 # They also serve as DM devices to test partition removal on those.
134 0 $((SIZE/SECTSIZ-OFFS)) linear $DEV1 $OFFS
135 $((SIZE/SECTSIZ-OFFS)) $((SIZE/SECTSIZ-OFFS)) linear $DEV2 $OFFS"
138 SPAN2=$(basename $LO2)
139 dmsetup create $SPAN1 <<<"$TABLE"
140 push_cleanup 'dmsetup remove -f $SPAN1'
142 dmsetup create $SPAN2 <<<"$TABLE"
143 push_cleanup 'dmsetup remove -f $SPAN2'
146 [[ -b /dev/mapper/$SPAN1 ]]
147 [[ -b /dev/mapper/$SPAN2 ]]
149 step "create vg on $LO3"
150 # On the 3rd loop device, we create a VG and an LV
151 # The LV should not be removed by kpartx.
152 pvcreate --config "$LVMCONF" -f $LO3
153 vgcreate --config "$LVMCONF" $VG $LO3
154 push_cleanup 'vgremove --config "$LVMCONF" -f $VG'
155 lvcreate --config "$LVMCONF" -L $((SIZE/2))B -n $LV $VG
156 push_cleanup 'lvremove --config "$LVMCONF" -f $VG/$LV'
159 [[ -b /dev/mapper/$VG-$LV ]]
161 # dmsetup table /dev/mapper/$VG-$LV
162 # dmsetup info /dev/mapper/$VG-$LV
164 step "create partitions on loop devices"
169 # Test invocation of kpartx with regular file here
170 LO2P1=/dev/mapper/$(basename $LO2)-foo1
171 $KPARTX $KPARTX_OPTS -a -p -foo $FILE2
173 push_cleanup 'dmsetup remove -f $(basename $LO2P1)'
175 step "remove partitions with deleted ptable"
177 $KPARTX $KPARTX_OPTS -d $LO2
181 $KPARTX $KPARTX_OPTS -a -p -foo $FILE2
184 LO1P1=/dev/mapper/$(basename $LO1)-eggs1
185 $KPARTX $KPARTX_OPTS -a -p -eggs $LO1
186 push_cleanup 'dmsetup remove -f $(basename $LO1P1)'
192 # dmsetup info $LO2P1
194 # Set pathological name for partition on $LO1 (same as loop device itself)
195 dmsetup rename $(basename $LO1P1) $(basename $LO1)
196 LO1P1=/dev/mapper/$(basename $LO1)
198 push_cleanup 'dmsetup remove -f $(basename $LO1P1)'
200 # dmsetup info $LO1P1
202 step "create partitions on DM devices"
203 mk_partitions /dev/mapper/$SPAN2
205 $KPARTX $KPARTX_OPTS -a -p -bar /dev/mapper/$SPAN2
206 SPAN2P1=/dev/mapper/${SPAN2}-bar1
208 # udev rules may have created partition mappings without UUIDs
209 # which aren't removed by default (if system standard kpartx doesn't
210 # set the UUID). Remove them using -f
211 push_cleanup '$KPARTX $KPARTX_OPTS -f -d /dev/mapper/$SPAN2'
212 push_cleanup 'dmsetup remove -f $(basename $SPAN2P1)'
214 $KPARTX $KPARTX_OPTS -a -p -spam /dev/mapper/$SPAN1
215 SPAN1P1=/dev/mapper/${SPAN1}-spam1
217 push_cleanup '$KPARTX $KPARTX_OPTS -f -d /dev/mapper/$SPAN1'
218 push_cleanup 'dmsetup remove -f $(basename $SPAN1P1)'
224 step "rename partitions on DM device to default"
225 $KPARTX $KPARTX_OPTS -u /dev/mapper/$SPAN1
226 [[ ! -b ${SPAN1P1} ]]
227 # This assumes that $SPAN1 ends in a non-digit
228 [[ -b ${SPAN1P1//-spam/} ]]
230 step "rename partitions on DM device back from default"
231 $KPARTX $KPARTX_OPTS -u -p -spam /dev/mapper/$SPAN1
233 [[ ! -b ${SPANP1//-foo/} ]]
235 step "delete partitions on DM devices"
236 $KPARTX $KPARTX_OPTS -d /dev/mapper/$SPAN1 >&2
244 $KPARTX $KPARTX_OPTS -d /dev/mapper/$SPAN2
251 step "rename partitions on loop device"
252 $KPARTX $KPARTX_OPTS -u -p -spam $LO2
254 [[ -b ${LO2P1//-foo/-spam} ]]
256 step "rename partitions on loop device back"
257 $KPARTX $KPARTX_OPTS -u -p -foo $LO2
259 [[ ! -b ${LO2P1//-foo/-spam} ]]
261 step "rename partitions on loop device to default"
262 $KPARTX $KPARTX_OPTS -u $LO2
265 # $LO1 ends in a digit
266 [[ -b ${LO2P1//-foo/p} ]]
268 step "rename partitions on loop device back from default"
269 $KPARTX $KPARTX_OPTS -u -p -foo $LO2
271 [[ ! -b ${LO2P1//-foo/p} ]]
273 step "rename partitions on loop devices"
274 $KPARTX $KPARTX_OPTS -u -p spam $LO2
276 step "delete partitions on loop devices"
278 $KPARTX $KPARTX_OPTS -d $LO3
280 # This will also delete the loop device
281 $KPARTX $KPARTX_OPTS -d $FILE2
282 $KPARTX $KPARTX_OPTS -d $LO1
290 # spans should not have been removed
291 [[ -b /dev/mapper/$SPAN1 ]]
292 [[ -b /dev/mapper/$SPAN2 ]]
294 [[ -b /dev/mapper/$VG-$LV ]]
296 step "delete partitions on $LO3 with -f"
298 $KPARTX $KPARTX_OPTS -f -d $LO3
299 # -d -f should delete the LV, too
300 [[ ! -b /dev/mapper/$VG-$LV ]]
301 [[ -b /dev/mapper/$SPAN1 ]]
302 [[ -b /dev/mapper/$SPAN2 ]]