xref: /freebsd/tests/sys/geom/class/part/misc.sh (revision bdd1243d)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2018 Alan Somers
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD$
27
28MD_DEVS="md.devs"
29
30atf_test_case blank_physpath cleanup
31blank_physpath_head()
32{
33	atf_set "descr" "gpart shouldn't add physical paths to underlying providers that have none"
34	atf_set "require.user" "root"
35}
36blank_physpath_body()
37{
38	load_gnop
39	load_gpart
40	md=$(alloc_md)
41	atf_check -o empty -e ignore diskinfo -p ${md}
42	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}
43	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}
44	atf_check -o empty -e ignore diskinfo -p ${md}a
45}
46blank_physpath_cleanup()
47{
48	common_cleanup
49}
50
51
52atf_test_case bsd_physpath cleanup
53bsd_physpath_head()
54{
55	atf_set "descr" "BSD partitions should append /X to the underlying device's physical path"
56	atf_set "require.user" "root"
57}
58bsd_physpath_body()
59{
60	load_gnop
61	load_gpart
62	md=$(alloc_md)
63	physpath="some/physical/path"
64	atf_check gnop create -z $physpath /dev/${md}
65	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nop
66	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nop
67	gpart_physpath=$(diskinfo -p ${md}.nopa)
68	atf_check_equal "${physpath}/a" "$gpart_physpath"
69}
70bsd_physpath_cleanup()
71{
72	common_cleanup
73}
74
75atf_test_case gpt_physpath cleanup
76gpt_physpath_head()
77{
78	atf_set "descr" "GPT partitions should append /pX to the underlying device's physical path"
79	atf_set "require.user" "root"
80}
81gpt_physpath_body()
82{
83	load_gnop
84	load_gpart
85	md=$(alloc_md)
86	physpath="some/physical/path"
87	atf_check gnop create -z $physpath /dev/${md}
88	atf_check -s exit:0 -o ignore gpart create -s gpt ${md}.nop
89	atf_check -s exit:0 -o ignore gpart add -t efi ${md}.nop
90	gpart_physpath=$(diskinfo -p ${md}.nopp1)
91	atf_check_equal "${physpath}/p1" "$gpart_physpath"
92}
93gpt_physpath_cleanup()
94{
95	common_cleanup
96}
97
98atf_test_case mbr_physpath cleanup
99mbr_physpath_head()
100{
101	atf_set "descr" "MBR partitions should append /sX to the underlying device's physical path"
102	atf_set "require.user" "root"
103}
104mbr_physpath_body()
105{
106	load_gnop
107	load_gpart
108	md=$(alloc_md)
109	physpath="some/physical/path"
110	atf_check gnop create -z $physpath /dev/${md}
111	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
112	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
113	gpart_physpath=$(diskinfo -p ${md}.nops1)
114	atf_check_equal "${physpath}/s1" "$gpart_physpath"
115}
116mbr_physpath_cleanup()
117{
118	common_cleanup
119}
120
121atf_test_case mbr_bsd_physpath cleanup
122mbr_bsd_physpath_head()
123{
124	atf_set "descr" "BSD partitions nested within MBR partitions should append /sX/Y to the underlying device's physical path"
125	atf_set "require.user" "root"
126}
127mbr_bsd_physpath_body()
128{
129	load_gnop
130	load_gpart
131	md=$(alloc_md)
132	physpath="some/physical/path"
133	atf_check gnop create -z $physpath /dev/${md}
134	atf_check -s exit:0 -o ignore gpart create -s mbr ${md}.nop
135	atf_check -s exit:0 -o ignore gpart add -t freebsd ${md}.nop
136	atf_check -s exit:0 -o ignore gpart create -s bsd ${md}.nops1
137	atf_check -s exit:0 -o ignore gpart add -t freebsd-ufs ${md}.nops1
138	gpart_physpath=$(diskinfo -p ${md}.nops1a)
139	atf_check_equal "${physpath}/s1/a" "$gpart_physpath"
140}
141mbr_bsd_physpath_cleanup()
142{
143	common_cleanup
144}
145
146atf_init_test_cases()
147{
148	atf_add_test_case blank_physpath
149	atf_add_test_case bsd_physpath
150	atf_add_test_case gpt_physpath
151	atf_add_test_case mbr_physpath
152	atf_add_test_case mbr_bsd_physpath
153}
154
155alloc_md()
156{
157	local md
158
159	md=$(mdconfig -a -t swap -s 1M) || atf_fail "mdconfig -a failed"
160	echo ${md} >> $MD_DEVS
161	echo ${md}
162}
163
164common_cleanup()
165{
166	if [ -f "$MD_DEVS" ]; then
167		while read test_md; do
168			gnop destroy -f ${test_md}.nop 2>/dev/null
169			mdconfig -d -u $test_md 2>/dev/null
170		done < $MD_DEVS
171		rm $MD_DEVS
172	fi
173	true
174}
175
176load_gpart()
177{
178	if ! kldstat -q -m g_part; then
179		geom part load || atf_skip "could not load module for geom part"
180	fi
181}
182
183load_gnop()
184{
185	if ! kldstat -q -m g_nop; then
186		geom nop load || atf_skip "could not load module for geom nop"
187	fi
188}
189