====== Convert W7 ISO to USB key ====== http://github.com/abock/win7-usb/blob/master/win7-iso-to-usb.sh Linux script to convert the Windows 7 ISO to a USB key for easier and faster installation of #!/usr/bin/env bash # # win7-iso-to-usb.sh # Written by Aaron Bockover # Copyright 2009 Aaron Bockover # Licensed under the MIT/X11 license # function usage { echo "Written by Aaron Bockover ." echo "Copyright 2009 Aaron Bockover" echo "Licensed under the MIT/X11 license" echo echo "Usage: $0 ISO [TARGET DEVICE]" echo echo " ISO Path to the Windows 7 ISO file" echo echo " TARGET DEVICE Target USB block device node. This argument" echo " is optional if devkit-disks is installed." echo echo " Omitting this argument will present a list" echo " of available target USB block devices." echo } function check-program { full_path="$(which "$1" 2>/dev/null)" if ! test -x "$full_path"; then usage echo "You must have $1 installed and in your PATH." exit 1 fi } function dk-enum-device-files { devkit-disks --dump | grep device-file: | cut -f2 -d: } function dk-get-device-field { devkit-disks --show-info $1 | awk -F: '/'"$2"':[ ]*/{ sub (/^[ ]*/, "", $2); sub (/[ ]*$/, "", $2); print $2 }' } function dk-compare-device-field { test "$(dk-get-device-field $1 "$2")" $3 "$4" } function dk-id-for-device-file { devkit-disks --show-info $1 | head -n1 | awk -F' ' '{print $NF}' } function select-target-device { declare -a devices=() device_count=0 echo Select a target USB device: echo for device in $(dk-enum-device-files); do if dk-compare-device-field $device removable -eq 1 && \ dk-compare-device-field $device interface = usb; then devkit_id=$(dk-id-for-device-file $device) display_name=$(dk-get-device-field $device model) echo " 1) $display_name [$device]" devices[$device_count]="$device:" for child_device in $(dk-enum-device-files); do if dk-compare-device-field $child_device \ "part of" = $devkit_id && \ dk-compare-device-field $child_device "is mounted" -eq 1; then mount_path=$(dk-get-device-field $child_device "mount paths") devices[$device_count]="${devices[$device_count]}$child_device " echo " $child_device mounted at $mount_path" fi done let device_count++ fi done if test $device_count -eq 0; then echo " No available devices" exit 0 fi echo ; read -p "Device number (1): " device_index ; echo test -z $device_index && device_index=1 let device_index-- if test $device_index -lt 0 || test $device_index -ge $device_count; then echo Invalid device selection. exit 1 fi device="${devices[$device_index]}" device_file="$(echo "$device" | cut -f1 -d:)" device_mounts="$(echo "$device" | cut -f2 -d:)" if ! test -z "$device_mounts"; then echo "Device $device_file has mounted partitions." read -p "Unmount all partitions on $device_file? [Y/N]: " unmount_device case "$unmount_device" in Y|y) ;; N|n) echo "Cannot continue with mounted partitions."; exit 1 ;; *) echo "Invalid choice."; exit 1 ;; esac for mount in $device_mounts; do echo "Unmounting $mount..." if ! umount $mount; then echo "Failed to unmount $mount" exit 1 fi done fi } if test $# -le 0; then usage exit 1 fi if test $UID -ne 0; then usage echo "You must be root to run this tool." exit 1 fi check-program ntfs-3g check-program sfdisk check-program mount check-program umount check-program mkntfs iso_path="$1" if ! test -f "$iso_path"; then usage echo "You must specify a path to the Windows 7 ISO." exit 1 fi if test -e "$2"; then device_file=$2 else if test -x $(which devkit-disks 2>/dev/null); then select-target-device else usage echo You do not have devkit-disks installed. Without this tool echo the target device must be specified as the second argument echo to this script. echo echo Ensure there are no mounted partitions on the device first. echo exit 1 fi fi ## Windows 7 Installation ## read -p "WARNING: All data on $device_file will be DESTROYED. Continue? [Y/N]: " destroy_device case "$destroy_device" in Y|y) ;; N|n) echo "Quitting. $device_file has not been touched."; exit 1 ;; *) echo "Invalid choice."; exit 1 ;; esac echo # Create mount points for later use win7_iso_mount=$(mktemp -d /tmp/win7-iso.XXXX) win7_usb_mount=$(mktemp -d /tmp/win7-usb.XXXX) if ! test -d "$win7_iso_mount" || ! test -d "$win7_usb_mount"; then echo "Could not create mount directories." exit 1 fi # From here on we will just bail if something fails set -e # Partition the USB key with a bootable NTFS # partition that consumes the entire disk echo "Creating partitions on $device_file..." sfdisk $device_file &>/dev/null </dev/null sync echo Done.