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