1
0
mirror of git://projects.qi-hardware.com/xburst-tools.git synced 2024-11-22 18:47:10 +02:00

My revised version of the partition tool

The fdisk and mkfs was factored out into a shell function
so that it is easier to create dirfferent partition layouts.

The default case was left the same as the original script

The partitions can be specified as percentages of the
available space (after Qi reserved space has been taken out)
by providing a comma separated list of values:
e.g. 25,0,50
Which would create partitions 1 and 3 with 25% and 50%
of the usable capacity. Partitions 2 and 4 would not be
assigned.

Signed-off-by: Christopher Hall <hsw@openmoko.com>
This commit is contained in:
Christopher Hall 2009-03-11 07:32:15 +00:00 committed by Andy Green
parent 8b75ed7105
commit 683fc5a92a

View File

@ -3,7 +3,7 @@
# (C) 2008 Openmoko, Inc # (C) 2008 Openmoko, Inc
# Author: Andy Green <andy@openmoko.com> # Author: Andy Green <andy@openmoko.com>
# LAYOUT # LAYOUT (if partition parameter is not specified)
# Partition table, then # Partition table, then
# VFAT takes up remaining space here # VFAT takes up remaining space here
# then... # then...
@ -13,7 +13,7 @@ EXT3_BACKUP_FS_SECTORS=$(( 8 * 1024 * 2 ))
QI_ALLOCATION=$(( 256 * 2 )) QI_ALLOCATION=$(( 256 * 2 ))
# #
# lastly fixed stuff: 8KByte initial boot, sig, padding # lastly fixed stuff: 8KByte initial boot, sig, padding
#
# ---------------------- # ----------------------
echo "s3c6410 bootable SD partitioning utility" echo "s3c6410 bootable SD partitioning utility"
@ -24,109 +24,258 @@ echo
QI_INITIAL=$(( 8 * 2 )) QI_INITIAL=$(( 8 * 2 ))
SIG=1 SIG=1
FDISK_SCRIPT=/tmp/_fds
if [ -z "$1" -o -z "$2" -o -z "$3" ] ; then # display usage message and exit
echo "This formats a SD card for usage on SD Card boot" # any arguments are displayed as an error message
echo " on 6410 based systems" USAGE()
{
echo echo
echo "Usage:" [ -z "$1" ] || echo ERROR: $*
echo echo
echo "$0 <device for SD, eg, sde> <sd|sdhc> <bootloader>" echo 'This formats a SD card for usage on SD Card boot'
echo ' on 6410 based systems'
echo
echo Usage: $(basename "$0") '<device> <card> <bootloader> <partition>'
echo ' device = disk device name for SD Card, e.g. sde /dev/sdf'
echo ' card = sd | sdhc'
echo ' bootloader = /path/to/qi-binary'
echo ' partition = vfat | NN | NN,NN | NN,NN,NN | NN,NN,NN,NN | no'
echo ' * vfat -> main-vfat[rest] + rootfs[256M] + backupfs[8M]'
echo ' NN -> rootfs1[NN%] + .. + rootfs4[NN%]'
echo ' NN=0 -> will skip the partition'
echo ' no -> leave partitions alone'
echo
echo 'Note: * => default action if no parameter specified'
echo ' sum(NN) must be in [1..100]'
echo
echo 'e.g. '$(basename "$0")' sdb sdhc images/qi 0,30,0,45'
echo ' will format an SDHC with partition 2 receiving 20% and partition 4'
echo ' receiving 45% of the disk capacity and the remaining 35% will be'
echo ' unused.'
echo ' Capacity is calculated after subtracting the space reserved for Qi.'
echo ' Partitions 1 and 3 will not be used.'
exit 1 exit 1
fi }
if [ $2 = "sdhc" ] ; then [ -z "$1" -o -z "$2" -o -z "$3" ] && USAGE 'Missing arguments'
PADDING=1025
else
PADDING=1
fi
EXT3_TOTAL_SECTORS=$(( $EXT3_ROOTFS_SECTORS + $EXT3_BACKUP_FS_SECTORS )) dev="$1"
REARSECTORS=$(( $QI_ALLOCATION + $QI_INITIAL + $SIG + $PADDING )) card="$2"
qi="$3"
partition="$4"
if [ ! -z "`grep $1 /proc/mounts`" ] ; then case "${card}" in
echo "ERROR $1 seems to be mounted, that ain't right" [sS][dD][hH][cC])
exit 2 PADDING=1025
fi ;;
[sS][dD])
PADDING=1
;;
*)
USAGE "${card} is an unknown card type"
esac
bytes=`echo p | fdisk /dev/$1 2>&1 | sed '/^Disk.*, \([0-9]*\) bytes/s//\1/p;d'` # the amount of space that must remain unused at the end of the disk
echo bytes = $bytes REAR_SECTORS=$(( $QI_ALLOCATION + $QI_INITIAL + $SIG + $PADDING ))
SECTORS=`expr $bytes / 512` # validate parameters
[ -b "${dev}" ] || dev="/dev/${dev}"
[ -b "${dev}" ] || USAGE "${dev} is not a valid block device"
[ X"${dev}" = X"${dev%%[0-9]}" ] || USAGE "${dev} is a partition, please use device: perhaps ${dev%%[0-9]}"
if [ -z "$SECTORS" ] ; then echo "Checking for mounted partitions..."
echo "problem finding size for /dev/$1" grep "${dev}" /proc/mounts && USAGE "partitions on ${dev} are mounted, please unmount them"
exit 4 [ -e "${qi}" ] || USAGE "bootloader file: ${qi} does not exist"
fi
if [ "$SECTORS" -le 0 ] ; then # get size of device
echo "problem finding size for /dev/$1" bytes=$(echo p | fdisk "${dev}" 2>&1 | sed '/^Disk.*, \([0-9]*\) bytes/s//\1/p;d')
exit 3 SECTORS=$(($bytes / 512))
fi
echo "$1 is $SECTORS 512-byte blocks" [ -z "$SECTORS" ] && USAGE "could not find size for ${dev}"
[ "$SECTORS" -le 0 ] && USAGE "invalid size: '${SECTORS}' for ${dev}"
if [ -z "$4" ] ; then echo "${dev} is $SECTORS 512-byte blocks"
FATSECTORS=$(( $SECTORS - $EXT3_TOTAL_SECTORS - $REARSECTORS )) # Partition and format a disk (or SD card)
FATMB=$(( $FATSECTORS / 2048 )) # Parameters to format function are:
#
# device -> device to partition e.g. /dev/sdX
#
# Partition 1 parameters:
#
# label -> file system volume label e.g. rootfs
# sizeMB -> size of the partition in MB e.g. 256
# fstype -> filesystem type e.g. ext2, ext3, vfat (look at /sbin/mkfs.* for others)
#
# Notes: 1. Repeat "label, sizeMB, fstype" for partitions 2..4
# 2. Partitions 2..4 are optional
# 3. Do not repeat device parameter
# 4. To skip a partition use: 'null 0 none' for that partition
echo "Creating VFAT section $FATMB MB" FORMAT()
{
local device label sizeMB fstype p partition flag skip
device="$1"; shift
(
p=0
flag=0
echo o
while [ $# -gt 0 ]
do
label="$1"; shift
sizeMB="$1"; shift
fstype="$1"; shift
p=$((${p} + 1))
skip=NO
[ ${sizeMB} -le 0 ] && skip=YES
case "${label}" in
[nN][uU][lL][lL])
skip=YES
;;
*)
;;
esac
case "${skip}" in
[yY][eE][sS]|[yY])
;;
*)
echo n
echo p
echo ${p}
echo
echo +${sizeMB}M
case "${fstype}" in
[vV][fF][aA][tT]|[mM][sS][dD][oO][sS])
echo t
# fdisk is "helpful" & will auto select partition if there is only one
# so do not output partition number if this is the first partition
[ "${flag}" -eq 1 ] && echo ${p}
echo 0b
;;
*)
;;
esac
flag=1
;;
esac
done
echo p
echo w
echo q
) | fdisk "${device}"
p=0
while [ $# -gt 0 ]
do
label="$1"; shift
sizeMB="$1"; shift
fstype="$1"; shift
p=$((${p} + 1))
partition="${dev}${p}"
skip=NO
[ ${sizeMB} -eq 0 ] && skip=YES
case "${label}" in
[nN][uU][lL][lL])
skip=YES
;;
esac
# create the script for fdisk case "${skip}" in
# clear the existing partition table [yY][eE][sS]|[yY])
echo "o" >$FDISK_SCRIPT echo "Skipping: ${partition}"
;;
*)
echo "Formatting: ${partition} -> ${fstype}[${sizeMB}MB]"
case "${fstype}" in
[vV][fF][aA][tT]|[mM][sS][dD][oO][sS])
mkfs.${fstype} -n "${label}" "${partition}"
;;
*)
mkfs.${fstype} -L "${label}" "${partition}"
;;
esac
;;
esac
done
}
# add main VFAT storage partition # format the disk
echo "n" >>$FDISK_SCRIPT case "${partition}" in
echo "p" >>$FDISK_SCRIPT
echo "1" >>$FDISK_SCRIPT
# first partition == 1
echo "" >>$FDISK_SCRIPT
echo "+$FATMB"M >>$FDISK_SCRIPT
# add the normal EXT3 rootfs # this case also hadles the default case (i.e. empty string: "")
echo "n" >>$FDISK_SCRIPT [vV][fF][aA][tT]|"")
echo "p" >>$FDISK_SCRIPT EXT3_TOTAL_SECTORS=$(( $EXT3_ROOTFS_SECTORS + $EXT3_BACKUP_FS_SECTORS ))
echo "2" >>$FDISK_SCRIPT FAT_SECTORS=$(( $SECTORS - $EXT3_TOTAL_SECTORS - $REAR_SECTORS ))
# continue after last FAT_MB=$(( $FAT_SECTORS / 2048 ))
echo "" >>$FDISK_SCRIPT EXT3_ROOTFS_MB=$(( ${EXT3_ROOTFS_SECTORS} / 2048 ))
echo "+$(( $EXT3_ROOTFS_SECTORS / 2048 ))"M >>$FDISK_SCRIPT EXT3_BACKUP_FS_MB=$(( ${EXT3_BACKUP_FS_SECTORS} / 2048 ))
# add the backup EXT3 rootfs echo Creating VFAT partition of ${FAT_MB} MB
echo "n" >>$FDISK_SCRIPT echo Creating Linux partition of ${EXT3_ROOTFS_MB} MB
echo "p" >>$FDISK_SCRIPT echo Creating backup Linux partition of ${EXT3_BACKUP_FS_MB} MB
echo "3" >>$FDISK_SCRIPT FORMAT "${dev}" \
# continue after last main-vfat "${FAT_MB}" vfat \
echo "" >>$FDISK_SCRIPT rootfs "${EXT3_ROOTFS_MB}" ext3 \
echo "+$(( $EXT3_BACKUP_FS_SECTORS / 2048 ))"M >>$FDISK_SCRIPT backupfs "${EXT3_BACKUP_FS_MB}" ext3
;;
# commit it and exit # decode partition or partition list
echo "w" >>$FDISK_SCRIPT *,*|100|[1-9][0-9]|[1-9])
echo "q" >>$FDISK_SCRIPT arg="${partition},"
for v in 1 2 3 4
do
eval n${v}="\${arg%%,*}"
eval n${v}="\${n${v}:-0}"
arg="${arg#*,},"
done
total=$(( ${n1} + ${n2} + ${n3} + ${n4} ))
echo Percentage of disk partitioned = ${total}%
[ ${total} -gt 100 -o ${total} -lt 1 ] && USAGE partition: "'${partition}' => ${total}% outside [1..100]"
# do the partitioning action EXT3_TOTAL_SECTORS=$(( $SECTORS - $REAR_SECTORS ))
fdisk /dev/$1 <$FDISK_SCRIPT EXT3_ROOTFS1_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n1 / 100 ))
EXT3_ROOTFS2_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n2 / 100 ))
EXT3_ROOTFS3_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n3 / 100 ))
EXT3_ROOTFS4_SECTORS=$(( $EXT3_TOTAL_SECTORS * $n4 / 100 ))
EXT3_ROOTFS1_MB=$(( ${EXT3_ROOTFS1_SECTORS} / 2048 ))
EXT3_ROOTFS2_MB=$(( ${EXT3_ROOTFS2_SECTORS} / 2048 ))
EXT3_ROOTFS3_MB=$(( ${EXT3_ROOTFS3_SECTORS} / 2048 ))
EXT3_ROOTFS4_MB=$(( ${EXT3_ROOTFS4_SECTORS} / 2048 ))
# prep the filesystems echo Creating Linux partition 1 of ${EXT3_ROOTFS1_MB} MB
echo Creating Linux partition 2 of ${EXT3_ROOTFS2_MB} MB
echo Creating Linux partition 3 of ${EXT3_ROOTFS3_MB} MB
echo Creating Linux partition 4 of ${EXT3_ROOTFS4_MB} MB
mkfs.vfat "/dev/$1"1 -n main-vfat FORMAT "${dev}" \
mkfs.ext3 "/dev/$1"2 -L rootfs rootfs1 "${EXT3_ROOTFS1_MB}" ext3 \
mkfs.ext3 "/dev/$1"3 -L backupfs rootfs2 "${EXT3_ROOTFS2_MB}" ext3 \
rootfs3 "${EXT3_ROOTFS3_MB}" ext3 \
rootfs4 "${EXT3_ROOTFS4_MB}" ext3
;;
[Nn]*)
# do not format
;;
*)
USAGE "'${partition}' is an unknown partition config"
;;
esac
fi # if -z $4
# copy the full bootloader image to the right place after the # copy the full bootloader image to the right place after the
# partitioned area # partitioned area
dd if=$3 of=/dev/$1 bs=512 count=512 \ echo
seek=$(( $SECTORS - $REARSECTORS )) echo Installing Qi bootloader from: ${qi}
dd if=$3 of=/dev/$1 bs=512 \
seek=$(( $SECTORS - $REARSECTORS + $QI_ALLOCATION )) \ dd if="${qi}" of="${dev}" bs=512 count=512 \
seek=$(( $SECTORS - $REAR_SECTORS ))
dd if="${qi}" of="${dev}" bs=512 \
seek=$(( $SECTORS - $REAR_SECTORS + $QI_ALLOCATION )) \
count=$QI_INITIAL count=$QI_INITIAL
dd if=/dev/zero of=/dev/$1 bs=512 \ dd if=/dev/zero of="${dev}" bs=512 \
seek=$(( $SECTORS - $REARSECTORS + $QI_ALLOCATION + $QI_INITIAL )) \ seek=$(( $SECTORS - $REAR_SECTORS + $QI_ALLOCATION + $QI_INITIAL )) \
count=$(( $SIG + $PADDING )) count=$(( $SIG + $PADDING ))
# done # done