Enabling Nested Virtualization

By enabling nested virtualization, you can run virtual machines (VMs) within other VMs. This option is only available for VMs created in dedicated groups. For more information, see About Dedicated Groups.

This technical guide explains how to enable and use nested virtualization on Rocky Linux and Windows Server 2019.

In this example procedure, the image used for the nested VM is based on a live CD. The operating system will be loaded directly into the nested VM’s memory.

Enabling Nested Virtualization on Linux

  1. Create a dedicated VM, for example using the following parameters:

    $ osc-cli api CreateVms \
        --profile "default" \
        --VmType "tinav4.c16r32p2" \
        --ImageId "ami-e58ac287" \
        --KeypairName "MyKeyPair" \
        --NestedVirtualization True \
        --Placement '{"Tenancy": "dedicated"}'
        --BlockDeviceMappings '[
          {
            "Bsu": {"VolumeSize": 50}, "DeviceName": "/dev/sda1"
          }
        ]'

    This command contains the following attributes that you need to specify:

    • (optional) profile: The named profile you want to use, created when configuring OSC CLI. For more information, see Installing and Configuring OSC CLI.

    • VmType: The type of the VM.

    • ImageId: The ID of the OMI to use to create the VM. For more information, see Official OMIs Reference.

    • KeypairName: The name of your keypair.

    • NestedVirtualization: Must be set to true to enable nested virtualization.

    • Placement: The tenancy option for the VM. Must be set to dedicated. For more information, see About Instances > Instance Tenancy.

    • BlockDeviceMappings: One or more block device mappings. For more information, see Defining Block Device Mappings.

      Make sure your host VM has enough memory and cores to run nested VMs.

  2. Access your VM. For more information, see Accessing a Linux VM.

  3. Run the following command to update the existing system packages:

    $ sudo dnf update
  4. (optional) Make sure nested virtualization is enabled:

    $ cat /proc/cpuinfo | egrep "vmx|svm"

    You must get vmx or svm in the result, depending on if your VM has an Intel or an AMD CPU.

  5. Install required packages:

    $ sudo dnf install @virt virt-top libguestfs-tools virt-install
  6. Start and enable the libvirtd daemon:

    $ sudo systemctl enable --now libvirtd
  7. Download the Linux virtual image you want to run.
    In this example, we are downloading Debian 11:

    $ curl -O https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-11.5.0-amd64-DVD-1.iso
  8. Make sure QEMU can access and read your file:

    $ sudo cp /home/outscale/debian-11.5.0-amd64-DVD-1.iso /var/lib/libvirt/images/debian-11.5.0-amd64-DVD-1.iso
  9. Create a virtual hard drive:

    $ sudo qemu-img create -f qcow2 /var/lib/libvirt/images/debian.qcow2 30G

    This command contains the following attributes that you need to specify:

    • debian: The name of the virtual hard drive you want to create.

    • 30: The space you want to assign to the virtual hard drive.

  10. Create a VM within your VM:

    $ sudo virt-install \
        --virt-type kvm \
        --name Debian11 \
        --ram 2024 \
        --disk /var/lib/libvirt/images/debian.qcow2 \
        --network network=default \
        --graphics vnc,listen=0.0.0.0 \
        --noautoconsole \
        --os-type=linux \
        --os-variant=generic \
        --cdrom=/var/lib/libvirt/images/debian-11.5.0-amd64-DVD-1.iso

    This command contains the following attributes that you need to specify:

    • name: The name you want to give to your VM

    • ram: The amount of memory you want to assign your VM, in megabytes (MB)

    • disk: The path of the virtual hard drive created

    • cd-rom: The path of the ISO file you want to use to install the VM

  • Run the following command to list all running and stopped VMs:

    $ sudo virsh list --all
  • You can now use SSH to connect to the nested VM.

Enabling Nested Virtualization on Windows Server 2019

  1. Create a dedicated VM, for example using the following parameters:

    $ osc-cli api CreateVms \
        --profile "default" \
        --VmType "tinav4.c16r32p2" \
        --ImageId "ami-f4901add" \
        --KeypairName "MyKeyPair" \
        --NestedVirtualization True \
        --Placement '{"Tenancy": "dedicated"}'

    This command contains the following attributes that you need to specify:

    • (optional) profile: The named profile you want to use, created when configuring OSC CLI. For more information, see Installing and Configuring OSC CLI.

    • VmType: The type of the VM.

    • ImageId: The ID of the OMI to use to create the VM. For more information, see Official OMIs Reference.

    • KeypairName: The name of your keypair.

    • NestedVirtualization: Must be set to true to enable nested virtualization.

    • Placement: The tenancy option for the VM. Must be set to dedicated. For more information, see About Instances > Instance Tenancy.

      Make sure your host VM has enough memory, cores, and volume space to run nested VMs.

  2. Access your VM. For more information, see Accessing a Windows VM.

  3. Click the Start menu and type Turn Windows features on or off.

  4. The Add Roles and Features Wizard window opens.

  5. Select the installation type you prefer.
    Click Next.

  6. Select the destination on which to install roles and features.
    Click Next.

  7. Make sure Hyper-V is checked.
    A window opens. Make sure Include management tools (if applicable) is checked.

  8. Click Add Features.

  9. Continue the installation.

  10. Restart your VM.

  11. Open Hyper-V Manager from the Start menu.

  12. Select your device name.

  13. Click New then Virtual Machine in the right action menu.

    You can also click Quick Create and select one of the operating system images available.

  14. Type a name for your VM.
    Click Next.

  15. Select Generation 1.
    Click Next.

  16. Make sure to allocate at least 4096 MB of startup memory.
    Click Next.

  17. (optional) Select a network adapter.
    Click Next.

  18. Select Create a virtual hard disk. Make sure the size is less than 400 GB.
    Click Next.

  19. Select Install an operating system from a bootable CD/DVD-ROM > Image file (.iso), and import the image of the operating system you want to run.

  20. Select Next then Finish.

  21. Right-click your newly created VM and select Start.
    Repeat the operation then select Connect.

Related Pages