QEMU Virtual Machine Setup

Setting up a QEMU virtual machine (VM) is a task I perform frequently. Here is my go-to method for getting a VM up and running quickly.

Download ISO and Setup SSH

Download and boot a Debian nocloud image using QEMU with SSH port forwarding (Host:5573). Then, install openssh-server and enable root login within the guest to allow ssh access.

$ wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.raw
$ qemu-system-x86_64 -M pc,accel=kvm -m 1G \
		-drive file=./debian-12-nocloud-amd64.raw,if=virtio \
		-netdev type=user,hostfwd=tcp::5573-:22,id=net0 \
		-device virtio-net,netdev=net0 -rtc base=localtime -smp 4 \
		-nographic
# Login with user 'root'
VM$ apt update && apt install openssh-server
VM$ echo "PermitRootLogin yes" > /etc/ssh/sshd_config.d/root_login.conf
VM$ systemctl restart sshd
# Set root password
VM$ passwd
$ ssh root@localhost -p 5573

Resize Disk

In case the default disk size is too small we need to resize the image and resize the partition.

$ qemu-img resize debian-12-nocloud-amd64.raw +20G
# Boot the VM
VM$ apt update
VM$ apt install fdisk
VM$ cfdisk /dev/vda
# Delete all partitions except `vda1`
# Use `Resize` to resize partition `vda1`
VM$ vim /etc/fstab
# Delete the entry for `/boot/efi`
VM$ reboot
VM$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1 1024M  0 rom
vda    253:0    0   22G  0 disk
└─vda1 253:1    0 21.9G  0 part /