Commit f2d9e514 authored by Karol Latecki's avatar Karol Latecki Committed by Ben Walker
Browse files

scripts/vagrant: automate vhost test VM creation



Enable users to automatically create VM image dependency
needed to run vhost tests.

Current state of Vagrantfile_vhost_vm is only valid
for Ubuntu 16.04 and 18.04.

Change-Id: I9cdf46c1db7fcd55cfda0dd7db7ff5570610c5ee
Signed-off-by: default avatarKarol Latecki <karol.latecki@intel.com>
Reviewed-on: https://review.gerrithub.io/422948


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: default avatarSeth Howell <seth.howell5141@gmail.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
Reviewed-by: default avatarBen Walker <benjamin.walker@intel.com>
Chandler-Test-Pool: SPDK Automated Test System <sys_sgsw@intel.com>
parent 369719c2
Loading
Loading
Loading
Loading
+111 −0
Original line number Diff line number Diff line
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(2) do |config|

  # Pick the right distro and bootstrap, default is ubuntu1604
  distro = ( ENV['SPDK_VAGRANT_DISTRO'] || "ubuntu16")
  case distro
  when "ubuntu16"
    # See: https://app.vagrantup.com/puppetlabs/boxes/ubuntu-16.04-64-nocm
    config.vm.box = "puppetlabs/ubuntu-16.04-64-nocm"
    config.vm.box_version = "1.0.0"
  when "ubuntu18"
    # See: https://app.vagrantup.com/bento/boxes/ubuntu-18.04
    config.vm.box = "bento/ubuntu-18.04"
    config.vm.box_version = "201808.24.0"
  else
    "Invalid argument #{distro}"
    abort("Invalid argument!")
  end
  config.vm.box_check_update = false

  # vagrant-cachier caches apt/yum etc to speed subsequent
  # vagrant up
  # to enable, run
  # vagrant plugin install vagrant-cachier
  #
  if Vagrant.has_plugin?("vagrant-cachier")
    config.cache.scope = :box
  end

  # use http proxy if avaiable
  if ENV['http_proxy'] && Vagrant.has_plugin?("vagrant-proxyconf")
   config.proxy.http     = ENV['http_proxy']
   config.proxy.https    = ENV['https_proxy']
   config.proxy.no_proxy = "localhost,127.0.0.1"
  end

  vmcpu=(ENV['SPDK_VAGRANT_VMCPU'] || 2)
  vmram=(ENV['SPDK_VAGRANT_VMRAM'] || 4096)
  ssh_key_dir=(ENV['SPDK_VAGRANT_SSH_KEY'])
  spdk_dir=(ENV['SPDK_DIR'] || "none")
  install_deps=(ENV['INSTALL_DEPS'] || "false")

  config.ssh.forward_agent = true
  config.ssh.forward_x11 = true

  # Change root passwd and allow root SSH
  config.vm.provision "shell", inline: 'echo -e "root\nroot" | sudo passwd root'
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"PermitRootLogin yes\" >> /etc/ssh/sshd_config"'

  # Use previously generated SSH keys for setting up a key pair
  $ssh_key_gen_script = <<-SCRIPT
  sudo mkdir -p /root/.ssh
  cat /vagrant/ssh_keys/spdk_vhost_id_rsa.pub > /root/.ssh/authorized_keys
  sudo chmod 644 /root/.ssh/authorized_keys
  SCRIPT
  config.vm.provision "shell", inline: $ssh_key_gen_script

  # Install needed deps
  $apt_script = <<-SCRIPT
  sudo apt -y update
  sudo DEBIAN_FRONTEND=noninteractive apt -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade
  sudo apt -y install -y fio sg3-utils bc
  SCRIPT
  config.vm.provision "shell", inline: $apt_script

  # Modify GRUB options
  # console=ttyS0 earlyprintk=ttyS0 - reroute output to serial dev, so that QEMU can write output to file
  # scsi_mod.use_blk_mq=1 - for multiqueue use
  # net.ifnames=0 biosdevname=0 - do not rename NICs on boot. That way we ensure that addded NIC is always eth0.
  #     Reason for these options is that NIC can have different udev name during provisioning with Vagrant
  #     and then some other name while running SPDK tests which use Qemu without any hypervisor like vbox or libvirt
  #     so no corresponding configuration for this NIC name will be present in /etc.
  config.vm.provision "shell", inline: 'sudo sed -ir s#GRUB_CMDLINE_LINUX=\"\"#GRUB_CMDLINE_LINUX=\"console=ttyS0\ earlyprintk=ttyS0\ scsi_mod.use_blk_mq=1\ net.ifnames=0\ biosdevname=0\"#g /etc/default/grub'
  config.vm.provision "shell", inline: 'sudo update-grub'

  # TODO: Next 2 lines break any future ssh communication via "vagrant ssh"
  # I'd be good to check NIC names in ifconfig and then sed them in /etc/network/interfaces to eht0, eht1, and so on
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"auto eth0\" >> /etc/network/interfaces"'
  config.vm.provision "shell", inline: 'sudo sh -c "echo \"iface eth0 inet dhcp\" >> /etc/network/interfaces"'

  if distro.include? "ubuntu18"
    # This is to avoid annoying "Start job is running for wait for network to be configured" 2 minute timeout
    # in case of not-so-perfect NIC and virtual network configuration for the VM
    config.vm.provision "shell", inline: 'systemctl disable systemd-networkd-wait-online.service'
    config.vm.provision "shell", inline: 'systemctl mask systemd-networkd-wait-online.service'
  end

  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--ioapic", "on"]
    vb.memory = "#{vmram}"
    vb.cpus = "#{vmcpu}"

    #support for the SSE4.x instruction is required in some versions of VB.
    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.1", "1"]
    vb.customize ["setextradata", :id, "VBoxInternal/CPUM/SSE4.2", "1"]
  end

  if spdk_dir != "none"
    config.vm.synced_folder "#{spdk_dir}", "/home/vagrant/spdk_repo/spdk", type: "rsync", rsync__auto: false
    if install_deps.include? "true"
      config.vm.provision "shell", inline: 'sudo /home/vagrant/spdk_repo/spdk/scripts/pkgdep.sh'
    end
  end

  # Copy in the user's tools if they exists
  if File.directory?(File.expand_path("~/vagrant_tools"))
    config.vm.synced_folder "~/vagrant_tools", "/home/vagrant/tools", type: "rsync", rsync__auto: false
  end
