1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/makesd-partition-table Thu May 23 22:30:49 2019 +0200
1.3 @@ -0,0 +1,209 @@
1.4 +#!/bin/sh
1.5 +
1.6 +# Emit a partition table for a device employing parameters such as the start,
1.7 +# size, and type.
1.8 +
1.9 +PROGNAME=`basename "$0"`
1.10 +THISDIR=`dirname "$0"`
1.11 +
1.12 +SFDISK="/sbin/sfdisk"
1.13 +
1.14 +
1.15 +
1.16 +# Global partition number and previous partition details.
1.17 +
1.18 +PARTNUM=1
1.19 +PREVSTART=
1.20 +PREVSIZE=
1.21 +PREVTYPE=
1.22 +
1.23 +# Emit any previous partition details as a complete sfdisk-compatible record.
1.24 +
1.25 +emit_partition()
1.26 +{
1.27 + if [ "$PREVSTART" ] || [ "$PREVSIZE" ] || [ "$PREVTYPE" ] ; then
1.28 + echo -n "$PARTNUM : "
1.29 + emit_partition_fields | sed 's/^ //;s/ /,/g'
1.30 + PARTNUM=$(($PARTNUM + 1))
1.31 + fi
1.32 +}
1.33 +
1.34 +# Emit the previous partition fields.
1.35 +
1.36 +emit_partition_fields()
1.37 +{
1.38 + if [ "$PREVSTART" ] ; then
1.39 + echo -n " start=${PREVSTART}${SFDISK_UNIT_SUFFIX}"
1.40 + fi
1.41 +
1.42 + # Obtain any explicitly stated size.
1.43 + # Without a size, use start details to calculate a size.
1.44 + # Without start details, claim the rest of the device.
1.45 +
1.46 + REMAINING=$(($DEVSIZE - $PREVSTART))
1.47 +
1.48 + if [ "$PREVSIZE" ] ; then
1.49 + PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100))
1.50 + elif [ "$START" ] ; then
1.51 + PREVSIZE=$(($START - $PREVSTART))
1.52 + else
1.53 + PREVSIZE=$REMAINING
1.54 + fi
1.55 +
1.56 + if [ "$PREVSIZE" -gt "$REMAINING" ] ; then
1.57 + PREVSIZE=$REMAINING
1.58 + fi
1.59 +
1.60 + echo -n " size=${PREVSIZE}${SFDISK_UNIT_SUFFIX}"
1.61 +
1.62 + if [ "$PREVTYPE" ] ; then
1.63 + echo -n " Id=`partition_type $PREVTYPE`"
1.64 + fi
1.65 + echo
1.66 +}
1.67 +
1.68 +# Retain the current partition details.
1.69 +
1.70 +store_partition()
1.71 +{
1.72 + if [ "$START" ] ; then
1.73 + PREVSTART=$START
1.74 + elif [ "$PREVSTART" ] && [ "$PREVSIZE" ] ; then
1.75 + PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100))
1.76 + PREVSTART=$(($PREVSTART + $PREVSIZE))
1.77 + else
1.78 + PREVSTART=0
1.79 + fi
1.80 +
1.81 + PREVSIZE=$SIZE
1.82 + PREVTYPE=$TYPE
1.83 +}
1.84 +
1.85 +# Reset the current partition details.
1.86 +
1.87 +reset_partition()
1.88 +{
1.89 + START=
1.90 + SIZE=
1.91 + TYPE=
1.92 +}
1.93 +
1.94 +# Emit the current partition details and proceed to the next partition.
1.95 +
1.96 +next_partition()
1.97 +{
1.98 + if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then
1.99 + emit_partition
1.100 + store_partition
1.101 + reset_partition
1.102 + fi
1.103 +}
1.104 +
1.105 +# Convert the partition type to an sdisk-compatible identifier.
1.106 +
1.107 +partition_type()
1.108 +{
1.109 + case "$1" in
1.110 + ( ext[2-4]? ) echo "0x83" ;;
1.111 + ( swap ) echo "0x82" ;;
1.112 + ( fat ) echo "0x0c" ;;
1.113 + ( * ) echo "0x83" ;;
1.114 + esac
1.115 +}
1.116 +
1.117 +
1.118 +
1.119 +# device_size <device>
1.120 +#
1.121 +# Obtain the size of the given disk or partition in 1024-byte blocks.
1.122 +
1.123 +device_size()
1.124 +{
1.125 + "$SFDISK" -s "$1"
1.126 +}
1.127 +
1.128 +# Test sfdisk behaviour and obtain useful information.
1.129 +
1.130 +init_sfdisk()
1.131 +{
1.132 + if "$SFDISK" -uB -s "$1" > /dev/null 2>&1 ; then
1.133 + SFDISK_UNIT_SUFFIX=
1.134 + else
1.135 + SFDISK_UNIT_SUFFIX="K"
1.136 + fi
1.137 +
1.138 + # Obtain the device size in blocks.
1.139 +
1.140 + DEVSIZE=`device_size "$1"`
1.141 +}
1.142 +
1.143 +
1.144 +
1.145 +# Emit the help message if requested.
1.146 +
1.147 +if [ "$1" = '--help' ] ; then
1.148 + cat 1>&2 <<EOF
1.149 +Usage: $PROGNAME ( -f <type> | -p <start> | -s <size> )...
1.150 +
1.151 +Produce partition descriptions, indicating partition type, start position and
1.152 +size for each partition. Each occurrence of the -f option starts a new partition
1.153 +description.
1.154 +
1.155 +Start positions are indicated as numbers of 1024-byte blocks.
1.156 +
1.157 +Sizes are indicated as percentages of the entire device; if omitted, the
1.158 +remainder of the device will be used.
1.159 +
1.160 +Types recognised include ext, ext2, ext3, ext4, fat and swap. Other types are
1.161 +interpreted as Linux partitions.
1.162 +EOF
1.163 + exit 0
1.164 +fi
1.165 +
1.166 +# Obtain details of the selected device.
1.167 +
1.168 +if [ ! "$DEV" ] ; then
1.169 + cat 1>&2 <<EOF
1.170 +No device specified. Use the DEV environment variable to indicate a device.
1.171 +EOF
1.172 + exit 1
1.173 +fi
1.174 +
1.175 +# Initialise sfdisk usage.
1.176 +
1.177 +init_sfdisk "$DEV"
1.178 +
1.179 +# Process the arguments, building a partition description.
1.180 +
1.181 +reset_partition
1.182 +
1.183 +while [ "$1" ] ; do
1.184 + case "$1" in
1.185 + -f )
1.186 + if [ "$TYPE" ] ; then next_partition ; fi
1.187 + TYPE="$2"
1.188 + shift 2
1.189 + ;;
1.190 + -p )
1.191 + if [ "$START" ] ; then next_partition ; fi
1.192 + START="$2"
1.193 + shift 2
1.194 + ;;
1.195 + -s )
1.196 + if [ "$SIZE" ] ; then next_partition ; fi
1.197 + SIZE="$2"
1.198 + shift 2
1.199 + ;;
1.200 + * )
1.201 + shift 1
1.202 + ;;
1.203 + esac
1.204 +done
1.205 +
1.206 +# Terminate any unfinished partition, emitting the previous partition.
1.207 +
1.208 +next_partition
1.209 +
1.210 +# Emit the last partition.
1.211 +
1.212 +emit_partition