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@7 | 9 | COMMON="$THISDIR/makesd-common" |
paul@1 | 10 | SFDISK="/sbin/sfdisk" |
paul@1 | 11 | |
paul@7 | 12 | . "$COMMON" |
paul@7 | 13 | |
paul@1 | 14 | |
paul@1 | 15 | |
paul@2 | 16 | # Cylinder size in sectors for alignment purposes. |
paul@2 | 17 | |
paul@2 | 18 | HEADS=255 |
paul@2 | 19 | SECTORS=63 |
paul@2 | 20 | CYLINDER_SIZE=$(($HEADS * $SECTORS)) |
paul@2 | 21 | |
paul@1 | 22 | # Global partition number and previous partition details. |
paul@1 | 23 | |
paul@1 | 24 | PARTNUM=1 |
paul@1 | 25 | PREVSTART= |
paul@1 | 26 | PREVSIZE= |
paul@1 | 27 | PREVTYPE= |
paul@1 | 28 | |
paul@1 | 29 | # Emit any previous partition details as a complete sfdisk-compatible record. |
paul@1 | 30 | |
paul@1 | 31 | emit_partition() |
paul@1 | 32 | { |
paul@1 | 33 | if [ "$PREVSTART" ] || [ "$PREVSIZE" ] || [ "$PREVTYPE" ] ; then |
paul@1 | 34 | echo -n "$PARTNUM : " |
paul@1 | 35 | emit_partition_fields | sed 's/^ //;s/ /,/g' |
paul@1 | 36 | PARTNUM=$(($PARTNUM + 1)) |
paul@1 | 37 | fi |
paul@1 | 38 | } |
paul@1 | 39 | |
paul@1 | 40 | # Emit the previous partition fields. |
paul@1 | 41 | |
paul@1 | 42 | emit_partition_fields() |
paul@1 | 43 | { |
paul@2 | 44 | echo -n " start=${PREVSTART}${SFDISK_UNIT_SUFFIX}" |
paul@2 | 45 | |
paul@3 | 46 | REMAINING=$(($DEVSIZE - $PREVSTART)) |
paul@3 | 47 | |
paul@3 | 48 | # Retain any previously calculated size. |
paul@1 | 49 | # Without a size, use start details to calculate a size. |
paul@1 | 50 | # Without start details, claim the rest of the device. |
paul@1 | 51 | |
paul@1 | 52 | if [ "$PREVSIZE" ] ; then |
paul@3 | 53 | : |
paul@1 | 54 | elif [ "$START" ] ; then |
paul@2 | 55 | PREVSIZE=$((`align_cylinder $START` - $PREVSTART)) |
paul@1 | 56 | else |
paul@1 | 57 | PREVSIZE=$REMAINING |
paul@1 | 58 | fi |
paul@1 | 59 | |
paul@1 | 60 | if [ "$PREVSIZE" -gt "$REMAINING" ] ; then |
paul@1 | 61 | PREVSIZE=$REMAINING |
paul@1 | 62 | fi |
paul@1 | 63 | |
paul@1 | 64 | echo -n " size=${PREVSIZE}${SFDISK_UNIT_SUFFIX}" |
paul@1 | 65 | |
paul@1 | 66 | if [ "$PREVTYPE" ] ; then |
paul@1 | 67 | echo -n " Id=`partition_type $PREVTYPE`" |
paul@1 | 68 | fi |
paul@2 | 69 | |
paul@1 | 70 | echo |
paul@1 | 71 | } |
paul@1 | 72 | |
paul@1 | 73 | # Retain the current partition details. |
paul@1 | 74 | |
paul@1 | 75 | store_partition() |
paul@1 | 76 | { |
paul@3 | 77 | # Define the partition start using a specified position. |
paul@3 | 78 | # Alternatively, use previous start and size information. |
paul@3 | 79 | # Otherwise, the first partition is being defined. |
paul@3 | 80 | |
paul@5 | 81 | if [ "$START" ] && [ "$START" != '-' ] ; then |
paul@3 | 82 | PREVSTART=`align_cylinder $START` |
paul@1 | 83 | elif [ "$PREVSTART" ] && [ "$PREVSIZE" ] ; then |
paul@3 | 84 | PREVSTART=`align_cylinder $(($PREVSTART + $PREVSIZE))` |
paul@1 | 85 | else |
paul@1 | 86 | PREVSTART=0 |
paul@1 | 87 | fi |
paul@1 | 88 | |
paul@3 | 89 | # Ensure that the first partition starts at sector one. |
paul@3 | 90 | |
paul@3 | 91 | if [ "$PREVSTART" = '0' ] ; then |
paul@3 | 92 | PREVSTART=1 |
paul@3 | 93 | fi |
paul@3 | 94 | |
paul@3 | 95 | # Calculate and align the stated size. |
paul@3 | 96 | # Otherwise, defer the calculation until emitting the details. |
paul@3 | 97 | |
paul@5 | 98 | if [ "$SIZE" ] && [ "$SIZE" != '-' ] ; then |
paul@3 | 99 | PREVSIZE=$(($SIZE * $DEVSIZE / 100)) |
paul@3 | 100 | PREVSIZE=`align_cylinder $PREVSIZE` |
paul@3 | 101 | |
paul@3 | 102 | if [ "$PREVSTART" = '1' ] ; then |
paul@3 | 103 | PREVSIZE=$(($PREVSIZE - 1)) |
paul@3 | 104 | fi |
paul@3 | 105 | else |
paul@3 | 106 | PREVSIZE= |
paul@3 | 107 | fi |
paul@3 | 108 | |
paul@1 | 109 | PREVTYPE=$TYPE |
paul@1 | 110 | } |
paul@1 | 111 | |
paul@1 | 112 | # Emit the current partition details and proceed to the next partition. |
paul@1 | 113 | |
paul@1 | 114 | next_partition() |
paul@1 | 115 | { |
paul@5 | 116 | emit_partition |
paul@5 | 117 | store_partition |
paul@1 | 118 | } |
paul@1 | 119 | |
paul@1 | 120 | # Convert the partition type to an sdisk-compatible identifier. |
paul@1 | 121 | |
paul@1 | 122 | partition_type() |
paul@1 | 123 | { |
paul@1 | 124 | case "$1" in |
paul@7 | 125 | ( ext* ) echo "0x83" ;; |
paul@7 | 126 | ( swap ) echo "0x82" ;; |
paul@7 | 127 | ( fat ) echo "0x0c" ;; |
paul@7 | 128 | ( * ) echo "0x83" ;; |
paul@1 | 129 | esac |
paul@1 | 130 | } |
paul@1 | 131 | |
paul@1 | 132 | |
paul@1 | 133 | |
paul@1 | 134 | # device_size <device> |
paul@1 | 135 | # |
paul@1 | 136 | # Obtain the size of the given disk or partition in 1024-byte blocks. |
paul@1 | 137 | |
paul@1 | 138 | device_size() |
paul@1 | 139 | { |
paul@1 | 140 | "$SFDISK" -s "$1" |
paul@1 | 141 | } |
paul@1 | 142 | |
paul@2 | 143 | # Align to cylinders. |
paul@2 | 144 | |
paul@2 | 145 | align_cylinder() |
paul@2 | 146 | { |
paul@4 | 147 | if [ ! "$ALIGN_CYLINDER" ] ; then |
paul@4 | 148 | echo $1 |
paul@4 | 149 | return |
paul@4 | 150 | fi |
paul@4 | 151 | |
paul@2 | 152 | # Round up to the nearest cylinder. |
paul@2 | 153 | |
paul@2 | 154 | CYLINDER=$((($1 + $CYLINDER_SIZE - 1) / $CYLINDER_SIZE)) |
paul@2 | 155 | |
paul@2 | 156 | # Emit the number of sectors. |
paul@2 | 157 | |
paul@2 | 158 | echo $(($CYLINDER * $CYLINDER_SIZE)) |
paul@2 | 159 | } |
paul@2 | 160 | |
paul@1 | 161 | # Test sfdisk behaviour and obtain useful information. |
paul@1 | 162 | |
paul@1 | 163 | init_sfdisk() |
paul@1 | 164 | { |
paul@2 | 165 | SFDISK_UNIT_SUFFIX= |
paul@2 | 166 | |
paul@2 | 167 | # Obtain the device size in 512-byte sectors. |
paul@1 | 168 | |
paul@2 | 169 | DEVSIZE=$((`device_size "$1"` * 2)) |
paul@1 | 170 | |
paul@2 | 171 | # Find the cylinder-addressable size. |
paul@2 | 172 | |
paul@4 | 173 | if [ "$ALIGN_CYLINDER" ] ; then |
paul@4 | 174 | CYLINDERS=$(($DEVSIZE / $CYLINDER_SIZE)) |
paul@4 | 175 | DEVSIZE=$(($CYLINDERS * $CYLINDER_SIZE)) |
paul@4 | 176 | fi |
paul@1 | 177 | } |
paul@1 | 178 | |
paul@1 | 179 | |
paul@1 | 180 | |
paul@1 | 181 | # Emit the help message if requested. |
paul@1 | 182 | |
paul@1 | 183 | if [ "$1" = '--help' ] ; then |
paul@1 | 184 | cat 1>&2 <<EOF |
paul@13 | 185 | Usage: $PROGNAME [ --align ] |
paul@1 | 186 | |
paul@1 | 187 | Produce partition descriptions, indicating partition type, start position and |
paul@13 | 188 | size for each partition. |
paul@13 | 189 | |
paul@13 | 190 | The input for this program is supplied via standard input as a partitioning |
paul@13 | 191 | summary with each line providing a collection of tab-separated values. These |
paul@13 | 192 | values appear in the following order: |
paul@13 | 193 | |
paul@13 | 194 | <start> <size> <type> |
paul@1 | 195 | |
paul@2 | 196 | Start positions are indicated as numbers of 512-byte sectors. |
paul@1 | 197 | |
paul@1 | 198 | Sizes are indicated as percentages of the entire device; if omitted, the |
paul@1 | 199 | remainder of the device will be used. |
paul@1 | 200 | |
paul@1 | 201 | Types recognised include ext, ext2, ext3, ext4, fat and swap. Other types are |
paul@4 | 202 | interpreted as Linux partitions. |
paul@4 | 203 | |
paul@13 | 204 | $(align_cylinder_description) |
paul@1 | 205 | EOF |
paul@1 | 206 | exit 0 |
paul@1 | 207 | fi |
paul@1 | 208 | |
paul@4 | 209 | # Test for the cylinder alignment option. |
paul@4 | 210 | |
paul@11 | 211 | if test_align_cylinder $* ; then shift 1 ; fi |
paul@4 | 212 | |
paul@1 | 213 | # Obtain details of the selected device. |
paul@1 | 214 | |
paul@7 | 215 | check_device |
paul@4 | 216 | |
paul@1 | 217 | # Initialise sfdisk usage. |
paul@1 | 218 | |
paul@1 | 219 | init_sfdisk "$DEV" |
paul@1 | 220 | |
paul@5 | 221 | while read_fields ; do |
paul@5 | 222 | next_partition |
paul@1 | 223 | done |
paul@1 | 224 | |
paul@1 | 225 | # Emit the last partition. |
paul@1 | 226 | |
paul@1 | 227 | emit_partition |