Commit 30dbca86 authored by Pawel Niedzwiecki's avatar Pawel Niedzwiecki Committed by Jim Harris
Browse files

test/vhost: test for the vhost-blk readonly feature.



Change-Id: I5dde0555bd7d0e03dea3779dfd59c13ece92696a
Signed-off-by: default avatarPawel Niedzwiecki <pawelx.niedzwiecki@intel.com>
Reviewed-on: https://review.gerrithub.io/382835


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
parent e4c4fec2
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -139,6 +139,10 @@ if [ $SPDK_TEST_VHOST -eq 1 ]; then
		timing_enter integrity
		run_test ./test/vhost/spdk_vhost.sh --integrity
		timing_exit integrity

		timing_enter readonly
		run_test ./test/vhost/spdk_vhost.sh --readonly
		timing_exit readonly
	fi

	timing_enter integrity_lvol_scsi
+42 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

set -e
BASE_DIR=$(readlink -f $(dirname $0))

disk_name="vda"
test_folder_name="readonly_test"
test_file_name="some_test_file"

function error()
{
	echo "==========="
	echo -e "ERROR: $@"
	echo "==========="
	trap - ERR
	set +e
	umount "$test_folder_name"
	rm -rf "$BASE_DIR/$test_folder_name"
	exit 1
}

trap 'error "In delete_partition_vm.sh, line:" "${LINENO}"' ERR

if [[ ! -d "/sys/block/$disk_name" ]]; then
	error "No vhost-blk disk found!"
fi

if (( $(lsblk -r -n -o RO -d "/dev/$disk_name") == 1 )); then
	error "Vhost-blk disk is set as readonly!"
fi

mkdir -p $test_folder_name

echo "INFO: Mounting disk"
mount /dev/$disk_name"1" $test_folder_name

echo "INFO: Removing folder and unmounting $test_folder_name"
umount "$test_folder_name"
rm -rf "$BASE_DIR/$test_folder_name"

echo "INFO: Deleting partition"
echo -e "d\n1\nw" | fdisk /dev/$disk_name
+47 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

set -e
BASE_DIR=$(readlink -f $(dirname $0))

disk_name="vda"
test_folder_name="readonly_test"
test_file_name="some_test_file"

function error()
{
	echo "==========="
	echo -e "ERROR: $@"
	echo "==========="
	trap - ERR
	set +e
	umount "$test_folder_name"
	rm -rf "$BASE_DIR/$test_folder_name"
	exit 1
}

trap 'error "In disabled_readonly_vm.sh, line:" "${LINENO}"' ERR

if [[ ! -d "/sys/block/$disk_name" ]]; then
	error "No vhost-blk disk found!"
fi

if (( $(lsblk -r -n -o RO -d "/dev/$disk_name") == 1 )); then
	error "Vhost-blk disk is set as readonly!"
fi

parted -s /dev/$disk_name mklabel gpt
parted -s /dev/$disk_name mkpart primary 2048s 100%
partprobe
sleep 0.1

echo "INFO: Creating file system"
mkfs.ext4 -F /dev/$disk_name"1"

echo "INFO: Mounting disk"
mkdir -p $test_folder_name
mount /dev/$disk_name"1" $test_folder_name

echo "INFO: Creating a test file $test_file_name"
truncate -s "200M" $test_folder_name/$test_file_name
umount "$test_folder_name"
rm -rf "$BASE_DIR/$test_folder_name"
+75 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

BASE_DIR=$(readlink -f $(dirname $0))

disk_name="vda"
test_folder_name="readonly_test"
test_file_name="some_test_file"

function error()
{
	echo "==========="
	echo -e "ERROR: $@"
	echo "==========="
	umount "$test_folder_name"
	rm -rf "$BASE_DIR/$test_folder_name"
	exit 1
}

if [[ ! -d "/sys/block/$disk_name" ]]; then
	error "No vhost-blk disk found!"
fi

if (( $(lsblk -r -n -o RO -d "/dev/$disk_name") == 0 )); then
	error "Vhost-blk disk is not set as readonly!"
fi

echo "INFO: Found vhost-blk disk with readonly flag"
if [[ ! -b "/dev/$disk_name"1"" ]]; then
	error "Partition not found!"
fi

mkdir $BASE_DIR/$test_folder_name
if [[ $? != 0 ]]; then
	error "Failed to create test folder $test_folder_name"
fi

echo "INFO: Mounting partition"
mount /dev/$disk_name"1" $BASE_DIR/$test_folder_name
if [[ $? != 0 ]]; then
	error "Failed to mount partition $disk_name""1"
fi

echo "INFO: Trying to create file on readonly disk"
truncate -s "200M" $test_folder_name/$test_file_name"_on_readonly"
if [[ $? == 0 ]]; then
	error "Created a file on a readonly disk!"
fi

if [[ -f $test_folder_name/$test_file_name ]]; then
	echo "INFO: Trying to delete previously created file"
	rm $test_folder_name/$test_file_name
	if [[ $? == 0 ]]; then
		error "Deleted a file from a readonly disk!"
	fi
else
	error "Previously created file not found!"
fi

echo "INFO: Copying file from readonly disk"
cp $test_folder_name/$test_file_name $BASE_DIR
if ! rm $BASE_DIR/$test_file_name; then
	error "Copied file from a readonly disk was not found!"
fi

umount "$test_folder_name"
rm -rf "$BASE_DIR/$test_folder_name"
echo "INFO: Trying to create file system on a readonly disk"
if mkfs.ext4 -F /dev/$disk_name"1"; then
	error "Created file system on a readonly disk!"
