# Generates the sdcard partitions for Raspberry Pi sdcard
# Thomas Bohm <thomas.bohm@cs.upt.ro>
# Based on scripts by:
# Graeme Gregory <dp@xora.org.uk>
# Edward Lin <linuxfae@technexion.com>
# This script is GPLv3 licensed!
#!/bin/bash
SPREF="/sys/class/block"
RBP_DIR="Raspberry Pi"
GPU_DIR="GPU partition"
DRIVE=""
devicearray[0]=""
if [ "`whoami`" != "root" ]; then
echo "Must be root"
exit 1
fi
clear
echo "List of removable devices smaller than 10GB : "
echo
devindex=1
for ii in $SPREF/sd?/removable ; do
if [ `cat $ii` == 1 ]; then
devfound=`echo $ii | awk -F "/" {'print $5 '}`
dev_model=`cat $SPREF/$devfound/device/model`
dev_vendor=`cat $SPREF/$devfound/device/vendor`
dev_size=$((`cat $SPREF/$devfound/size`/1024/2))
if [ "$dev_size" -gt "10000" ]; then
continue;
fi
echo "[$devindex]: $dev_vendor -- $dev_model ( $devfound ) size is $dev_size MBytes. "
devicearray[$devindex]=$devfound
devindex=$(($devindex+1))
fi
done
if [ "$devindex" == "1" ] ; then
echo
echo "No removable devices found !"
exit 1
fi
echo
while [ "$DRIVE" == "" ]; do
echo -n "Input device number: "
read choice
if [ "${devicearray[${choice}]}" == "" ]; then
echo
echo "Invalid number ! "
echo
else
DRIVE=${devicearray[${choice}]}
fi
done
echo
echo "Are you 100 % sure you want to FORMAT [ $DRIVE ] ?. All data will be lost."
echo -n "Enter YES in uppercase to continue: "
read sure
echo
if [ "$sure" != "YES" ]; then
exit 1
fi
echo
echo "Umounting all partitions of $DRIVE."
for ii in $SPREF/$DRIVE? ; do
umount /dev/`echo $ii | awk -F "/" {'print $5 '}` 2>/dev/null
done
echo "Partitioning"
#clear the beginning of the card
dd if=/dev/zero of=/dev/$DRIVE bs=1024 count=1024
#install a blank mbr (blank from the partitions point of view)
# dd if=blank_mbr.img of=/dev/$DRIVE bs=512 count=1
SIZE=`fdisk -l /dev/$DRIVE | grep Disk | awk '{print $5}'`
echo DISK SIZE – $SIZE bytes
CYLINDERS=`echo $SIZE/255/63/512 | bc`
echo CYLINDERS – $CYLINDERS
{
#create a new partition table
#ext partition for linux, bootable
# start, size, type ( default 0x83 - linux , 0x0C FAT32 ), bootable ( * yes, - no )
echo ,19,0x83,*
#FAT 32 partition filling rest of card, maybe for /tmp ?
echo ,,0x0C,-
} | sfdisk -D -H 122 -S 62 -C $CYLINDERS /dev/$DRIVE
##############################################
umount /dev/${DRIVE}1
umount /dev/${DRIVE}2
echo "Formatting partition as EXT"
mke2fs -F -L "$MNT_DIR" /dev/${DRIVE}1
mkfs.vfat -F 32 -n "$GPU_DIR" /dev/${DRIVE}2
sync
echo "Done. Please remove the card."