Created
April 16, 2024 08:04
-
-
Save asmodehn/d90a04696a2b212a0400ee22f17ffafa to your computer and use it in GitHub Desktop.
Debian VirtualBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -ex | |
MACHINENAME=$1 | |
SCRIPT_DIR=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd) | |
SCRIPT_SIGN=created_by_$(basename "${BASH_SOURCE[0]}" .sh) | |
# Download debian.iso | |
if [ ! -f ./debian.iso ]; then | |
wget https://cdimage.debian.org/debian-cd/current-live/amd64/iso-hybrid/debian-live-12.5.0-amd64-standard.iso -O debian.iso | |
fi | |
if [ -d "$SCRIPT_DIR"/$MACHINENAME ]; then | |
echo "$MACHINENAME already exists." | |
if [ -f "$SCRIPT_DIR"/$MACHINENAME/$SCRIPT_SIGN ]; then | |
echo "WARNING: You will lose the existing machine and drive !" | |
read -p "Are you sure ?" -n 1 -r | |
echo # new line | |
if [ "$REPLY" != "${REPLY#[Yy]}" ]; then | |
echo "Removing $MACHINENAME..." | |
VBoxManage unregistervm $MACHINENAME --delete-all | |
# remove everything remaining in that folder | |
rm -f "$SCRIPT_DIR"/$MACHINENAME/$SCRIPT_SIGN | |
rmdir "$SCRIPT_DIR"/$MACHINENAME | |
echo "Done." | |
# exit 0 # uncomment to debug file system status at this stage | |
else | |
exit 1 | |
fi | |
else | |
exit 1 | |
fi | |
fi | |
# Workaround since VBoxManage is not setting proper adapter on nic2 | |
function patch_hostonlyadapter { | |
xmlstarlet ed --pf --inplace \ | |
-N VB="http://www.virtualbox.org/" \ | |
--insert "//VB:Network/VB:Adapter[@slot='1']/VB:HostOnlyInterface" -t attr -n "name" -v "vboxnet0" \ | |
"$SCRIPT_DIR"/$MACHINENAME/$MACHINENAME.vbox | |
} | |
function auto_install { | |
echo "Automated install informations:" | |
read -p "Login: " -r install_login | |
read -p "Password: " -s install_passwd | |
VBoxManage unattended install $MACHINENAME --iso="$SCRIPT_DIR"/debian.iso --user=$install_login --password=$install_passwd --hostname=${MACHINENAME//_/-}.vbox.local --dry-run | |
} | |
#Create VM | |
VBoxManage createvm --name=$MACHINENAME --ostype="Debian_64" --register --basefolder="$SCRIPT_DIR" | |
# mark it as managed by this script | |
touch "$SCRIPT_DIR"/$MACHINENAME/$SCRIPT_SIGN | |
# Set CPUs, memory and network | |
VBoxManage modifyvm $MACHINENAME --ioapic on | |
VBoxManage modifyvm $MACHINENAME --cpus=2 | |
VBoxManage modifyvm $MACHINENAME --graphicscontroller=vmsvga | |
VBoxManage modifyvm $MACHINENAME --memory 1024 --vram 128 | |
VBoxManage modifyvm $MACHINENAME --nic1 nat | |
# BUG : on VBoxManage --version 7.0.10_Ubuntur158379 this doesnt work as expected. | |
# TODO : Proper Fix ! | |
VBoxManage modifyvm $MACHINENAME --nic2 hostonly --hostonlyadapter1=vboxnet0 | |
# TMP WORKAROUND | |
patch_hostonlyadapter | |
# PATCH WORKING BUT MIGHT GET ERASED SOMEHOW... | |
#Create Disk and connect Debian Iso | |
VBoxManage createhd --filename "$SCRIPT_DIR"/$MACHINENAME/${MACHINENAME}_disk.vdi --size 20000 --format VDI | |
VBoxManage storagectl $MACHINENAME --name "SATA Controller" --add sata --controller IntelAhci | |
VBoxManage storageattach $MACHINENAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "$SCRIPT_DIR"/$MACHINENAME/${MACHINENAME}_disk.vdi | |
VBoxManage storagectl $MACHINENAME --name "IDE Controller" --add ide --controller PIIX4 | |
VBoxManage storageattach $MACHINENAME --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium "$SCRIPT_DIR"/debian.iso | |
VBoxManage modifyvm $MACHINENAME --boot1 dvd --boot2 disk --boot3 none --boot4 none | |
#TODO : shared clipboard ? useful without guest additions ? | |
echo "Please verify nic2 is connected to the hostonly network, and configured properly !" | |
echo "Then you can start the machine to process with debian installation." | |
#Start the VM | |
#VBoxSDL --startvm $MACHINENAME --verbose | |
#VBoxManage startvm $MACHINENAME --type gui | |
#or maybe ? --type headless | |
#If the machine gets locked you might need this | |
#VBoxManage startvm $MACHINENAME --type emergencystop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment