Skip to content

Instantly share code, notes, and snippets.

@arges
Last active January 8, 2018 10:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save arges/9d21c6da03a8c10d3980 to your computer and use it in GitHub Desktop.
Save arges/9d21c6da03a8c10d3980 to your computer and use it in GitHub Desktop.
Nested Virtualization Testcase for LP: #1329434
#!/bin/bash -x
#
# simple nested ubuntu vm testcase
#
# (c) 2014,2015 Chris J Arges <christopherarges@gmail.com>
L1_NAME="nested-L1"
L2_NAME="nested-L2"
L1_MEMORY="1024"
L2_MEMORY="768"
L1_CPU="2"
L2_CPU="1"
# ensure we have the right packages and kvm is setup
sudo apt-get install -qy uvtool-libvirt cpu-checker ubuntu-virt-server
if ! kvm-ok; then
echo "ERROR! Please ensure kvm is configured correctly!"
exit 1
fi
# check if intel/amd
VIRT_TYPE=$(egrep -wo 'vmx|svm' /proc/cpuinfo | uniq)
if [[ $VIRT_TYPE == "svm" ]]; then
CPU_TYPE="amd"
else
CPU_TYPE="intel"
fi
# ensure nested virtualization is setup
if [[ "$(cat /sys/module/kvm_*/parameters/nested)" == "N" ]]; then
echo "INFO: Enabling nested virtualization"
sudo modprobe -r kvm_${CPU_TYPE}
sudo modprobe kvm_${CPU_TYPE} nested=1
if [[ "$(cat /sys/module/kvm_*/parameters/nested)" == "N" ]]; then
echo "ERROR: Couldn't enable nested virtualization"
exit 1
fi
fi
# ensure keys are generated on host
if [ ! -e ~/.ssh/id_rsa ]; then
ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''
fi
# setup nested-L1 machine
uvt-simplestreams-libvirt sync release=trusty arch=amd64
if virsh list --all | grep $L1_NAME; then
uvt-kvm destroy $L1_NAME
sleep 10
fi
uvt-kvm create $L1_NAME --password ubuntu --mem ${L1_MEMORY} --cpu ${L1_CPU}
# wait for nested L1 to be available
while ! uvt-kvm ip $L1_NAME; do
sleep 10
done
sleep 5
# setup nested L1 guest
SSH_CMD="ssh ubuntu@`uvt-kvm ip $L1_NAME` -o StrictHostKeyChecking=no "
$SSH_CMD 'sudo apt-get -y -q update > /dev/null'
$SSH_CMD 'sudo apt-get -y -q install libvirt-bin uvtool-libvirt > /dev/null'
$SSH_CMD "ssh-keygen -f ~/.ssh/id_rsa -t rsa -N ''"
$SSH_CMD 'uvt-simplestreams-libvirt sync release=trusty arch=amd64'
# modify and start the default network to be a differnet subnet
$SSH_CMD "EDITOR='sed -i \"s/192.168.122/192.168.123/g\"' virsh net-edit default"
$SSH_CMD "virsh net-start default"
$SSH_CMD "virsh net-autostart default"
# create and start the L2 guest
$SSH_CMD "uvt-kvm create $L2_NAME --password ubuntu --mem ${L2_MEMORY} --cpu ${L2_CPU}"
sleep 5
# check if $L2_NAME is not paused
if $SSH_CMD "virsh list | grep $L2_NAME | grep paused"; then
echo "ERROR! Nested VM is Paused."
exit 1
fi
# check if $L1_NAME guest froze
if ! $SSH_CMD "ls"; then
echo "ERROR! Nested L1 VM is not responding"
exit 1
fi
# wait for nested L2 to be available
while ! $SSH_CMD uvt-kvm ip $L2_NAME; do
sleep 10
done
sleep 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment