Skip to content

Instantly share code, notes, and snippets.

@sstelfox
Created January 2, 2014 15:21
Show Gist options
  • Save sstelfox/8220773 to your computer and use it in GitHub Desktop.
Save sstelfox/8220773 to your computer and use it in GitHub Desktop.
This was a helper for formatting SD cards in a very specific way. Cleaned this off of a laptop and shared here for posterity.
#!/bin/bash
if [[ "$1" == "" ]]; then
echo "You need to provide the device to operate on"
exit 1
fi
echo 'Blowing away any partition tables...'
#dd if=/dev/zero of=$1 bs=1k count=1024
echo 'Syncing disk...'
#sync
echo 'Calculating disk architecture...'
#HEADS=$(fdisk -l $1 | grep -oE "^[0-9]+ heads" | grep -oE "[0-9]+")
DISK_BYTE_COUNT=$(fdisk -l $1 | grep Disk | grep -oE "[0-9]+ bytes$" | grep -oE "[0-9]+")
FORCED_SECTOR_COUNT=$(echo "($DISK_BYTE_COUNT / (255 * 63 * 512))" | bc)
echo "Disk has $DISK_BYTE_COUNT bytes"
echo "Disk will now have $FORCED_SECTOR_COUNT sectors"
echo "Beginning sfdisk forced architecture change..."
# Removed -D flag, try -E in it's place
# force sfdisk to do it's thing (--force), use sectors as the unit (-uS),
# specify the number of heads regardless of what the kernel thinks (-H 255),
# specify the number of sectors per track regardless of what the kernel thinks
# (-S 63), specify the number of cylinders regardless of what the kernel thinks
# (-C $FORCED_SECTOR_COUNT)
#echo -e "1,409600,0xc0,*\n409601,,," | sfdisk --force -uS -H 255 -S 63 -C $FORCED_SECTOR_COUNT $1
echo -e "1,126975,0xc0,*\n126976,21098496,0xc0," | sfdisk --force -uS -H 255 -S 63 -C $FORCED_SECTOR_COUNT $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment