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