-Setting up swapspace version 1, size = 20967 kB
+Setting up swapspace version 1, size = 5238 kB
LABEL=testSwap, no uuid
fi
}
+function ts_device_init {
+ IMAGE="$TS_OUTDIR/$TS_NAME.img"
+ IMAGE_RE=$( echo "$IMAGE" | sed 's:/:\\/:g' )
+ dd if=/dev/zero of="$IMAGE" bs=1M count=5 &> /dev/null
+ $TS_CMD_LOSETUP -f "$IMAGE" 2>&1 >> $TS_OUTPUT
+ DEVICE=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' )
+
+ if [ -z "$DEVICE" ]; then
+ ts_device_deinit
+ return 1 # error
+ fi
+
+ return 0 # succes
+}
+
+function ts_device_deinit {
+ if [ -b "$DEVICE" ]; then
+ $TS_CMD_UMOUNT "$DEVICE" &> /dev/null
+ $TS_CMD_LOSETUP -d "$DEVICE" &> /dev/null
+ rm -f "$IMAGE" &> /dev/null
+ fi
+}
+
set -o pipefail
-
touch $TS_OUTPUT
-IMAGE="$TS_OUTDIR/loop-swap.img"
-IMAGE_RE=$( echo $IMAGE | sed 's:/:\\/:g' )
LABEL="testSwap"
ldd $TS_CMD_SWAPON | grep -q 'libvolume_id' 2>&1 >> $TS_OUTPUT
HAS_VOLUMEID="yes"
fi
-dd if=/dev/zero of=$IMAGE bs=1M count=20 &> /dev/null
-
-DEVICE=$( $TS_CMD_LOSETUP -f )
-$TS_CMD_LOSETUP $DEVICE $IMAGE 2>&1 >> $TS_OUTPUT
-
-# XXX: atomic version: depends on "losetup -a"
-## create the device
-#$TS_CMD_LOSETUP -f $IMAGE 2>&1 >> $TS_OUTPUT
-#
-## get the device name
-#DEVICE=$( $TS_CMD_LOSETUP -a | gawk 'BEGIN {FS=":"} /'$IMAGE_RE'/ { print $1 }' )
-
-# make the swap
+ts_device_init || ts_finalize
$TS_CMD_MKSWAP -L $LABEL $DEVICE 2>&1 >> $TS_OUTPUT
# check it
if [ "$($TS_CMD_MOUNT --guess-fstype $DEVICE)" != "swap" ]; then
echo "Cannot found Linux swap on $DEVICE" >> $TS_OUTPUT
- $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT
+ ts_device_deinit
ts_finalize
fi
if [ -n "$HAS_VOLUMEID" ] && [ ! -L "/dev/disk/by-label/$LABEL" ]; then
- $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT
+ ts_device_deinit
ts_skip "udev ignores /dev/loop*"
fi
if [ "$?" != "0" ]; then
echo "Cannot found $DEVICE in /proc/swaps" >> $TS_OUTPUT
- $TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT
+ ts_device_deinit
ts_finalize
fi
REMSWAPOFF="true"
fi
$TS_CMD_SWAPOFF $DEVICE 2>&1 >> $TS_OUTPUT
-$TS_CMD_LOSETUP -d $DEVICE 2>&1 >> $TS_OUTPUT
+ts_device_deinit
if [ -n "$REMSWAPOFF" ]; then
rm -f $TS_CMD_SWAPOFF
ts_init
ts_skip_nonroot
-# clean up
-test_cleanup() {
- $TS_CMD_UMOUNT $MOUNTPOINT &> /dev/null
- rmdir $MOUNTPOINT &> /dev/null
- $TS_CMD_LOSETUP -d $DEVICE &> /dev/null
- rm -f $IMAGE
-}
-
touch $TS_OUTPUT
-PWD=$(pwd)
-
# mountpoint
-MOUNTPOINT="$PWD/$TS_OUTDIR/mnt-remount"
-mkdir $MOUNTPOINT
-
-# initializa device
-IMAGE="$TS_OUTDIR/loop-remount.img"
-
-dd if=/dev/zero of=$IMAGE bs=1M count=20 &> /dev/null
+MOUNTPOINT="$(pwd)/$TS_OUTDIR/mnt-remount"
+mkdir -p $MOUNTPOINT
-DEVICE=$( $TS_CMD_LOSETUP -f )
-$TS_CMD_LOSETUP $DEVICE $IMAGE 2>&1 >> $TS_OUTPUT
+ts_device_init || ts_finalize
mkfs.ext2 $DEVICE &> /dev/null
# mount read-write
if [ "$?" != "0" ]; then
echo "Cannot found $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT
- test_cleanup
+ ts_device_deinit
ts_finalize
fi
if [ "$?" != "0" ]; then
echo "Cannot found remounted $MOUNTPOINT in /etc/mtab" >> $TS_OUTPUT
- test_cleanup
+ ts_device_deinit
ts_finalize
fi
-test_cleanup
+ts_device_deinit
echo "Success" >> $TS_OUTPUT
ts_finalize