Created
May 26, 2024 13:10
-
-
Save M1cha/8724657aa78a78b5a09fb14ecb536b49 to your computer and use it in GitHub Desktop.
create_win11_usb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Based on: https://nixaid.com/bootable-usb-windows-linux/ | |
# Dependencies: ntfs-3g rsync | |
set -euo pipefail | |
iso="$1" | |
drive="$2" | |
script="$(readlink -f "$0")" | |
log() { echo "$@" >&2; } | |
die() { log "$@"; exit 1; } | |
if [ ! -f "$iso" ]; then | |
die "ISO doesn't exist or is not a file: $iso" | |
fi | |
if [ ! -b "$drive" ]; then | |
die "drive doesn't exist or is not a block device: $drive" | |
fi | |
if [ "${CREATE_WIN11_USB_INSIDE_NS:-}" != 1 ]; then | |
export CREATE_WIN11_USB_INSIDE_NS=1 | |
exec unshare --mount "$script" "$@" | |
fi | |
set -x | |
# Use a path that we can easily append partition IDs to | |
drive="/dev/disk/by-id/$(lsblk -dno ID-LINK "$drive")" | |
part_boot="$drive-part1" | |
part_install="$drive-part2" | |
# Make sure our changes to /mnt won't be visible to the parent namespace | |
if mountpoint -q /mnt; then | |
umount -R /mnt | |
fi | |
mount -t tmpfs tmpfs /mnt | |
# Create partition table | |
wipefs -a "$drive" | |
sfdisk "$drive" <<EOF | |
label: gpt | |
unit: sectors | |
start=2048, size=2095104, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, name="BOOT" | |
start=2097152, size=18874368, type=EBD0A0A2-B9E5-4433-87C0-68B6B72699C7, name="INSTALL" | |
EOF | |
# Wait for udev to create partition nodes | |
udevadm settle --timeout=15 --exit-if-exists="$part_boot" | |
udevadm settle --timeout=15 --exit-if-exists="$part_install" | |
# Mount ISO | |
mount --mkdir "$iso" /mnt/iso | |
# Format boot partition | |
mkfs.vfat -n BOOT "$part_boot" | |
mount --mkdir "$part_boot" /mnt/boot | |
# Copy files to boot partition | |
rsync -r --progress --exclude sources --delete-before /mnt/iso/ /mnt/boot/ | |
mkdir /mnt/boot/sources | |
cp /mnt/iso/sources/boot.wim /mnt/boot/sources/ | |
# Format install partition | |
mkfs.ntfs --quick -L INSTALL "$part_install" | |
mount --mkdir "$part_install" /mnt/install | |
# Copy files to install partition | |
rsync -r --progress --delete-before /mnt/iso/ /mnt/install/ | |
# Cleanup | |
umount -R /mnt | |
sync | |
log "Successful" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment