Create VM
First, create a directory for vm-01:
mkdir -p /data/vm-01
sudo setfacl -m u:libvirt-qemu:rx /data
Next, create a storage pool for vm-01:
virsh pool-create-as --name vm-01 --type dir --target /data/vm-01 --build
You can also create the storage pool from an XML file. But first, check your user ID and group ID:
id
Your output will be something like this:
uid=1000(userx) gid=1000(userx) groups=1000(userx)
Now, you can create the XML file:
cat <<EOF | tee vm-pool.xml
<pool type='dir'>
<name>vm-01</name>
<source>
</source>
<target>
<path>/data/vm-01</path>
<permissions>
<mode>0711</mode>
<owner>1000</owner>
<group>1000</group>
</permissions>
</target>
</pool>
EOF
Define the pool and start it:
virsh pool-define vm-pool.xml
virsh pool-list --all
virsh pool-start vm-01
virsh pool-autostart vm-01
Next, copy the image to /data/vm-01:
curl -o /data/vm-01/ubuntu22_04.qcow2 https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
Resize the disk to 30GB:
qemu-img resize /data/vm-01/ubuntu22_04.qcow2 30G
You can get info about the image:
qemu-img info /data/vm-01/ubuntu22_04.qcow2
Create a cloud-init configuration to set the password and hostname:
VM_NAME="vm-01"
USERNAME="Username"
PASSWORD="Your_Password"
echo "#cloud-config
system_info:
default_user:
name: $USERNAME
home: /home/$USERNAME
lock_passwd: false
password: $PASSWORD
chpasswd: { expire: False }
shell: /bin/bash
hostname: $VM_NAME
ssh_pwauth: True
" | tee /data/vm-01/cloud-init.cfg
Create network settings:
echo "
network:
ethernets:
ens1:
addresses:
- 192.168.0.100/24
gateway4: 192.168.0.1
nameservers:
addresses:
- 1.1.1.1
search:
- cloudresource.io
version: 2
" | tee /data/vm-01/network.yaml
Create a cloud-init.iso:
cloud-localds -v --network-config=/data/vm-01/network.yaml /data/vm-01/cloud-init.iso /data/vm-01/cloud-init.cfg
Now, deploy the VM image and set the VNC port to 5901. Each VM must have a unique VNC port, or set it to auto:
virt-install --import --name vm-01 \
--memory 4096 --vcpus 4 --cpu host-passthrough \
--disk /data/vm-01/ubuntu22_04.qcow2,format=qcow2,bus=virtio \
--disk /data/vm-01/cloud-init.iso,device=cdrom \
--network bridge=br0,model=virtio \
--os-variant=ubuntu22.04 \
--graphics vnc,listen=0.0.0.0,port=5901,passwd='********' \
--noautoconsole \
--noreboot
Optionally, you can modify the VM settings:
virsh edit vm-01
Start the VM:
virsh start vm-01
List all VMs:
virsh list --all
Eject the CDROM media:
virsh change-media vm-01 --path $(readlink -f /data/vm-01/cloud-init.iso --eject --force
You can log in via the console (vns) or SSH and install the Qemu guest agent in vm-01:
apt -y install qemu-guest-agent
systemctl enable qemu-guest-agent
reboot
After the reboot, check the agent status:
virsh qemu-agent-command vm-01 '{"execute":"guest-info"}' --pretty
If the guest agent is stopped or removed, you will see the following error:
error: Guest agent is not responding: QEMU guest agent is not connected
You can also create an extra disk (vdb) for VM vm-01:
virsh pool-list
Your output will be something like this:
Name State Autostart
-------------------------------------
vm-01 active yes
Create a volume inside the pool vm-01, using qcow2 format and a size of 320GB:
virsh vol-create-as vm-01 vdb.qcow2 320G --format qcow2
Attach the volume to VM vm-01:
virsh attach-disk vm-01 /data/vm-01/vdb.qcow2 vdb --persistent --subdriver=qcow2