From 4c53d5893e1f6193b9350b3a47d7ff534049d142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Collopy?= Date: Sun, 26 Jan 2025 17:36:36 -0500 Subject: [PATCH 1/2] enables make vm-create in gentoo linux --- scripts/vm.sh | 234 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 139 insertions(+), 95 deletions(-) diff --git a/scripts/vm.sh b/scripts/vm.sh index 19a0621..25c7427 100755 --- a/scripts/vm.sh +++ b/scripts/vm.sh @@ -21,36 +21,22 @@ VM_VCPUS=2 VM_RAM=2048 SHARED_FOLDER="/var/lib/libvirt/shared/${VM_NAME}" - function log() { - echo - echo - echo "[$(date --iso=seconds)] $@" + echo + echo + echo "[$(date --iso=seconds)] $@" } 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" - - log "Creating cloud-init YAML files" - cat < "$CLOUDINIT_USER_YAML" + log "Installing system packages" + install_libs + config_permissions + qemu_connect + create_shared_folder + download_image + + log "Creating cloud-init YAML files" + cat <"$CLOUDINIT_USER_YAML" #cloud-config locale: en_US @@ -81,96 +67,154 @@ runcmd: - echo 'LANG=en_US.UTF-8' > /etc/default/locale - locale-gen EOF - cat < "$CLOUDINIT_META_YAML" + cat <"$CLOUDINIT_META_YAML" instance-id: $VM_NAME local-hostname: $VM_NAME EOF - log "Creating overlay qcow2 image" - qemu-img create -f qcow2 -F qcow2 -b "$DEBIAN_QCOW2" "$OVERLAY_QCOW2" "$OVERLAY_DISK_SIZE" - - log "Creating virtual machine" - virt-install \ - --connect "qemu:///system" \ - --name "$VM_NAME" \ - --memory $VM_RAM \ - --vcpus $VM_VCPUS \ - --os-variant debian12 \ - --disk "path=$OVERLAY_QCOW2,format=qcow2,bus=virtio" \ - --filesystem "source.dir=${SHARED_FOLDER},target.dir=host_shared,driver.type=virtiofs" \ - --memorybacking "source.type=memfd,access.mode=shared" \ - --cloud-init "meta-data=$CLOUDINIT_META_YAML,disable=on" \ - --cloud-init "user-data=$CLOUDINIT_USER_YAML,disable=on" \ - --network "network=default,model=virtio" \ - --graphics "none" \ - --console "pty,target_type=serial" \ - --boot hd \ - --noautoconsole - - log "Waiting for the VM network to be up..." - ip=$(vm_wait_for_ip) - echo "VM IP address: $ip" - echo "Use: ssh $DEFAULT_USERNAME@$ip (password: $DEFAULT_PASSWORD)" + log "Creating overlay qcow2 image" + 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 + + virt-install \ + --connect "qemu:///system" \ + --name "$VM_NAME" \ + --memory $VM_RAM \ + --vcpus $VM_VCPUS \ + --os-variant debian12 \ + --disk "path=$OVERLAY_QCOW2,format=qcow2,bus=virtio" \ + --filesystem "source.dir=${SHARED_FOLDER},target.dir=host_shared,driver.type=virtiofs" \ + --memorybacking "source.type=memfd,access.mode=shared" \ + --cloud-init "meta-data=$CLOUDINIT_META_YAML,disable=on" \ + --cloud-init "user-data=$CLOUDINIT_USER_YAML,disable=on" \ + --network "network=default,model=virtio" \ + --graphics "none" \ + --console "pty,target_type=serial" \ + --boot hd \ + --noautoconsole + + log "Waiting for the VM network to be up..." + ip=$(vm_wait_for_ip) + echo "VM IP address: $ip" + 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() { + 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 + 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 - sleep 0.1 - done + virsh --connect "qemu:///system" start "$VM_NAME" + while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "running" ]]; do + sleep 0.1 + done } function vm_stop() { - virsh --connect "qemu:///system" shutdown "$VM_NAME" - while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "shut off" ]]; do - sleep 0.1 - done + virsh --connect "qemu:///system" shutdown "$VM_NAME" + while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "shut off" ]]; do + sleep 0.1 + done } function vm_delete() { - virsh --connect "qemu:///system" destroy "$VM_NAME" 2>/dev/null || true - virsh --connect "qemu:///system" undefine "$VM_NAME" --remove-all-storage 2>/dev/null || true - rm -f "$OVERLAY_QCOW2" "$CLOUDINIT_USER_YAML" "$CLOUDINIT_META_YAML" - rm -rf "$SHARED_FOLDER" + virsh --connect "qemu:///system" destroy "$VM_NAME" 2>/dev/null || true + virsh --connect "qemu:///system" undefine "$VM_NAME" --remove-all-storage 2>/dev/null || true + rm -f "$OVERLAY_QCOW2" "$CLOUDINIT_USER_YAML" "$CLOUDINIT_META_YAML" + rm -rf "$SHARED_FOLDER" } function vm_wait_for_ip() { - local VM_IP="" - while [[ -z "$VM_IP" ]]; do - VM_IP=$(virsh --connect "qemu:///system" -q domifaddr "$VM_NAME" | grep --color=no ipv4 | sed 's/.*ipv4\s\+//; s/\/.*//') - sleep 1 - done - echo "$VM_IP" + local VM_IP="" + while [[ -z "$VM_IP" ]]; do + VM_IP=$(virsh --connect "qemu:///system" -q domifaddr "$VM_NAME" | grep --color=no ipv4 | sed 's/.*ipv4\s\+//; s/\/.*//') + sleep 1 + done + echo "$VM_IP" } function vm_ssh() { - ip=$(vm_wait_for_ip) - echo "Connecting to ${ip}. Use the password: $DEFAULT_PASSWORD" - ssh $DEFAULT_USERNAME@$ip + ip=$(vm_wait_for_ip) + echo "Connecting to ${ip}. Use the password: $DEFAULT_PASSWORD" + ssh $DEFAULT_USERNAME@$ip } subcommand="${1:-}" case "$subcommand" in - create) - vm_create - ;; - start) - vm_start - ;; - stop) - vm_stop - ;; - ip) - vm_wait_for_ip - ;; - ssh) - vm_ssh - ;; - delete) - vm_delete - ;; - *) - echo "Usage: $0 {create|start|stop|ip|delete}" - exit 1 - ;; +create) + vm_create + ;; +start) + vm_start + ;; +stop) + vm_stop + ;; +ip) + vm_wait_for_ip + ;; +ssh) + vm_ssh + ;; +delete) + vm_delete + ;; +*) + echo "Usage: $0 {create|start|stop|ip|delete}" + exit 1 + ;; esac From 4ebc4fcc06c9f90d9ba0d89c29595986ef3d1a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Collopy?= Date: Sun, 26 Jan 2025 17:43:23 -0500 Subject: [PATCH 2/2] rollback identation --- scripts/vm.sh | 240 +++++++++++++++++++++++++------------------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/scripts/vm.sh b/scripts/vm.sh index 25c7427..08c0c74 100755 --- a/scripts/vm.sh +++ b/scripts/vm.sh @@ -22,21 +22,21 @@ VM_RAM=2048 SHARED_FOLDER="/var/lib/libvirt/shared/${VM_NAME}" function log() { - echo - echo - echo "[$(date --iso=seconds)] $@" + echo + echo + echo "[$(date --iso=seconds)] $@" } function vm_create() { - log "Installing system packages" - install_libs - config_permissions - qemu_connect - create_shared_folder - download_image - - log "Creating cloud-init YAML files" - cat <"$CLOUDINIT_USER_YAML" + log "Installing system packages" + install_libs + config_permissions + qemu_connect + create_shared_folder + download_image + + log "Creating cloud-init YAML files" + cat < "$CLOUDINIT_USER_YAML" #cloud-config locale: en_US @@ -67,154 +67,154 @@ runcmd: - echo 'LANG=en_US.UTF-8' > /etc/default/locale - locale-gen EOF - cat <"$CLOUDINIT_META_YAML" + cat < "$CLOUDINIT_META_YAML" instance-id: $VM_NAME local-hostname: $VM_NAME EOF - log "Creating overlay qcow2 image" - 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 - - virt-install \ - --connect "qemu:///system" \ - --name "$VM_NAME" \ - --memory $VM_RAM \ - --vcpus $VM_VCPUS \ - --os-variant debian12 \ - --disk "path=$OVERLAY_QCOW2,format=qcow2,bus=virtio" \ - --filesystem "source.dir=${SHARED_FOLDER},target.dir=host_shared,driver.type=virtiofs" \ - --memorybacking "source.type=memfd,access.mode=shared" \ - --cloud-init "meta-data=$CLOUDINIT_META_YAML,disable=on" \ - --cloud-init "user-data=$CLOUDINIT_USER_YAML,disable=on" \ - --network "network=default,model=virtio" \ - --graphics "none" \ - --console "pty,target_type=serial" \ - --boot hd \ - --noautoconsole - - log "Waiting for the VM network to be up..." - ip=$(vm_wait_for_ip) - echo "VM IP address: $ip" - echo "Use: ssh $DEFAULT_USERNAME@$ip (password: $DEFAULT_PASSWORD)" + log "Creating overlay qcow2 image" + 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 + + virt-install \ + --connect "qemu:///system" \ + --name "$VM_NAME" \ + --memory $VM_RAM \ + --vcpus $VM_VCPUS \ + --os-variant debian12 \ + --disk "path=$OVERLAY_QCOW2,format=qcow2,bus=virtio" \ + --filesystem "source.dir=${SHARED_FOLDER},target.dir=host_shared,driver.type=virtiofs" \ + --memorybacking "source.type=memfd,access.mode=shared" \ + --cloud-init "meta-data=$CLOUDINIT_META_YAML,disable=on" \ + --cloud-init "user-data=$CLOUDINIT_USER_YAML,disable=on" \ + --network "network=default,model=virtio" \ + --graphics "none" \ + --console "pty,target_type=serial" \ + --boot hd \ + --noautoconsole + + log "Waiting for the VM network to be up..." + ip=$(vm_wait_for_ip) + echo "VM IP address: $ip" + echo "Use: ssh $DEFAULT_USERNAME@$ip (password: $DEFAULT_PASSWORD)" } function is_gentoo() { - if uname -r | grep -iq "gentoo"; then - true - else - false - fi + 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 + 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 + log "Configuring libvirt user permission and network" + if ! groups $USER | grep -q "\blibvirt\b"; then + usermod -a -G libvirt $USER + fi } function qemu_connect() { - 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 + 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" + 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" + log "Downloading Debian cloud-ready image" - if is_gentoo; then - mkdir -p /var/lib/libvirt/images - fi - wget -c -t 0 -O "$DEBIAN_QCOW2" "$DEBIAN_QCOW2_URL" + if is_gentoo; then + mkdir -p /var/lib/libvirt/images + fi + 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 - sleep 0.1 - done + virsh --connect "qemu:///system" start "$VM_NAME" + while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "running" ]]; do + sleep 0.1 + done } function vm_stop() { - virsh --connect "qemu:///system" shutdown "$VM_NAME" - while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "shut off" ]]; do - sleep 0.1 - done + virsh --connect "qemu:///system" shutdown "$VM_NAME" + while [[ $(virsh --connect "qemu:///system" -q domstate "$VM_NAME") != "shut off" ]]; do + sleep 0.1 + done } function vm_delete() { - virsh --connect "qemu:///system" destroy "$VM_NAME" 2>/dev/null || true - virsh --connect "qemu:///system" undefine "$VM_NAME" --remove-all-storage 2>/dev/null || true - rm -f "$OVERLAY_QCOW2" "$CLOUDINIT_USER_YAML" "$CLOUDINIT_META_YAML" - rm -rf "$SHARED_FOLDER" + virsh --connect "qemu:///system" destroy "$VM_NAME" 2>/dev/null || true + virsh --connect "qemu:///system" undefine "$VM_NAME" --remove-all-storage 2>/dev/null || true + rm -f "$OVERLAY_QCOW2" "$CLOUDINIT_USER_YAML" "$CLOUDINIT_META_YAML" + rm -rf "$SHARED_FOLDER" } function vm_wait_for_ip() { - local VM_IP="" - while [[ -z "$VM_IP" ]]; do - VM_IP=$(virsh --connect "qemu:///system" -q domifaddr "$VM_NAME" | grep --color=no ipv4 | sed 's/.*ipv4\s\+//; s/\/.*//') - sleep 1 - done - echo "$VM_IP" + local VM_IP="" + while [[ -z "$VM_IP" ]]; do + VM_IP=$(virsh --connect "qemu:///system" -q domifaddr "$VM_NAME" | grep --color=no ipv4 | sed 's/.*ipv4\s\+//; s/\/.*//') + sleep 1 + done + echo "$VM_IP" } function vm_ssh() { - ip=$(vm_wait_for_ip) - echo "Connecting to ${ip}. Use the password: $DEFAULT_PASSWORD" - ssh $DEFAULT_USERNAME@$ip + ip=$(vm_wait_for_ip) + echo "Connecting to ${ip}. Use the password: $DEFAULT_PASSWORD" + ssh $DEFAULT_USERNAME@$ip } subcommand="${1:-}" case "$subcommand" in -create) - vm_create - ;; -start) - vm_start - ;; -stop) - vm_stop - ;; -ip) - vm_wait_for_ip - ;; -ssh) - vm_ssh - ;; -delete) - vm_delete - ;; -*) - echo "Usage: $0 {create|start|stop|ip|delete}" - exit 1 - ;; + create) + vm_create + ;; + start) + vm_start + ;; + stop) + vm_stop + ;; + ip) + vm_wait_for_ip + ;; + ssh) + vm_ssh + ;; + delete) + vm_delete + ;; + *) + echo "Usage: $0 {create|start|stop|ip|delete}" + exit 1 + ;; esac