| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | # Variables |
| 6 | VIRTIO_ISO_URL="https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.266-1/virtio-win-0.1.266.iso" |
| 7 | VIRTIO_ISO="/tmp/virtio-win.iso" |
| 8 | |
| 9 | # Check if ISO path is provided |
| 10 | if [ -z "$1" ]; then |
| 11 | echo "Usage: $0 <path_to_windows_iso>" |
| 12 | exit 1 |
| 13 | else |
| 14 | ISO_PATH=$1 |
| 15 | fi |
| 16 | |
| 17 | if [ "$(whoami)" = "root" ]; then |
| 18 | echo "Do not run this script as root: it prevents detecting the correct user name" |
| 19 | exit 1 |
| 20 | fi |
| 21 | |
| 22 | # Prompt settings |
| 23 | read -p "Enter the name of the VM (default is vm): " VM_NAME |
| 24 | VM_NAME=${VM_NAME:-vm} |
| 25 | read -p "Enter the amount of RAM in MB (default is 8192): " RAM_MB |
| 26 | RAM_MB=${RAM_MB:-8192} |
| 27 | read -p "Enter the disk size in GB (default is 100): " DISK_SIZE |
| 28 | DISK_SIZE=${DISK_SIZE:-100} |
| 29 | |
| 30 | SSH_PUBKEYS=("$HOME/.ssh"/*.pub) |
| 31 | if [ ${#SSH_PUBKEYS[@]} -eq 0 ]; then |
| 32 | echo "No .pub files found in ~/.ssh directory." |
| 33 | exit 1 |
| 34 | elif [ ${#SSH_PUBKEYS[@]} -eq 1 ]; then |
| 35 | SSH_PUBKEY=${SSH_PUBKEYS[0]} |
| 36 | else |
| 37 | SSH_PUBKEY=$(printf "%s\n" "${SSH_PUBKEYS[@]}" | fzf --prompt="Select a .pub file: ") |
| 38 | fi |
| 39 | |
| 40 | if [ -z "$SSH_PUBKEY" ]; then |
| 41 | echo "No SSH pubkey selected." |
| 42 | exit 1 |
| 43 | fi |
| 44 | |
| 45 | # Check disk before starting download |
| 46 | VM_DISK="/var/lib/libvirt/images/${VM_NAME}.qcow2" |
| 47 | |
| 48 | if [ -f "$VM_DISK" ]; then |
| 49 | echo "Error: $VM_DISK already exists" |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | # Download Virtio Drivers ISO |
| 54 | echo "Downloading Virtio drivers ISO..." |
| 55 | curl -L -o "$VIRTIO_ISO" --etag-save "$VIRTIO_ISO.tmp" --etag-compare "$VIRTIO_ISO.etag" "$VIRTIO_ISO_URL" |
| 56 | mv "$VIRTIO_ISO.tmp" "$VIRTIO_ISO.etag" |
| 57 | |
| 58 | # Create autounattend |
| 59 | temp="$(mktemp -d)" |
| 60 | |
| 61 | cleanup() { |
| 62 | sudo umount "$temp/mount" 2> /dev/null |
| 63 | rm -rf "$temp" |
| 64 | } |
| 65 | trap cleanup EXIT |
| 66 | |
| 67 | chmod 0755 "$temp" |
| 68 | mkdir -p "$temp/mount" "$temp/modifications" |
| 69 | |
| 70 | |
| 71 | # Prepare an installation file automatically installs Windows. |
| 72 | # |
| 73 | # Allows ssh authentication with all public keys found in ~/.ssh. |
| 74 | AUTHORIZED_KEYS="$(cat "$SSH_PUBKEY")" envsubst '$USER $AUTHORIZED_KEYS' < autounattend.xml > "$temp/modifications/autounattend.xml" |
| 75 | |
| 76 | # Generate bootable ISO. |
| 77 | # |
| 78 | # This ISO contains the autounattend.xml and doesn't require pressing a button |
| 79 | # to start the installation. See: |
| 80 | # * https://palant.info/2023/02/13/automating-windows-installation-in-a-vm/ |
| 81 | sudo mount -o loop "$ISO_PATH" "$temp/mount" |
| 82 | |
| 83 | genisoimage \ |
| 84 | -iso-level 4 -rock -udf \ |
| 85 | -disable-deep-relocation \ |
| 86 | -untranslated-filenames \ |
| 87 | -allow-limited-size \ |
| 88 | -no-emul-boot \ |
| 89 | -boot-load-size 8 \ |
| 90 | -eltorito-boot boot/etfsboot.com \ |
| 91 | -eltorito-alt-boot \ |
| 92 | -eltorito-boot efi/microsoft/boot/efisys_noprompt.bin \ |
| 93 | -o "$temp/win.iso" \ |
| 94 | "$temp/mount" "$temp/modifications" |
| 95 | |
| 96 | # Create VM Disk |
| 97 | sudo qemu-img create -f qcow2 "$VM_DISK" "${DISK_SIZE}G" |
| 98 | |
| 99 | # Define and create the VM using virt-install. |
| 100 | # This will start the VM. |
| 101 | sudo virt-install \ |
| 102 | --connect qemu:///system \ |
| 103 | --name "$VM_NAME" \ |
| 104 | --ram "$RAM_MB" \ |
| 105 | --vcpus "$(nproc),cores=$(nproc)" \ |
| 106 | --cpu "host-passthrough,check=none,migratable=off,feature.vmx=require,feature.hle=disable,feature.rtm=disable" \ |
| 107 | --os-variant win11 \ |
| 108 | --network network=default,model=e1000 \ |
| 109 | --channel type=unix,source.mode=bind,target.type=virtio,target.name=org.qemu.guest_agent.0 \ |
| 110 | --graphics spice \ |
| 111 | --disk path="$VM_DISK",format=qcow2,bus=sata,size="$DISK_SIZE",boot.order=1 \ |
| 112 | --disk path="$temp/win.iso",device=cdrom,bus=sata,boot.order=2 \ |
| 113 | --disk path="$VIRTIO_ISO",device=cdrom,bus=sata \ |
| 114 | --install bootdev=cdrom \ |
| 115 | --boot uefi,firmware.feature0.name=enrolled-keys,firmware.feature0.enabled=no \ |
| 116 | --noautoconsole |
| 117 | |
| 118 | echo "Windows VM setup initiated, click through the installer." |
| 119 | echo "You may have to manually start the VM a couple of times." |
| 120 | |
| 121 | # Show the graphical output so that the user can follow along. |
| 122 | virt-manager --connect qemu:///system --show-domain-console "$VM_NAME" |
| 123 | |
| 124 | echo "Waiting for VM to receive an IP." |
| 125 | ip="" |
| 126 | while [ -z "$ip" ]; do |
| 127 | sleep 10 |
| 128 | ip="$(virsh --connect qemu:///system domifaddr "$VM_NAME" | gawk 'match($0, /([[:digit:]\.]+)\//, a) { print a[1] }')" |
| 129 | echo -n . |
| 130 | done |
| 131 | echo |
| 132 | |
| 133 | echo "Waiting for SSH to become available to continue installation." |
| 134 | while ! ssh -o ConnectTimeout=10 -T "$ip" '$true' &> /dev/null; do |
| 135 | echo -n . |
| 136 | done |
| 137 | echo |
| 138 | |
| 139 | ./setup-efw.sh "$VM_NAME" |