paul@1 | 1 | #!/bin/sh |
paul@1 | 2 | |
paul@1 | 3 | # Emit a partition table for a device employing parameters such as the start, |
paul@1 | 4 | # size, and type. |
paul@1 | 5 | |
paul@1 | 6 | PROGNAME=`basename "$0"` |
paul@1 | 7 | THISDIR=`dirname "$0"` |
paul@1 | 8 | |
paul@1 | 9 | SFDISK="/sbin/sfdisk" |
paul@1 | 10 | |
paul@1 | 11 | |
paul@1 | 12 | |
paul@1 | 13 | # Global partition number and previous partition details. |
paul@1 | 14 | |
paul@1 | 15 | PARTNUM=1 |
paul@1 | 16 | PREVSTART= |
paul@1 | 17 | PREVSIZE= |
paul@1 | 18 | PREVTYPE= |
paul@1 | 19 | |
paul@1 | 20 | # Emit any previous partition details as a complete sfdisk-compatible record. |
paul@1 | 21 | |
paul@1 | 22 | emit_partition() |
paul@1 | 23 | { |
paul@1 | 24 | if [ "$PREVSTART" ] || [ "$PREVSIZE" ] || [ "$PREVTYPE" ] ; then |
paul@1 | 25 | echo -n "$PARTNUM : " |
paul@1 | 26 | emit_partition_fields | sed 's/^ //;s/ /,/g' |
paul@1 | 27 | PARTNUM=$(($PARTNUM + 1)) |
paul@1 | 28 | fi |
paul@1 | 29 | } |
paul@1 | 30 | |
paul@1 | 31 | # Emit the previous partition fields. |
paul@1 | 32 | |
paul@1 | 33 | emit_partition_fields() |
paul@1 | 34 | { |
paul@1 | 35 | if [ "$PREVSTART" ] ; then |
paul@1 | 36 | echo -n " start=${PREVSTART}${SFDISK_UNIT_SUFFIX}" |
paul@1 | 37 | fi |
paul@1 | 38 | |
paul@1 | 39 | # Obtain any explicitly stated size. |
paul@1 | 40 | # Without a size, use start details to calculate a size. |
paul@1 | 41 | # Without start details, claim the rest of the device. |
paul@1 | 42 | |
paul@1 | 43 | REMAINING=$(($DEVSIZE - $PREVSTART)) |
paul@1 | 44 | |
paul@1 | 45 | if [ "$PREVSIZE" ] ; then |
paul@1 | 46 | PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100)) |
paul@1 | 47 | elif [ "$START" ] ; then |
paul@1 | 48 | PREVSIZE=$(($START - $PREVSTART)) |
paul@1 | 49 | else |
paul@1 | 50 | PREVSIZE=$REMAINING |
paul@1 | 51 | fi |
paul@1 | 52 | |
paul@1 | 53 | if [ "$PREVSIZE" -gt "$REMAINING" ] ; then |
paul@1 | 54 | PREVSIZE=$REMAINING |
paul@1 | 55 | fi |
paul@1 | 56 | |
paul@1 | 57 | echo -n " size=${PREVSIZE}${SFDISK_UNIT_SUFFIX}" |
paul@1 | 58 | |
paul@1 | 59 | if [ "$PREVTYPE" ] ; then |
paul@1 | 60 | echo -n " Id=`partition_type $PREVTYPE`" |
paul@1 | 61 | fi |
paul@1 | 62 | echo |
paul@1 | 63 | } |
paul@1 | 64 | |
paul@1 | 65 | # Retain the current partition details. |
paul@1 | 66 | |
paul@1 | 67 | store_partition() |
paul@1 | 68 | { |
paul@1 | 69 | if [ "$START" ] ; then |
paul@1 | 70 | PREVSTART=$START |
paul@1 | 71 | elif [ "$PREVSTART" ] && [ "$PREVSIZE" ] ; then |
paul@1 | 72 | PREVSIZE=$(($PREVSIZE * $DEVSIZE / 100)) |
paul@1 | 73 | PREVSTART=$(($PREVSTART + $PREVSIZE)) |
paul@1 | 74 | else |
paul@1 | 75 | PREVSTART=0 |
paul@1 | 76 | fi |
paul@1 | 77 | |
paul@1 | 78 | PREVSIZE=$SIZE |
paul@1 | 79 | PREVTYPE=$TYPE |
paul@1 | 80 | } |
paul@1 | 81 | |
paul@1 | 82 | # Reset the current partition details. |
paul@1 | 83 | |
paul@1 | 84 | reset_partition() |
paul@1 | 85 | { |
paul@1 | 86 | START= |
paul@1 | 87 | SIZE= |
paul@1 | 88 | TYPE= |
paul@1 | 89 | } |
paul@1 | 90 | |
paul@1 | 91 | # Emit the current partition details and proceed to the next partition. |
paul@1 | 92 | |
paul@1 | 93 | next_partition() |
paul@1 | 94 | { |
paul@1 | 95 | if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then |
paul@1 | 96 | emit_partition |
paul@1 | 97 | store_partition |
paul@1 | 98 | reset_partition |
paul@1 | 99 | fi |
paul@1 | 100 | } |
paul@1 | 101 | |
paul@1 | 102 | # Convert the partition type to an sdisk-compatible identifier. |
paul@1 | 103 | |
paul@1 | 104 | partition_type() |
paul@1 | 105 | { |
paul@1 | 106 | case "$1" in |
paul@1 | 107 | ( ext[2-4]? ) echo "0x83" ;; |
paul@1 | 108 | ( swap ) echo "0x82" ;; |
paul@1 | 109 | ( fat ) echo "0x0c" ;; |
paul@1 | 110 | ( * ) echo "0x83" ;; |
paul@1 | 111 | esac |
paul@1 | 112 | } |
paul@1 | 113 | |
paul@1 | 114 | |
paul@1 | 115 | |
paul@1 | 116 | # device_size <device> |
paul@1 | 117 | # |
paul@1 | 118 | # Obtain the size of the given disk or partition in 1024-byte blocks. |
paul@1 | 119 | |
paul@1 | 120 | device_size() |
paul@1 | 121 | { |
paul@1 | 122 | "$SFDISK" -s "$1" |
paul@1 | 123 | } |
paul@1 | 124 | |
paul@1 | 125 | # Test sfdisk behaviour and obtain useful information. |
paul@1 | 126 | |
paul@1 | 127 | init_sfdisk() |
paul@1 | 128 | { |
paul@1 | 129 | if "$SFDISK" -uB -s "$1" > /dev/null 2>&1 ; then |
paul@1 | 130 | SFDISK_UNIT_SUFFIX= |
paul@1 | 131 | else |
paul@1 | 132 | SFDISK_UNIT_SUFFIX="K" |
paul@1 | 133 | fi |
paul@1 | 134 | |
paul@1 | 135 | # Obtain the device size in blocks. |
paul@1 | 136 | |
paul@1 | 137 | DEVSIZE=`device_size "$1"` |
paul@1 | 138 | } |
paul@1 | 139 | |
paul@1 | 140 | |
paul@1 | 141 | |
paul@1 | 142 | # Emit the help message if requested. |
paul@1 | 143 | |
paul@1 | 144 | if [ "$1" = '--help' ] ; then |
paul@1 | 145 | cat 1>&2 <<EOF |
paul@1 | 146 | Usage: $PROGNAME ( -f <type> | -p <start> | -s <size> )... |
paul@1 | 147 | |
paul@1 | 148 | Produce partition descriptions, indicating partition type, start position and |
paul@1 | 149 | size for each partition. Each occurrence of the -f option starts a new partition |
paul@1 | 150 | description. |
paul@1 | 151 | |
paul@1 | 152 | Start positions are indicated as numbers of 1024-byte blocks. |
paul@1 | 153 | |
paul@1 | 154 | Sizes are indicated as percentages of the entire device; if omitted, the |
paul@1 | 155 | remainder of the device will be used. |
paul@1 | 156 | |
paul@1 | 157 | Types recognised include ext, ext2, ext3, ext4, fat and swap. Other types are |
paul@1 | 158 | interpreted as Linux partitions. |
paul@1 | 159 | EOF |
paul@1 | 160 | exit 0 |
paul@1 | 161 | fi |
paul@1 | 162 | |
paul@1 | 163 | # Obtain details of the selected device. |
paul@1 | 164 | |
paul@1 | 165 | if [ ! "$DEV" ] ; then |
paul@1 | 166 | cat 1>&2 <<EOF |
paul@1 | 167 | No device specified. Use the DEV environment variable to indicate a device. |
paul@1 | 168 | EOF |
paul@1 | 169 | exit 1 |
paul@1 | 170 | fi |
paul@1 | 171 | |
paul@1 | 172 | # Initialise sfdisk usage. |
paul@1 | 173 | |
paul@1 | 174 | init_sfdisk "$DEV" |
paul@1 | 175 | |
paul@1 | 176 | # Process the arguments, building a partition description. |
paul@1 | 177 | |
paul@1 | 178 | reset_partition |
paul@1 | 179 | |
paul@1 | 180 | while [ "$1" ] ; do |
paul@1 | 181 | case "$1" in |
paul@1 | 182 | -f ) |
paul@1 | 183 | if [ "$TYPE" ] ; then next_partition ; fi |
paul@1 | 184 | TYPE="$2" |
paul@1 | 185 | shift 2 |
paul@1 | 186 | ;; |
paul@1 | 187 | -p ) |
paul@1 | 188 | if [ "$START" ] ; then next_partition ; fi |
paul@1 | 189 | START="$2" |
paul@1 | 190 | shift 2 |
paul@1 | 191 | ;; |
paul@1 | 192 | -s ) |
paul@1 | 193 | if [ "$SIZE" ] ; then next_partition ; fi |
paul@1 | 194 | SIZE="$2" |
paul@1 | 195 | shift 2 |
paul@1 | 196 | ;; |
paul@1 | 197 | * ) |
paul@1 | 198 | shift 1 |
paul@1 | 199 | ;; |
paul@1 | 200 | esac |
paul@1 | 201 | done |
paul@1 | 202 | |
paul@1 | 203 | # Terminate any unfinished partition, emitting the previous partition. |
paul@1 | 204 | |
paul@1 | 205 | next_partition |
paul@1 | 206 | |
paul@1 | 207 | # Emit the last partition. |
paul@1 | 208 | |
paul@1 | 209 | emit_partition |