end
+129 −0
Original line number Diff line number Diff line
#!/usr/bin/env bash

# create_vhost_vm.sh
#
# Creates a virtual machine image used as a dependency for running vhost tests

set -e

VAGRANT_TARGET="$PWD"

DIR="$( cd "$( dirname $0 )" && pwd )"
SPDK_DIR="$( cd "${DIR}/../../" && pwd )"
USE_SSH_DIR=""
MOVE_TO_DEFAULT_DIR=false
INSTALL_DEPS=false

# The command line help
display_help() {
	echo
	echo " Usage: ${0##*/} <distro>"
	echo
	echo "  distro = <ubuntu16 | ubuntu18> "
	echo
	echo "  --use-ssh-dir=<dir path>    Use existing spdk_vhost_id_rsa keys from specified directory"
	echo "                              for setting up SSH key pair on VM"
	echo "  --install-deps              Install SPDK build dependencies on VM. Needed by some of the"
	echo "                              vhost and vhost initiator tests. Default: false."
	echo "  --move-to-default-dir           Move generated files to default directories used by vhost test scripts."
	echo "                              Default: false."
	echo "  --http-proxy                Default: \"${SPDK_VAGRANT_HTTP_PROXY}\""
	echo "  -h help"
	echo
	echo " Examples:"
	echo
}

while getopts ":h-:" opt; do
	case "${opt}" in
		-)
		case "${OPTARG}" in
			use-ssh-dir=*) USE_SSH_DIR="${OPTARG#*=}" ;;
			move-to-default-dir) MOVE_TO_DEFAULT_DIR=true ;;
			install-deps) INSTALL_DEPS=true ;;
			http-proxy=*)
				http_proxy=$OPTARG
				https_proxy=$http_proxy
				SPDK_VAGRANT_HTTP_PROXY="${http_proxy}"
			;;
			*)
				echo "  Invalid argument -$OPTARG" >&2
				echo "  Try \"$0 -h\"" >&2
				exit 1
				;;
		esac
		;;
		h)
			display_help >&2
			exit 0
		;;
		*)
			echo "  Invalid argument: -$OPTARG" >&2
			echo "  Try: \"$0 -h\"" >&2
			exit 1
		;;
	esac
