Replace some tabs.
[platform/upstream/cryptsetup.git] / tests / discards-test
1 #!/bin/bash
2
3 CRYPTSETUP="../src/cryptsetup"
4 DEV_NAME="discard-t3st"
5 DEV=""
6
7 cleanup() {
8         [ -b /dev/mapper/$DEV_NAME ] && dmsetup remove $DEV_NAME
9         udevadm settle >/dev/null 2>&1
10         rmmod scsi_debug 2>/dev/null
11         sleep 2
12 }
13
14 fail()
15 {
16         echo "FAILED"
17         cleanup
18         exit 100
19 }
20
21 add_device() {
22         modprobe scsi_debug $@
23         if [ $? -ne 0 ] ; then
24                 echo "This kernel seems to not support proper scsi_debug module, test skipped."
25                 exit 0
26         fi
27
28         sleep 2
29         DEV=$(grep scsi_debug /sys/block/*/device/model | cut -f4 -d /)
30
31         DEV="/dev/$DEV"
32         [ -b $DEV ] || fail "Cannot find $DEV."
33 }
34
35 function check_version()
36 {
37         VER_STR=$(dmsetup targets | grep crypt | cut -f 2 -dv)
38         VER_MAJ=$(echo $VER_STR | cut -f 1 -d.)
39         VER_MIN=$(echo $VER_STR | cut -f 2 -d.)
40
41         # option supported in 1.11
42         test $VER_MAJ -gt 1 && return 0
43         test $VER_MIN -ge 11 && return 0
44         return 1
45 }
46
47 if [ $(id -u) != 0 ]; then
48         echo "WARNING: You must be root to run this test, test skipped."
49         exit 0
50 fi
51
52 modprobe --dry-run scsi_debug || exit 0
53 modprobe dm-crypt >/dev/null 2>&1
54 if ! check_version ; then
55         echo "Probably old kernel, test skipped."
56         exit 0
57 fi
58
59 add_device dev_size_mb=16 sector_size=512 num_tgts=1 lbpu=1
60
61 # FIXME test hash of device (unmap -> zero)
62 # for now just check that flag is enabled
63
64 echo "[1] Allowing discards for LUKS device"
65 echo xxx | $CRYPTSETUP luksFormat $DEV -q -i1 || fail
66 echo xxx | $CRYPTSETUP luksOpen $DEV $DEV_NAME --allow-discards || fail
67 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
68 $CRYPTSETUP resize $DEV_NAME --size 100 || fail
69 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
70 dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
71 $CRYPTSETUP luksClose $DEV_NAME || fail
72
73 echo "[2] Allowing discards for plain device"
74 echo xxx | $CRYPTSETUP create $DEV_NAME $DEV --hash sha1 --allow-discards || fail
75 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
76 $CRYPTSETUP resize $DEV_NAME --size 100 || fail
77 $CRYPTSETUP status $DEV_NAME | grep flags | grep discards >/dev/null || fail
78 dmsetup table $DEV_NAME | grep allow_discards >/dev/null || fail
79 $CRYPTSETUP remove $DEV_NAME || fail
80
81 cleanup