1293436Sngie#!/bin/sh
2212934Sbrian# $FreeBSD: stable/11/tests/sys/geom/class/eli/resize_test.sh 345394 2019-03-21 22:23:52Z asomers $
3212934Sbrian
4345394Sasomers. $(atf_get_srcdir)/conf.sh
5345394Sasomers
6328811Sasomersatf_test_case resize cleanup
7328811Sasomersresize_head()
8324691Sngie{
9328811Sasomers	atf_set "descr" "geli resize will resize a geli provider"
10328811Sasomers	atf_set "require.user" "root"
11324691Sngie}
12328811Sasomersresize_body()
13328811Sasomers{
14345394Sasomers	geli_test_setup
15345394Sasomers
16328811Sasomers	BLK=512
17328811Sasomers	BLKS_PER_MB=2048
18324691Sngie
19328811Sasomers	md=$(attach_md -t malloc -s40m)
20212934Sbrian
21328811Sasomers	# Initialise
22328811Sasomers	atf_check -s exit:0 -o ignore gpart create -s BSD ${md}
23328811Sasomers	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs -s 10m ${md}
24212934Sbrian
25328811Sasomers	echo secret >tmp.key
26328811Sasomers	atf_check geli init -Bnone -PKtmp.key ${md}a
27328811Sasomers	atf_check geli attach -pk tmp.key ${md}a
28212934Sbrian
29328811Sasomers	atf_check -s exit:0 -o ignore newfs -U ${md}a.eli
30328811Sasomers	atf_check -s exit:7 -o ignore fsck_ffs -Ffy ${md}a.eli
31212934Sbrian
32328811Sasomers	# Doing a backup, resize & restore must be forced (with -f) as geli
33328811Sasomers	# verifies that the provider size in the metadata matches the consumer.
34212934Sbrian
35328811Sasomers	atf_check geli backup ${md}a tmp.meta
36328811Sasomers	atf_check geli detach ${md}a.eli
37328811Sasomers	atf_check -s exit:0 -o match:resized gpart resize -i1 -s 20m ${md}
38328811Sasomers	atf_check -s not-exit:0 -e ignore geli attach -pktmp.key ${md}a
39328811Sasomers	atf_check -s not-exit:0 -e ignore geli restore tmp.meta ${md}a
40328811Sasomers	atf_check geli restore -f tmp.meta ${md}a
41328811Sasomers	atf_check geli attach -pktmp.key ${md}a
42328811Sasomers	atf_check -s exit:0 -o ignore growfs -y ${md}a.eli
43328811Sasomers	atf_check -s exit:7 -o ignore fsck_ffs -Ffy ${md}a.eli
44212934Sbrian
45328811Sasomers	# Now do the resize properly
46212934Sbrian
47328811Sasomers	atf_check geli detach ${md}a.eli
48328811Sasomers	atf_check -s exit:0 -o match:resized gpart resize -i1 -s 30m ${md}
49328811Sasomers	atf_check geli resize -s20m ${md}a
50328811Sasomers	atf_check -s not-exit:0 -e match:"Inconsistent provider.*metadata" \
51328811Sasomers		geli resize -s20m ${md}a
52328811Sasomers	atf_check geli attach -pktmp.key ${md}a
53328811Sasomers	atf_check -s exit:0 -o ignore growfs -y ${md}a.eli
54328811Sasomers	atf_check -s exit:7 -o ignore fsck_ffs -Ffy ${md}a.eli
55212934Sbrian
56328811Sasomers	atf_check geli detach ${md}a.eli
57328811Sasomers	atf_check -s exit:0 -o ignore gpart destroy -F $md
58212934Sbrian
59212934Sbrian
60328811Sasomers	# Verify that the man page example works, changing ada0 to $md,
61328811Sasomers	# 1g to 20m, 2g to 30m and keyfile to tmp.key, and adding -B none
62328811Sasomers	# to geli init.
63212934Sbrian
64328811Sasomers	atf_check -s exit:0 -o ignore gpart create -s GPT $md
65328811Sasomers	atf_check -s exit:0 -o ignore gpart add -s 20m -t freebsd-ufs -i 1 $md
66328811Sasomers	atf_check geli init -B none -K tmp.key -P ${md}p1
67328811Sasomers	atf_check -s exit:0 -o match:resized gpart resize -s 30m -i 1 $md
68328811Sasomers	atf_check geli resize -s 20m ${md}p1
69328811Sasomers	atf_check geli attach -k tmp.key -p ${md}p1
70328811Sasomers}
71328811Sasomersresize_cleanup()
72328811Sasomers{
73328811Sasomers	if [ -f "$TEST_MDS_FILE" ]; then
74328811Sasomers		while read md; do
75328811Sasomers			[ -c /dev/${md}a.eli ] && \
76328811Sasomers				geli detach ${md}a.eli 2>/dev/null
77328811Sasomers			[ -c /dev/${md}p1.eli ] && \
78328811Sasomers				geli detach ${md}p1.eli
79328811Sasomers			[ -c /dev/${md}.eli ] && \
80328811Sasomers				geli detach ${md}.eli 2>/dev/null
81328811Sasomers			mdconfig -d -u $md 2>/dev/null
82328811Sasomers		done < $TEST_MDS_FILE
83328811Sasomers	fi
84328811Sasomers}
85212934Sbrian
86328811Sasomersatf_init_test_cases()
87328811Sasomers{
88328811Sasomers	atf_add_test_case resize
89328811Sasomers}
90