#!/bin/bash
# - bof - #



# -  - #
# - Overwrite checkComputer - #
 overwriteCc_="No";
#overwriteCc_="Yes";

vMi_=~/VManagerInstall.sh;



<<'comment'

bash <(curl -s https://www.education.isdevelopment.us/VirtualManager.Ins/VManagerInstall.sh)



# - Configured - #

# -  - #
# - Vim commands - #
https://vim.rtorr.com/

rm -rf ~/.bashrc;
nvim ~/.bashrc;

rm -rf ~/.zshrc;
nvim ~/.zshrc;

ssh-keygen -R 192.168.1.1

# - On client - #
cd ~/.ssh;
ssh-keygen -t rsa;

cat ~/.ssh/id_rsa.pub;

# - On server - #
nvim ~/.ssh/authorized_keys;

sudo apt install curl git htop inxi net-tools ntp neovim tree -y;
sudo pacman -S curl git htop inxi net-tools ntp neovim tree --noconfirm --needed;


sh -c "$(curl -fsSL 'https://www.education.isdevelopment.us/VirtualManager.Ins/VManagerInstall.sh')"


cd ~/; rm -rf ~/VManagerInstall.sh; vim ~/VManagerInstall.sh;
# - Paste this content - #
chmod +x ~/VManagerInstall.sh; ~/./VManagerInstall.sh;

comment



# -  - #
# - checkVirtualization_ - #
function checkVirtualization_(){
  echo -e "Getting computer virtualization availability...\n";

  LC_ALL=C lscpu | grep Virtualization;
  
  echo -e "\nIf you can't see any details about virtualization here,";
  echo -e "the system is not ready for Virtual manager...\n\n\n";
  sleep 20;
}



# -  - #
# - Header_ - #
function Header_(){
  clear;
  echo "";
  figlet "Virtual" | lolcat;
  figlet "Manager" | lolcat;
}



# -  - #
# - Greetings_ - #
function Greetings_(){
  Header_;
  echo -e "\nby Djalmar Enrique Rayo (2025) - GNU General Public License (GPL)";
  echo "----------------------- Light version: $lightVersion_ -----------------------";
  echo -e "\n  Warning: This script is going to install Virtual Manager"
  echo -e "\nImportant: This procedure will take a few minutes.";
  echo -e "\n\nProcedure: Open terminal & paste next command:\n";

cat <<"EOF"
    bash <(curl -s https://www.education.isdevelopment.us/VirtualManager.Ins/VManagerInstall.sh)
EOF

  echo -e "\n\n\n";

  while true; do
    read -p "Do you want to start the installation now? (Yy/Nn): " yn
    case $yn in
        [Yy]* )
          echo -e "\n\n\nInstallation starting...\n\n"
          break;;
        [Nn]* )
          rm -rf $vMi_;
          exit;
          break;;
        * ) echo "Please answer (y)es or (n)o.";;
    esac
done
}



# -  - #
# - installVManager_ - #
function installVManager_(){
  echo -e "Installing virtual manager...\n";
  case $Opt in
    1)
      sudo apt install virt-manager -y;
      ;;
    2)
      archVManagerInstall_;
      ;;
    3)
      sudo dnf install virt-manager -y;
      ;;
  esac
  echo -e "\nVirtual manager was installed successfully...\n\n\n";
  sleep 10;
}



# -  - #
# - archVManagerInstall_ - #
function archVManagerInstall_(){

<<'comment'
  # -  - #
  # - Documentation - #
  https://gist.github.com/tatumroaquin/c6464e1ccaef40fd098a4f31db61ab22
comment

  # -  - #
  # - Install software - #
  sudo pacman -S dnsmasq edk2-ovmf guestfs-tools libosinfo libvirt qemu-full qemu-img \
                 swtpm tuned virt-install virt-manager virt-viewer --needed;

  sudo systemctl enable libvirtd.service;

  # -  - #
  # - Enable services - #
<<'comment'
  Option 1: Enable the modular daemon
    for drv in qemu interface network nodedev nwfilter secret storage; do
        sudo systemctl enable virt${drv}d.service;
        sudo systemctl enable virt${drv}d{,-ro,-admin}.socket;
    done

  Option 2: Enable the monolithic daemon.
    sudo systemctl enable libvirtd.service;
comment

  # -  - #
  # - Verify Host Virtualization - #
  sudo virt-host-validate qemu;

}



