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