Create VM (cloud-init)

Info

For this entire section you need to perform the VM config as the libvirt-admin user.

Login to the shell account of libvirt-admin:

[bash]: Run this on your workstation:
sudo su libvirt-admin -l

Source the config

Now, and anytime you come back later to work on the same VM, source the config file:

Run this as the libvirt-admin user
NAME=debian-dev
source ~/libvirt/${NAME}.env

Create directories to hold the VM disks and config files:

Run this as the libvirt-admin user
mkdir -p ~/libvirt/{cloud-images,disks,cloud-init,iso}

Create the cloud-init config file:

Run this as the libvirt-admin user
cat << EOF | sed 's/\xe2\x80\x8b//g' > ${USER_DATA}
#cloud-config
hostname: ${NAME}
users:
​  - name: root
    ssh_authorized_keys:
​      - $(cat ~/libvirt/user-ssh.pub)
EOF

Download the cloud image:

Tip

You only need to download each CLOUD_IMAGE once, they will be cached in ~/libvirt/cloud-images, so they can be be reused.

Run this as the libvirt-admin user
(set -e
cd ~/libvirt/cloud-images
curl -LO ${CLOUD_IMAGE}
chmod a-w $(echo ${CLOUD_IMAGE} | grep -Po ".*/\K.*$")
)

Clean up old VMs with the same name:

Warning

If you already have a VM with the same name, and you want to start again from scratch, you need to clean up from the previous install first:

Run this as the libvirt-admin user
## To cleanup and REMOVE an old VM named debian-dev:
virsh destroy debian-dev
virsh managedsave-remove debian-dev
virsh undefine debian-dev

Create the disk image for the new VM:

Warning

This is destructive of the existing disk file!

Run this as the libvirt-admin user
(set -e
cp ~/libvirt/cloud-images/$(echo ${CLOUD_IMAGE} | grep -Po ".*/\K.*") \
   ~/libvirt/disks/${NAME}.qcow2
chmod u+w ~/libvirt/disks/${NAME}.qcow2
qemu-img resize ~/libvirt/disks/${NAME}.qcow2 +${DISK_SIZE}G
echo Created ~/libvirt/disks/${NAME}.qcow2
)

Create the VM

Run this as the libvirt-admin user
virt-install \
  --name ${NAME} \
  --os-variant ${OS_VARIANT} \
  --virt-type kvm \
  --cpu host \
  --vcpus ${CPUS} \
  --memory ${MEMORY} \
  --graphics none \
  --console pty,target_type=serial \
  --network bridge=virbr0,model=virtio,mac=${MAC_ADDRESS} \
  --cloud-init user-data=${USER_DATA} \
  --import \
  --disk ~/libvirt/disks/${NAME}.qcow2

Watch the console for any errors

As the VM starts up, your terminal will attach to the console output of the VM. This is to monitor any errors that may occur during the bootup, especially relating to cloud-init.

Wait until you see this Login message:

(stdout)
debian-dev login:

Disconnect from the VM console

To disconnect from the VM console, press the keyboard combination Ctrl+] (meaning to hold the Control key and the right square bracket key at the same time.)

Shutdown the VM

Info

It is important to shut down the VM the first time after install, otherwise you will get an error about the unejected cloud-init ISO.

Run this as the libvirt-admin usre
virsh shutdown ${NAME}

Verify VM is shut down

Run this as the libvirt-admin user
virsh list --all
(stdout)
​ Id   Name         State
​-----------------------------
​ -    debian-dev   shut off

Before proceeding to the next step, make sure the VM is in the off state.