| 1 | #!/usr/bin/env bash
|
|---|
| 2 | set -o errexit -o nounset -o pipefail
|
|---|
| 3 | #set -o xtrace
|
|---|
| 4 |
|
|---|
| 5 | unattended-templates-dir() {
|
|---|
| 6 | local dir=
|
|---|
| 7 | local dirs=(
|
|---|
| 8 | $HOME/Applications/VirtualBox.app/Contents/MacOS/UnattendedTemplates
|
|---|
| 9 | /Applications/VirtualBox.app/Contents/MacOS/UnattendedTemplates
|
|---|
| 10 | /usr/etc/virtualbox/UnattendedTemplates
|
|---|
| 11 | /usr/lib/virtualbox/UnattendedTemplates
|
|---|
| 12 | /usr/local/etc/virtualbox/UnattendedTemplates
|
|---|
| 13 | /usr/local/lib/virtualbox/UnattendedTemplates
|
|---|
| 14 | )
|
|---|
| 15 | for dir in "${dirs[@]}"; do
|
|---|
| 16 | if test -d "$dir"; then
|
|---|
| 17 | echo "$dir"
|
|---|
| 18 | return 0
|
|---|
| 19 | fi
|
|---|
| 20 | done
|
|---|
| 21 | return 1
|
|---|
| 22 | } # unattended-templates-dir()
|
|---|
| 23 | start() {
|
|---|
| 24 | local vm=ubuntu-bionic-server
|
|---|
| 25 | local ostype=Ubuntu_64
|
|---|
| 26 | local os_iso_url=http://cdimage.ubuntu.com/releases/bionic/release/ubuntu-18.04.1-server-amd64.iso
|
|---|
| 27 | local os_iso=/tmp/$(basename $os_iso_url)
|
|---|
| 28 |
|
|---|
| 29 | if ! test -e $os_iso; then
|
|---|
| 30 | curl --fail --location --create-dirs --output $os_iso-$$ $os_iso_url
|
|---|
| 31 | mv $os_iso-$$ $os_iso # this only happens if previous cmds succeeds (because of errexit)
|
|---|
| 32 | fi
|
|---|
| 33 |
|
|---|
| 34 | local base_dir=/tmp/vms
|
|---|
| 35 | local aux_base_path=/tmp/$vm/$vm-unattended-install-
|
|---|
| 36 |
|
|---|
| 37 | VBoxManage unregistervm $vm --delete > /dev/null || true
|
|---|
| 38 |
|
|---|
| 39 | rm -fr $base_dir/$vm
|
|---|
| 40 | mkdir -p $base_dir/$vm
|
|---|
| 41 |
|
|---|
| 42 | rm -fr $(dirname $aux_base_path)
|
|---|
| 43 | mkdir -p $(dirname $aux_base_path)
|
|---|
| 44 |
|
|---|
| 45 | VBoxManage createvm \
|
|---|
| 46 | --name $vm \
|
|---|
| 47 | --basefolder $base_dir \
|
|---|
| 48 | --ostype $ostype \
|
|---|
| 49 | --register \
|
|---|
| 50 |
|
|---|
| 51 | VBoxManage modifyvm $vm \
|
|---|
| 52 | --memory 1024 \
|
|---|
| 53 | --vram 16 \
|
|---|
| 54 |
|
|---|
| 55 | VBoxManage storagectl $vm \
|
|---|
| 56 | --name SAS \
|
|---|
| 57 | --add sas \
|
|---|
| 58 | --portcount 1 \
|
|---|
| 59 | --bootable on \
|
|---|
| 60 |
|
|---|
| 61 | VBoxManage createmedium disk \
|
|---|
| 62 | --filename $base_dir/$vm/$vm.vdi \
|
|---|
| 63 | --size 8192 \
|
|---|
| 64 | --format VDI \
|
|---|
| 65 |
|
|---|
| 66 | # SAS-0-0
|
|---|
| 67 | VBoxManage storageattach $vm \
|
|---|
| 68 | --medium $base_dir/$vm/$vm.vdi \
|
|---|
| 69 | --storagectl SAS \
|
|---|
| 70 | --port 0 \
|
|---|
| 71 | --device 0 \
|
|---|
| 72 | --type hdd \
|
|---|
| 73 |
|
|---|
| 74 | VBoxManage storagectl $vm \
|
|---|
| 75 | --name SATA \
|
|---|
| 76 | --add sata \
|
|---|
| 77 | --portcount 1 \
|
|---|
| 78 | --bootable on \
|
|---|
| 79 |
|
|---|
| 80 | # SATA-0-0
|
|---|
| 81 | VBoxManage storageattach $vm \
|
|---|
| 82 | --medium emptydrive \
|
|---|
| 83 | --storagectl SATA \
|
|---|
| 84 | --port 0 \
|
|---|
| 85 | --device 0 \
|
|---|
| 86 | --type dvddrive \
|
|---|
| 87 | --mtype readonly \
|
|---|
| 88 |
|
|---|
| 89 | local aux_base_path=/tmp/$vm/$vm-unattended-install-
|
|---|
| 90 | local vbox_script_template=$(unattended-templates-dir)/ubuntu_preseed.cfg
|
|---|
| 91 | local script_template; script_template="$(dirname $aux_base_path)/$(basename "$vbox_script_template")"
|
|---|
| 92 | mkdir -p $(dirname $script_template)
|
|---|
| 93 | cp "$vbox_script_template" $script_template
|
|---|
| 94 |
|
|---|
| 95 | if test ${fix:-0} -ne 0; then # fix ubuntu_preseed.cfg
|
|---|
| 96 | echo "fixing $script_template"
|
|---|
| 97 | cp $script_template $script_template.orig
|
|---|
| 98 |
|
|---|
| 99 | patch $script_template < fix.patch
|
|---|
| 100 |
|
|---|
| 101 | diff $script_template.orig $script_template || true
|
|---|
| 102 | fi
|
|---|
| 103 |
|
|---|
| 104 | VBoxManage unattended install $vm \
|
|---|
| 105 | --auxiliary-base-path $aux_base_path \
|
|---|
| 106 | --script-template $script_template \
|
|---|
| 107 | --iso $os_iso \
|
|---|
| 108 |
|
|---|
| 109 | VBoxManage startvm $vm
|
|---|
| 110 | }
|
|---|
| 111 | start
|
|---|