#!/usr/bin/env bash # Boot the Windows 11 Setup ISO in a throwaway UEFI VM with the backup drive passed # through over USB, so chkdsk can repair it without a Windows machine. # Run on a separate Linux PC (not the server), as yourself — not with sudo: # scripts/chkdsk-vm.sh [path/to/Win11.iso] # Without an argument it uses the newest vm/Win*11*.iso in this repo. # vm/ is gitignored: it holds the ISO and the VM's UEFI variable store. # Then: press Enter repeatedly at "Press any key to boot from CD or DVD", # Shift+F10 at the language screen, diskpart / list volume / exit, # chkdsk X: /f. See OKF/guides/ntfs-repair.md rung 3. set -euo pipefail VM_DIR=$(cd "$(dirname "$0")/.." && pwd)/vm if [[ -n ${1:-} ]]; then ISO=$1 else ISO=$(ls -t "$VM_DIR"/Win*11*.iso 2>/dev/null | head -n1) || true fi DRIVE_USB=0000:0000 # your drive's vendor:product ID, from lsusb OVMF_CODE=/usr/share/edk2/x64/OVMF_CODE.4m.fd OVMF_VARS_TEMPLATE=/usr/share/edk2/x64/OVMF_VARS.4m.fd OVMF_VARS=$VM_DIR/OVMF_VARS.4m.fd die() { printf 'ERROR: %s\n' "$*" >&2; exit 1; } (( EUID != 0 )) || die "run as yourself: root cannot open a window on the Wayland desktop" [[ -n $ISO && -f $ISO ]] || die "no Windows 11 ISO — pass its path, or put it in $VM_DIR/" [[ -f $OVMF_CODE ]] || die "no UEFI firmware — sudo pacman -S edk2-ovmf" command -v qemu-system-x86_64 >/dev/null || die "no QEMU — sudo pacman -S qemu-desktop" [[ -w /dev/kvm ]] || die "/dev/kvm not usable" if pgrep -x qemu-system-x86_64 >/dev/null; then die "a QEMU is already running — close it first (pkill -x qemu-system-x86_64)" fi line=$(lsusb -d "$DRIVE_USB") || die "the backup drive ($DRIVE_USB) not plugged into this PC" bus=$(awk '{print $2}' <<<"$line") devnum=$(awk '{print substr($4,1,3)}' <<<"$line") node=/dev/bus/usb/$bus/$devnum if findmnt -rn -S LABEL=BACKUP >/dev/null; then die "the backup drive is mounted on this PC — unmount it first: udisksctl unmount -b /dev/disk/by-label/BACKUP" fi # Raw USB access for just this device node; lasts until unplug/reboot. if [[ ! -w $node ]]; then echo "==> granting $USER access to $node (sudo)" sudo setfacl -m "u:$USER:rw" "$node" fi mkdir -p "$VM_DIR" [[ -f $OVMF_VARS ]] || cp "$OVMF_VARS_TEMPLATE" "$OVMF_VARS" echo "==> booting $(basename "$ISO") with the backup drive at $node" echo " press Enter repeatedly as soon as the window opens" exec qemu-system-x86_64 \ -enable-kvm -machine q35 -cpu host -smp 4 -m 4G -nic none \ -drive if=pflash,format=raw,readonly=on,file="$OVMF_CODE" \ -drive if=pflash,format=raw,file="$OVMF_VARS" \ -cdrom "$ISO" -boot d \ -device qemu-xhci -device usb-host,vendorid=0x${DRIVE_USB%%:*},productid=0x${DRIVE_USB##*:}