Create a KVM VM using Terraform
-
Check if the kvm module is loaded
kvm-md or kvm-intel also have to be loaded — depending on your chipsetlsmod | grep -i kvm
-
Install virt-manager — that's a package to create VMs with a GUI, but we're only using it to install all of its dependencies
sudo apt install virt-manager
-
Launch libvirt
sudo systemctl start libvirtd
To allow terraform to do its thing:
-
Edit libvirt configurations
sudo vi /etc/libvirt/qemu.conf
-
Update
#security_driver = "selinux"
tosecurity_driver = "none"
-
Restart libvirt
sudo systemctl restart libvirtd
-
For easier debugging: manually get the image and update the root password
mkdir local cd local wget http://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
sudo apt install -y libguestfs-tools genisoimage sudo virt-customize -a jammy-server-cloudimg-amd64.img --root-password password:mysecretpassword [ 0.0] Examining the guest ... [ 12.4] Setting a random seed virt-customize: warning: random seed could not be set for this type of guest [ 12.4] Setting the machine ID in /etc/machine-id [ 12.4] Setting passwords [ 13.5] Finishing off
# Create the directory
$ sudo mkdir -p /kvm/pools/homelab
# Define the "default" pool
$ sudo virsh pool-define-as --name default --type dir --target /kvm/pools/homelab
Pool default defined
# Start the pool
$ sudo virsh pool-start default
Pool default started
# Set the pool to start at the same time as libvirtd
$ sudo virsh pool-autostart default
Pool default marked as autostarted
-
Install terraform plugins
$ terraform init
-
Create SSH Keys
mkdir .ssh ssh-keygen -t ed25519 -b 4096 -f .ssh/id_ed25519
-
Check the plan
$ terraform plan
-
Launch it
$ sudo terraform apply
$ sudo virsh list --all
Id Name State
----------------------
1 test running
$ sudo virsh net-dhcp-leases default
Expiry Time MAC address Protocol IP address Hostname Client ID or DUID
-----------------------------------------------------------------------------------------------------------------------------------------------
2024-03-16 11:28:18 ... ipv4 192.168.122.21/24 ubuntu ...
$ terraform output
vm_ips = [
tolist([
"192.168.122.21",
]),
]
$ ssh -i .ssh/id_ed25519 ubuntu@192.168.122.21
Welcome to Ubuntu 22.04.4 LTS (GNU/Linux 5.15.0-100-generic x86_64)
ubuntu@test:~$
$ sudo terraform destroy -auto-approve