Advertisement
adamchilcott

fixUSB.sh

Oct 13th, 2018
544
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.02 KB | None | 0 0
  1. #!/bin/sh
  2. # This small script will fix the "missing space" caused by dd-ing fatdog isohybrid to a flash drive
  3. # making the rest of the space available again for use.
  4. # Only run this after dd-ing fatdog iso and not after anything else.
  5. # © jamesbond 2013
  6.  
  7. ### configuration
  8. RESERVED_SPACE=786432   # reserve this amount of sectors by default, it will be made larger if necessary.
  9.  
  10. ### can't run from X to prevent accident
  11. if ! [ -t 0 ]; then
  12.     Xdialog --title "Error!" --infobox "Please run this from console." 0 0 10000
  13.     exit
  14. fi
  15.  
  16. ### usage
  17. if [ -z "$1" ]; then
  18.     cat << EOF
  19. This is a small tool to make the extra space that was lost when you dd
  20. fatdog.iso to a USB flash drive to be usable again.
  21.  
  22. Usage: $0 /dev/xxx
  23.  
  24. Where xxx is the your USB flash drive (e.g. sdb, sdc etc).
  25.  
  26. Warning: Please make sure you pass the correct device, if you pass the wrong
  27. one you can EASILY DESTROY your harddisk irrecoverably.
  28.  
  29. EOF
  30.     exit
  31. fi
  32. DEV=${1##*/}
  33.  
  34. ### paranoia check - make sure sfdisk exist
  35. if ! which sfdisk > /dev/null; then
  36.     echo "This tool needs to use sfdisk"
  37.     echo "Aborting."
  38.     exit
  39. fi
  40.  
  41. ### paranoia check - make sure we have rights to write to it
  42. if ! [ -w $1 ]; then
  43.     echo "You don't have the rights to write to $1"
  44.     echo "Become root or become a member of the disk group first."
  45.     echo "Aborting."
  46.     exit
  47. fi
  48.  
  49. ### paranoia check - block device must exist
  50. if ! [ -e /sys/block/$DEV ]; then
  51.     echo "$1 isn't a block device. Please specify the block device (e.g. /dev/sdb)"
  52.     echo "and NOT the partition (e.g. /dev/sdb1)"
  53.     echo "Aborting."
  54.     exit
  55. fi
  56.  
  57. ### paranoia check - must be removable
  58. read check < /sys/block/$DEV/removable
  59. if [ $check -eq 0 ]; then
  60.     echo "$1 is non-removable, I don't think you want to do this."
  61.     echo "Aborting."
  62.     exit
  63. fi
  64.  
  65. ### paranoia check - must be symlinked to "usb" bus
  66. case $(readlink -f /sys/block/$DEV) in
  67.     */usb[0-9]*) ;;
  68.     *)  echo "$1 is not a USB device."
  69.         echo "Aborting."
  70.         exit
  71. esac
  72.  
  73. ### paranoia check - partition 3 must be empty
  74. check=$(sfdisk -uS -l $1 2>/dev/null| awk -v dev=${1}3 '$1==dev {print $6}')
  75. case $check in
  76.     Empty) ;;
  77.     *)  echo "Partition 3 of $1 (${1}3) is not empty."
  78.         echo "Aborting."
  79.         exit
  80. esac
  81.  
  82. ### ask filesystem type
  83. cat << EOF
  84. Specify filesystem type (in hex number). These are common ones:
  85. L  - linux (ext2/3/4)
  86. b  - FAT32
  87. 7  - NTFS or exFAT
  88. ef - UEFI boot partition
  89. Default is FAT32
  90. EOF
  91. read partition
  92. if [ -z "$partition" ]; then
  93.     echo "No partition type is supplied, will assume FAT32"
  94.     partition=b
  95. fi
  96. echo You choose \"$partition\" as the type.
  97.  
  98. ### check reserved space
  99. ACTUAL_USED=$(sfdisk -uS -l $1 2>/dev/null | awk '/\*/ {print $4}')
  100. if [ $RESERVED_SPACE -lt $ACTUAL_USED ]; then
  101.     RESERVED_SPACE=$(( (($ACTUAL_USED/4096)+4)*4096 )) # round up to next nearest 4096
  102. fi
  103. echo "Reserving space for $RESERVED_SPACE sectors."
  104.  
  105. ### all checks go, one more time to confirm
  106. read -p "Last chance to abort - are you sure to you want to fix $1 [y/N]? " check
  107. case "$check" in
  108.     y|Y|yes|Yes|YES) ;;
  109.     *)  echo "Aborting."
  110.         exit ;;
  111. esac
  112.  
  113. ### create the partition
  114. echo "$RESERVED_SPACE,$(( $(sfdisk -s $1 2>/dev/null) * 2 - $RESERVED_SPACE)),$partition" | sfdisk -f -N3 -uS $1
  115.  
  116. ### final message
  117. cat << EOF
  118.  
  119. Done. Now you need to make filesystem in it. How to do it depends on the
  120. filesystem you have chosen.
  121. For FAT32, do "mkdosfs ${1}3"
  122. For NTFS,  do "mkntfs ${1}3"
  123. For exFAT, do "mkfs -t exfat ${1}3"
  124. For Linux, do "mkfs -t ext4 ${1}3" (or ext3 or ext2 as you wish)
  125. For other filesystem - I assume you know what you're doing :)
  126.  
  127. If you already have a previous filesystem there (ie you dd fatdog.iso to
  128. a USB flash drive that has previously been "fixed"), you may not need to
  129. format it.
  130.  
  131. If you need to format it, after formatting type "sfdisk -R $1" to refresh
  132. the drive icons.
  133.  
  134. === THE END ===
  135.  
  136. EOF
  137.  
  138. #############
  139. # START NOTES
  140. #############
  141.  
  142. ## Reference:
  143. ## <http://www.oilshell.org/release/0.3.0/test/wild.wwz/distro/woof-CE/woof-code/rootfs-skeleton/usr/sbin/fix-usb.sh.txt>
  144.  
  145. ###########
  146. # END NOTES
  147. ###########
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement