From c75eae71b2fc8fb5692ac2e745b7105cc3c22b67 Mon Sep 17 00:00:00 2001 From: victor Date: Fri, 1 Aug 2025 13:39:13 +0200 Subject: [PATCH] Improved setup process. Tested on Fedora --- env_scripts/common.sh | 1 + env_scripts/functions.sh | 74 ++++++++++++++++++++++++++++++++++++++-- install.sh | 26 +++++++++++++- 3 files changed, 98 insertions(+), 3 deletions(-) diff --git a/env_scripts/common.sh b/env_scripts/common.sh index 9421094..2195e34 100644 --- a/env_scripts/common.sh +++ b/env_scripts/common.sh @@ -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" diff --git a/env_scripts/functions.sh b/env_scripts/functions.sh index 300ffc4..9df360f 100644 --- a/env_scripts/functions.sh +++ b/env_scripts/functions.sh @@ -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}') @@ -99,7 +169,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}" @@ -257,7 +327,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 --recursive \ --user-agent="Mozilla/5.0 (X11; Linux x86_64)" \ -O "${VM_BASE_IMAGE_LOCATION}" \ ${VM_BASE_IMAGE_URL} diff --git a/install.sh b/install.sh index abbb8b6..59d2ad2 100755 --- a/install.sh +++ b/install.sh @@ -2,8 +2,32 @@ #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 +newgrp libvirt mkdir -p "${VM_BASE_DIR}"/{images,xml,init,base,ssh} cp files/network.xml ${VM_BASE_DIR}/xml/network.xml