1

Topic: QEMU I tell you how I use qemu inside qemu or nested virtualization

First step, activate on modprobe nested were you CPU is provided, in my case:

a file `/etc/modprobe.d/kvm.conf`

options kvm_intel nested=1

next I like use a folder with folers about distros, for example:

hyperbola/
trisquel/
guix/

then inside had one folder I put a iso, example:

hyperbola/iso.iso
trisquel/iso.iso
guiix/iso.iso

then, for run a nested virtualization for example hyperbola like a host inside trisquel with inside guix you cant start your trisquel machine how the next:

#!/bin/bash

cd path/to/trisquel

isoi=iso.iso
disk=img.img
vm_name=Trisquel
## CPUs
cpus=8
## RAM
ram=6 #GBs de RAM
ram=$(( $ram * 1024 ))
# o se puede comentar lo de arriba y establecer los MBs personalizados
#ram=2560
#net=(-netdev user,id=vmnic -device virtio-net,netdev=vmnic)
net="-netdev user,id=${vm_name} -device e1000,netdev=${vm_name}"

qemu="qemu-system-x86_64"
audio=''
screen='-display sdl'
if echo $1 | grep audio &> /dev/null || \
        echo $2 | grep audio &> /dev/null
then
    echo 'Enabling audio'
    audio+='-device sb16       '
    audio+='-device adlib     '
    audio+='-device ES1370    '
    audio+='-device cs4231a   '
    audio+='-device gus       '
    audio+='-device AC97      '
    audio+='-device intel-hda '
    audio+='-device hda-duplex'
fi
if echo $1 | grep virtio &> /dev/null || \
        echo $2 | grep virtio &> /dev/null || \
        echo $1 | grep full &> /dev/null || \
        echo $2 | grep full &> /dev/null
then
    echo 'Enabling full screen screen'
    screen+='-full-screen -vga virtio'
fi
if echo $1 | grep std &> /dev/null || \
        echo $2 | grep std &> /dev/null
then
    echo 'Enabling std screen'
    screen+='-vga std'
fi

screen=''

if ! [ -f img.img ]
then
    qemu-img create -f qcow2 -o size=80G img.img
fi

if du -sh img.img | grep G &> /dev/null || \
    du -sh img.img | grep M &> /dev/null && \
        ! echo "$1" | grep boot &> /dev/null
then
    $qemu -k es                            \
          $audio                           \
          $screen                          \
          -accel kvm,kernel-irqchip=split  \
          -cpu host                        \
          -name $vm_name                   \
          -rtc base=utc                    \
          -smp cpus=$cpus -m $ram          \
          $net $disk
else
    $qemu -k es                            \
          $audio                           \
          $screen                          \
          -accel kvm,kernel-irqchip=split  \
          -cpu host                        \
          -name $vm_name                   \
          -rtc base=utc                    \
          -smp cpus=$cpus -m $ram          \
          $net -boot d -cdrom $isoi -hdd $disk
fi

^ then you only need replace folder of iso.iso containe, and the last script create in automate img.img ← for your new virtual disk if not exist, for when you want delete, only delete img.img and run again for start on zero, then inside you can run again a qemu some similar or another xD!!