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