Debian Codegraph @

scripts/windows/setup.sh

raw symbols SCIP
1 #!/usr/bin/envscip-shell filesystem . . `/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.isoscip-shell filesystem . . `/tmp/virtio-win.iso`."
8
9 # Check if ISO path is provided
10 if [scip-shell system . . `[`. -z "$1" ]; then
11 echoscip-shell system . . echo. "Usage: $0 <path_to_windows_iso>"
12 exit 1
13 else
14 ISO_PATH=$1
15 fi
16
17 if [scip-shell system . . `[`. "$(whoamiscip-shell system . . whoami.)" = "root" ]; then
18 echoscip-shell system . . 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 [scip-shell system . . `[`. ${#SSH_PUBKEYS[@]} -eq 0 ]; then
32 echoscip-shell system . . echo. "No .pub files found in ~/.ssh directory."
33 exit 1
34 elif [scip-shell system . . `[`. ${#SSH_PUBKEYS[@]} -eq 1 ]; then
35 SSH_PUBKEY=${SSH_PUBKEYS[0]}
36 else
37 SSH_PUBKEY=$(printfscip-shell system . . printf. "%s\n" "${SSH_PUBKEYS[@]}" | fzf --prompt="Select a .pub file: ")
38 fi
39
40 if [scip-shell system . . `[`. -z "$SSH_PUBKEY" ]; then
41 echoscip-shell system . . 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 [scip-shell system . . `[`. -f "$VM_DISK" ]; then
49 echoscip-shell system . . echo. "Error: $VM_DISK already exists"
50 exit 1
51 fi
52
53 # Download Virtio Drivers ISO
54 echoscip-shell system . . echo. "Downloading Virtio drivers ISO..."
55 curlscip-shell system . . curl. -L -o "$VIRTIO_ISO" --etag-save "$VIRTIO_ISO.tmp" --etag-compare "$VIRTIO_ISO.etag" "$VIRTIO_ISO_URL"
56 mvscip-shell system . . mv. "$VIRTIO_ISO.tmp" "$VIRTIO_ISO.etag"
57
58 # Create autounattend
59 temp="$(mktempscip-shell system . . mktemp. -d)"
60
61 cleanupscip-shell shell shell-project 0.0.0 cleanup().() {
62 sudoscip-shell system . . sudo. umount "$temp/mount" 2> /dev/nullscip-shell filesystem . . `/dev/null`.
63 rmscip-shell system . . rm. -rf "$temp"
64 }
65 trap cleanup EXIT
66
67 chmodscip-shell system . . chmod. 0755 "$temp"
68 mkdirscip-shell system . . 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="$(catscip-shell system . . cat. "$SSH_PUBKEY")" envsubstscip-shell system . . 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 sudoscip-shell system . . 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 sudoscip-shell system . . 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 sudoscip-shell system . . sudo. virt-install \
102 --connect qemu:///system \
103 --name "$VM_NAME" \
104 --ram "$RAM_MB" \
105 --vcpus "$(nprocscip-shell system . . nproc.),cores=$(nprocscip-shell system . . 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 echoscip-shell system . . echo. "Windows VM setup initiated, click through the installer."
119 echoscip-shell system . . 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 echoscip-shell system . . echo. "Waiting for VM to receive an IP."
125 ip=""
126 while [scip-shell system . . `[`. -z "$ip" ]; do
127 sleepscip-shell system . . sleep. 10
128 ip="$(virsh --connect qemu:///system domifaddr "$VM_NAME" | gawkscip-shell system . . gawk. 'match($0, /([[:digit:]\.]+)\//, a) { print a[1] }')"
129 echoscip-shell system . . echo. -n .
130 done
131 echoscip-shell system . . echo.
132
133 echoscip-shell system . . echo. "Waiting for SSH to become available to continue installation."
134 while ! sshscip-shell system . . ssh. -o ConnectTimeout=10 -T "$ip" '$true' &> /dev/nullscip-shell filesystem . . `/dev/null`.; do
135 echoscip-shell system . . echo. -n .
136 done
137 echoscip-shell system . . echo.
138
139 ./setup-efw.sh "$VM_NAME"