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