Sachin Patil

Sachin Patil

Free Software Developer | GNU Emacs Hacker

How to create a nested VM?
Published on Dec 19, 2018 by Sachin.

This post provides steps to create a virtual-machine inside another virtual-machine. We will use virt-manager to achieve this. Please note that the physical host should support Virtualization Technology(VT). Follow this post to verify and enable VT in the host.

Assuming that the virtual-machine is already running on the host, SSH to the VM and install following packages needed to create and manage a nested VM:

sudo yum install virt-manager virt-install libvirt qemu-kvm

Now load the kvm kernel module that provides virtualization capabilities:

sudo modprobe kvm

Optionally, you can load CPU specific kvm module: kvm_intel or kvm_amd.

Enable and start the libvirtd service:

1: sudo systemctl enable libvirtd
2: sudo systemctl start libvirtd

For the sake of this post, we will use an Cirros OS image as the root disk. Download the image inside /var/lib/libvirt/images/:

sudo curl https://download.cirros-cloud.net/0.4.0/cirros-0.4.0-x86_64-disk.img -o /var/lib/libvirt/images/cirros.img -#

You may need to add a non-root user to kvm & libvirt group to use the virt-install command:

1: sudo usermod -aG kvm NON_ROOT_USER
2: sudo usermod -aG libvirt NON_ROOT_USER

Once the image is in place, we can create a VM using virt-install:

1: virt-install --os-type=linux \
2:              --os-variant=rhel7 \
3:              --name vm01 \
4:              --ram=256 \
5:              --vcpus=1 \
6:              --disk path=/var/lib/libvirt/images/cirros.img \
7:              --graphic none \
8:              --import

Finally start the domain using,

virsh start vm01