done
export SPDK_DIR
export SPDK_VAGRANT_HTTP_PROXY
export INSTALL_DEPS


shift "$((OPTIND-1))"   # Discard the options and sentinel --
SPDK_VAGRANT_DISTRO="$@"

case "$SPDK_VAGRANT_DISTRO" in
	ubuntu16)
		export SPDK_VAGRANT_DISTRO
	;;
	ubuntu18)
		export SPDK_VAGRANT_DISTRO
	;;
	*)
		echo "  Invalid argument \"${SPDK_VAGRANT_DISTRO}\""
		echo "  Try: \"$0 -h\"" >&2
		exit 1
	;;
esac

mkdir -vp "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}"
cp ${DIR}/Vagrantfile_vhost_vm ${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/Vagrantfile

# Copy or generate SSH keys to the VM
mkdir -vp "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/ssh_keys"

if [[ -n $USE_SSH_DIR ]]; then
	cp ${USE_SSH_DIR}/spdk_vhost_id_rsa* "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/ssh_keys"
else
	ssh-keygen -f "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/ssh_keys/spdk_vhost_id_rsa" -N "" -q
fi
export SPDK_VAGRANT_SSH_KEY="${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/ssh_keys/spdk_vhost_id_rsa"

pushd "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}"
if [ ! -z "${http_proxy}" ]; then
	export http_proxy
	export https_proxy
	if vagrant plugin list | grep -q vagrant-proxyconf; then
		echo "vagrant-proxyconf already installed... skipping"
	else
		vagrant plugin install vagrant-proxyconf
	fi
fi
VBoxManage setproperty machinefolder "${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}"
vagrant up
vagrant halt
VBoxManage setproperty machinefolder default

# Convert Vbox .vmkd image to qcow2
vmdk_img=$(find ${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO} -name "*.vmdk")
qemu-img convert -f vmdk -O qcow2 ${vmdk_img} ${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/vhost_vm_image.qcow2

if $MOVE_TO_DEFAULT_DIR; then
	sudo mkdir -p /home/sys_sgsw
	sudo mv -f ${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/vhost_vm_image.qcow2 /home/sys_sgsw/vhost_vm_image.qcow2
	sudo mv -f ${VAGRANT_TARGET}/${SPDK_VAGRANT_DISTRO}/ssh_keys/spdk_vhost_id_rsa* ~/.ssh/
fi

echo ""
echo "  SUCCESS!"
echo ""
+20 −0
Original line number Diff line number Diff line
@@ -40,6 +40,26 @@ configuration file. For a full list of the variable declarations available for a
The Vhost tests also require the creation of a second virtual machine nested inside of the test VM.
Please follow the directions below to complete that installation. Note that host refers to the Fedora VM
created above and guest or VM refer to the Ubuntu VM created in this section.

1. Follow instructions from spdk/scripts/vagrant/README.md
	- install all needed packages mentioned in "Mac OSX Setup" or "Windows 10 Setup" sections
	- follow steps from "Configure Vagrant" section

2. Use Vagrant scripts located in spdk/scripts/vagrant to automatically generate
	VM image to use in SPDK vhost tests.
	Example command:
	~~~{.sh}
	spdk/scripts/vagrant/create_vhost_vm.sh --move-to-def-dirs ubuntu16
	~~~
	This command will:
		- Download a Ubuntu 16.04 image file
		- upgrade the system and install needed dependencies (fio, sg3-utils, bc)
		- add entry to VM's ~/.ssh/autorized_keys
		- add appropriate options to GRUB command line and update grub
		- convert the image to .qcow2 format
		- move .qcow2 file and ssh keys to default locations used by vhost test scripts

Alternatively it is possible to create the VM image manually using following steps:
1. Create an image file for the VM. It does not have to be large, about 3.5G should suffice.
2. Create an ssh keypair for host-guest communications (performed on the host):
    - Generate an ssh keypair with the name spdk_vhost_id_rsa and save it in `/root/.ssh`.