Monday, June 10, 2013

Create a Bootable Windows 7 USB Drive in Linux

There are a ridiculous number of guides on how to create a bootable Windows 7 USB drive from within 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. You will need to 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 tail -f /var/log/messages as root in Fedora; other Linux distributions should be similar but Ubuntu uses /var/log/syslog.

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 that changes made using fdisk can be undone as long as those changes have not been written. On the other hand, changes made using parted are applied in real time. In either case be SURE you are making changes to the proper device.

Partition the USB Drive With fdisk

fdisk /dev/sdX

Type p and Enter to print the current partition table. I do this out of habit.

Delete all the current partitions by typing d then Enter for each partition.

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.

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.

Type a and Enter, then type 1 and Enter to turn on the Boot flag.

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.

Type w and Enter to write the changes.

Partition the USB Drive With parted

Make SURE you are using parted on the right device. Everything done using parted is applied in real time.

parted /dev/sdX 

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

Format the New Partition on the USB Drive as NTFS

You will need the Linux NTFS userspace driver installed. On Fedora run yum install ntfs-3g to install it, or on Ubuntu apt-get install ntfs-3g.

mkfs.ntfs -f /dev/sdX1

Install ms-sys

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

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

make install

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

ms-sys -7 /dev/sdX

Mount the USB Drive

mkdir -p /mnt/flash

mount /dev/sdX1 /mnt/flash

Mount the Windows 7 ISO

mkdir -p /mnt/iso

mount -o loop /root/en_windows_7_professional_x64_dvd_X15-65805.iso /mnt/iso

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

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

Unmount the USB Drive

I encountered several instances where the copy command finished before all I/O was written to the USB drive. Because of this, the unmount command below will stall until all I/O is finished. I/O can be watched in real time by installing iotop.

umount /mnt/flash

Boot from the USB Drive

Plugin the USB drive to the computer you want to install Windows on and boot to USB-HDD or USB-ZIP. There could be instances 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, but on a Lenovo ThinkPad X220 I booted to USB-HDD.



comments powered by Disqus


Other Posts