Creating a UEFI bootable Win7 USB stick from Linux

I run linux-only, and have for several years now.  I've also recently gotten into gaming with Steam.  They've made great progress in making things playable on linux, and most of what's not available specifically on linux works nearly-flawlessly under wine.  I started playing Half-Life 2, which runs pretty great natively on linux; but when I learned about CinematicMod, which is sadly windows-only, I decided I had to make a windows install to try it out.  Lacking an optical drive of any nature on this machine, the natural solution was to install windows from a USB stick.

Note: If you install windows to a partition on a drive that already has linux installed, windows will nuke your grub setup.  Read up on how to restore grub after a windows install.  If you install windows to a separate drive, you'll need to run sudo update-grub2 (or sudo update-grub for legacy grub users) from linux in order to add windows to your grub menu.  Of course, your BIOS will happily boot windows every time if you make your windows drive the first boot choice, so don't forget to make sure your linux drive is at the top of your BIOS's boot list.

Requirements:

  • Existing linux installation
  • 4GB or larger USB stick.  Yes, 4GB is the minimum; the files will take somewhere around 3.7GB.
  • WinUSB; Ubuntu users will find convenient links to .deb files for all the latest Ubuntu versions, as well as an apt repository.  Everyone else has to compile from the source tarfile.
  • Windows 7 installation disc ISO (or physical media and a drive to read it with; a Windows 8 or 8.1 disc will also likely work, but I don't know if Vista or lower will)

Process:

  1. Prepare the USB stick.  Remove all the files you want to keep from the stick, because you will be nuking it -- no, you can't keep your old partition(s), because we have to create a new partition table type (unless this drive already has a gpt partition table, but chances are it's msdos, and I know of no way to easily tell which you already have).  Open it in gparted (sudo gparted /dev/sdx, replacing x as appropriate); go to the Device menu, select Create Partition Table..., open the "advanced" option, choose gpt from the dropdown, and click Apply.  This will nuke all data on the drive, instantly, so be well warned.   Once that's done, create a 4GB or larger FAT16 partition.  Why FAT16?  The UEFI device compatibility specification explicitly requires support for FAT32 for drives and FAT16 or FAT12 for removable media.  If you want to use FAT32, sure, go ahead, but to play it safe use FAT16 or FAT12 instead.  FAT32 worked fine for me.
  2. Use WinUSB to install the ISO.  Once you have WinUSB installed, you'll have two commands, winusb and winusbgui.  Don't use winusbgui.  This command is the easy graphical way to do it, but it will re-nuke your drive and install an NTFS partition, which is probably not supported by your motherboard.  So to keep it FAT, as it were, use winusb's --install command: winusb --install /path/to/win7.iso /dev/sdx1, with the ISO path adjusted as appropriate, and /dev/sdx1 is the FAT partition you created in step 1.  If you get curious and run winusb --help, you'll notice there's also a --format command.  Don't use --format.  This will also create an NTFS partition, and is the same command winusbgui will use.
  3. Grab a coffee, energy drink, OR relaxing beverage of your choice.  I don't recommend combining coffee and energy drinks.  I've known people to go completely jittery and shaky from that mix.  If you do try that, I don't recommend adding other beverages to that mix, either.  But whatever.
  4. Wait.  How long?  That depends.  You're copying 4GB of data over a USB connection.  My machine has a 3.4GHz Core i7, but the USB stick and USB port were both USB 2.0 (I didn't notice a difference between 2.0 and 3.0 ports, but I didn't benchmark it, either), so I ended up waiting about 8 minutes for the process to finish.  Using a USB 3.0 device in a similarly-equipped slot will probably go much faster; a lesser CPU may make it go slower.  Enjoy that coffee (you mixed it with an energy drink, didn't you?  Don't say I didn't warn you...)
  5. Final touches.  Turns out WinUSB isn't quite so thorough with its EFI setup as we would like it to be, so there's one last step.  Mount the FAT partition and copy the efi/microsoft/boot directory to efi/boot -- that is, cp -r /path/to/mount/efi/microsoft/boot /path/to/mount/efi/.
  6. Boot it.  sync your changes, umount the drive, and reboot.  Tell your BIOS to boot from the USB stick, and you should be greeted with the Windows installation wizard.  If not, see this article for a few other things you can try (specifically, see step 11 under Manually Create a Bootable UEFI USB Flash Drive).

And enjoy trying to shoot high-definition antlions and headcrabs while you shake uncontrollably because you mixed coffee with energy drinks.