ISO Image to USB Stick


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/bin/bash
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; version 2 of the License.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU Library General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software
  14. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. #
  16. # $Id$
  17.  
  18. export PATH=/sbin:/usr/sbin:/bin:/usr/bin
  19.  
  20. err() {
  21. echo "$*" 1>&2
  22. exit 1
  23. }
  24.  
  25. usage() {
  26. err "Usage: $0 [--reset-mbr ] <isopath> <usb device>"
  27. }
  28.  
  29. if [ $(id -u) != 0 ]; then
  30. err "You need to be root to run this script"
  31. fi
  32.  
  33. if [ ! -x /usr/bin/syslinux ]; then
  34. err "syslinux needs to be installed for this script to run"
  35. fi
  36.  
  37. while [ $# -gt 2 ]; do
  38. case $1 in
  39. --reset-mbr)
  40. resetmbr="yes"
  41. ;;
  42. *)
  43. usage
  44. ;;
  45. esac
  46. shift
  47. done
  48.  
  49. cleanup() {
  50. [ -d "${ISOMNT}" ] && umount "${ISOMNT}" && rmdir "${ISOMNT}"
  51. [ -d "${USBMNT}" ] && umount "${USBMNT}" && rmdir "${USBMNT}"
  52. }
  53.  
  54. checkfs() {
  55. device=$1
  56. usbfs=$(/lib/udev/vol_id -t $device)
  57. case "${usbfs}" in
  58. vfat|msdos|ext[23])
  59. ;;
  60. *)
  61. err "USB filesystem must be vfat/ext2/ext3"
  62. ;;
  63. esac
  64. }
  65.  
  66. resetmbr() {
  67. device=$1
  68. sysdev=$(udevadm info -q path -n ${device})
  69. if [ -e "/sys/${sysdev}/device" ]; then
  70. dev=$(basename /sys${sysdev})
  71. else
  72. dev=$(basename $(readlink -f /sys${sysdev}/../))
  73. fi
  74. if [ ! -e /sys/block/${dev} -o ! -e /dev/${dev} ]; then
  75. err "Error finding ${dev} block device. Bailing out"
  76. fi
  77. dev=/dev/$dev
  78. if [ -f /usr/lib/syslinux/mbr.bin ]; then
  79. cat /usr/lib/syslinux/mbr.bin > $dev
  80. elif [ -f /usr/share/syslinux/mbr.bin ]; then
  81. cat /usr/share/syslinux/mbr.bin > $dev
  82. else
  83. err "Unable to find syslinux mbr"
  84. fi
  85. }
  86.  
  87. copyfiles() {
  88. echo "Copying files..."
  89. for item in ${COPYLIST}; do
  90. cp -af ${ISOMNT}/${item} ${USBMNT}
  91. done
  92. if [ ! -d ${USBMNT}/syslinux ]; then mkdir ${USBMNT}/syslinux; fi
  93. cp ${ISOMNT}/isolinux/* ${USBMNT}/syslinux
  94. }
  95.  
  96. sanitychecks() {
  97. if [ -z "${ISOIMAGE}" -o ! -f "${ISOIMAGE}" ]; then
  98. usage
  99. fi
  100. if [ -z "${USBDEV}" -o ! -b "${USBDEV}" ]; then
  101. usage
  102. fi
  103. }
  104.  
  105. install_syslinux() {
  106. device=$1
  107. echo "Updating boot config"
  108. # Figure out the filesystem and setup (sys|ext)linux accordingly
  109. usbfs=$(/lib/udev/vol_id -t $device)
  110. echo "Installing bootloader on ${device}"
  111. case $usbfs in
  112. msdos|vfat)
  113. mv ${USBMNT}/syslinux/isolinux.cfg ${USBMNT}/syslinux/syslinux.cfg
  114. syslinux -d syslinux ${device}
  115. ;;
  116. ext[23])
  117. mv ${USBMNT}/syslinux/isolinux.cfg ${USBMNT}/syslinux/extlinux.conf
  118. extlinux -i ${USBMNT}/syslinux
  119. ;;
  120. esac
  121. # TODO: Mark the partition bootable.
  122. # parted $device toggle N boot
  123. }
  124.  
  125. ISOIMAGE=$1
  126. USBDEV=$2
  127. ISOMNT=$(mktemp -d /tmp/iso.XXXXXXXXXX)
  128. USBMNT=$(mktemp -d /tmp/usb.XXXXXXXXXX)
  129.  
  130. # Files needed from the iso
  131. COPYLIST="casper dists pool preseed .disk README.diskdefines md5sum.txt install"
  132.  
  133. sanitychecks
  134. checkfs ${USBDEV}
  135. if [ "$resetmbr" == "yes" ]; then resetmbr ${USBDEV}; fi
  136. mount -o loop $ISOIMAGE $ISOMNT
  137. mount $USBDEV $USBMNT
  138.  
  139. trap cleanup EXIT
  140. copyfiles
  141. install_syslinux ${USBDEV}
  142.  
  143. echo "Done!"
  144. exit 0

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.