installs/virt-manager.sh
2025-05-18 12:11:13 +02:00

125 lines
3.8 KiB
Bash

#!/usr/bin/env bash
PACKAGE=virt-manager
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
if command_exists "$PACKAGE"; then
echo_warning "$PACKAGE is already installed!"
echo_warning "Exiting now!"
exit 69
fi
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the _install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
getImports() {
local url="https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh"
local import="$(mktemp)"
if command_exists curl; then
curl -fsSL $url -o $import
elif command_exists wget; then
wget -o $import $url
else
echo "curl/wget is required, but missing.."
exit 69
fi
source "$import"
sleep 0.2
rm "$import"
}
getDependencies() {
echo_info "Checking build dependencies.."
# INFO:
# ╭─────────────────────────────────────────────────────────────────────────╮
# │ You can define dependencies for various linux distros here. It will │
# │ automagically be pulled via the $pkgArray[$distro] variable │
# ╰─────────────────────────────────────────────────────────────────────────╯
genericDeps=(virt-manager)
depsDebian=(qemu-system libvirt-dev libvirt-glib-1.0-dev)
depsFedora=(qemu libvirt)
depsOpensuse=(qemu libvirt)
depsArch=(qemu-full libvirt)
# depsAlpine=()
declare -A deps=(
[debian]="depsDebian"
[ubuntu]="depsUbuntu"
[fedora]="depsFedora"
[opensuse]="depsOpensuse"
[arch]="depsArch"
# [alpine]="depsAlpine"
)
# INFO:
# ╭────────────────────────────────────────────────────────────────╮
# │ This variable stores the packages you provided for each distro │
# ╰────────────────────────────────────────────────────────────────╯
declare -n pkgArray="${deps[$distro]}"
case "$distro" in
debian | ubuntu | arch | fedora | opensuse)
echo_info "Installing generic dependencies.."
checkAndInstall "${genericDeps[@]}"
echo_info "Installing special dependencies.."
checkAndInstall "${pkgArray[@]}"
;;
*)
echo_error "There are no dependencies to install for $distro"
return 69
;;
esac
}
setupVM() {
echo_info "Setting up services and group-permissions.."
if command_exists virt-manager && command_exists libvirt; then
$_sudo usermod -aG libvirt $USER &&
echo_note "User $USER is now in the libvirt group.."
$_sudo systemctl enable --now libvirtd
fi
}
main() {
case "$distro" in
arch | debian | ubuntu | fedora | opensuse)
if getDependencies; then
setupVM
fi
;;
*)
echo "$distro is not supported by this script!"
exit 1
;;
esac
}
if getImports; then
case "$@" in
--silent | -s)
silent=true
echo_warning "Running script silently!"
;;
*)
silent=false
;;
esac
main
fi