Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,28 @@ to install the necessary virtualization packages:
- python3-libvirt
- virt-manager
- virtinst
- cloud-image-utils
```

If you're not using Ansible just `apt-get install` the above packages.

## Permissions

The `libvirtd` daemon runs under the `libvirt-qemu` user service account. The `libvirt-qemu` user
must be able to read the files in `${VM_IMAGE_DIR}`. If your ${HOME} directory has permissions set to
must be able to read the files in `${VM_IMAGE_DIR}`. If your `${HOME}` directory has permissions set to
`0x750` then `libvirt-qemu` won't be able to read the `${VM_IMAGE_DIR}` directory.

You could open up your home directory, e.g.:

```
```sh
chmod 755 ${HOME}
```

... but that allows anyone logged into your Linux host to read everything in your home directory. A
better approach is just to add `libvirt-qemu` to your home directory's group. For instance, on my host
my home directory is `/home/earl` owned by user `earl` and group `earl`, permissions `0x750`:

```
```sh
$ chmod 750 /home/earl
$ ls -al /home
total 24
Expand All @@ -153,7 +154,7 @@ drwxr-x--- 142 earl earl 4096 Feb 16 09:27 earl

To make sure that _only_ the `libvirt-qemu` user can read my files I can add the user to the `earl` group:

```
```sh
$ sudo usermod --append --groups earl libvirt-qemu
$ sudo systemctl restart libvirtd
$ grep libvirt-qemu /etc/group
Expand All @@ -165,7 +166,7 @@ That shows that the group `earl`, group ID 1000, has a member `libvirt-qemu`. Si

Note: The `libvirtd` daemon will chown some of the files in the directory, including the files in the `~/vms/virsh/images` directory, to be owned by `libvirt-qemu` group `kvm`. In order to delete these files without sudo, add yourself to the `kvm` group, e.g.:

```
```sh
$ sudo usermod --append --groups kvm earl
```

Expand All @@ -186,6 +187,9 @@ OPTIONS:
-s Amount of storage to allocate in GB (defaults to 80)
-b Bridge interface to use (defaults to virbr0)
-m MAC address to use (default is to use a randomly-generated MAC)
-t The hypervisor to install on. Example choices are kvm, qemu, or xen.
Example choices are kvm, qemu, or xen. (defaults to kvm)
Available options are listed via 'virsh capabilities' in the <domain> tags.
-v Verbose
```

Expand All @@ -197,14 +201,14 @@ This creates an Ubuntu 22.04 "Jammy Jellyfish" VM with a 40G hard drive.

First download a copy of the Ubuntu 22.04 "Jammy Jellyfish" cloud image:

```
```sh
mkdir -p ~/vms/virsh/base
cd ~/vms/virsh/base
wget http://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img
```

Then create the VM:
```
```sh
create-vm -n node1 \
-i ~/vms/virsh/base/jammy-server-cloudimg-amd64.img \
-k ~/.ssh/id_rsa_ansible.pub \
Expand Down Expand Up @@ -321,3 +325,15 @@ which is why I wrote the `get-vm-ip` script.

`virsh net-dhcp-leases $network` - Shows current DHCP leases when virsh is acting as the
DHCP server. Leases may be shown for machines that no longer exist.

### Other
In the case of using the network in the `bridge` mode, the arp table will not possibly contain any IP addresses, use this command instead.
```sh
sudo arp-scan --interface br0 --localnet | grep QEMU
```
Sample output
```
192.168.104.103 52:54:00:1d:4a:0c QEMU
192.168.104.118 52:54:00:35:2d:5f QEMU
```

20 changes: 10 additions & 10 deletions create-vm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ STORAGE=80
BRIDGE=virbr0
MAC=
VERBOSE=
VIRT_TYPE=$(command -v kvm-ok > /dev/null && echo kvm || echo qemu)

usage()
{
Expand All @@ -45,6 +46,9 @@ OPTIONS:
-s Amount of storage to allocate in GB (defaults to ${STORAGE})
-b Bridge interface to use (defaults to ${BRIDGE})
-m MAC address to use (default is to use a randomly-generated MAC)
-t The hypervisor to install on. Example choices are kvm, qemu, or xen.
Example choices are kvm, qemu, or xen. (defaults to ${VIRT_TYPE})
Available options are listed via 'virsh capabilities' in the <domain> tags.
-v Verbose
EOF
}
Expand All @@ -64,6 +68,7 @@ while getopts "h:n:i:k:r:c:s:b:m:v" option; do
s) STORAGE=${OPTARG};;
b) BRIDGE=${OPTARG};;
m) MAC=${OPTARG};;
t) VIRT_TYPE=${OPTARG};;
v) VERBOSE=1;;
*)
usage
Expand Down Expand Up @@ -130,14 +135,11 @@ while IFS= read -r key; do
echo " - $key" >> "$VM_IMAGE_DIR/init/user-data"
done < <(grep -v '^ *#' < "$AUTH_KEYS_FQN")

echo "Generating the cidata ISO file $VM_IMAGE_DIR/images/${HOSTNAME}-cidata.iso"
echo "Generating the cidata ISO file $VM_IMAGE_DIR/images/${HOSTNAME}-cidata.img"
(
cd "$VM_IMAGE_DIR/init/"
genisoimage \
-output "$VM_IMAGE_DIR/images/${HOSTNAME}-cidata.img" \
-volid cidata \
-rational-rock \
-joliet \
cloud-localds \
"$VM_IMAGE_DIR/images/${HOSTNAME}-cidata.img" \
user-data meta-data
)

Expand All @@ -148,18 +150,16 @@ fi

virt-install \
--name="${HOSTNAME}" \
--virt-type=${VIRT_TYPE} \
--network "bridge=${BRIDGE},model=virtio" $MACCMD \
--import \
--disk "path=${VM_IMAGE_DIR}/images/${HOSTNAME}.img,format=qcow2" \
--disk "path=$VM_IMAGE_DIR/images/${HOSTNAME}-cidata.img,device=cdrom" \
--ram="${RAM}" \
--vcpus="${VCPUS}" \
--autostart \
--hvm \
--arch x86_64 \
--accelerate \
--cpu host \
--check-cpu \
--osinfo detect=on,require=off \
--force \
--watchdog=default \
--graphics vnc,listen=0.0.0.0 \
Expand Down
5 changes: 2 additions & 3 deletions delete-vm
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ fi
if [[ -e $VM_IMAGE ]]; then
# VM exists
virsh destroy "$VM"
virsh undefine "$VM"
rm -fv "$VM_IMAGE" "$CI_IMAGE"
virsh undefine "$VM" --remove-all-storage
else
echo "Cannot find an VM image file named '$VM_IMAGE'. Attempting undefine..."
virsh undefine "$VM"
virsh undefine "$VM" --remove-all-storage
fi
3 changes: 1 addition & 2 deletions get-vm-ip
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,4 @@ if [[ -z $HOSTNAME ]]; then
exit 1
fi

MAC=$(virsh domiflist $HOSTNAME | awk '{ print $5 }' | tail -2 | head -1)
arp -a | grep $MAC | awk '{ print $2 }' | sed 's/[()]//g'
virsh domifaddr --domain ${HOSTNAME} --source arp | tail -2 | head -1 | awk '{ print $4 }' | sed -E 's/\/[[:digit:]]+//'