# -  - #
# - addUserToLibvirt_ - #
function addUserToLibvirt_(){
  echo -e "Adding user to virtual manager...\n";
  case $Opt in
    1)
      sudo usermod -aG libvirt $USER;
      ;;
    2)
      sudo usermod -aG kvm $USER;
      sudo usermod -aG libvirt $USER;
      ;;
    3)
      sudo usermod -aG libvirt $USER;
      ;;
  esac
  echo -e "\nUser was added to virtual manager successfully...\n\n\n";
  sleep 10;
}



# -  - #
# - enablingVirtualization_ - #
function enablingVirtualization_(){
  echo -e "Enabling virtualization...\n";

  sudo systemctl enable --now libvirtd.service;
  sudo systemctl status libvirtd;

<<'comment'
  sudo systemctl enable libvirtd;
  sudo systemctl start libvirtd;
comment

  echo -e "\nVirtualization was enabled successfully...\n\n\n";
  sleep 10;
}



# -  - #
# - activatingNetwork_ - #
function activatingNetwork_(){
  echo -e "Activating network...\n";

  sudo virsh net-start default;
  sudo virsh net-autostart default;

  echo -e "\nNetwork was activated successfully...\n\n\n";
  sleep 10;
}



<<'comment'
# -  - #
# - Create dynamic disks .qcow2 for VirtualManager as VirtualBox (VDI) - #

# -  - #
# - Data - #
dF_=/Images/VManager/;


# -  - #
# - HPLaptopArch - #
sD_=/Data$dF_;

# -  - #
# - HPDesktopArch - #
sD_=/DataBtrfs$dF_;



qemu-img create -f qcow2 -o preallocation=off "$sD_"UbuntuDesktop_25.10 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"Ubuntu_22.04_64bit 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_2022-06-25_DataExtend 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"ZorinOS_16.1_Core(64bit)-TestingZfs_DataExtend 100G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_2024-10 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_2024-10-Data 200G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_Data1_2022-01 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_Data2_2022-01 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_Data3_2022-01 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_Data4_2022-01 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"VManagerTemplate_HDD 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"'Arch_2022-01-29_Base(64bit)_Bios-Zfs' 200G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"'Arch_2022-02-14_Base(64bit)_UEFI-Zfs' 200G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"Windows10-Arch_DualBoot_Bios 200G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"'Ubuntu_22-04_LiveServer(64bit)-JammyJellyfish_2022-02-15_Bios_DataPool1' 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"'Ubuntu_22-04_LiveServer(64bit)-JammyJellyfish_2022-02-15_Bios_DataPool2' 150G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_2022-02-16-Bios_ZPool1 150G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"uServer_2022-02-16-Bios_ZPool2 150G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"'Arch_2022-02-22_Base(64bit)_UEFI1' 100G;
qemu-img create -f qcow2 -o preallocation=off "$sD_"VManagerTemplate_EmptyHDD 100G;

qemu-img create -f qcow2 -o preallocation=off "$sD_"Xubuntu 100G



# -  - #
# - Convert VirtualBox images to VirtualManager - #
sudo qemu-img convert -f vdi -O qcow2 Ubuntu\ 20.04.vdi /var/lib/libvirt/images/ubuntu-20-04.qcow2
comment



source <(curl -s https://www.education.isdevelopment.us/Functions/Functions.sh)
lightVersion_=$( lVersion_ );



checkComputer_ $vMi_;
Greetings_;
distroVersion_;
update_;
checkVirtualization_;
installVManager_;
addUserToLibvirt_;
enablingVirtualization_;

if [ "$Opt" == "2" ]; then
  activatingNetwork_; fi;



rm -rf ~/$vMi_;

echo -e "\n\nVirtual manager was installed successfully...\n\n\n";
sleep 10;



# - eof - #