Compare commits

..

2 Commits

Author SHA1 Message Date
5cce3a0337 Added fedora guest VM support 2025-08-01 15:42:27 +02:00
c75eae71b2 Improved setup process. Tested on Fedora 2025-08-01 13:39:13 +02:00
5 changed files with 142 additions and 14 deletions

View File

@ -1,4 +1,5 @@
#!/bin/env bash
DISTRO=""
LIBVIRT_NET_MODEL="virtio"
LIBVIRT_NET_OPTION="network=$VM_NETWORK,model=$LIBVIRT_NET_MODEL"
OS_JSON_FILE="files/os_options.json"

View File

@ -7,6 +7,76 @@ pause()
echo ""
}
# Printe messages
print_info() {
echo -e "\e[1;34m[INFO]\e[0m $1"
}
print_success() {
echo -e "\e[1;32m[OK]\e[0m $1"
}
print_error() {
echo -e "\e[1;31m[ERROR]\e[0m $1"
}
# Detectar distribución
detect_distro()
{
if [ -f /etc/os-release ]; then
. /etc/os-release
DISTRO=$ID
# elif [ -f /etc/centos-release ] || [ -f /etc/fedora-release ]; then
# if grep -q "Fedora" /etc/fedora-release; then
# DISTRO="fedora"
# else
# DISTRO="centos"
# fi
else
print_error "No se pudo detectar la distribución."
exit 1
fi
}
install_debian_ubuntu() {
print_info "Updating packages..."
sudo apt update || { print_error "Error updating packages."; exit 1; }
print_info "Installing libvirt"
sudo apt install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils|| {
print_error "Error installing packages."
exit 1
}
# Habilitar e iniciar el servicio
sudo systemctl enable libvirtd || sudo systemctl enable libvirt-bin
sudo systemctl start libvirtd || sudo systemctl start libvirt-bin
}
install_arch() {
print_info "Updating packages..."
sudo pacman -Syu --noconfirm || { print_error "Error updating packages."; exit 1; }
print_info "Installing libvirt."
sudo pacman -S --noconfirm qemu libvirt virt-manager dnsmasq iptables bridge-utils|| {
print_error "Error installing packages."
exit 1
}
}
install_fedora() {
print_info "Updating packages..."
sudo dnf upgrade -y|| { print_error "Error updating packages."; exit 1; }
print_info "Installing libvirt."
sudo sudo dnf install -y @virtualization qemu libvirt bridge-utils|| {
print_error "Error installing packages."
exit 1
}
}
check_host_os()
{
local HOST_OS=$(cat /etc/os-release | grep -v VERSION_ID |grep "ID=" | awk -F'=' '{print $2}')
@ -17,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()
{
@ -32,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
@ -99,7 +176,7 @@ compare_checksum()
{
CHECKSUM_TMP_FOLDER=$(mktemp)
wget -L \
wget --recursive \
--user-agent="Mozilla/5.0 (X11; Linux x86_64)" \
-O "${CHECKSUM_TMP_FOLDER}" \
"${VM_CHECKSUMS_URL}"
@ -111,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
@ -257,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 -L \
wget \
--user-agent="Mozilla/5.0 (X11; Linux x86_64)" \
-O "${VM_BASE_IMAGE_LOCATION}" \
${VM_BASE_IMAGE_URL}
@ -282,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
@ -436,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}"

View File

@ -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",

View File

@ -2,12 +2,36 @@
#Define variable names on env_scripts/common.sh
#VM_NETWORK=
#VM_BASE_DIR=
#Install dependencies - TODO
#Install dependencies
source env_scripts/common.sh
source env_scripts/functions.sh
detect_distro
case $DISTRO in
ubuntu|debian)
install_debian_ubuntu
;;
arch)
install_arch
;;
fedora)
install_fedora
;;
*)
print_error "Distribution not supported: $DISTRO"
print_info "Supported: Ubuntu, Debian, Arch, Fedora"
exit 1
;;
esac
sudo usermod -aG libvirt $(whoami)
sudo usermod -aG kvm $(whoami)
sudo systemctl enable libvirtd
sudo systemctl start libvirtd
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}
virsh net-start ${VM_NETWORK}
newgrp libvirt

View File

@ -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