# HG changeset patch # User Paul Boddie # Date 1558643449 -7200 # Node ID a3585c5fc0d1d435161980a530b3632ceb4a5649 # Parent 93f929488a1feadf71a4db5dae021ee7cc736b63 Added a script to generate a partition table description for sfdisk. diff -r 93f929488a1f -r a3585c5fc0d1 makesd-partition-table --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/makesd-partition-table Thu May 23 22:30:49 2019 +0200 @@ -0,0 +1,209 @@ +#!/bin/sh + +# Emit a partition table for a device employing parameters such as the start, +# size, and type. + +PROGNAME=`basename "$0"` +THISDIR=`dirname "$0"` + +SFDISK="/sbin/sfdisk" + + + +# Global partition number and previous partition details. + +PARTNUM=1 +PREVSTART= +PREVSIZE= +PREVTYPE= + +# Emit any previous partition details as a complete sfdisk-compatible record. + +emit_partition() +{ + if [ "$PREVSTART" ] || [ "$PREVSIZE" ] || [ "$PREVTYPE" ] ; then + echo -n "$PARTNUM : " + emit_partition_fields | sed 's/^ //;s/ /,/g' + PARTNUM=$(($PARTNUM + 1)) + fi +} + +# Emit the previous partition fields. + +emit_partition_fields() +{ + if [ "$PREVSTART" ] ; then + echo -n " start=${PREVSTART}${SFDISK_UNIT_SUFFIX}" + fi + + # Obtain any explicitly stated size. + # Without a size, use start details to calculate a size. + # Without start details, claim the rest of the device. + + REMAINING=$(($DEVSIZE - $PREVSTART)) + + if [ "$PREVSIZE" ] ; then + PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100)) + elif [ "$START" ] ; then + PREVSIZE=$(($START - $PREVSTART)) + else + PREVSIZE=$REMAINING + fi + + if [ "$PREVSIZE" -gt "$REMAINING" ] ; then + PREVSIZE=$REMAINING + fi + + echo -n " size=${PREVSIZE}${SFDISK_UNIT_SUFFIX}" + + if [ "$PREVTYPE" ] ; then + echo -n " Id=`partition_type $PREVTYPE`" + fi + echo +} + +# Retain the current partition details. + +store_partition() +{ + if [ "$START" ] ; then + PREVSTART=$START + elif [ "$PREVSTART" ] && [ "$PREVSIZE" ] ; then + PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100)) + PREVSTART=$(($PREVSTART + $PREVSIZE)) + else + PREVSTART=0 + fi + + PREVSIZE=$SIZE + PREVTYPE=$TYPE +} + +# Reset the current partition details. + +reset_partition() +{ + START= + SIZE= + TYPE= +} + +# Emit the current partition details and proceed to the next partition. + +next_partition() +{ + if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then + emit_partition + store_partition + reset_partition + fi +} + +# Convert the partition type to an sdisk-compatible identifier. + +partition_type() +{ + case "$1" in + ( ext[2-4]? ) echo "0x83" ;; + ( swap ) echo "0x82" ;; + ( fat ) echo "0x0c" ;; + ( * ) echo "0x83" ;; + esac +} + + + +# device_size +# +# Obtain the size of the given disk or partition in 1024-byte blocks. + +device_size() +{ + "$SFDISK" -s "$1" +} + +# Test sfdisk behaviour and obtain useful information. + +init_sfdisk() +{ + if "$SFDISK" -uB -s "$1" > /dev/null 2>&1 ; then + SFDISK_UNIT_SUFFIX= + else + SFDISK_UNIT_SUFFIX="K" + fi + + # Obtain the device size in blocks. + + DEVSIZE=`device_size "$1"` +} + + + +# Emit the help message if requested. + +if [ "$1" = '--help' ] ; then + cat 1>&2 < | -p | -s )... + +Produce partition descriptions, indicating partition type, start position and +size for each partition. Each occurrence of the -f option starts a new partition +description. + +Start positions are indicated as numbers of 1024-byte blocks. + +Sizes are indicated as percentages of the entire device; if omitted, the +remainder of the device will be used. + +Types recognised include ext, ext2, ext3, ext4, fat and swap. Other types are +interpreted as Linux partitions. +EOF + exit 0 +fi + +# Obtain details of the selected device. + +if [ ! "$DEV" ] ; then + cat 1>&2 <