xref: /spdk/test/lvol/hotremove.sh (revision eb53c23236cccb6b698b7ca70ee783da1c574b5f)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2019 Intel Corporation
4#  All rights reserved.
5#
6testdir=$(readlink -f $(dirname $0))
7rootdir=$(readlink -f $testdir/../..)
8source $rootdir/test/common/autotest_common.sh
9source $rootdir/test/lvol/common.sh
10
11# create an lvol on lvs, then remove the lvs
12function test_hotremove_lvol_store() {
13	# create lvs + lvol on top
14	malloc_name=$(rpc_cmd bdev_malloc_create $MALLOC_SIZE_MB $MALLOC_BS)
15	lvs_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_name" lvs_test)
16	lvol_uuid=$(rpc_cmd bdev_lvol_create -u "$lvs_uuid" lvol_test "$LVS_DEFAULT_CAPACITY_MB")
17
18	# remove lvs (with one lvol open)
19	rpc_cmd bdev_lvol_delete_lvstore -u "$lvs_uuid"
20	rpc_cmd bdev_lvol_get_lvstores -u "$lvs_uuid" && false
21	lvolstores=$(rpc_cmd bdev_lvol_get_lvstores)
22	[ "$(jq length <<< "$lvolstores")" == "0" ]
23
24	# make sure we can't destroy the lvs again
25	rpc_cmd bdev_lvol_delete_lvstore -u "$lvs_uuid" && false
26
27	# make sure the lvol is also gone
28	rpc_cmd bdev_get_bdevs -b "$lvol_uuid" && false
29	lvols=$(rpc_cmd bdev_get_bdevs | jq -r '[ .[] | select(.product_name == "Logical Volume") ]')
30	[ "$(jq length <<< "$lvols")" == "0" ]
31
32	# clean up
33	rpc_cmd bdev_malloc_delete "$malloc_name"
34	check_leftover_devices
35}
36
37# destroy lvs with 4 lvols on top
38function test_hotremove_lvol_store_multiple_lvols() {
39	# create lvs
40	malloc_name=$(rpc_cmd bdev_malloc_create $MALLOC_SIZE_MB $MALLOC_BS)
41	lvs_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_name" lvs_test)
42
43	# calculate lvol size
44	lvol_size_mb=$(round_down $(((MALLOC_SIZE_MB - LVS_DEFAULT_CLUSTER_SIZE_MB) / 4)))
45
46	# create 4 lvols
47	for i in $(seq 1 4); do
48		rpc_cmd bdev_lvol_create -u "$lvs_uuid" "lvol_test${i}" "$lvol_size_mb"
49	done
50
51	lvols=$(rpc_cmd bdev_get_bdevs | jq -r '[ .[] | select(.product_name == "Logical Volume") ]')
52	[ "$(jq length <<< "$lvols")" == "4" ]
53
54	# remove lvs (with 4 lvols open)
55	rpc_cmd bdev_lvol_delete_lvstore -u "$lvs_uuid"
56	rpc_cmd bdev_lvol_get_lvstores -u "$lvs_uuid" && false
57
58	# make sure all lvols are gone
59	lvols=$(rpc_cmd bdev_get_bdevs | jq -r '[ .[] | select(.product_name == "Logical Volume") ]')
60	[ "$(jq length <<< "$lvols")" == "0" ]
61
62	# clean up
63	rpc_cmd bdev_malloc_delete "$malloc_name"
64	check_leftover_devices
65}
66
67# create an lvs on malloc, then remove just the malloc
68function test_hotremove_lvol_store_base() {
69	# create lvs + lvol on top
70	malloc_name=$(rpc_cmd bdev_malloc_create $MALLOC_SIZE_MB $MALLOC_BS)
71	lvs_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_name" lvs_test)
72
73	# clean up
74	rpc_cmd bdev_malloc_delete "$malloc_name"
75	# make sure the lvs is gone
76	rpc_cmd bdev_lvol_get_lvstores -u "$lvs_uuid" && false
77	# make sure we can't delete the lvs again
78	rpc_cmd bdev_lvol_delete_lvstore -u "$lvs_uuid" && false
79	check_leftover_devices
80}
81
82# create an lvs on malloc, then an lvol, then remove just the malloc
83function test_hotremove_lvol_store_base_with_lvols() {
84	# create lvs + lvol on top
85	malloc_name=$(rpc_cmd bdev_malloc_create $MALLOC_SIZE_MB $MALLOC_BS)
86	lvs_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_name" lvs_test)
87	lvol_uuid=$(rpc_cmd bdev_lvol_create -u "$lvs_uuid" lvol_test "$LVS_DEFAULT_CAPACITY_MB")
88
89	rpc_cmd bdev_get_bdevs -b "$lvol_uuid"
90
91	# clean up
92	rpc_cmd bdev_malloc_delete "$malloc_name"
93	# make sure the lvol is gone
94	rpc_cmd bdev_get_bdevs -b "$lvol_uuid" && false
95	# make sure the lvs is gone as well
96	rpc_cmd bdev_lvol_get_lvstores -u "$lvs_uuid" && false
97
98	# make sure we can't delete the lvs again
99	rpc_cmd bdev_lvol_delete_lvstore -u "$lvs_uuid" && false
100	check_leftover_devices
101}
102
103function test_bdev_lvol_delete_lvstore_with_clones() {
104	local snapshot_name1=snapshot1 snapshot_uuid1
105	local snapshot_name2=snapshot2 snapshot_uuid2
106	local clone_name=clone clone_uuid
107	local lbd_name=lbd_test
108
109	local bdev_uuid
110	local lvstore_name=lvs_name lvstore_uuid
111	local malloc_dev
112
113	malloc_dev=$(rpc_cmd bdev_malloc_create 256 "$MALLOC_BS")
114	lvstore_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_dev" "$lvstore_name")
115
116	get_lvs_jq bdev_lvol_get_lvstores -u "$lvstore_uuid"
117	[[ ${jq_out["uuid"]} == "$lvstore_uuid" ]]
118	[[ ${jq_out["name"]} == "$lvstore_name" ]]
119	[[ ${jq_out["base_bdev"]} == "$malloc_dev" ]]
120
121	size=$((jq_out["free_clusters"] * jq_out["cluster_size"] / 4 / 1024 ** 2))
122
123	bdev_uuid=$(rpc_cmd bdev_lvol_create -u "$lvstore_uuid" "$lbd_name" "$size")
124
125	get_bdev_jq bdev_get_bdevs -b "$bdev_uuid"
126
127	snapshot_uuid1=$(rpc_cmd bdev_lvol_snapshot "${jq_out["name"]}" "$snapshot_name1")
128
129	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$snapshot_name1"
130	[[ ${jq_out["name"]} == "$snapshot_uuid1" ]]
131	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
132	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$snapshot_name1" ]]
133
134	clone_uuid=$(rpc_cmd bdev_lvol_clone "$lvstore_name/$snapshot_name1" "$clone_name")
135
136	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$clone_name"
137	[[ ${jq_out["name"]} == "$clone_uuid" ]]
138	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
139	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$clone_name" ]]
140
141	snapshot_uuid2=$(rpc_cmd bdev_lvol_snapshot "${jq_out["name"]}" "$snapshot_name2")
142
143	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$snapshot_name2"
144	[[ ${jq_out["name"]} == "$snapshot_uuid2" ]]
145	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
146	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$snapshot_name2" ]]
147
148	rpc_cmd bdev_lvol_delete "$snapshot_uuid1" && false
149	rpc_cmd bdev_lvol_delete_lvstore -u "$lvstore_uuid"
150	rpc_cmd bdev_malloc_delete "$malloc_dev"
151
152	check_leftover_devices
153}
154
155# Test for unregistering the lvol bdevs. Removing malloc bdev under an lvol
156# store triggers unregister of all lvol bdevs. Verify it with clones present.
157function test_unregister_lvol_bdev() {
158	local snapshot_name1=snapshot1 snapshot_uuid1
159	local snapshot_name2=snapshot2 snapshot_uuid2
160	local clone_name=clone clone_uuid
161	local lbd_name=lbd_test
162
163	local bdev_uuid
164	local lvstore_name=lvs_name lvstore_uuid
165	local malloc_dev
166
167	malloc_dev=$(rpc_cmd bdev_malloc_create 256 "$MALLOC_BS")
168	lvstore_uuid=$(rpc_cmd bdev_lvol_create_lvstore "$malloc_dev" "$lvstore_name")
169
170	get_lvs_jq bdev_lvol_get_lvstores -u "$lvstore_uuid"
171	[[ ${jq_out["uuid"]} == "$lvstore_uuid" ]]
172	[[ ${jq_out["name"]} == "$lvstore_name" ]]
173	[[ ${jq_out["base_bdev"]} == "$malloc_dev" ]]
174
175	size=$((jq_out["free_clusters"] * jq_out["cluster_size"] / 4 / 1024 ** 2))
176
177	bdev_uuid=$(rpc_cmd bdev_lvol_create -u "$lvstore_uuid" "$lbd_name" "$size")
178
179	get_bdev_jq bdev_get_bdevs -b "$bdev_uuid"
180
181	snapshot_uuid1=$(rpc_cmd bdev_lvol_snapshot "${jq_out["name"]}" "$snapshot_name1")
182
183	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$snapshot_name1"
184	[[ ${jq_out["name"]} == "$snapshot_uuid1" ]]
185	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
186	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$snapshot_name1" ]]
187
188	clone_uuid=$(rpc_cmd bdev_lvol_clone "$lvstore_name/$snapshot_name1" "$clone_name")
189
190	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$clone_name"
191	[[ ${jq_out["name"]} == "$clone_uuid" ]]
192	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
193	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$clone_name" ]]
194
195	snapshot_uuid2=$(rpc_cmd bdev_lvol_snapshot "${jq_out["name"]}" "$snapshot_name2")
196
197	get_bdev_jq bdev_get_bdevs -b "$lvstore_name/$snapshot_name2"
198	[[ ${jq_out["name"]} == "$snapshot_uuid2" ]]
199	[[ ${jq_out["product_name"]} == "Logical Volume" ]]
200	[[ ${jq_out["aliases[0]"]} == "$lvstore_name/$snapshot_name2" ]]
201
202	rpc_cmd bdev_malloc_delete "$malloc_dev"
203	check_leftover_devices
204}
205
206$SPDK_BIN_DIR/spdk_tgt &
207spdk_pid=$!
208trap 'killprocess "$spdk_pid"; exit 1' SIGINT SIGTERM EXIT
209waitforlisten $spdk_pid
210
211run_test "test_hotremove_lvol_store" test_hotremove_lvol_store
212run_test "test_hotremove_lvol_store_multiple_lvols" test_hotremove_lvol_store_multiple_lvols
213run_test "test_hotremove_lvol_store_base" test_hotremove_lvol_store_base
214run_test "test_hotremove_lvol_store_base_with_lvols" test_hotremove_lvol_store_base_with_lvols
215run_test "test_bdev_lvol_delete_lvstore_with_clones" test_bdev_lvol_delete_lvstore_with_clones
216run_test "test_unregister_lvol_bdev" test_unregister_lvol_bdev
217
218trap - SIGINT SIGTERM EXIT
219killprocess $spdk_pid
220