Create a Bootable Windows 7 USB Drive in Linux

Monday, June 10, 2013

There are a ridiculous number of guides on how to create a bootable Windows 7 USB drive in Linux. However, many of the guides have missing steps, involve using Windows, or use programs not readily available on other Linux distributions. The following guide should work on any Linux distribution to create a bootable Windows 7 USB drive.

Plugin the USB Drive

Most modern Linux distributions should automount the USB drive. If not, you can see what device it is assigned to by running df -h or mount.

syslog can also be monitored to find the device it is assigned to by running sudo tail -f /var/log/messages as Fedora, CentOS, or RHEL or by running sudo tail -f /var/log/syslog on Ubuntu or Debian.

Once you have figured out what device the USB drive is assigned to, be sure to unmount it before continuing.

Also, the following steps will erase everything on the USB drive you are using. You are responsible for your own data.

Partition the USB Drive

Either fdisk or parted can be used to partition the USB drive. Use whatever command you are more familiar with.

Be aware, changes made using fdisk can be undone as long as those changes have not been written; changes made using parted cannot be undone because they are applied in real time. In either case, be SURE you are making changes to the right device.

Partition the USB Drive with fdisk

Open the USB drive in fdisk:

sudo fdisk /dev/sdX

Once you are in the fdisk interactive menu, partition the USB drive with the following steps:

  1. Type p and Enter to print the current partition table. I do this out of habit.
  2. Delete all the current partitions by typing d then Enter for each partition.
  3. Type n and Enter, then type p and Enter, then type 1 and Enter three times to create one new primary partition that uses all available space.
  4. Type t and Enter (Partition 1 is selected because it’s the only partition), then type 7 and Enter to change the type to HPFS/NTFS/exFAT.
  5. Type a and Enter, then type 1 and Enter to turn on the Boot flag.
  6. To verify everything worked, type p and Enter and make sure the Boot column has an * set and the Id column is set to 7.
  7. Type w and Enter to write the changes.

Next, go to the section titled Format the New Partition on the USB Drive as NTFS.

Partition the USB Drive with parted

Open the USB drive in parted:

sudo parted /dev/sdX

Once you are in the parted interactive menu, partition the USB drive with the following steps (Make SURE you are using parted on the right device. Everything done using parted is applied in real time.):

(parted) mklabel msdos 
(parted) mkpart primary ntfs 1 -1 
(parted) set 1 boot on
(parted) quit

Next, go to the section titled Format the New Partition on the USB Drive as NTFS.

Format the New Partition on the USB Drive as NTFS

You will need the Linux NTFS userspace driver installed.

To install it on Fedora, CentOS, or RHEL run sudo yum install ntfs-3g.

To install it on Ubuntu or Debian run sudo apt-get install ntfs-3g.

Then, format the partition as NTFS:

sudo mkfs.ntfs -f /dev/sdX1

Install ms-sys

You will need ms-sys to write a Master Boot Record (MBR) to the USB drive.

Make sure you have installed the gcc, make, and gettext repository packages in order to compile the source code. The package names should be the same on Fedora, CentOS, RHEL, Ubuntu, or Debian.

Download the latest source code from http://ms-sys.sourceforge.net/#Download.

Un-tar the source code and change into the source code directory:

tar xvzf ms-sys-2.3.0.tar.gz

cd ms-sys-2.3.0

Compile and install the binary:

make

sudo make install

ms-sys will install to /usr/local/bin. Be sure to add this path if it is not in the root user’s environment PATH:

su -

export PATH=$PATH:/usr/local/bin

Write Windows 7 Master Boot Record (MBR) to the USB Drive

sudo ms-sys -7 /dev/sdX

Mount the USB Drive

sudo mkdir -p /mnt/flash

sudo mount /dev/sdX1 /mnt/flash

Mount the Windows 7 ISO

sudo mkdir -p /mnt/iso

sudo mount -o loop /tmp/en_windows_7_professional_x64_dvd_X15-65805.iso /mnt/iso

Copy the Contents of the Windows 7 ISO to the USB Drive

sudo cp -av /mnt/iso/* /mnt/flash/

Unmount the USB Drive

I encountered several situations where the cp command finished before all I/O was written to the USB drive.

Because of this, the following umount command will stall until all I/O is finished. I/O can be watched in real time by installing iotop.

sudo umount /mnt/flash

Boot from the USB Drive

Plugin the USB drive to the computer you want to install Windows 7 on and boot to USB-HDD or USB-ZIP.

Depending on the type of motherboard, there may be situations where you have to boot to USB-FDD or USB-CDROM instead.

For example, on a Gigabyte GA-MA74GM-S2 motherboard, booting to USB-HDD did not work. I had to boot to USB-ZIP. However, on a Lenovo ThinkPad X220 I had to boot to USB-HDD.



comments powered by Disqus