Skip to content

Instantly share code, notes, and snippets.

@jpramosi
Last active November 6, 2021 01:49
Show Gist options
  • Save jpramosi/688078710741e1efeaaf8da586df1287 to your computer and use it in GitHub Desktop.
Save jpramosi/688078710741e1efeaaf8da586df1287 to your computer and use it in GitHub Desktop.
Setup script for creating virtual machines with VirtualBox on Linux based operating systems
#!/bin/bash
# Simple setup script for creating virtual machines with VirtualBox on Linux based operating systems.
# e.g:
# ./mkvm -n server -d 50000 -r 2048 -c 2 -o Ubuntu_64 -i ubuntu-20.04.3-live-server-amd64.iso
VM_INFO=`VBoxManage list systemproperties | grep folder`
VM_PATH=${VM_INFO##* }
NAME=;
SIZE=;
RAM=;
CPU=;
OSID=;
ISO=;
usage() {
echo "$0 usage:" && grep " .)\ #" $0; exit 0;
}
[ $# -eq 0 ] && usage
while getopts ":h:i:o:c:r:d:n:" arg; do
case $arg in
n) # Specify virtual machine name.
NAME=${OPTARG}
;;
d) # Specify disk size in MB.
SIZE=${OPTARG}
;;
r) # Specify RAM size in MB.
RAM=${OPTARG}
;;
c) # Specify virtual cpu cores.
CPU=${OPTARG}
;;
o) # Specify OS-ID. See 'VBoxManage list ostypes'
OSID=${OPTARG}
;;
i) # Specify iso-image.
ISO=${OPTARG}
;;
h | *) # Display help.
usage
exit 0
;;
esac
done
if [ -z "$NAME" ] || [ -z "$SIZE" ] || [ -z "$RAM" ] || [ -z "$CPU" ] || [ -z "$OSID" ] || [ -z "$ISO" ]; then
usage
exit 0
fi
VM_STORAGE="${VM_PATH}${NAME}/${NAME}.vdi"
DEFAULT_ROUTE=`route | grep default`
DEFAULT_INTERFACE=${DEFAULT_ROUTE##* }
echo ""
echo "Create machine..."
VBoxManage createvm --name $NAME --ostype "$OSID" --register
echo ""
echo "Create disk..."
VBoxManage createhd --filename $VM_STORAGE --size $SIZE
echo ""
echo "Add SATA controller..."
VBoxManage storagectl $NAME --name "SATA Controller" --add sata --controller IntelAHCI
echo ""
echo "Attach storage to machine..."
VBoxManage storageattach $NAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium $VM_STORAGE
echo ""
echo "Add IDE controller..."
VBoxManage storagectl $NAME --name "IDE Controller" --add ide
echo ""
echo "Attach drive to machine..."
VBoxManage storageattach $NAME --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "$ISO"
echo ""
echo "Configure machine..."
VBoxManage modifyvm $NAME --ioapic on
VBoxManage modifyvm $NAME --boot1 dvd --boot2 disk --boot3 none --boot4 none
VBoxManage modifyvm $NAME --memory $RAM
VBoxManage modifyvm $NAME --cpus $CPU
VBoxManage modifyvm $NAME --audio none
VBoxManage modifyvm $NAME --nic1 bridged --bridgeadapter1 $DEFAULT_INTERFACE
echo ""
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment