xref: /spdk/test/setup/devices.sh (revision cc6920a4763d4b9a43aa40583c8397d8f14fa100)
1#!/usr/bin/env bash
2testdir=$(readlink -f "$(dirname "$0")")
3rootdir=$(readlink -f "$testdir/../../")
4source "$testdir/common.sh"
5
6shopt -s nullglob
7
8cleanup() {
9	cleanup_nvme
10	cleanup_dm
11
12	if [[ -b /dev/$test_disk ]]; then
13		wipefs --all "/dev/$test_disk"
14	fi
15}
16
17cleanup_nvme() {
18	if mountpoint -q "$nvme_mount"; then
19		umount "$nvme_mount"
20	fi
21
22	if [[ -b /dev/$nvme_disk_p ]]; then
23		wipefs --all "/dev/$nvme_disk_p"
24	fi
25	if [[ -b /dev/$nvme_disk ]]; then
26		wipefs --all "/dev/$nvme_disk"
27	fi
28}
29
30cleanup_dm() {
31	if mountpoint -q "$dm_mount"; then
32		umount "$dm_mount"
33	fi
34	if [[ -L /dev/mapper/$dm_name ]]; then
35		dmsetup remove --force "$dm_name"
36	fi
37	if [[ -b /dev/$pv0 ]]; then
38		wipefs --all "/dev/$pv0"
39	fi
40	if [[ -b /dev/$pv1 ]]; then
41		wipefs --all "/dev/$pv1"
42	fi
43}
44
45verify() {
46	local dev=$1
47	local mounts=$2
48	local mount_point=$3
49	local test_file=$4
50
51	local found=0
52
53	: > "$test_file"
54
55	local pci status
56	while read -r pci _ _ status; do
57		if [[ $pci == "$dev" && \
58			$status == *"Active mountpoints on $mounts"* ]]; then
59			found=1
60		fi
61	done < <(PCI_ALLOWED="$dev" setup output config)
62	((found == 1))
63
64	# Does the mount still exist?
65	mountpoint -q "$mount_point"
66	# Does the test file still exist?
67	[[ -e $test_file ]]
68	rm "$test_file"
69}
70
71nvme_mount() {
72	# Agenda 1:
73	# - Create single partition on the nvme drive
74	# - Install ext4 fs on the first partition
75	# - Mount the partition
76	# - Run tests and check if setup.sh skipped
77	#   nvme controller given block device is
78	#   bound to.
79
80	# Agenda 2:
81	# - Install ext4 on the entire nvme drive
82	# - Mount the drive
83	#   Run tests and check if setup.sh skipped
84	#   nvme controller given block device is
85	#   bound to.
86
87	# Keep scope of all the variables global to make the cleanup process easier.
88
89	nvme_disk=$test_disk
90	nvme_disk_p=${nvme_disk}p1
91	nvme_mount=$SPDK_TEST_STORAGE/nvme_mount
92	nvme_dummy_test_file=$nvme_mount/test_nvme
93
94	# Agenda 1
95	partition_drive "$nvme_disk" 1
96	mkfs "/dev/$nvme_disk_p" "$nvme_mount"
97
98	verify \
99		"${blocks_to_pci["$nvme_disk"]}" \
100		"$nvme_disk:$nvme_disk_p" \
101		"$nvme_mount" \
102		"$nvme_dummy_test_file"
103
104	cleanup_nvme
105
106	# Agenda 2
107	mkfs "/dev/$nvme_disk" "$nvme_mount" 1024M
108
109	verify \
110		"${blocks_to_pci["$nvme_disk"]}" \
111		"$nvme_disk:$nvme_disk" \
112		"$nvme_mount" \
113		"$nvme_dummy_test_file"
114
115	# All done, final cleanup
116	cleanup_nvme
117}
118
119dm_mount() {
120	# Agenda:
121	#  - Create two partitions on the nvme drive
122	#  - Create dm device consisting of half of
123	#    the size of each partition.
124	#  - Install ext4 fs on the dm device
125	#  - Mount dm device
126	#  - Run tests and check if setup.sh skipped
127	#    nvme controller given block devices are
128	#    bound to.
129
130	# Keep scope of all the variables global to make the cleanup process easier.
131
132	pv=$test_disk
133	pv0=${pv}p1
134	pv1=${pv}p2
135
136	partition_drive "$pv"
137
138	dm_name=nvme_dm_test
139	dm_mount=$SPDK_TEST_STORAGE/dm_mount
140	dm_dummy_test_file=$dm_mount/test_dm
141
142	# Each partition is 1G in size, join their halfs
143	dmsetup create "$dm_name" <<- DM_TABLE
144		0 1048576 linear /dev/$pv0 0
145		1048576 1048576 linear /dev/$pv1 0
146	DM_TABLE
147
148	[[ -e /dev/mapper/$dm_name ]]
149	dm=$(readlink -f "/dev/mapper/$dm_name")
150	dm=${dm##*/}
151
152	[[ -e /sys/class/block/$pv0/holders/$dm ]]
153	[[ -e /sys/class/block/$pv1/holders/$dm ]]
154
155	mkfs "/dev/mapper/$dm_name" "$dm_mount"
156
157	verify \
158		"${blocks_to_pci["$pv"]}" \
159		"$pv:$dm_name" \
160		"$dm_mount" \
161		"$dm_dummy_test_file"
162
163	# All done, start tiding up
164	cleanup_dm
165}
166
167trap "cleanup" EXIT
168
169setup reset
170
171declare -a blocks=()
172declare -A blocks_to_pci=()
173min_disk_size=$((1024 ** 3 * 2)) # 2GB
174
175for block in "/sys/block/nvme"*; do
176	pci=$(readlink -f "$block/device/device")
177	pci=${pci##*/}
178	if ! block_in_use "${block##*/}" && (($(sec_size_to_bytes "${block##*/}") >= min_disk_size)); then
179		blocks+=("${block##*/}")
180		blocks_to_pci["${block##*/}"]=$pci
181	fi
182done
183((${#blocks[@]} > 0))
184
185declare -r test_disk=${blocks[0]}
186
187run_test "nvme_mount" nvme_mount
188run_test "dm_mount" dm_mount
189