#!/usr/bin/env bash# Interactive script to uninstall virt-manager or completely purge the KVM/libvirt virtualization stack.set-euopipefail
# Ensure script is executed as rootif[["${EUID}"-ne0]];thenecho"[-] Error: This script must be run as root or with sudo.">&2exit1fi# Detect available package managerifcommand-vdnf&>/dev/null;thenPKG_MANAGER="dnf"elifcommand-vapt-get&>/dev/null;thenPKG_MANAGER="apt"elseecho"[-] Error: Unsupported package manager. Only dnf and apt are supported.">&2exit1fi# Resolve the home directory of the calling user (when run via sudo)REAL_USER="${SUDO_USER:-$USER}"REAL_USER_HOME=$(getentpasswd"${REAL_USER}"|cut-d:-f6)echo"=================================================="echo" Virt-Manager / KVM Stack Purge Utility "echo"=================================================="echo"Package Manager Detected: ${PKG_MANAGER}"echo"Target User Configuration: ${REAL_USER_HOME}/.config/virt-manager"echo""echo"Select purge mode:"echo"1) GUI Only - Remove virt-manager (preserves VMs, libvirt, and QEMU)"echo"2) Full Purge - Remove virt-manager, libvirt, QEMU, VM disk images, and network bridges"echo"3) Exit"echo""read-rp"Enter option [1-3]: "CHOICE
case"${CHOICE}"in1)echo"[+] Removing virt-manager GUI package..."if[["${PKG_MANAGER}"=="dnf"]];thendnfremove-yvirt-manager
elif[["${PKG_MANAGER}"=="apt"]];thenapt-getpurge-yvirt-manager
apt-getautoremove--purge-y
fiecho"[+] Removing user configuration files..."rm-rf"${REAL_USER_HOME}/.config/virt-manager"echo"[+] Virt-Manager GUI successfully removed.";;2)echo""echo"[!] WARNING: Full purge will permanently delete all virtual machine images inside /var/lib/libvirt,"echo" configurations in /etc/libvirt, and virtual network interfaces."read-rp"Are you sure you want to proceed? [y/N]: "CONFIRM
if[["${CONFIRM}"!=[yY]&&"${CONFIRM}"!=[yY][eE][sS]]];thenecho"[-] Operation cancelled."exit0fiecho"[+] Stopping virtualization services..."systemctlstoplibvirtd.servicelibvirtd.socketvirtqemud.servicevirtqemud.socketvirtnetworkd.servicevirtnetworkd.socket2>/dev/null||truesystemctldisablelibvirtd.servicelibvirtd.socketvirtqemud.servicevirtqemud.socketvirtnetworkd.servicevirtnetworkd.socket2>/dev/null||trueecho"[+] Uninstalling packages..."if[["${PKG_MANAGER}"=="dnf"]];thendnfremove-yvirt-manager"libvirt*""qemu*"elif[["${PKG_MANAGER}"=="apt"]];thenapt-getpurge-yvirt-manager"libvirt*""qemu*"qemu-kvm
apt-getautoremove--purge-y
fiecho"[+] Removing system and user configuration directories..."rm-rf/etc/libvirt/var/lib/libvirt/var/log/libvirt/var/cache/libvirt
rm-rf"${REAL_USER_HOME}/.config/virt-manager""${REAL_USER_HOME}/.config/libvirt"echo"[+] Deleting virtual network bridge (virbr0) if active..."ifiplinkshowvirbr0&>/dev/null;theniplinksetdevvirbr0down2>/dev/null||trueiplinkdeletevirbr02>/dev/null||truefiecho"[+] Reloading systemd manager configuration..."systemctldaemon-reload
echo"[+] Complete virtualization stack purge finished.";;3)echo"[-] Exiting script."exit0;;*)echo"[-] Invalid selection. Exiting."exit1;;esac