Linux in Linux with KVM
By Mikael Ståldal
You can do quite a lot with Docker, but sometimes you want greater capabilities or increased security, then a proper virtual machine with KVM is a good alternative. An example is when you want to run Docker containers in the VM, it’s not easy to nest Docker without forgoing all security.
Just like Alpine Linux is suitable as a base for Docker images, it is also a good option as a guest in a virtual machine.
Here is how you can run Alpine Linux 3.20 in Ubuntu 24.04.
Install KVM with friends:
apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virtiofsd
Create a VM called dev-vm
running Alpine Linux and share directory ~/src
with it:
wget https://dl-cdn.alpinelinux.org/alpine/v3.20/releases/x86_64/alpine-virt-3.20.2-x86_64.iso
virt-install --name dev-vm \
--disk size=4 \
--location $HOME/Downloads/alpine-virt-3.20.2-x86_64.iso,kernel=boot/vmlinuz-virt,initrd=boot/initramfs-virt \
--extra-args console=ttyS0 \
--osinfo alpinelinux3.19 \
--graphics none \
--console pty,target_type=serial \
--filesystem=$HOME/src,src,driver.type=virtiofs \
--memorybacking=source.type=memfd,access.mode=shared
Setup Alpine in the newly created VM:
setup-alpine
reboot
Mount the shared directory (and make sure it mounts automatically on boot):
mkdir /src
echo "src /src virtiofs defaults 0 0" >>/etc/fstab
mount /src
Then you can shut down the VM from inside with poweroff
, and reboot it from inside with reboot
.
Start it from outside with virsh start dev-vm
, and (re)connect to it with virsh console dev-vm
,
no need to install SSH server in the VM. The VM image ends up in ~/.local/share/libvirt/images
.
I wish I could script/automate the initialization of the VM more. Alpine docs suggests using cloud-init, but I could not get that to work. Anyway, this works, and you only have to do the initialization once since the VM image is stored on disk and can be restarted multiple times.