sfdisk: The Scriptable fdisk

There was a brief mention of scripting partition editing in my post a couple of days ago about setting up an SD card for a Beagle Board. I revisited the script that was used to prepare the SD card partitions, took a closer look at it, and figured it was important enough to give it a mention in it’s own post — sfdisk.

Simply put, sfdisk is a scriptable fdisk which allows you to automate steps taken to create a partition layout. Here’s the standard blurb from the man page:

sfdisk has four (main) uses: list the size of a partition, list the partitions on a device, check the partitions on a device, and – very dangerous – repartition a device.

sfdisk accepts commands from stdin and the format is mostly comma separated for each field that is available which is:

<start> <size> <id> <bootable> <c,h,s> <c,h,s>

Be sure to check the man page for more information (as always). One quick example is presented in the man page:

sfdisk /dev/hdc << EOF

Since the Beagle Board SD card setup script by XorA gave such a great usage example, I’d like to present it here again in case you missed it, except in a minimal form to avoid distractions:

# Example Usage: create_sd /dev/sdb


dd if=/dev/zero of=$DRIVE bs=1024 count=1024

SIZE=`fdisk -l $DRIVE | grep Disk | awk '{print $5}'`

echo DISK SIZE - $SIZE bytes

CYLINDERS=`echo $SIZE/255/63/512 | bc`


echo ,9,0x0C,*
echo ,,,-
} | sfdisk -D -H 255 -S 63 -C $CYLINDERS $DRIVE

mkfs.vfat -F 32 -n "boot" ${DRIVE}1
mke2fs -j -L "rootfs" ${DRIVE}2

Have fun with it! While you’re at it, you may want to check out the parted command with the --script option. It’s very handy if you work with disk partitions frequently.

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:

There's 6 Comments So Far

Share your thoughts, leave a comment!