From 30dbca865b05db8c7b7402aebaa001f4ca11ddff Mon Sep 17 00:00:00 2001 From: Pawel Niedzwiecki Date: Tue, 17 Oct 2017 20:59:36 +0200 Subject: [PATCH] test/vhost: test for the vhost-blk readonly feature. Change-Id: I5dde0555bd7d0e03dea3779dfd59c13ece92696a Signed-off-by: Pawel Niedzwiecki Reviewed-on: https://review.gerrithub.io/382835 Tested-by: SPDK Automated Test System Reviewed-by: Jim Harris Reviewed-by: Daniel Verkamp --- autotest.sh | 4 + test/vhost/readonly/delete_partition_vm.sh | 42 ++++++ test/vhost/readonly/disabled_readonly_vm.sh | 47 +++++++ test/vhost/readonly/enabled_readonly_vm.sh | 75 +++++++++++ test/vhost/readonly/readonly.sh | 135 ++++++++++++++++++++ test/vhost/readonly/test_plan.md | 30 +++++ test/vhost/readonly/vhost.conf.in | 2 + test/vhost/spdk_vhost.sh | 5 + 8 files changed, 340 insertions(+) create mode 100755 test/vhost/readonly/delete_partition_vm.sh create mode 100755 test/vhost/readonly/disabled_readonly_vm.sh create mode 100755 test/vhost/readonly/enabled_readonly_vm.sh create mode 100755 test/vhost/readonly/readonly.sh create mode 100644 test/vhost/readonly/test_plan.md create mode 100644 test/vhost/readonly/vhost.conf.in diff --git a/autotest.sh b/autotest.sh index c31cbd0f7..cb6da0f36 100755 --- a/autotest.sh +++ b/autotest.sh @@ -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 diff --git a/test/vhost/readonly/delete_partition_vm.sh b/test/vhost/readonly/delete_partition_vm.sh new file mode 100755 index 000000000..989febb7a --- /dev/null +++ b/test/vhost/readonly/delete_partition_vm.sh @@ -0,0 +1,42 @@ +#!/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 diff --git a/test/vhost/readonly/disabled_readonly_vm.sh b/test/vhost/readonly/disabled_readonly_vm.sh new file mode 100755 index 000000000..b2e49fccf --- /dev/null +++ b/test/vhost/readonly/disabled_readonly_vm.sh @@ -0,0 +1,47 @@ +#!/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" diff --git a/test/vhost/readonly/enabled_readonly_vm.sh b/test/vhost/readonly/enabled_readonly_vm.sh new file mode 100755 index 000000000..bc9a6c9d9 --- /dev/null +++ b/test/vhost/readonly/enabled_readonly_vm.sh @@ -0,0 +1,75 @@ +#!/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 diff --git a/test/vhost/readonly/readonly.sh b/test/vhost/readonly/readonly.sh new file mode 100755 index 000000000..fbe81cf08 --- /dev/null +++ b/test/vhost/readonly/readonly.sh @@ -0,0 +1,135 @@ +#!/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 diff --git a/test/vhost/readonly/test_plan.md b/test/vhost/readonly/test_plan.md new file mode 100644 index 000000000..957000e87 --- /dev/null +++ b/test/vhost/readonly/test_plan.md @@ -0,0 +1,30 @@ +# vhost-block readonly feature test plan + +## Objective +Vhost block controllers can be created with readonly feature which prevents any write operations on this device. +The purpose of this test is to verify proper operation of this feature. + +## Test cases description +To test readonly feature, this test will create normal vhost-blk controller with NVMe device and on a VM it will +create and mount a partition to which it will copy a file. Next it will poweroff a VM, remove vhost controller and +create new readonly vhost-blk controller with the same device. + +## Test cases + +### blk_ro_tc1 +1. Start vhost +2. Create vhost-blk controller with NVMe device and readonly feature disabled using RPC +3. Run VM with attached vhost-blk controller +4. Check visibility of readonly flag using lsblk, fdisk +5. Create new partition +6. Create new file on new partition +7. Shutdown VM, remove vhost controller +8. Create vhost-blk with previously used NVMe device and readonly feature now enabled using RPC +9. Run VM with attached vhost-blk controller +10. Check visibility of readonly flag using lsblk, fdisk +11. Try to delete previous file +12. Try to create new file +13. Try to remove partition +14. Repeat steps 2 to 4 +15. Remove file from disk, delete partition +16. Shutdown VM, exit vhost diff --git a/test/vhost/readonly/vhost.conf.in b/test/vhost/readonly/vhost.conf.in new file mode 100644 index 000000000..f23f7e478 --- /dev/null +++ b/test/vhost/readonly/vhost.conf.in @@ -0,0 +1,2 @@ +[Ioat] + Disable Yes diff --git a/test/vhost/spdk_vhost.sh b/test/vhost/spdk_vhost.sh index 3855f5a68..c6eb91ad8 100755 --- a/test/vhost/spdk_vhost.sh +++ b/test/vhost/spdk_vhost.sh @@ -17,6 +17,7 @@ case $1 in echo " -ils|--integrity-lvol-scsi for running an integrity test with vhost scsi and lvol backends" echo " -ilb|--integrity-lvol-blk for running an integrity test with vhost blk and lvol backends" echo " -hp|--hotplug for running hotplug tests" + echo " -ro|--readonly for running readonly test for vhost blk" echo " -h |--help prints this message" echo "" echo "Environment:" @@ -114,6 +115,10 @@ case $1 in --test-type=spdk_vhost_scsi \ --fio-jobs=$WORKDIR/hotplug/fio_jobs/default_integrity.job -x ;; + -ro|--readonly) + echo 'Running readonly tests suite...' + ./readonly/readonly.sh --vm_image=$VM_IMAGE --disk=Nvme0n1_size_1G + ;; *) echo "unknown test type: $1" exit 1