Check if your pc supports virtualization (it probably does:))
lscpu | grep -E 'vmx|svm'Install it
sudo apt update
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-managerEnable and start libvirtd:
sudo systemctl enable --now libvirtdCheck if default network is already started, if not start it:
sudo virsh net-list --all
sudo virsh net-start default # Run only if not started !!!
sudo virsh net-autostart defaultNow you are mostly ready to go.
Check for IP address of a VM and ssh to it:
sudo virsh net-dhcp-leases default
ssh <vm-user>@<vm-ip>Destroy the VM when you are done:
sudo virsh destroy <vm-name> 2>/dev/null || true
sudo virsh undefine <vm-name> --nvram 2>/dev/null || true
sudo rm -f /var/lib/libvirt/images/<vm-disk>.qcow2List existing VMs:
sudo virsh list --allStart a VM:
sudo virsh start <vm-name>Lets create a ubuntu 22.04 server VM
sudo mkdir -p /var/lib/libvirt/images/
cd /var/lib/libvirt/images/; sudo wget https://releases.ubuntu.com/jammy/ubuntu-22.04.5-live-server-amd64.iso; cd -
sudo chmod 644 /var/lib/libvirt/images/ubuntu-22.04.5-live-server-amd64.isoInstall cloud image tools:
sudo apt install -y cloud-image-utilsCreate cloud temp image directory:
mkdir -p ~/vmseed/ubuntu-server
cd ~/vmseed/ubuntu-serverGenerate password hash for cloud image and store the hash:
openssl passwd -6Create the user-data file with the following content (replace the password hash with the one you generated):
#cloud-config
autoinstall:
version: 1
locale: en_US.UTF-8
keyboard:
layout: us
identity:
hostname: ubuntu-server
username: ubuntu
password: "$6$PUT_YOUR_HASH_HERE"
ssh:
install-server: true
allow-pw: true
#authorized-keys:
# - ssh-ed25519 AAAA... only if you want also ssh key auth
storage:
layout:
name: direct
packages:
- qemu-guest-agent
late-commands:
- curtin in-target -- systemctl enable ssh
- curtin in-target -- systemctl enable qemu-guest-agentCreate the meta-data file with the following content:
instance-id: ubuntu-server-01
local-hostname: ubuntu-serverBuild seed iso with cloud image tools and then copy it to /var/lib/libvirt/images/:
cloud-localds -f iso seed.iso user-data meta-data
sudo cp ~/vmseed/ubuntu-server/seed.iso /var/lib/libvirt/images/seed-ubuntu-server-22.isoThen lets creat a disk(sparse, thin client) for our VM
sudo qemu-img create -f qcow2 /var/lib/libvirt/images/ubuntu-server-22.qcow2 30G(if for some reason you wanted a full raw disk, then there exists -o preallocation=full flag)
Now mount the iso locally:
sudo mkdir -p /mnt/ubuntu-server-22
sudo mount -o loop ~/Documents/isos/ubuntu-22.04.5-live-server-amd64.iso /mnt/ubuntu-server-22And now create VM with preffered settings:
sudo virt-install \
--name ubuntu-server-22 \
--memory 4096 \
--vcpus 6 \
--cpu host \
--osinfo ubuntu22.04 \
--disk path=/var/lib/libvirt/images/ubuntu-server-22.qcow2,format=qcow2,bus=virtio \
--disk path=/var/lib/libvirt/images/seed-ubuntu-server-22.iso,device=cdrom,readonly=on \
--disk path=/var/lib/libvirt/images/ubuntu-22.04.5-live-server-amd64.iso,device=cdrom,readonly=on \
--network network=default,model=virtio \
--graphics none \
--console pty,target_type=serial \
--location /mnt/ubuntu-server-22,kernel=casper/vmlinuz,initrd=casper/initrd \
--extra-args "autoinstall console=ttyS0,115200n8 serial" \
--noautoconsole \
--wait=-1Same process as with ubuntu, but this time we will use their cloud image.
sudo mkdir -p /var/lib/libvirt/images/
cd /var/lib/libvirt/images/; sudo wget sudo wget https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-generic-amd64.qcow2 -O debian-12-generic-amd64.qcow2; cd -
sudo chmod 644 /var/lib/libvirt/images/debian-12-generic-amd64.qcow2Resize the qcow2 image to desired size (default is 10G):
sudo qemu-img resize /var/lib/libvirt/images/debian-12-generic-amd64.qcow2 30GCreate temp cloud image directory:
mkdir -p ~/vmseed/debian-server
cd ~/vmseed/debian-serverGenerate password hash:
openssl passwd -6Create the user-data file with the following content (replace the password hash with the one you generated):
#cloud-config
hostname: debian-server
fqdn: debian-server.local
users:
- name: debian
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
shell: /bin/bash
lock_passwd: false
passwd: "$6$PUT_YOUR_HASH_HERE"
ssh_pwauth: true
package_update: true
packages:
- qemu-guest-agent
runcmd:
- systemctl enable qemu-guest-agent
- systemctl start qemu-guest-agentCreate the meta-data file with the following content:
instance-id: debian-server-01
local-hostname: debian-serverBuild seed iso with cloud image tools and then copy it to /var/lib/libvirt/images/:
cloud-localds -f iso seed.iso user-data meta-data
sudo cp ~/vmseed/debian-server/seed.iso /var/lib/libvirt/images/seed-debian-12.iso
sudo chmod 644 /var/lib/libvirt/images/seed-debian-12.isoNow create the VM with preffered settings:
sudo virt-install \
--name debian-server-12 \
--memory 4096 \
--vcpus 6 \
--cpu host \
--osinfo detect=on,name=debian12 \
--import \
--disk path=/var/lib/libvirt/images/debian-12-generic-amd64.qcow2,format=qcow2,bus=virtio \
--disk path=/var/lib/libvirt/images/seed-debian-12.iso,device=cdrom,readonly=on \
--network network=default,model=virtio \
--graphics none \
--console pty,target_type=serial \
--boot hd,useserial=on \
--noautoconsoleSimilar as other os-es, just has the diffrent cloud init. Check for newer images.
sudo mkdir -p /var/lib/libvirt/images
cd /var/lib/libvirt/images
sudo wget https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2 -O almalinux-9-genericcloud.qcow2
sudo chmod 644 almalinux-9-genericcloud.qcow2
sudo qemu-img create -f qcow2 -F qcow2 \
-b /var/lib/libvirt/images/almalinux-9-genericcloud.qcow2 \
/var/lib/libvirt/images/almalinux-server.qcow2 30G
sudo chmod 644 /var/lib/libvirt/images/almalinux-server.qcow2
mkdir -p ~/vmseed/almalinux-server
cd ~/vmseed/almalinux-server
openssl passwd -6usar-data file:
#cloud-config
ssh_pwauth: true
password: "$6$PUT_YOUR_HASH_HERE"
chpasswd:
expire: false
hostname: almalinux-server
packages:
- qemu-guest-agent
runcmd:
- systemctl enable qemu-guest-agent
- systemctl start qemu-guest-agent.. defult username is almalinux.
meta-data file:
instance-id: almalinux-server-01
local-hostname: almalinux-serverand then install it:
sudo virt-install \
--connect qemu:///system \
--name almalinux-server \
--memory 4096 \
--vcpus 6 \
--cpu host-passthrough \
--machine q35 \
--import \
--osinfo name=almalinux9 \
--disk path=/var/lib/libvirt/images/almalinux-server.qcow2,format=qcow2,bus=virtio \
--network network=default,model=virtio \
--graphics none \
--cloud-init user-data=$HOME/vmseed/almalinux-server/user-data,meta-data=$HOME/vmseed/almalinux-server/meta-data \
--noautoconsoleAgain a bit different, here is source.
sudo mkdir -p /var/lib/libvirt/images
cd /var/lib/libvirt/images
sudo wget https://dl-cdn.alpinelinux.org/alpine/v3.23/releases/cloud/generic_alpine-3.23.3-x86_64-bios-cloudinit-r0.qcow2 \
-O alpine-3.23.3-cloudinit.qcow2
sudo chmod 644 /var/lib/libvirt/images/alpine-3.23.3-cloudinit.qcow2
sudo qemu-img create -f qcow2 -F qcow2 \
-b /var/lib/libvirt/images/alpine-3.23.3-cloudinit.qcow2 \
/var/lib/libvirt/images/alpine-server.qcow2 20G
sudo chmod 644 /var/lib/libvirt/images/alpine-server.qcow2A bit different cloud file:
mkdir -p ~/vmseed/alpine-server
cd ~/vmseed/alpine-serveruser-data file:
#cloud-config
hostname: alpine-server
ssh_pwauth: true
chpasswd:
list: |
alpine:PASS
expire: false
packages:
- qemu-guest-agent
- openssh
- sudo
runcmd:
- rc-update add qemu-guest-agent default
- rc-service qemu-guest-agent start || true
- rc-update add sshd default
- rc-service sshd start || true
- echo "cloud-init finished" > /root/cloud-init.donemeta-data file:
instance-id: alpine-server-01
local-hostname: alpine-serverAnd then install like:
sudo virt-install \
--connect qemu:///system \
--name alpine-server \
--memory 2048 \
--vcpus 2 \
--cpu host-passthrough \
--import \
--disk path=/var/lib/libvirt/images/alpine-server.qcow2,format=qcow2,bus=virtio \
--network network=default,model=virtio \
--graphics none \
--cloud-init user-data=$HOME/vmseed/alpine-server/user-data,meta-data=$HOME/vmseed/alpine-server/meta-data \
--noautoconsole \
--osinfo detect=on,require=offSame procedure as for all above, just now from another source
Again download and setup the files:
sudo mkdir -p /var/lib/libvirt/images
cd /var/lib/libvirt/images
sudo wget https://cloud.centos.org/centos/10-stream/x86_64/images/CentOS-Stream-GenericCloud-10-latest.x86_64.qcow2 \
-O centos-stream-10-genericcloud.qcow2
sudo chmod 644 /var/lib/libvirt/images/centos-stream-10-genericcloud.qcow2
sudo qemu-img create -f qcow2 -F qcow2 \
-b /var/lib/libvirt/images/centos-stream-10-genericcloud.qcow2 \
/var/lib/libvirt/images/centos-server.qcow2 30G
sudo chmod 644 /var/lib/libvirt/images/centos-server.qcow2
mkdir -p ~/vmseed/centos-server
cd ~/vmseed/centos-server
openssl passwd -6user-data file:
#cloud-config
hostname: centos-server
users:
- name: centos
lock_passwd: false
passwd: "$6$PUT_YOUR_HASH_HERE"
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel
shell: /bin/bash
ssh_pwauth: true
packages:
- qemu-guest-agent
runcmd:
- systemctl enable qemu-guest-agent
- systemctl start qemu-guest-agent
- echo "cloud-init finished" > /root/cloud-init.donemeta-data file:
instance-id: centos-server-01
local-hostname: centos-serverAnd the vm cration:
sudo virt-install \
--connect qemu:///system \
--name centos-server \
--memory 4096 \
--vcpus 6 \
--cpu host-passthrough \
--machine q35 \
--import \
--osinfo detect=on,name=centos-stream10 \
--disk path=/var/lib/libvirt/images/centos-server.qcow2,format=qcow2,bus=virtio \
--network network=default,model=virtio \
--graphics none \
--cloud-init user-data=$HOME/vmseed/centos-server/user-data,meta-data=$HOME/vmseed/centos-server/meta-data \
--noautoconsoleAgain download source from official site
sudo mkdir -p /var/lib/libvirt/images
cd /var/lib/libvirt/images
sudo wget https://dl.rockylinux.org/pub/rocky/9/images/x86_64/Rocky-9-GenericCloud.latest.x86_64.qcow2 \
-O rocky-9-genericcloud.qcow2
sudo chmod 644 /var/lib/libvirt/images/rocky-9-genericcloud.qcow2
sudo qemu-img create -f qcow2 -F qcow2 \
-b /var/lib/libvirt/images/rocky-9-genericcloud.qcow2 \
/var/lib/libvirt/images/rocky-server.qcow2 30G
sudo chmod 644 /var/lib/libvirt/images/rocky-server.qcow2
mkdir -p ~/vmseed/rocky-server
cd ~/vmseed/rocky-server
openssl passwd -6Create seed files:
user-data:
#cloud-config
hostname: rocky-server
users:
- name: rocky
plain_text_passwd: "YOURPASS"
lock_passwd: false
sudo: ALL=(ALL) NOPASSWD:ALL
groups: wheel
shell: /bin/bash
ssh_pwauth: true
runcmd:
- [ sh, -c, "echo cloud-init-worked > /root/cloud-init.done" ]meta-data:
instance-id: rocky-server-01
local-hostname: rocky-serverGenerate iso image:
cd ~/vmseed/rocky-server
genisoimage -output seed.iso -volid cidata -joliet -rock user-data meta-data
sudo cp ~/vmseed/rocky-server/seed.iso /var/lib/libvirt/images/rocky-server-seed.iso
sudo chmod 644 /var/lib/libvirt/images/rocky-server-seed.isoAnd then install it:
sudo virt-install \
--connect qemu:///system \
--name rocky-server \
--memory 4096 \
--vcpus 6 \
--cpu host-passthrough \
--machine q35 \
--import \
--os-variant rocky9 \
--disk path=/var/lib/libvirt/images/rocky-server.qcow2,format=qcow2,bus=virtio \
--disk path=/var/lib/libvirt/images/rocky-server-seed.iso,device=cdrom \
--network network=default,model=virtio \
--graphics vnc \
--noautoconsole