xref: /spdk/test/vhost/readonly/readonly.sh (revision 7366e569fbc7dff6dcc6709ffb52a890284de67e)
1#!/usr/bin/env bash
2
3testdir=$(readlink -f $(dirname $0))
4rootdir=$(readlink -f $testdir/../../..)
5source $rootdir/test/common/autotest_common.sh
6source $rootdir/test/vhost/common.sh
7
8rpc_py="$testdir/../../../scripts/rpc.py -s $(get_vhost_dir 0)/rpc.sock"
9
10vm_img=""
11disk="Nvme0n1"
12x=""
13
14function usage() {
15	[[ -n $2 ]] && (
16		echo "$2"
17		echo ""
18	)
19	echo "Shortcut script for automated readonly test for vhost-block"
20	echo "For test details check test_plan.md"
21	echo
22	echo "Usage: $(basename $1) [OPTIONS]"
23	echo
24	echo "-h, --help                Print help and exit"
25	echo "    --vm_image=           Path to VM image"
26	echo "    --disk=               Disk name."
27	echo "                          If disk=malloc, then creates malloc disk. For malloc disks, size is always 512M,"
28	echo "                          e.g. --disk=malloc. (Default: Nvme0n1)"
29	echo "-x                        set -x for script debug"
30}
31
32while getopts 'xh-:' optchar; do
33	case "$optchar" in
34		-)
35			case "$OPTARG" in
36				help) usage $0 && exit 0 ;;
37				vm_image=*) vm_img="${OPTARG#*=}" ;;
38				disk=*) disk="${OPTARG#*=}" ;;
39				*) usage $0 "Invalid argument '$OPTARG'" && exit 1 ;;
40			esac
41			;;
42		h) usage $0 && exit 0 ;;
43		x)
44			set -x
45			x="-x"
46			;;
47		*) usage $0 "Invalid argument '$OPTARG'" && exit 1 ;;
48	esac
49done
50
51vhosttestinit
52
53trap 'error_exit "${FUNCNAME}" "${LINENO}"' ERR
54
55if [[ $EUID -ne 0 ]]; then
56	fail "Go away user come back as root"
57fi
58
59function print_tc_name() {
60	notice ""
61	notice "==============================================================="
62	notice "Now running: $1"
63	notice "==============================================================="
64}
65
66function blk_ro_tc1() {
67	print_tc_name ${FUNCNAME[0]}
68	local vm_no="0"
69	local disk_name=$disk
70	local vhost_blk_name=""
71	local vm_dir="$VHOST_DIR/vms/$vm_no"
72
73	if [[ $disk =~ .*malloc.* ]]; then
74		if ! disk_name=$($rpc_py bdev_malloc_create 512 4096); then
75			fail "Failed to create malloc bdev"
76		fi
77
78		disk=$disk_name
79	else
80		disk_name=${disk%%_*}
81		if ! $rpc_py bdev_get_bdevs | jq -r '.[] .name' | grep -qi $disk_name$; then
82			fail "$disk_name bdev not found!"
83		fi
84	fi
85
86	#Create controller and create file on disk for later test
87	notice "Creating vhost_blk controller"
88	vhost_blk_name="naa.$disk_name.$vm_no"
89	$rpc_py vhost_create_blk_controller $vhost_blk_name $disk_name
90	vm_setup --disk-type=spdk_vhost_blk --force=$vm_no --os=$vm_img --disks=$disk --read-only=true
91
92	vm_run $vm_no
93	vm_wait_for_boot 300 $vm_no
94	notice "Preparing partition and file on guest VM"
95	vm_exec $vm_no "bash -s" < $testdir/disabled_readonly_vm.sh
96	sleep 1
97
98	vm_shutdown_all
99	#Create readonly controller and test readonly feature
100	notice "Removing controller and creating new one with readonly flag"
101	$rpc_py vhost_delete_controller $vhost_blk_name
102	$rpc_py vhost_create_blk_controller -r $vhost_blk_name $disk_name
103
104	vm_run $vm_no
105	vm_wait_for_boot 300 $vm_no
106	notice "Testing readonly feature on guest VM"
107	vm_exec $vm_no "bash -s" < $testdir/enabled_readonly_vm.sh
108	sleep 3
109
110	vm_shutdown_all
111	#Delete file from disk and delete partition
112	echo "INFO: Removing controller and creating new one"
113	$rpc_py vhost_delete_controller $vhost_blk_name
114	$rpc_py vhost_create_blk_controller $vhost_blk_name $disk_name
115
116	vm_run $vm_no
117	vm_wait_for_boot 300 $vm_no
118	notice "Removing partition and file from test disk on guest VM"
119	vm_exec $vm_no "bash -s" < $testdir/delete_partition_vm.sh
120	sleep 1
121
122	vm_shutdown_all
123}
124
125vhost_run -n 0
126if [[ -z $x ]]; then
127	set +x
128fi
129
130blk_ro_tc1
131
132$rpc_py bdev_nvme_detach_controller Nvme0
133
134vhost_kill 0
135
136vhosttestfini
137