Create VM (cloud-init)
For this entire section you need to perform the VM config as the libvirt-admin
user.
Login to the shell account of libvirt-admin
:
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:
NAME=debian-dev
source ~/libvirt/${NAME}.env
Create directories to hold the VM disks and config files:
mkdir -p ~/libvirt/{cloud-images,disks,cloud-init,iso}
Create the cloud-init config file:
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:
You only need to download each CLOUD_IMAGE once, they will be cached
in ~/libvirt/cloud-images
, so they can be be reused.
(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:
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:
## 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:
This is destructive of the existing disk file!
(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
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:
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
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.
virsh shutdown ${NAME}
Verify VM is shut down
virsh list --all
Id Name State ----------------------------- - debian-dev shut off
Before proceeding to the next step, make sure the VM is in the off state.