Include 'sys/sysmacros.h' for GCC-9
[platform/upstream/cryptsetup.git] / tests / align-test
1 #!/bin/bash
2
3 CRYPTSETUP="../src/cryptsetup"
4 DEV=""
5 DEV_STACKED="luks0xbabe"
6 MNT_DIR="./mnt_luks"
7 PWD1="93R4P4pIqAH8"
8 PWD2="mymJeD8ivEhE"
9
10 cleanup() {
11         udevadm settle >/dev/null 2>&1
12         if [ -d "$MNT_DIR" ] ; then
13             umount -f $MNT_DIR 2>/dev/null
14             rmdir $MNT_DIR 2>/dev/null
15         fi
16         [ -b /dev/mapper/$DEV_STACKED ] && dmsetup remove $DEV_STACKED >/dev/null 2>&1
17         rmmod scsi_debug 2>/dev/null
18         sleep 2
19 }
20
21 fail()
22 {
23         if [ -n "$1" ] ; then echo "FAIL $1" ; else echo "FAIL" ; fi
24         cleanup
25         exit 100
26 }
27
28 skip()
29 {
30         echo "TEST SKIPPED: $1"
31         cleanup
32         exit 0
33 }
34
35 add_device() {
36         modprobe scsi_debug $@
37         if [ $? -ne 0 ] ; then
38                 echo "This kernel seems to not support proper scsi_debug module, test skipped."
39                 exit 0
40         fi
41
42         sleep 2
43         DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
44
45         if [ ! -e /sys/block/$DEV/alignment_offset ] ; then
46                 echo "This kernel seems to not support topology info, test skipped."
47                 cleanup
48                 exit 0
49         fi
50
51         DEV="/dev/$DEV"
52         [ -b $DEV ] || fail "Cannot find $DEV."
53 }
54
55 format() # key_bits expected [forced]
56 {
57         if [ -z "$3" ] ; then
58                 echo -n "Formatting using topology info ($1 bits key)..."
59                 echo $PWD1 | $CRYPTSETUP luksFormat $DEV -q -i1 -c aes-cbc-essiv:sha256 -s $1
60         else
61                 echo -n "Formatting using forced sector alignment $3 ($1 bits key)..."
62                 echo $PWD1 | $CRYPTSETUP luksFormat $DEV -q -i1 -s $1 -c aes-cbc-essiv:sha256 --align-payload=$2
63         fi
64
65         ALIGN=$($CRYPTSETUP luksDump $DEV |grep "Payload offset" | sed -e s/.*\\t//)
66         #echo "ALIGN = $ALIGN"
67
68         [ -z "$ALIGN" ] && fail
69         [ $ALIGN -ne $2 ] && fail "Expected alignment differs: expected $2 != detected $ALIGN"
70
71         # test some operation, just in case
72         echo -e "$PWD1\n$PWD2" | $CRYPTSETUP luksAddKey $DEV -i1 --key-slot 1
73         [ $? -ne 0 ] && fail "Keyslot add failed."
74
75         $CRYPTSETUP -q luksKillSlot $DEV 1
76         [ $? -ne 0 ] && fail "Keyslot removal failed."
77
78         echo "PASSED"
79 }
80
81 get_offsets()
82 {
83         $CRYPTSETUP luksDump $DEV | grep "$1" | cut -s -d ':' -f 2 | sed  -e 's/\s//g' -e :a -e N -e 's/\n/:/g' -e 's/\s//g' -e ta
84 }
85
86 format_null()
87 {
88         if [ $3 -eq 0 ] ; then
89                 echo -n "Formatting using topology info ($1 bits key) [slot 0"
90                 echo $PWD1 | $CRYPTSETUP luksFormat $DEV -q -i1 -c null -s $1
91         else
92                 echo -n "Formatting using forced sector alignment $3 ($1 bits key) [slot 0"
93                 echo $PWD1 | $CRYPTSETUP luksFormat $DEV -q -i1 -c null -s $1 --align-payload=$3
94         fi
95
96         POFF=$(get_offsets "Payload offset")
97         [ -z "$POFF" ] && fail
98         [ $POFF != $2 ] && fail "Expected data offset differs: expected $2 != detected $POFF"
99         if [ -n "$4" ] ; then
100                 for j in 1 2 3 4 5 6 7 ; do
101                         echo -e "$PWD1\n$PWD2$j" | $CRYPTSETUP luksAddKey $DEV -q -i1 --key-slot $j -c null $PARAMS
102                         echo -n $j
103                         [ $? -ne 0 ] && fail
104                 done
105
106                 KOFF=$(get_offsets "Key material offset")
107                 [ -z "$KOFF" ] && fail
108                 [ $KOFF != $4 ] && fail "Expected keyslots offsets differ: expected $4 != detected $KOFF"
109         fi
110
111         echo "]...PASSED"
112 }
113
114 if [ $(id -u) != 0 ]; then
115         echo "WARNING: You must be root to run this test, test skipped."
116         exit 0
117 fi
118
119 modprobe --dry-run scsi_debug || exit 0
120 cleanup
121
122 echo "# Create desktop-class 4K drive"
123 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=0)"
124 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 num_tgts=1
125 format 256 4096
126 format 256 2112 8
127 format 128 2048
128 format 128 1088 8
129 format 256 8192 8192
130 format 128 8192 8192
131 cleanup
132
133 echo "# Create desktop-class 4K drive w/ 63-sector DOS partition compensation"
134 echo "# (logical_block_size=512, physical_block_size=4096, alignment_offset=3584)"
135 add_device dev_size_mb=16 sector_size=512 physblk_exp=3 lowest_aligned=7 num_tgts=1
136 format 256 4103
137 format 256 2119 8
138 format 128 2055
139 format 128 1095 8
140 cleanup
141
142 echo "# Create enterprise-class 4K drive"
143 echo "# (logical_block_size=4096, physical_block_size=4096, alignment_offset=0)"
144 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
145 format 256 4096
146 format 256 2560 8
147 format 128 2048
148 format 128 1536 8
149 cleanup
150
151 echo "# Create classic 512B drive and stack dm-linear"
152 echo "# (logical_block_size=512, physical_block_size=512, alignment_offset=0)"
153 add_device dev_size_mb=16 sector_size=512 num_tgts=1
154 DEV2=$DEV
155 DEV=/dev/mapper/$DEV_STACKED
156 dmsetup create $DEV_STACKED --table "0 32768 linear $DEV2 0"
157 format 256 4096
158 format 256 2112 8
159 format 128 2048
160 format 128 1088 8
161 format 128 8192 8192
162 cleanup
163
164 echo "# Offset check: 512B sector drive"
165 add_device dev_size_mb=16 sector_size=512 num_tgts=1
166 #           |k| expO reqO expected slot offsets
167 format_null  64 2048    0 8:72:136:200:264:328:392:456
168 format_null  64  520    1
169 format_null  64  520    8
170 format_null  64  640  128
171 format_null  64 2048 2048
172 format_null 128 2048    0 8:136:264:392:520:648:776:904
173 format_null 128 1032    1
174 format_null 128 1032    8
175 format_null 128 1152  128
176 format_null 128 2048 2048
177 format_null 256 4096    0 8:264:520:776:1032:1288:1544:1800
178 format_null 256 2056    1
179 format_null 256 2056    8
180 format_null 256 2176  128
181 format_null 256 4096 2048
182 format_null 512 4096    0 8:512:1016:1520:2024:2528:3032:3536
183 format_null 512 4040    1
184 format_null 512 4040    8
185 format_null 512 4096  128
186 format_null 512 4096 2048
187 cleanup
188
189 echo "# Offset check: 4096B sector drive"
190 add_device dev_size_mb=16 sector_size=4096 num_tgts=1
191 format_null  64 2048    0 8:72:136:200:264:328:392:456
192 format_null  64  520    1
193 format_null  64  520    8
194 format_null  64  640  128
195 format_null  64 2048 2048
196 format_null 128 2048    0 8:136:264:392:520:648:776:904
197 format_null 128 1032    1
198 format_null 128 1032    8
199 format_null 128 1152  128
200 format_null 128 2048 2048
201 format_null 256 4096    0 8:264:520:776:1032:1288:1544:1800
202 format_null 256 2056    1
203 format_null 256 2056    8
204 format_null 256 2176  128
205 format_null 256 4096 2048
206 format_null 512 4096    0 8:512:1016:1520:2024:2528:3032:3536
207 format_null 512 4040    1
208 format_null 512 4040    8
209 format_null 512 4096  128
210 format_null 512 4096 2048
211 cleanup
212
213 echo "# Create enterprise-class 4K drive with fs and LUKS images."
214 # loop device here presents 512 block but images have 4k block
215 # cryptsetup should properly use 4k block on direct-io
216 add_device dev_size_mb=16 sector_size=4096 physblk_exp=0 num_tgts=1
217 for file in $(ls img_fs_*.img.bz2) ; do
218     echo "Format using fs image $file."
219     bzip2 -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
220     [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
221     mount $DEV $MNT_DIR || skip "Mounting image is not available."
222     echo $PWD1 | $CRYPTSETUP luksFormat -i 1 $MNT_DIR/luks.img || fail
223     echo $PWD2 | $CRYPTSETUP luksFormat -i 1 $MNT_DIR/luks.img --header $MNT_DIR/luks_header.img || fail
224     umount $MNT_DIR
225 done
226 cleanup