Skip to content

Instantly share code, notes, and snippets.

@JonnyTech
Last active August 13, 2023 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyTech/0be66f6855ac206fdc0211474c6365bd to your computer and use it in GitHub Desktop.
Save JonnyTech/0be66f6855ac206fdc0211474c6365bd to your computer and use it in GitHub Desktop.
Create bootable Windows installer drive from Linux
#!/bin/bash
die(){
echo -e "\n$1"
echo -e "\nUsage: $0 /path/to/windows.iso /path/to/usb [gpt|mbr] [full|quick]\n"
exit
}
[ "$(id -u)" != "0" ] && die "You must be root to run this script"
[ -f "$1" ] && iso="$1" || die "Error: File $1 does not exist"
[ -e "$2" ] && usb="$2" || die "Error: Drive $2 not present"
[[ ${3,,} = "mbr" ]] && { mode=1; echo "Mode: MBR"; } || { mode=0; echo "Mode: GPT"; }
[[ ${4,,} = "full" ]] && { files=1; echo "Files: Full"; } || { files=0; echo "Files: Quick"; }
curl -LJO https://github.com/pbatard/rufus/raw/master/res/uefi/uefi-ntfs.img
umount "${usb}"?*
wipefs -fa "${usb}"
parted "${usb}" -s mklabel `[ $mode = 1 ] && echo msdos || echo gpt`
bs=`lsblk -bno log-sec "$usb" |head -1`
size=$(((`stat -c '%s' uefi-ntfs.img`+$bs-1)/$bs))
parted "${usb}" -s mkpart primary ntfs 4MiB -- -`[ $mode = 1 ] && echo $(($size+1)) || echo $(($size+1+33))`s
parted "${usb}" -a none -s mkpart primary fat16 -- -`[ $mode = 1 ] && echo $size || echo $(($size+33))`s -`[ $mode = 1 ] && echo 1 || echo $((1+33))`s
sync "${usb}"
mkntfs -v -Q -I -T -F -L "Windows_USB" "${usb}1"
tmpiso=$(mktemp /tmp/tmpisoXXXX -d)
tmpusb=$(mktemp /tmp/tmpusbXXXX -d)
mount --options loop,ro --types udf,iso9660 "${iso}" "${tmpiso}"
mount "${usb}1" "${tmpusb}"
if [ $files = 1 ]
then
rsync -aP "${tmpiso}"/ "${tmpusb}"
else
rsync -aP --exclude 'sources/install.wim' "${tmpiso}"/ "${tmpusb}"
touch "${tmpusb}"/sources/install.wim
fi
if [ $mode = 1 ]
then
grub-install --target=i386-pc --boot-directory="${tmpusb}" --force "${usb}"
echo -e "ntldr /bootmgr\nboot" > "${tmpusb}"/grub/grub.cfg
fi
umount "${tmpiso}" "${tmpusb}"
rm -rf "${tmpiso}" "${tmpusb}"
dd if=uefi-ntfs.img of="${usb}2"
unset -v iso usb mode bs size
@JonnyTech
Copy link
Author

Warning: This will wipe your entire drive. Ensure that the drive specified is correct.

@JonnyTech
Copy link
Author

The process creates a GPT structured drive. Note that most systems are able to boot UEFI loaders from MBR partitioned drives. It is a common misconception that a drive has to be partitioned as GPT to boot UEFI bootloaders.

@andrewdbate
Copy link

andrewdbate commented Jan 21, 2022

Having run this, I get the following:

dd: writing to '/dev/sdb2': No space left on device
2016+0 records in
2015+0 records out
1031680 bytes (1.0 MB, 1008 KiB) copied, 0.181442 s, 5.7 MB/s

I believe that this is because the offsets used are incorrect. You have not allowed for the GPT secondary data at the end of the drive.

The offset given for the UEFI:NTFS partition is at -2048s. The size of the UEFI:NTFS should be 2048 sectors, because a sector on a USB is (almost certainly) 512 bytes and 512 bytes * 2048 sectors / 1024 = 1 MB. So I agree that the UEFI:NTFS partition should be 2048 sectors. But this does not account for the last 33 sectors being reserved for the secondary GPT data. Hence the size of the partition you create is actually 2048 - 33 = 2015 sectors, and so less than the 1 MB required.

It can be verified that the partition created is indeed 2015 sectors by inspecting the partition table created by the above steps, e.g. using parted or GParted (as I'm sure you know).

To fix this, add 33 to the negative offsets in the two calls to mkpart as follows:

parted $usb -s mkpart primary ntfs 4MiB -- -2082s
parted $usb -a none -s mkpart primary fat16 -- -2081s 100%

After making this change to the script I do not get the No space left on device warning from dd.

@JonnyTech
Copy link
Author

Script has been updated, thank you @andrewdbate for testing and reporting back.

Changes:

  • supports both gpt and mbr schemes via optional argument
  • calculate offsets and sizes from downloaded uefi-ntfs.img file
  • added reserved space at end of GPT drive as per feedback

This is still untested (due to lack of hardware) so any further feedback is welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment