Skip to content

Instantly share code, notes, and snippets.

@brownman
Last active September 7, 2015 10:06
Show Gist options
  • Save brownman/f9680887560dad5859f4 to your computer and use it in GitHub Desktop.
Save brownman/f9680887560dad5859f4 to your computer and use it in GitHub Desktop.
Script creates bootable usb from windows iso, tested on Ubuntu 15.04 with Windows 8 and Windows 10 iso files on BIOS and UEFI machines.
#!/bin/sh
# Requirements:
# parted - to make gpt and partion
# sfdisk - to make mbr and partition over gpt
# blkid - to get UUID of partition for grub
# grub-pc-bin - files to install grub from UEFI-booted machines to MBR
usb="/dev/sdX" # path to usb device file (without number at end) sudo fdisk -l
iso="/path/to/windows.iso" # path to windows iso
usb_mount="/mnt/usb"
iso_mount="/mnt/iso"
mbr=true # mbr=true creates BIOS bootable, if not, then UEFI bootable
# Find and unmount all partitions of device
UmountUSB () {
mounts=`mount -l | grep "${usb}" | grep -o "^\S*"`
for i in ${mounts}
do
sudo umount ${i}
done
}
UmountUSB
if [ "${mbr}" = "true" ]
then
# Create MBR and bootable FAT32 partition starting at 2048 sector.
sudo parted -s ${usb} mklabel msdos
sudo parted -s ${usb} mkpart primary fat32 2048s 100%
sudo sfdisk --activate=1 ${usb}
else
# Create GPT and FAT32 partition starting at 2048 sector.
sudo parted -s ${usb} mklabel gpt
sudo parted -s ${usb} mkpart primary fat32 2048s 100%
fi
# Format partition
sudo mkfs.vfat -F 32 ${usb}1
# Mount ISO and USB
sudo mkdir -p ${usb_mount}
sudo mount ${usb}1 ${usb_mount}
sudo mkdir -p ${iso_mount}
sudo mount ${iso} ${iso_mount} -o loop
# Copy Windows files
echo "Copying Windows files ..."
sudo cp -r ${iso_mount}/* ${usb_mount}
if [ "${mbr}" = "true" ]
then
# Install GRUB
sudo grub-install --target=i386-pc \
--boot-directory="${usb_mount}/boot" \
--force \
${usb}
# Find partition UUID by blkid
uuid=`sudo blkid | grep "${usb}1" | sed -n 's/.*\sUUID=\"\([^\"]*\)\".*/\1/p'`
# Create GRUB conf pointing to UUID of partition
config="${usb_mount}/boot/grub/grub.cfg"
echo "echo \"Booting from USB\"
insmod part_msdos
insmod fat
insmod ntldr
search --no-floppy --fs-uuid --set=root ${uuid}
ntldr (\$root)/bootmgr
boot
" | sudo tee ${config} > /dev/null
fi
# Unmount ISO and USB
sudo umount ${usb_mount} ${iso_mount}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment