Rename align test (Anyone mentioned autoconfigure?). Add some minor changes, run...
[platform/upstream/cryptsetup.git] / tests / align-test
1 #!/bin/bash
2
3 CRYPTSETUP="../src/cryptsetup"
4 DEV=""
5
6 cleanup() {
7         udevadm settle
8         rmmod scsi_debug 2>/dev/null
9         sleep 2
10 }
11
12 fail()
13 {
14         [ -n "$1" ] && echo "$1"
15         cleanup
16         exit 100
17 }
18
19 add_device() {
20         modprobe scsi_debug $@
21         sleep 2
22         DEV=$(grep scsi_debug /sys/block/*/device/model | cut -f4 -d /)
23
24         if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
25                 echo "This kernel seems to not support topology info, test skipped."
26                 cleanup
27                 exit 0
28         fi
29
30         DEV="/dev/$DEV"
31         [ -b $DEV ] || fail "Cannot find $DEV."
32 }
33
34
35 format() # key_bits expected [forced]
36 {
37         if [ -z "$3" ] ; then
38                 echo -n "Formatting using topology info ($1 bits key)...."
39                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -s $1
40         else
41                 echo -n "Formatting using forced offset $3 ($1 bits key)..."
42                 echo xxx| $CRYPTSETUP luksFormat $DEV -q -s $1 --align-payload=$2
43         fi
44
45         ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
46         #echo "ALIGN = $ALIGN"
47
48         if [ $ALIGN -ne $2 ] ; then
49                 echo "FAIL"
50                 echo "Expected alignment differs: expected $2 != detected $ALIGN"
51                 fail
52         fi
53         echo "PASSED"
54 }
55
56 if [ $(id -u) != 0 ]; then
57         echo "WARNING: You must be root to run this test, test skipped."
58         exit 0
59 fi
60
61 modprobe --dry-run scsi_debug || exit 0
62 cleanup
63
64 echo "# Create desktop-class 4K drive"
65 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
66 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
67 format 256 2112
68 format 128 1088
69 format 256 8192 8192
70 format 128 8192 8192
71 cleanup
72
73 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
74 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
75 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
76 format 256 2119
77 format 128 1095
78 cleanup
79
80 echo "# Create enterprise-class 4K drive"
81 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
82 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
83 format 256 2560
84 format 128 1536 
85 cleanup
86
87 echo "# Create classic 512b drive and stack dm-linear"
88 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
89 add_device dev_size_mb=16 sector_size=512 num_tgts=1
90 DEV2=$DEV
91 DEV=/dev/mapper/luks0xbabe
92 dmsetup create luks0xbabe --table "0 32768 linear $DEV2 0"
93 format 256 2112
94 format 128 1088
95 format 128 8192 8192
96 dmsetup remove luks0xbabe
97 cleanup