Skip to content

Instantly share code, notes, and snippets.

@fumiyas
Last active November 9, 2023 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fumiyas/b85150857b739958ac8cad4186967c92 to your computer and use it in GitHub Desktop.
Save fumiyas/b85150857b739958ac8cad4186967c92 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -u
## ======================================================================
vm_cpus=''
vm_memory_size='8192' ## MB
vm_storage_size='65536' ## MB
vm_nic_mac='' ## 'AABBCCDDEEFF' style
vm_os_type='Windows11_64'
os_hostname=''
os_product_key=''
os_user_name='vagrant'
os_user_password='vagrant'
## ----------------------------------------------------------------------
vm_name="$1"; shift
iso_path="$1"; shift
## <language>_windows_11_consumer_editions_version_<yy>h<q>_<arch>_dvd_<hash>.iso,
## the Windows 11 install ISO image is a multi-session UDF file.
## #1: Windows 11 Home
## #2: Windows 11 Education
## #3: Windows 11 Pro
## #4: Windows 11 Pro Education
## #5: Windows 11 Pro for Workstations
iso_image_index="${1-3}"; ${1+shift}
## ----------------------------------------------------------------------
vms_dir=$(
VBoxManage list systemproperties \
|sed -n -e 's/^Default machine folder: *//p' \
;
)
vm_dir="$vms_dir/$vm_name"
vm_storage_file="$vm_dir/$vm_name.vdi" \
## ======================================================================
#vboxmanage controlvm "$vm_name" poweroff && sleep 3 || true
#vboxmanage unregistervm --delete-all "$vm_name" || true
#vboxmanage closemedium disk "$vm_storage_file" --delete || true
#rm -rf "$vm_dir" || true
## ======================================================================
VBoxManage createvm \
--name "$vm_name" \
--ostype "$vm_os_type" \
--default \
--register \
|| exit $? \
;
VBoxManage modifyvm "$vm_name" \
--nested-hw-virt on \
${vm_cpus:+--cpus "$vm_cpus"} \
${vm_memory_size:+--memory "$vm_memory_size"} \
${vm_nic_mac:+--mac-address1="$vm_nic_mac"} \
|| exit $? \
;
VBoxManage createmedium disk \
--filename "$vm_storage_file" \
${vm_storage_size:+--size "$vm_storage_size"} \
|| exit $? \
;
VBoxManage storageattach "$vm_name" \
--medium "$vm_storage_file" \
--type hdd \
--storagectl SATA \
--port 0 \
|| exit $? \
;
VBoxManage unattended install "$vm_name" \
--iso "$iso_path" \
--image-index "${iso_image_index}" \
--install-additions \
${os_product_key:+--key "$os_product_key"} \
${os_hostname:+--hostname "$os_hostname"} \
--user "$os_user_name" \
--password "$os_user_password" \
|| exit $? \
;
VBoxManage startvm "$vm_name" \
|| exit $? \
;
i=0
while [ "$i" -lt 120 ]; do
if \
VBoxManage showvminfo "$vm_name" --log 0 2>/dev/null \
|grep -q 'EFI: VBoxDbg> loadimage64 '\''cdboot.efi'\' \
; then
VBoxManage controlvm "$vm_name" keyboardputstring '.'
exit 0
fi
sleep 1
# shellcheck disable=SC2003 # expr is antiquated
i="$(expr "$i" + 1)"
done
echo "$0: ERROR: timed out" >&2
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment