Skip to content
Open
Changes from all 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
82 changes: 63 additions & 19 deletions scripts/vm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ VM_VCPUS=2
VM_RAM=2048
SHARED_FOLDER="/var/lib/libvirt/shared/${VM_NAME}"


function log() {
echo
echo
Expand All @@ -30,24 +29,11 @@ function log() {

function vm_create() {
log "Installing system packages"
apt update
apt install -y libvirt-daemon-system virtiofsd

log "Configuring libvirt user permission and network"
if ! groups $USER | grep -q "\blibvirt\b"; then
adduser $USER libvirt
fi
if ! virsh --connect "qemu:///system" net-info default | grep -q "Active:.*yes"; then
virsh --connect "qemu:///system" net-start default
fi
if ! virsh --connect "qemu:///system" net-info default | grep -q "Autostart:.*yes"; then
virsh --connect "qemu:///system" net-autostart default
fi
mkdir -p "$SHARED_FOLDER"
chown -R ${SUDO_USER:-$USER}:libvirt "$SHARED_FOLDER"

log "Downloading Debian cloud-ready image"
wget -c -t 0 -O "$DEBIAN_QCOW2" "$DEBIAN_QCOW2_URL"
install_libs
config_permissions
qemu_connect
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
qemu_connect
config_virsh_network

create_shared_folder
download_image

log "Creating cloud-init YAML files"
cat <<EOF > "$CLOUDINIT_USER_YAML"
Expand Down Expand Up @@ -90,6 +76,12 @@ EOF
qemu-img create -f qcow2 -F qcow2 -b "$DEBIAN_QCOW2" "$OVERLAY_QCOW2" "$OVERLAY_DISK_SIZE"

log "Creating virtual machine"

if is_gentoo; then
mkdir /var/lib/libvirt/shared/debian12-pydokku
mkdir /var/lib/libvirt/boot
fi

Comment on lines +79 to +84
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't understand why those lines are needed, since there's mkdir -p "$SHARED_FOLDER" inside create_shared_folder already (which does the same as the first line inside the if) and /var/lib/libvirt/boot is not used at all.
I suggest you trying to completely remove this if, completely remove the VM (virt-manager could help) and running the make vm-create without them to check if this is really required.

Suggested change
if is_gentoo; then
mkdir /var/lib/libvirt/shared/debian12-pydokku
mkdir /var/lib/libvirt/boot
fi

virt-install \
--connect "qemu:///system" \
--name "$VM_NAME" \
Expand All @@ -113,6 +105,58 @@ EOF
echo "Use: ssh $DEFAULT_USERNAME@$ip (password: $DEFAULT_PASSWORD)"
}

function is_gentoo() {
if uname -r | grep -iq "gentoo"; then
true
else
false
fi
}

function install_libs() {
if is_gentoo; then
echo "Gentoo system detected. Running emerge --sync..."
emerge --sync
emerge --deep app-emulation/libvirt app-emulation/qemu virtiofsd app-emulation/virt-manager whois
systemctl start libvirtd
else
echo "Non-Gentoo system detected. Running apt update..."
apt update
apt install -y libvirt-daemon-system virtiofsd
fi
}

function config_permissions() {
log "Configuring libvirt user permission and network"
if ! groups $USER | grep -q "\blibvirt\b"; then
usermod -a -G libvirt $USER
fi
}

function qemu_connect() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name does not describe what it does (all virsh commands connect to qemu in a way or another). I suggest config_virsh_network.

Suggested change
function qemu_connect() {
function config_virsh_network() {

if ! virsh --connect "qemu:///system" net-info default | grep -q "Active:.*yes"; then
virsh --connect "qemu:///system" net-start default
fi
if ! virsh --connect "qemu:///system" net-info default | grep -q "Autostart:.*yes"; then
virsh --connect "qemu:///system" net-autostart default
fi
}

function create_shared_folder() {
log "Creating shared folder"
mkdir -p "$SHARED_FOLDER"
chown -R ${SUDO_USER:-$USER}:libvirt "$SHARED_FOLDER"
}

function download_image() {
log "Downloading Debian cloud-ready image"

if is_gentoo; then
mkdir -p /var/lib/libvirt/images
fi
Comment on lines +154 to +156
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is not required by Gentoo only and also could be improved to reuse the variable where the image path is stored. I prefer changing it to:

Suggested change
if is_gentoo; then
mkdir -p /var/lib/libvirt/images
fi
mkdir -p $(dirname "$DEBIAN_QCOW2")

wget -c -t 0 -O "$DEBIAN_QCOW2" "$DEBIAN_QCOW2_URL"
}

function vm_start() {
virsh --connect "qemu:///system" start "$VM_NAME"
while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "running" ]]; do
Expand Down