discards-test 2.31 KB
Newer Older
1 2
#!/bin/bash

3 4
[ -z "$CRYPTSETUP_PATH" ] && CRYPTSETUP_PATH=".."
CRYPTSETUP=$CRYPTSETUP_PATH/cryptsetup
5 6
DEV_NAME="discard-t3st"
DEV=""
7
PWD1="93R4P4pIqAH8"
8 9 10 11 12 13 14 15 16 17

cleanup() {
	[ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
	udevadm settle >/dev/null 2>&1
	rmmod scsi_debug 2>/dev/null
	sleep 2
}

fail()
{
18
	echo "FAILED at line $(caller)"
19 20 21 22 23
	cleanup
	exit 100
}

add_device() {
24
	modprobe scsi_debug $@ delay=0
25 26
	if [ $? -ne 0 ] ; then
		echo "This kernel seems to not support proper scsi_debug module, test skipped."
27
		exit 77
28 29 30
	fi

	sleep 2
31
	DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
32 33 34 35 36 37 38

	DEV="/dev/$DEV"
	[ -b $DEV ] || fail "Cannot find $DEV."
}

function check_version()
{
Milan Broz's avatar
Milan Broz committed
39
	VER_STR=$(dmsetup targets | grep crypt | cut -f 2 -dv)
40 41
	[ -z "$VER_STR" ] && fail "Failed to parse dm-crypt version."

Milan Broz's avatar
Milan Broz committed
42 43
	VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
	VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
44 45

	# option supported in 1.11
Milan Broz's avatar
Milan Broz committed
46 47 48
	test $VER_MAJ -gt 1 && return 0
	test $VER_MIN -ge 11 && return 0
	return 1
49 50 51 52
}

if [ $(id -u) != 0 ]; then
	echo "WARNING: You must be root to run this test, test skipped."
53
	exit 77
54 55
fi

56
modprobe --dry-run scsi_debug || exit 77
57 58 59
modprobe dm-crypt >/dev/null 2>&1
if ! check_version ; then
	echo "Probably old kernel, test skipped."
60
	exit 77
61 62 63 64 65 66 67 68
fi

add_device dev_size_mb=16 sector_size=512 num_tgts=1 lbpu=1

# FIXME test hash of device (unmap -> zero)
# for now just check that flag is enabled

echo "[1] Allowing discards for LUKS device"
69
echo $PWD1 | $CRYPTSETUP luksFormat --type luks1 $DEV -q -i1 || fail
70
echo $PWD1 | $CRYPTSETUP luksOpen $DEV $DEV_NAME --allow-discards || fail
71 72 73 74 75 76 77
$CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
$CRYPTSETUP resize $DEV_NAME --size 100 || fail
$CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
$CRYPTSETUP luksClose $DEV_NAME || fail

echo "[2] Allowing discards for plain device"
78
echo $PWD1 | $CRYPTSETUP create -q $DEV_NAME $DEV --hash sha1 --allow-discards || fail
79 80 81 82 83 84 85
$CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
$CRYPTSETUP resize $DEV_NAME --size 100 || fail
$CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
$CRYPTSETUP remove $DEV_NAME || fail

cleanup