From 5cce3a03375b036cbaf41ff6d108a0b4c4d5216c Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 1 Aug 2025 15:42:27 +0200 Subject: [PATCH] Added fedora guest VM support --- env_scripts/functions.sh | 38 +++++++++++++++++++++++++++++--------- files/os_options.json | 14 ++++++++++++-- install.sh | 4 ++-- vm_manage.sh | 3 +++ 4 files changed, 46 insertions(+), 13 deletions(-) diff --git a/env_scripts/functions.sh b/env_scripts/functions.sh index 9df360f..25d583f 100644 --- a/env_scripts/functions.sh +++ b/env_scripts/functions.sh @@ -87,6 +87,13 @@ check_host_os() fi } +chown_image_permissions(){ + if [[ "${DISTRO}" == "fedora" ]]; then + USER_GROUP="$USER:qemu" + else + USER_GROUP="$USER:libvirt-qemu" + fi +} generate_openbsd_image() { @@ -102,7 +109,7 @@ generate_openbsd_image() -b if ! test -f "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}"; then mv images/${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION} ${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} - sudo chown -R $USER:libvirt-qemu "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}" + sudo chown -R ${USER_GROUP} ${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} cd ${CURRENT_PATH} rm -r openbsd-cloud-image else @@ -181,15 +188,28 @@ compare_checksum() VM_BASE_IMAGE_CHECKSUM=$(grep "FreeBSD-14.3-STABLE-amd64-BASIC-CLOUDINIT" "${CHECKSUM_TMP_FOLDER}" | grep "ufs.qcow2.xz" | awk '{print $4}') fi else - VM_BASE_IMAGE_CHECKSUM=$(grep "$VM_BASE_IMAGE_NAME.${VM_BASE_IMAGE_EXTENSION}" "${CHECKSUM_TMP_FOLDER}" | awk '{print $1}') + #Fedora things + if [[ "${VM_OS_VARIANT}" == "fedora41" ]]; then + VM_BASE_IMAGE_CHECKSUM=$(grep "${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION}" "${CHECKSUM_TMP_FOLDER}" | grep -v \# | awk '{print $4}') + else + VM_BASE_IMAGE_CHECKSUM=$(grep "${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION}" "${CHECKSUM_TMP_FOLDER}" | awk '{print $1}') + fi fi + if [[ "${VM_CHECKSUMS_URL}" == *"SHA256"* || "${VM_CHECKSUMS_URL}" == *"sha256"* ]]; then - HASH_CMD="sha256sum" + HASH_CMD="sha256sum" elif [[ "${VM_CHECKSUMS_URL}" == *"SHA512"* ]]; then - HASH_CMD="sha512sum" + HASH_CMD="sha512sum" + #Fedora things else - echo "ERROR: Unknown checksum type in URL: $CHECKSUM_URL" - exit 1 + if grep -qi "SHA256" "${CHECKSUM_TMP_FOLDER}"; then + HASH_CMD="sha256sum" + elif grep -qi "SHA512" "${CHECKSUM_TMP_FOLDER}"; then + HASH_CMD="sha512sum" + else + echo "ERROR: Cannot determinate checksum type on ${CHECKSUM_TMP_FOLDER}" + exit 1 + fi fi BASE_FILE_CHECKSUM=$(${HASH_CMD} ${VM_BASE_IMAGE_LOCATION} | awk '{print $1}') if [ "${BASE_FILE_CHECKSUM}" = "${VM_BASE_IMAGE_CHECKSUM}" ]; then @@ -327,7 +347,7 @@ vm_download_base_image() fi VM_BASE_IMAGE_LOCATION="${VM_BASE_DIR}/${VM_BASE_IMAGES}/${VM_BASE_IMAGE_NAME}.${VM_BASE_IMAGE_EXTENSION}" if ! test -f "${VM_BASE_IMAGE_LOCATION}"; then - wget --recursive \ + wget \ --user-agent="Mozilla/5.0 (X11; Linux x86_64)" \ -O "${VM_BASE_IMAGE_LOCATION}" \ ${VM_BASE_IMAGE_URL} @@ -352,7 +372,7 @@ vm_create_guest_image() qemu-img resize \ "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}" \ "${VM_DISK_SIZE}G" - sudo chown -R $USER:libvirt-qemu "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION}" + sudo chown -R ${USER_GROUP} ${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} else echo "${VM_BASE_DIR}/images/${VM_HOSTNAME}.${VM_DISK_EXTENSION} already exists. Delete VM with "delete" option" exit 1 @@ -506,7 +526,7 @@ vm_guest_install() eval virt-install $VM_INSTALL_OPTS virsh dumpxml "${VM_HOSTNAME}" > "${VM_BASE_DIR}/xml/${VM_HOSTNAME}.xml" - clear + #clear echo "VM ${VM_HOSTNAME} Created!" echo "NOTE: It may take some time for the virtual machine to be available if it is a BSD flavor. You can check the status of the virtual machine with the following command:" echo "root pass is(only for BSD flavour): ${VM_USER_PASS}" diff --git a/files/os_options.json b/files/os_options.json index ed48516..da0bc2c 100644 --- a/files/os_options.json +++ b/files/os_options.json @@ -73,6 +73,16 @@ } , { "id": 8, + "name": "Fedora CLoud", + "os_type": "GNULinux", + "variant": "fedora41", + "url": " https://download.fedoraproject.org/pub/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2", + "origin_image_name": "Fedora-Cloud-Base-Generic-42-1.1.x86_64.qcow2", + "md5sum": "https://fedora.mirrorservice.org/fedora/linux/releases/42/Cloud/x86_64/images/Fedora-Cloud-42-1.1-x86_64-CHECKSUM", + "boot_type": "bios" + } , + { + "id": 9, "name": "FreeBSD 14.3 UFS", "os_type": "BSD", "variant": "freebsd14.2", @@ -81,7 +91,7 @@ "md5sum": "https://download.freebsd.org/ftp/snapshots/VM-IMAGES/14.3-STABLE/amd64/Latest/CHECKSUM.SHA512" } , { - "id": 9, + "id": 10, "name": "FreeBSD 14.3 ZFS", "os_type": "BSD", "variant": "freebsd14.2", @@ -90,7 +100,7 @@ "md5sum": "https://download.freebsd.org/ftp/snapshots/VM-IMAGES/14.3-STABLE/amd64/Latest/CHECKSUM.SHA512" } , { - "id": 10, + "id": 11, "name": "OpenBSD 7.7 generic", "os_type": "BSD", "variant": "openbsd7.6", diff --git a/install.sh b/install.sh index 59d2ad2..c6b5dba 100755 --- a/install.sh +++ b/install.sh @@ -27,11 +27,11 @@ sudo usermod -aG libvirt $(whoami) sudo usermod -aG kvm $(whoami) sudo systemctl enable libvirtd sudo systemctl start libvirtd -newgrp libvirt mkdir -p "${VM_BASE_DIR}"/{images,xml,init,base,ssh} cp files/network.xml ${VM_BASE_DIR}/xml/network.xml sed -i "s/YOURNETWORK/${VM_NETWORK}/g" ${VM_BASE_DIR}/xml/network.xml virsh net-define ${VM_BASE_DIR}/xml/network.xml virsh net-autostart ${VM_NETWORK} -virsh net-start ${VM_NETWORK} \ No newline at end of file +virsh net-start ${VM_NETWORK} +newgrp libvirt diff --git a/vm_manage.sh b/vm_manage.sh index 1177329..049d8da 100755 --- a/vm_manage.sh +++ b/vm_manage.sh @@ -99,6 +99,7 @@ case "${ACTION}" in usage fi source env_scripts/common.sh + detect_distro #Check network type vm_net_set_bridge_mode #Check host os for guest debian type @@ -108,6 +109,8 @@ case "${ACTION}" in show_vm_menu #Set guest type based on check_host_os vm_set_guest_type + #set image permissions + chown_image_permissions if [[ "$VM_OS_TYPE" == "BSD" && "${VM_OS_VARIANT}" == *"openbsd"* ]]; then generate_openbsd_image else