#!/bin/bash # Script to create a virtual machine # # Copyright (C) 2024 Adrien 'neox' Bourmault # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # Exit on error set -e #set -v vm=$1 if [[ $# < 1 ]] then echo "ERROR: 1 parameter required : vm domain" exit 1 fi echo "Starting vm..." virsh start ${vm} || (echo -e "\e[1m\e[31mFail to start...(perhaps already done?)\e[0m") sleep 5 echo "Waiting for ${vm} to boot..." sudo -u admin666 ssh -4 -o ConnectTimeout=60 ${vm} "echo Connected!" || (echo -e "\e[1m\e[31mFail to wait...\e[0m" ; exit 1) echo "Setting up hostname..." sudo -u admin666 ssh -4 ${vm} "sudo hostnamectl set-hostname ${vm}" || (echo -e "\e[1m\e[31mFail to set up...\e[0m" ; exit 1) echo "Reloading postfix configuration..." sudo -u admin666 ssh -4 ${vm} "sudo postmap /etc/postfix/virtual" || (echo -e "\e[1m\e[31mFail to reload...\e[0m" ; exit 1) #echo "Reloading postfix..." #sudo -u admin666 ssh -4 ${vm} "sudo postfix reload" || (echo -e "\e[1m\e[31mFail to reload ($?)...\e[0m" ; exit 1) echo "Rebooting..." sudo -u admin666 ssh -4 ${vm} "sudo reboot" || echo "(rebooted quickly)" sleep 5 echo "Waiting for ${vm} to boot..." sudo -u admin666 ssh -4 -o ConnectTimeout=60 ${vm} "echo Connected!" || (echo -e "\e[1m\e[31mFail to wait...\e[0m" ; exit 1) echo "Updating packages..." sudo -u admin666 ssh -4 ${vm} "sudo apt-get update" || (echo -e "\e[1m\e[31mFail to update...\e[0m" ; exit 1) sudo -u admin666 ssh -4 ${vm} 'sudo DEBIAN_FRONTEND=noninteractive apt-get full-upgrade -y -o Dpkg::Options::="--force-confdef"' || (echo -e "\e[1m\e[31mFail to upgrade...\e[0m" ; exit 1) sudo -u admin666 ssh -4 ${vm} "sudo apt-get autoremove -y" || (echo -e "\e[1m\e[31mFail to remove...\e[0m" ; exit 1) echo "Rebooting..." sudo -u admin666 ssh -4 ${vm} "sudo reboot" || echo "(rebooted quickly)" sleep 5 echo "Waiting for ${vm} to boot..." sudo -u admin666 ssh -4 -o ConnectTimeout=60 ${vm} "echo Connected!" || (echo -e "\e[1m\e[31mFail to wait...\e[0m" ; exit 1) echo "Installing base packages..." sudo -u admin666 ssh -4 ${vm} "sudo apt-get install -y etckeeper molly-guard tig lm-sensors fancontrol screen emacs-nox needrestart iotop htop curl ncdu iptraf tig unzip" || (echo -e "\e[1m\e[31mFail to install...\e[0m" ; exit 1) echo -e "\e[36m${vm} successfully set up\e[0m" exit 0