# libvirt/virt-install ## Prerequisites `libvirt`, `qemu`, `virt-install` ### Archlinux: ```bash pacman -S virt-install libvirt qemu-desktop pacman -S virt-manager # optional, graphical interface to libvirt pacman -S qemu-emulators-full # optional, provides extra architectures emulation for qemu systemctl enable libvirtd.socket # socket activation ``` ### MacOS ```bash brew install qemu libvirt virt-manager # installing virt-manager might take a while since it requires GTK # optional brew install virt-viewer brew services start libvirt ``` The procedure for installing VMs on MacOS is similar to the standard processes, although some flags have to be changed when running on M1: 1. remove the `--virt-type kvm` flag as this is not supported, it will fallback to `qemu` hypervisor 2. remove the `--cpu host` flag and add `--arch x86_64` to `virt-install` in order to run an `x86_64` system (this might depends on the architecture you want to emulate) Follow instructions below, editing commands as required. ## Installing VMs ### Archlinux ```bash curl -O "https://mirror.checkdomain.de/archlinux/iso/2022.05.01/archlinux-2022.05.01-x86_64.iso" virt-install --name archlinux \ --memory 2048 \ --vcpus=2,maxvcpus=4 --cpu host \ --cdrom archlinux-2022.05.01-x86_64.iso \ --disk size=4,format=qcow2 \ --network user \ --virt-type kvm \ --console pty,target_type=virtio \ --nographics ``` Follow the installation procedure. Edit the kernel cmdline and a serial console before rebooting otherwise the `virsh` serial console won't work: In `/etc/default/grub`, add to the following to `GRUB_CMDLINE_LINUX` along with any other cmdline flags you require ``` GRUB_CMDLINE_LINUX=console=tty0 console=ttyS0,115200 # and any other flags ``` Update grub: ```bash grub-mkconfig -o /boot/grub/grub.cfg ``` ### Nixos ```bash curl -O "https://channels.nixos.org/nixos-21.11/latest-nixos-minimal-x86_64-linux.iso" virt-install --name nixos \ --memory 2048 \ --vcpus=2,maxvcpus=4 \ --cpu host \ --cdrom latest-nixos-minimal-x86_64-linux.iso \ --disk size=4,format=qcow2 \ --network user \ --virt-type kvm \ --console pty,target_type=virtio \ --nographics ``` Follow the installation procedure, adding the following to `/etc/nixos/configuration.nix`, otherwise the `virsh` serial console won't work ``` boot.kernelParams = [ "console=ttyS0,115200" "console=tty1" ]; ``` ### Ubuntu 21.10 (focal) ```bash virt-install --name ubuntu-focal \ --memory 2048 \ --vcpus=2,maxvcpus=4 \ --cpu host \ --pxe \ --disk size=4,format=qcow2 \ --network user \ --virt-type kvm \ --nographics \ --osinfo ubuntufocal \ --extra-args="console=tty0 console=ttyS0,115200" \ --location http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/ ``` ## Other OSs You can list other supported Operating Systems using ```bash virt-install --osinfo list # More info about available OSs osinfo-query os ``` ### Note about serial consoles In order for the virsh serial console to work, these kernel cmdline options have to be provided ``` console=tty0 console=ttyS0,115200 ``` ## Usage ```bash # list all available domains (running and stopped) virsh list --all # Start a domain ("archlinux") virsh start archlinux # Connect to the serial console virsh console archlinux # Destroy (destop) a domain ("archlinux") virsh destroy archlinux # Remove a domain virsh undefine archlinux # disks will **not** be removed # Edit a domain virsh edit archlinux # Get information about network configuration virsh domifaddr archlinux ``` ### Note about serial consoles Serial consoles might not work out of the box, so it might be required to edit the domain definition to add a serial console: ```bash virsh edit ``` then add a pty console: ```xml ``` Also see notes about serial consoles above.