#!/bin/bash VMimage="https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.ova" VMname="test" VMhostname="vb-mini" VMusername="user" VMtimezone=`cat /etc/timezone` VMlocale=`localectl | grep LANG | cut -f2 -d:` VMkeyboard=`localectl | grep "X11 Layout" | cut -f2 -d:` VMdir=$HOME"/VirtualBox VMs" ### DO NOT TOUCH AFTER THIS LINE (unless you know what you are doing) #Prepare vm-config.sh script to be run on the new VM echo "Prepare vm-config.sh script" echo "#!/bin/bash" > vm-config.sh echo "VMhostname=\"$VMhostname\"" >> vm-config.sh echo "VMusername=\"$VMusername\"" >> vm-config.sh echo "VMtimezone=\""`cat /etc/timezone`"\"" >> vm-config.sh echo "VMlocale=\""`localectl | grep LANG | cut -f2 -d:`"\"" >> vm-config.sh echo "VMkeyboard=\""`localectl | grep "X11 Layout" | cut -f2 -d:`"\"" >> vm-config.sh cat >> vm-config.sh <<"EOF" # Define colors LGREEN='\033[1;32m' NC='\033[0m' # No Color echo -e "${LGREEN}*** Now running on $HOSTNAME${NC}" echo -e "${LGREEN}*** Fixing /etc/hosts${NC}" if ! grep $VMhostname -q /etc/hosts then sed -i".bak" "/127\.0\.0\.1/ s/$/ ${VMhostname}/" /etc/hosts fi echo -e "${LGREEN}*** Configuring localization for "$HOSTNAME"....${NC}" timedatectl set-timezone $VMtimezone localectl set-locale $VMlocale localectl set-keymap $VMkeyboard echo -e "${LGREEN}*** Set password for $VMusername on "$HOSTNAME"....${NC}" passwd $VMusername echo -e "${LGREEN}*** Updating packages...${NC}" apt update apt -y upgrade echo -e "${LGREEN}*** Installing extra packages...${NC}" apt -y install linux-modules-extra-$(uname -r) tasksel echo -e "${LGREEN}*** Installing VirtualBox guest additions...${NC}" sudo apt -y install virtualbox-guest-additions-iso sudo mount -o loop /usr/share/virtualbox/VBoxGuestAdditions.iso /media/ sudo /media/VBoxLinuxAdditions.run echo -e "${LGREEN}*** Bundle package selection (with tasksel)...${NC}" tasksel echo -e "${LGREEN}*** Consider rebooting the VM before using${NC}" EOF # prepare the tools sudo apt-get install cloud-image-utils imgFile=`basename $VMimage` echo $imgFile # get the image if [ ! -e $imgFile ] then wget $VMimage fi VBoxManage import $imgFile --vsys 0 --vmname $VMname VBoxManage modifyvm $VMname --nic1 nat # Set networking as NAT VBoxManage modifyvm $VMname --natpf1 ,tcp,,2222,,22 # Enable port forwarding for ssh VBoxManage storagectl $VMname --name Floppy --remove # Remove floppy driver # Create the configuration DVD cat > meta-data < user-data <> user-data for f in ~/.ssh/*.pub do echo -n " - " >> user-data cat $f >> user-data done # Password initialization not working... why? #echo -n " passwd: " >> user-data #cat >> user-data <<"EOF" #...input from mkpasswd here #EOF cloud-localds ciconf.iso user-data meta-data rm user-data meta-data # Mount the configuration disk VBoxManage storageattach $VMname --storagectl IDE --port 0 --device 0 --type dvddrive --medium ciconf.iso VBoxManage startvm $VMname echo "Type 'return' on *this* terminal when the login prompt appears on the VM terminal..." read ssh-keygen -f "/home/augusto/.ssh/known_hosts" -R "[localhost]:2222" echo "Transfer the vmconfig.sh script" scp -P 2222 vm-config.sh user@localhost:/tmp/ echo "Running the script on the VM" ssh -tt -p 2222 $VMusername@localhost "sudo bash /tmp/vm-config.sh"