7
May

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
0,407
,407
;
;
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:

#!/bin/sh
# Example Usage: create_sd /dev/sdb

DRIVE=$1

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 CYLINDERS - $CYLINDERS

{
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

  • training beagles
    December 30th, 2010 at 3:43 pm

    […] sfdisk: The Scriptable fdisk | The Linux Daily […]

  • Reni
    March 28th, 2011 at 1:26 am

    Why do you use fdisk to return the size to calculate the cylinders?
    You could aswel use CYLINDERS=`sfdisk -g $DISK|awk ‘{print $2-1}’`

  • gKsam
    March 21st, 2012 at 8:44 am

    Why do you use fdisk to return the size to calculate the cylinders?

    SIZE=`fdisk -l $DRIVE | grep Disk | awk ‘{print $5}’` don’t work if your disk doesn’t contain a valid partition.

  • David Evans
    March 18th, 2013 at 3:41 am

    Is it possible to script sfdisk in such a way that it will not run a script on a device of more than 8GB?

    I am seeing many customers trash their host OS, when they select the wrong drive during the preparation of SD cards for embedded Linux systems.

    If there was a script that would automatically halt if the disk under examination was greater than 8GB, it would save a lot of wailing…

    TTFN.

    David

  • [email protected]
    March 18th, 2013 at 5:18 pm

    If it’s just a simple bash script, it would be pretty easy to come up with the disk size like df or fdisk -l | grep Disk, use cut or awk to parse the output, and then store that into a variable. Then, it’s just a matter of saying if [ $disksize -lt 8589934592 ]; then #.

  • Harvey
    May 10th, 2013 at 12:56 am

    I changed the cylinders line to the following so I can use the script with busybox.

    CYLINDERS=””
    if [ “$CYLINDERS” = “” ] && which bc >/dev/null 2>&1; then
    CYLINDERS=`echo $SIZE/255/63/512 | bc`
    fi
    if [ “$CYLINDERS” = “” ] && which dc >/dev/null 2>&1; then
    CYLINDERS=$(echo “$SIZE 255 / 63 / 512 / p” | dc | sed -e ‘s,\..*$,,’)
    fi
    if [ “$CYLINDERS” = “” ]; then
    echo “Failed to calculate CYLINDERS=$SIZE/255/63/512”
    exit 1
    fi

Share your thoughts, leave a comment!