Advertisement
corrosiontears

Format Entire Disk FAT32 Script (otimização de Fat32)

Mar 18th, 2016
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.83 KB | None | 0 0
  1. #! /bin/sh
  2. # fdisk portion of script based on mkcard.sh v0.4
  3. # (c) Copyright 2009 Graeme Gregory <dp@xora.org.uk>
  4. # Additional functionality by Steve Sakoman
  5. # (c) Copyright 2010-2011 Steve Sakoman <steve@sakoman.com>
  6. # Licensed under terms of GPLv2
  7. #
  8. # Parts of the procudure base on the work of Denys Dmytriyenko
  9. # http://wiki.omap.com/index.php/MMC_Boot_Format
  10. # Official Site: http://3gfp.com/wp/2014/07/formatting-sd-cards-for-speed-and-lifetime/
  11.  
  12. export LC_ALL=C
  13.  
  14. format_whole_disk_fat32() {
  15.     if ! id | grep -q root; then
  16.         echo "This utility must be run prefixed with sudo or as root"
  17.         return 1
  18.     fi
  19.  
  20.     local DRIVE=$1
  21.     local PART1=$2
  22.  
  23.     #make sure that DRIVE isn't mounted before we start
  24.     if [ -b ${DRIVE}1 ]; then
  25.         umount ${DRIVE}1
  26.         umount ${DRIVE}2
  27.     elif [ -b ${DRIVE}p1 ]; then
  28.         umount ${DRIVE}p1
  29.         umount ${DRIVE}p2
  30.     else
  31.         umount ${DRIVE}
  32.     fi
  33.  
  34.     # Erase first 8MiB of DRIVE
  35.     dd if=/dev/zero of=$DRIVE bs=1024 count=8192
  36.  
  37.     # Get disk size in bytes
  38.     local SIZE=$(fdisk -l $DRIVE | grep Disk | grep bytes | awk '{print $5}')
  39.     echo DISK SIZE – $SIZE bytes
  40.  
  41.     # Note: I'm changing our default cluster size to 32KiB since all of
  42.     # our 8GiB cards are arriving with 32KiB clusters. The manufacturers
  43.     # may know something that we do not *or* they're trading speed for
  44.     # more space.
  45.     local CLUSTER_SIZE_KB=32
  46.     local CLUSTER_SIZE_IN_SECTORS=$(( $CLUSTER_SIZE_KB * 2 ))
  47.  
  48.     # This won't work for drives bigger than 32GiB because
  49.     # 32GiB / 64kiB clusters = 524288 FAT entries
  50.     # 524288 FAT entries * 4 bytes / FAT = 2097152 bytes
  51.     # 2097152 bytes / 512 bytes = 4096 sectors for FAT size
  52.     # 4096 * 2 = 8192 sectors for both FAT tables which leaves no
  53.     # room for the BPB sector
  54.     if [ $SIZE -ge $(( ($CLUSTER_SIZE_KB / 2) * 1024 * 1024 * 1024 )) ]; then
  55.         echo -n "This drive is too large, >= $(($CLUSTER_SIZE_KB / 2))GiB, for this "
  56.         echo "formatting routine."
  57.         return 1
  58.     fi
  59.  
  60.     # Calculate number of cylinders based on "255 heads, 63 sectors/track"
  61.     local CYLINDERS=""
  62.     if [ "$CYLINDERS" = "" ] && which bc >/dev/null 2>&1; then
  63.         CYLINDERS=$(echo $SIZE/255/63/512 | bc)
  64.     fi
  65.     if [ "$CYLINDERS" = "" ] && which dc >/dev/null 2>&1; then
  66.         CYLINDERS=$(echo "$SIZE 255 / 63 / 512 / p" | dc | sed -e 's,\..*$,,')
  67.     fi
  68.     if [ "$CYLINDERS" = "" ]; then
  69.         echo "Failed to calculate CYLINDERS=$SIZE/255/63/512"
  70.         return 1
  71.     fi
  72.     echo CYLINDERS – $CYLINDERS
  73.  
  74.     # Align partitions for SD card performance/wear optimization
  75.     # Summary: start 1st partition at sector 8192 (4MiB) and align FAT32
  76.     #          data to start at 8MiB (4MiB logical)
  77.     #          There's a document that explains why, but its too long to
  78.     #          reproduce here.
  79.     {
  80.     echo 8192,,0x0C,*
  81.     } | sfdisk --force -D -uS -H 255 -S 63 -C $CYLINDERS -q $DRIVE
  82.  
  83.     sleep 1
  84.  
  85.     if [ "$PART1" = "" ]; then
  86.         if [ -b ${DRIVE}1 ]; then
  87.             PART1=${DRIVE}1
  88.         elif [ -b ${DRIVE}p1 ]; then
  89.             PART1=${DRIVE}p1
  90.         else
  91.             echo "Improper partitioning on $DRIVE"
  92.             return 1
  93.         fi
  94.     fi
  95.  
  96.     # Format FAT32 with 64kiB clusters (128 * 512)
  97.     # Format once to get the calculated FAT size
  98.     local FAT_SIZE=$(mkdosfs -F 32 -s $CLUSTER_SIZE_IN_SECTORS -n boot -v ${PART1} | \
  99.         sed -n -r -e '/^FAT size is/ s,FAT size is ([0-9]+) sectors.*$,\1,p')
  100.  
  101.     # Calculate the number of reserved sectors to pad in order to align
  102.     # the FAT32 data area to 4MiB
  103.     local RESERVED_SECTORS=$(( 8192 - 2 * $FAT_SIZE ))
  104.  
  105.     # Format again with padding
  106.     mkdosfs -F 32 -s $CLUSTER_SIZE_IN_SECTORS -n boot -v -R $RESERVED_SECTORS ${PART1}
  107. }
  108.  
  109. #set -x
  110.  
  111. format_whole_disk_fat32 "$@"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement