Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active March 2, 2024 19:14
Show Gist options
  • Save plembo/f592b80eade840a51c7c52d22b2c1b63 to your computer and use it in GitHub Desktop.
Save plembo/f592b80eade840a51c7c52d22b2c1b63 to your computer and use it in GitHub Desktop.
Install a Debian VM using virt-install

Installing a Debian VM with virt-install on KVM (libvirtd)

This turned out to be easier than I thought it would, especially after struggling with Ubuntu.

Both procedures use a common debian.env file to hold environment variables applied in the script.

When installing Debian always be sure to select an apt mirror when prompted, if you don't you'll regret it.

Environment variables

In a file, debian.env:

export VM_NAME="testsrv02"
export VM_ISO="/data/install/debian/debian-live-10.7.0-amd64-standard.iso"
export VM_URL="http://ftp.us.debian.org/debian/dists/stable/main/installer-amd64/"
export VM_OS="debian10"
export VM_IMG="/data/libvirt/images/${VM_NAME}.qcow2"
export VM_CORES=1
export VM_DISKSIZE=10
export VM_RAMSIZE=2048
export VM_NET="default"

Graphical install

Bash script to keep things straight, inst-debian.sh:

#!/bin/bash
source debian.env
sudo virt-install \
--name ${VM_NAME} \
--memory ${VM_RAMSIZE} \
--vcpus ${VM_CORES} \
--os-variant=${VM_OS} \
--virt-type=kvm \
--network network=${VM_NET},model=virtio \
--graphics vnc \
--disk path=${VM_IMG},size=${VM_DISKSIZE},bus=virtio,format=qcow2 \
--cdrom ${VM_ISO}

Non-graphical install

Another bash script, this time allowing you to do the install using a serial console:

#!/bin/bash
source debian.env
sudo virt-install \
--name ${VM_NAME} \
--memory ${VM_RAMSIZE} \
--vcpus ${VM_CORES} \
--os-variant=${VM_OS} \
--virt-type=kvm \
--network network=${VM_NET},model=virtio \
--graphics none \
--disk path=${VM_IMG},size=${VM_DISKSIZE},bus=virtio,format=qcow2 \
--location ${VM_URL} \
--extra-args="console=ttyS0"

Notes on post-install customization

The following packages should always get installed:

  • openssh-server
  • vim
  • sudo
  • spice-vdagent

After installing vim, as root run update-alternatives --config editor. This will allow you to select vim.basic as the default system editor. Unless you can actually stand nano.

The spice-vdagent will make it unnecessary to hit Ctl-Alt every time you want to move the mouse away from the VMM console.

If you ignored by advice above and didn't select an apt mirror when asked you'll just have to edit or add an /etc/apt/sources.list file containing your preferred mirrors. Here's mine from a recent Debian 10 (Buster) install:

deb http://deb.debian.org/debian/ buster main
deb-src http://deb.debian.org/debian/ buster main

deb http://security.debian.org/debian-security buster/updates main
deb-src http://security.debian.org/debian-security buster/updates main

# buster-updates, previously known as 'volatile'
deb http://deb.debian.org/debian/ buster-updates main
deb-src http://deb.debian.org/debian/ buster-updates main

What's next?

Next steps are to try to automate this a bit more by answering key questions raised by the installer, and doing things like injecting "net.ifnames=0" into /etc/default/grub. Both require using the "--location" and "--extra-args" options invoked in the non-graphical install procedure above (which, by the way, can also be used with "--graphics vnc" as well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment