grow-sd: Script for Resizing SD Card Partitions Using sfdisk

The purpose of this example script, which we call grow-sd , is to demonstrate a use case for using sfdisk, or what I like to call ‘scripted fdisk’, to modify a partition on an SD card provided by Technologic Systems.  This is useful when your production SD card image, originally copied from a 512 MB SD card, is being written to a much larger capacity SD card, and you want the additional storage space.  Keep in mind, the script itself is specific to Technologic Systems’ embedded boards, and only applies to those images which are shipped with four partitions (ie. TS-7350).  However, this script also generically demonstrates and example usage of sfdisk .  We thought it’d be helpful to publish in hopes it would be useful.  Feel free to modify it to fit your requirements.

Further notes:  This script mentions support for JFS, which is a special beast in that partitions can only be grown and not shrunk and even then, JFS does some hackery to grow the partition.  The TS-7800, for example, uses JFS, so make sure you test on a development board before applying to production processes.

#!/bin/sh
# This script will detect the last JFS or EXT3 partition of an SD card image
# provided by Technologic Systems and expand it to the end of the disk,
# leaving a small amount of padding.  It depends on tools such as sfdisk,
# which is part of core utilities of Linux, as well as and jfsutils if working
# with JFS filesystem.
# 
# In order to use the script, you will need to insert the SD card into the USB
# SD card reader and then use the 'fdisk -l' command to find the device node.
# The device node is typically enumerated as /dev/sdX, where X is what Linux
# decided.  Example usage is:  './grow-sd /dev/sdb'.
#
# Since this deals with modifying the partition table of the SD card and data
# may be lost, for this reason, you will be prompted if you want to proceed.
#
# Please understand that this script is only provided in the hopes that it will
# be useful to Technologic Systems customers and does not have any guarantees
# or warranties.  If you require additional assistance, custom engineering is
# available.  Take care and understand what you are doing!  If you're not 
# comfortable in running this script, then perhaps 'gparted' can be used.
#
# This script was tested by writing a factory 512MB SD image for the TS-7350 to
# a 2GB SD card in both JFS and EXT3 filesystem formats.

# CHECK FOR ROOT ACCESS
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# CHECK FOR COMMAND LINE ARGUMENT
if [ $# -lt 1 ]; then
   echo "Example Usage: $0 /dev/sdb"
   exit 1
fi

# INITIALIZE VARIABLES
TYPE=none

# CHECK DISK AND PARTITIONS
if [ -e $1 ]; then
        DISK=$1
        NUMBER=`fdisk -l $DISK | grep 83 | awk '{print $1}' | sed "s,$DISK,,g"`
        if [ -e $NUMBER ]; then
                echo "Valid TS partition not found!"
                echo "Exiting"
                exit 1
        fi
else
        echo "No disk found at ${1}!"
        echo "Exiting"
        exit 1
fi

expand_sd(){
        mkdir /mnt/tmp 2> /dev/null
        mount ${DISK}$NUMBER /mnt/tmp 2> /dev/null
        TYPE=`df -T | grep ${DISK}$NUMBER | awk '{print $2}'`
        umount ${DISK}$NUMBER 2> /dev/null

        ORIG=`fdisk -l $DISK | grep ${DISK}$NUMBER | awk '{ print $3 }'`
        SIZE=`fdisk -l $DISK | grep Disk | awk '{print $5}'`
        UNITS=`fdisk -l $DISK | grep Units | awk '{print $9}'`
        END=`echo "($SIZE/$UNITS)-5" | bc`

        if [ $ORIG -gt $END ]; then
                echo "Original ($ORIG) is larger than end goal ($END)!"
                echo "Exiting"
                exit
        else
                echo -n "Expanding ${DISK}$NUMBER from $ORIG to $END... "
                (
                        {
                                echo ,$END,0x83,-
                        } | sfdisk -f $DISK -N$NUMBER 
                ) >/dev/null 2>&1 </dev/null
                echo "done"
        fi
}

echo
echo "This program was created in hopes that it will be useful to Technologic "
echo "Systems customers.  There are no guarantees or warranties.  Please backup"
echo "your data before running this. Also, keep in mind that the JFS filesystem"
echo "can grow, but cannot shrink again." 
echo -e "Do you wish to proceed <y or n>? \c"
read WISH

if [ $WISH = "n" ] ; then
        echo "Exiting" 
        exit 1
fi
echo "Proceeding..."

expand_sd

if [ $TYPE == "jfs" ]; then
        # CHECK DEPENDENCIES
        which jfs_mkfs >/dev/null 2>&1 </dev/null
        jfsutils=$?

        if [ $jfsutils -eq 1 ]; then
                echo "Missing dependency jfsutils!"
                echo "You may be able to install them using 'apt-get install jfsutils'"
                echo "Exiting"  
                exit 1
        fi

        echo -n "Resizing JFS disk ${DISK}$NUMBER to fill new size of the partition... "
        (
                mkdir /mnt/tmp
                mount ${DISK}$NUMBER /mnt/tmp
                mount -o remount,resize /mnt/tmp
                umount ${DISK}$NUMBER
        ) >/dev/null 2>&1 </dev/null
        echo "done"
elif [ $TYPE == "ext3" ]; then
        echo -n "Resizing EXT3 disk ${DISK}$NUMBER to fill new size of the partition... "
        (
                e2fsck -f ${DISK}$NUMBER
                resize2fs ${DISK}$NUMBER
        ) >/dev/null 2>&1 </dev/null
        echo "done"
elif [ $TYPE == "none" ]; then
        echo "Valid partition not found!"
        echo "Exiting"
        exit 1
else
        echo "Only JFS and EXT3 filesystem types are supported!"
        echo "Exiting"
        exit 1
fi

 

Similar Posts:

    None Found

Author: Derek Hildreth

eBusiness Manager for Technologic Systems