Debian Installation
Check virtualisation in Debian: egrep -c '(vmx|svm)' /proc/cpuinfo
. This needs to be above 0
Debian install: sudo apt install qemu-kvm qemu-system qemu-utils python3 python3-pip libvirt-clients libvirt-daemon-system bridge-utils virtinst libvirt-daemon virt-manager -y
Verify install: sudo systemctl status libvirtd.service
Courtesy of Christitus
First use of QEMU
Create image qemu-img create -f qcow2 image.img 10G
-f
First image format- qcow2
Run VM:
qemu-system-x86_64 -enable-kvm -cdrom <ISO> -boot menu=on -drive file=image.img -m 4G -cpu host -smp 4 -vga virtio -display sdl,gl=on -serial none
qemu-system-x86_64
This refers to the system that is being emulated.-enable-kvm
enables Kernel based virtual machine, this turns the Linux kernel into a hypervisor.-boot menu=on
to have boot menu-drive file=image.img
is the ‘hard drive’-m 4G
means 4GB of RAM.-cpu host
This tells QEMU to emulate the same CPU as the host machine. It enables the virtual machine to use CPU features present on the host, improving performance.-smp 4
: Defines the number of CPU cores for the virtual machine. In this case, it assigns 4 cores.-vga virtio
Sets the VGA graphics device to VirtIO. VirtIO is a platform for I/O virtualization, often providing better performance than emulated graphics.-display sdl,gl=on
Specifies the display output settings. sdl uses the SDL (Simple DirectMedia Layer) for displaying the VM’s output window. gl=on enables OpenGL support for rendering, which can improve graphic performance and provide 3D acceleration support.
Networking
By default, QEMU uses User-Mode Networking (SLIRP). There is no configuration needed on the host nor the guest machine. The VM receives network access through a NAT (Network address translation) which is provided by QEMU.
Another option is Bridged networking, which shows the VM as a separate device on the network.
With User-Mode Networking you’re able to port-forward if you would need access to the VM from an external network. -net user,hostfwd=tcp::10022-:22
will forward a TCP connection from port 10022
on the host to port 22
on the VM.
You’re also able to specify the MAC Address using the following command -device virtio-net,netdev=hn0,mac=52:54:00:12:34:56
.
UEFI
In our command from Running our VM we didn’t use an UEFI compatible BIOS.
To boot QEMU in UEFI we need to have UEFI firmware (for QEMU). OVMF (Open virtual machine firmware) is one of the most popular options.
We can install the following Debian package: OVMF
Our command also gets the following options to specify the firmware: -drive if=pflash,format=raw,readonly=true,file=/usr/share/OVMF/OVMF_CODE.fd -drive if=pflash,format=raw,file=/usr/share/OVMF/OVMF_VARS.fd
if=pflash,format=raw,readonly,file=/usr/share/OVMF/OVMF_CODE.fd
: This option specifies the OVMF firmware.if=pflash,format=raw,file=/usr/share/OVMF/OVMF_VARS.fd
: This option specifies the UEFI variable store. It’s not read-only, so UEFI variables can be stored between reboots.
Note: /usr/share/OVMF
might have a different location and or need root priveleges.
Updated command to boot in UEFI: sudo qemu-system-x86_64 -enable-kvm -cdrom <ISO> -boot menu=on -drive if=pflash,format=raw,readonly=true,file=/usr/share/OVMF/OVMF_CODE.fd -drive if=pflash,format=raw,file=/usr/share/OVMF/OVMF_VARS.fd -drive file=image.img -m 4G -cpu host -smp 4 -vga virtio -display sdl,gl=on -serial none
Sendkey
Sometimes the host intercepts certain interrupts like sending keys at a low level. We can use QEMU’s sendkey
command to send these to the guest machine.
To bring up the QEMU Console use ctrl+alt+2
. We can switch back with ctrl+alt+1
or closing the console.
Example command: sendkey ctrl-alt-f1
Virtual file system & Sharing
We can have file sharing capabilities by mounting certain folders as a virtual file system.
If we add the option -virtfs local,path=/home/mathieu/Development/VirtualMachines/nixos/share/,mount_tag=hostshare,security_model=none,id=hostshare
we create a shared folder on the path specified.
Mounting in Linux can be done with the following command: mount -t 9p -o trans=virtio,version=9p2000.L hostshare /mnt/hostshare
Note: Make sure that the directory /mnt/hostshare
exists.
References
Setup Qemu in Debian Linux by Chris Titus QEMU: A proper guide! by DenshiVideo