fi

echo "INFO: Trying to delete partition from readonly disk"
if echo -e "d\n1\nw" | fdisk /dev/$disk_name; then
	error "Deleted partition from readonly disk!"
fi
+135 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

set -e
BASE_DIR=$(readlink -f $(dirname $0))
[[ -z "$TEST_DIR" ]] && TEST_DIR="$(cd $BASE_DIR/../../../../ && pwd)"
[[ -z "$COMMON_DIR" ]] && COMMON_DIR="$(cd $BASE_DIR/../common && pwd)"

rpc_py="$BASE_DIR/../../../scripts/rpc.py "

vm_img=""
disk="Nvme0n1_size_1G"
x=""

function usage()
{
	[[ ! -z $2 ]] && ( echo "$2"; echo ""; )
	echo "Shortcut script for automated readonly test fo vhost-block"
	echo "For test details check test_plan.md"
	echo
	echo "Usage: $(basename $1) [OPTIONS]"
	echo
	echo "-h, --help                Print help and exit"
	echo "    --vm_image=           Path to VM image"
	echo "    --disk=               Disk name and size. Disk can have additional size parameter,"
	echo "                          e.g. --disk=Nvme0n1_size_35G; unit can be M or G; default - 20G."
	echo "                          If disk=malloc, then creates malloc disk. For malloc disks, size is always 512M,"
	echo "                          e.g. --disk=malloc. (Default: Nvme0n1_size_1G)"
	echo "-x                        set -x for script debug"
}

while getopts 'xh-:' optchar; do
	case "$optchar" in
	-)
		case "$OPTARG" in
			help) usage $0 && exit 0;;
			vm_image=*) vm_img="${OPTARG#*=}" ;;
			disk=*) disk="${OPTARG#*=}" ;;
			*) usage $0 "Invalid argument '$OPTARG'" && exit 1
		esac
		;;
	h)	usage $0 && exit 0 ;;
	x)	set -x
		x="-x" ;;
	*)	usage $0 "Invalid argument '$OPTARG'" && exit 1
	esac
done

if [[ $EUID -ne 0 ]]; then
	echo "INFO: Go away user come back as root"
	exit 1
fi

. $COMMON_DIR/common.sh
trap 'error_exit "${FUNCNAME}" "${LINENO}"' ERR

function print_tc_name()
{
	echo ""
	echo "==============================================================="
	echo "Now running: $1"
	echo "==============================================================="
}

function blk_ro_tc1()
{
	print_tc_name ${FUNCNAME[0]}
	local vm_no="0"
	local disk_name=$disk
	local vhost_blk_name=""
	local vm_dir="$TEST_DIR/vms/$vm_no"

	if [[ $disk =~ .*malloc.* ]]; then
		disk_name=$($rpc_py construct_malloc_bdev 512 4096)
		if [ $? != 0 ]; then
			error "Failed to create malloc bdev"
		fi

		disk=$disk_name"_size_512M"
	else
		disk_name=${disk%%_*}
		if ! $rpc_py get_bdevs | jq -r '.[] .name' | grep -qi $disk_name$; then
			error "$disk_name bdev not found!"
		fi
	fi

#Create controller and create file on disk for later test
	echo "INFO: Creating vhost_blk controller"
	vhost_blk_name="naa.$disk_name.$vm_no"
	$rpc_py construct_vhost_blk_controller $vhost_blk_name $disk_name
	setup_cmd="$COMMON_DIR/vm_setup.sh $x --work-dir=$TEST_DIR --test-type=spdk_vhost_blk"
	setup_cmd+=" -f $vm_no"
	setup_cmd+=" --os=$vm_img"
	setup_cmd+=" --disk=$disk"
	$setup_cmd

	$COMMON_DIR/vm_run.sh $x --work-dir=$TEST_DIR $vm_no
	vm_wait_for_boot 600 $vm_no
	echo "INFO: Prepearing partition and file on guest VM"
	vm_ssh $vm_no "bash -s" < $BASE_DIR/disabled_readonly_vm.sh
	sleep 1

	vm_shutdown_all
#Create readonly controller and test readonly featchure
	echo "INFO: Removing controller and creating new one with readonly flag"
	$rpc_py remove_vhost_controller $vhost_blk_name
	$rpc_py construct_vhost_blk_controller -r $vhost_blk_name $disk_name

	$COMMON_DIR/vm_run.sh $x --work-dir=$TEST_DIR $vm_no
	vm_wait_for_boot 600 $vm_no
	echo "INFO: Testing readonly feature on guest VM"
	vm_ssh $vm_no "bash -s" < $BASE_DIR/enabled_readonly_vm.sh
	sleep 1

	vm_shutdown_all
#Delete file from disk and delete partition
	echo "INFO: Removing controller and creating new one"
	$rpc_py remove_vhost_controller $vhost_blk_name
	$rpc_py construct_vhost_blk_controller $vhost_blk_name $disk_name

	$COMMON_DIR/vm_run.sh $x --work-dir=$TEST_DIR $vm_no
	vm_wait_for_boot 600 $vm_no
	echo "INFO: removing partition and file from test disk on guest VM"
	vm_ssh $vm_no "bash -s" < $BASE_DIR/delete_partition_vm.sh
	sleep 1

	vm_shutdown_all
}

spdk_vhost_run $BASE_DIR
if [[ -z $x ]]; then
	set +x
fi

blk_ro_tc1
spdk_vhost_kill
Loading