| 1 | # Generate code to partition the disks. |
| 2 | |
| 3 | # The parted command is mandatory, |
| 4 | # see https://github.com/rear/rear/issues/1933#issuecomment-430207057 |
| 5 | has_binary parted || Error "Cannot find 'parted' command" |
| 6 | |
| 7 | # |
| 8 | # TODO: clean up that old code when parted doesn't support any unit. |
| 9 | # It's there since ages! |
| 10 | # |
| 11 | |
| 12 | # True if parted accepts values in units other than mebibytes. |
| 13 | FEATURE_PARTED_ANYUNIT= |
| 14 | |
| 15 | # Test by using the parted version numbers... |
| 16 | parted_version=$( get_version parted -v ) |
| 17 | |
| 18 | test "$parted_version" || BugError "Function get_version could not detect parted version" |
| 19 | |
| 20 | if version_newer "$parted_version" 1.6.23 ; then |
| 21 | FEATURE_PARTED_ANYUNIT="y" |
| 22 | fi |
| 23 | |
| 24 | ### Prepare a disk for partitioning/general usage. |
| 25 | create_disk() { |
| 26 | local component disk size label junk |
| 27 | local blocksize layout dasdtype dasdcyls junk2 |
| 28 | |
| 29 | disk='' |
| 30 | read component disk size label junk < <(grep "^disk $1 " "$LAYOUT_FILE") |
| 31 | test $disk || BugError "No 'disk $1 ' entry in $LAYOUT_FILE" |
| 32 | |
| 33 | cat >> "$LAYOUT_CODE" <<EOF |
| 34 | |
| 35 | # |
| 36 | # Code handling disk '$disk' |
| 37 | # |
| 38 | |
| 39 | ### Disks should be block devices. |
| 40 | test -b "$disk" || BugError "Disk '$disk' is not a block device" |
| 41 | |
| 42 | Log "Stop mdadm" |
| 43 | if grep -q md /proc/mdstat 2>/dev/null; then |
| 44 | mdadm --stop -s >&2 || echo "stop mdadm failed" |
| 45 | # Prevent udev waking up mdadm later. |
| 46 | # Reasoning: At least on RHEL6 when parted created a raid partition on disk, |
| 47 | # udev (via /lib/udev/rules.d/65-md-incremental.rules) wakes up mdadm which locks the disk, |
| 48 | # so further parted commands with the disk will fail since the disk is busy now. |
| 49 | # The /lib/udev/rules.d/65-md-incremental.rules detects anaconda (the Red Hat installer), |
| 50 | # and if it find itself running under anaconda, it will not run. |
| 51 | # Accordingly also for other installers (in particular the ReaR installer) |
| 52 | # this rule should not be there (and other Linux distros probably do not have it) |
| 53 | # which means removing it is the right solution to make ReaR work also for RHEL6: |
| 54 | if [ -e /lib/udev/rules.d/65-md-incremental.rules ] ; then |
| 55 | rm -f /lib/udev/rules.d/65-md-incremental.rules || echo "rm 65-md-incremental.rules failed" |
| 56 | fi |
| 57 | fi |
| 58 | Log "Erasing MBR of disk $disk" |
| 59 | dd if=/dev/zero of=$disk bs=512 count=1 |
| 60 | sync |
| 61 | |
| 62 | EOF |
| 63 | |
| 64 | # $junk can contain useful DASD-specific fields |
| 65 | create_partitions "$disk" "$label" "$junk" |
| 66 | |
| 67 | cat >> "$LAYOUT_CODE" <<EOF |
| 68 | # Make sure device nodes are visible (eg. in RHEL4) |
| 69 | my_udevtrigger |
| 70 | my_udevsettle |
| 71 | |
| 72 | # Clean up transient partitions and resize shrunk ones |
| 73 | delete_dummy_partitions_and_resize_real_ones |
| 74 | |
| 75 | # |
| 76 | # End of code handling disk '$disk' |
| 77 | # |
| 78 | |
| 79 | EOF |
| 80 | } |
| 81 | |
| 82 | ### Create partitions on a block device. |
| 83 | ### The block device does not necessarily exist yet... |
| 84 | create_partitions() { |
| 85 | local device=$1 |
| 86 | local label=$2 |
| 87 | |
| 88 | ### List partition types/names to detect disk label type. |
| 89 | local -a names=() |
| 90 | local part disk size pstart name junk |
| 91 | local orig_block_size layout dasdtype dasdcyls junk2 |
| 92 | if [ "$label" == dasd ]; then |
| 93 | # dasd has more fields - junk is not junk anymore |
| 94 | read orig_block_size layout dasdtype dasdcyls junk2 <<<$3 |
| 95 | fi |
| 96 | while read part disk size pstart name junk ; do |
| 97 | names+=( $name ) |
| 98 | case $name in |
| 99 | (primary|extended|logical) |
| 100 | if [[ -z "$label" ]] ; then |
| 101 | Log "Disk label for $device detected as msdos." |
| 102 | label="msdos" |
| 103 | fi |
| 104 | ;; |
| 105 | esac |
| 106 | done < <( grep "^part $device " "$LAYOUT_FILE" ) |
| 107 | |
| 108 | ### Early return for devices without partitions. |
| 109 | if [[ ${#names[@]} -eq 0 ]] ; then |
| 110 | Log "No partitions on device $device." |
| 111 | return 0 |
| 112 | fi |
| 113 | |
| 114 | if [[ -z "$label" ]] ; then |
| 115 | label="gpt" |
| 116 | ### msdos label types are detected earlier. |
| 117 | fi |
| 118 | # For the SUSE specific gpt_sync_mbr partitioning scheme |
| 119 | # see https://github.com/rear/rear/issues/544 |
| 120 | # For 'gpt_sync_mbr' labeled disks create_partitions was called e.g. as |
| 121 | # create_partitions /dev/sda gpt_sync_mbr |
| 122 | # so that $label is not empty but still set to 'gpt_sync_mbr' here. |
| 123 | |
| 124 | cat >> "$LAYOUT_CODE" <<EOF |
| 125 | create_disk_label $device $label |
| 126 | EOF |
| 127 | |
| 128 | # There are certrain conditions below that test for AUTORESIZE_PARTITIONS |
| 129 | # but all what belongs to autoresizing partitions must only happen in MIGRATION_MODE: |
| 130 | local autoresize_partitions="" |
| 131 | is_true "$MIGRATION_MODE" && autoresize_partitions="$AUTORESIZE_PARTITIONS" |
| 132 | |
| 133 | local block_size device_size sysfs_name |
| 134 | if [[ -b $device ]] ; then |
| 135 | sysfs_name=$(get_sysfs_name "$device") |
| 136 | if [[ "$sysfs_name" ]] && [[ -d "/sys/block/$sysfs_name" ]] ; then |
| 137 | block_size=$( get_block_size "$sysfs_name" ) |
| 138 | device_size=$( get_disk_size "$sysfs_name" ) |
| 139 | |
| 140 | ### GPT disks need 33 LBA blocks at the end of the disk |
| 141 | # For the SUSE specific gpt_sync_mbr partitioning scheme |
| 142 | # see https://github.com/rear/rear/issues/544 |
| 143 | # see https://github.com/rear/rear/pull/2142 for s390 partitioning |
| 144 | #if [[ "$label" == "gpt" || "$label" == "gpt_sync_mbr" || "$label" == "dasd" ]] ; then |
| 145 | if [[ "$label" == "gpt" || "$label" == "gpt_sync_mbr" ]] ; then |
| 146 | device_size=$( mathlib_calculate "$device_size - 33*$block_size" ) |
| 147 | # Only if resizing all partitions is explicitly wanted |
| 148 | # resizing of arbitrary partitions may also happen via the code below |
| 149 | # in addition to layout/prepare/default/430_autoresize_all_partitions.sh |
| 150 | if is_true "$autoresize_partitions" ; then |
| 151 | Log "Size reductions of GPT partitions probably needed." |
| 152 | fi |
| 153 | fi |
| 154 | fi |
| 155 | fi |
| 156 | |
| 157 | local start end start_mb end_mb number last_number |
| 158 | # let start=32768 # start after one cylinder 63*512 + multiple of 4k = 64*512 |
| 159 | let start=2097152 # start after cylinder 4096*512 (for grub2 - see issue #492) |
| 160 | let end=0 |
| 161 | let last_number=0 |
| 162 | |
| 163 | local flags partition |
| 164 | while read part disk size pstart name flags partition junk; do |
| 165 | |
| 166 | # Get the partition number from the name |
| 167 | number=$( get_partition_number "$partition" ) |
| 168 | |
| 169 | # Because parted creates partitions starting at number 1 consecutively, |
| 170 | # we expect the partition numbers to be increasing. Failing to do so |
| 171 | # will make the parted command setting the file system type die in |
| 172 | # error. |
| 173 | |
| 174 | if [[ $number -lt $last_number ]] ; then |
| 175 | # Admin probably reordered entries in disklayout.conf, die |
| 176 | Error "Device '$disk': partitions are not defined in expected order (partitions must be specified in ascending number)" |
| 177 | elif [[ $number -eq $last_number ]] ; then |
| 178 | Error "Device '$disk': partition with number $number is already defined" |
| 179 | elif [[ $( mathlib_calculate "$number - $last_number" ) -gt 1 ]] && [[ -z "$FEATURE_PARTED_ANYUNIT" ]] ; then |
| 180 | Error "Device '$disk': there are gaps between partitions, this is not supported" |
| 181 | fi |
| 182 | let last_number=$number |
| 183 | |
| 184 | # In layout/save/GNU/Linux/200_partition_layout.sh |
| 185 | # in particular a GPT partition name that can contain spaces |
| 186 | # like 'EFI System Partition' cf. https://github.com/rear/rear/issues/1563 |
| 187 | # was stored as a percent-encoded string in disklayout.conf |
| 188 | # so that here it needs to be percent-decoded: |
| 189 | name=$( percent_decode "$name" ) |
| 190 | |
| 191 | # Use the partition start value in disklayout.conf |
| 192 | # unless resizing all partitions is explicitly wanted: |
| 193 | if ! is_true "$autoresize_partitions" && test "$pstart" != "unknown" ; then |
| 194 | start="$pstart" |
| 195 | fi |
| 196 | |
| 197 | end=$(( start + size )) |
| 198 | |
| 199 | ### Test to make sure we're not past the end of the disk. |
| 200 | if [[ "$device_size" ]] && (( end > $device_size )) ; then |
| 201 | LogPrint "Partition $name on $device: size reduced to fit on disk." |
| 202 | Log "End changed from $end to $device_size." |
| 203 | end="$device_size" |
| 204 | fi |
| 205 | |
| 206 | # Extended partitions run to the end of disk (we assume) |
| 207 | # only if resizing all partitions is explicitly wanted: |
| 208 | if is_true "$autoresize_partitions" ; then |
| 209 | if [[ "$name" = "extended" ]] ; then |
| 210 | if [[ "$device_size" ]] ; then |
| 211 | end="$device_size" |
| 212 | else |
| 213 | ### We don't know the size of devices that don't exist yet |
| 214 | ### replaced by "100%" later on. |
| 215 | end= |
| 216 | fi |
| 217 | fi |
| 218 | fi |
| 219 | |
| 220 | # Avoid naming multiple partitions "rear-noname" as this will trigger systemd log messages |
| 221 | # "Dev dev-disk-by\x2dpartlabel-rear\x2dnoname.device appeared twice with different sysfs paths" |
| 222 | if [[ "$name" == "rear-noname" ]] ; then |
| 223 | name="$(basename "$partition")" |
| 224 | fi |
| 225 | |
| 226 | if [[ "$FEATURE_PARTED_ANYUNIT" ]] ; then |
| 227 | if [[ "$end" ]] ; then |
| 228 | end=$( mathlib_calculate "$end - 1" ) |
| 229 | fi |
| 230 | if [[ "$ARCH" == "Linux-s390" && "$label" == dasd ]] ; then |
| 231 | # LDL formatted disks are already partitioned and should not be partitioned with parted or fdasd , it will fail |
| 232 | if [ "$layout" == LDL ]; then |
| 233 | Debug "$device: LDL formatted DASD, do not create a partition" |
| 234 | else |
| 235 | Debug "$device: ${layout} formatted DASD, create a partition" |
| 236 | cat >> "$LAYOUT_CODE" <<EOF |
| 237 | create_disk_partition "$device" "$name" $number $start $end |
| 238 | EOF |
| 239 | fi |
| 240 | else |
| 241 | # default case when $ARCH is not "Linux-s390": |
| 242 | cat >> "$LAYOUT_CODE" <<EOF |
| 243 | create_disk_partition "$device" "$name" $number $start $end |
| 244 | EOF |
| 245 | fi |
| 246 | else |
| 247 | ### Old versions of parted accept only sizes in megabytes... |
| 248 | if (( $start > 0 )) ; then |
| 249 | start_mb=$( mathlib_calculate "$start / 1024 / 1024" ) |
| 250 | else |
| 251 | start_mb=0 |
| 252 | fi |
| 253 | end_mb=$( mathlib_calculate "$end / 1024 / 1024" ) |
| 254 | # The duplicated quoting "'$name'" is there because |
| 255 | # parted's internal parser needs single quotes for values with blanks. |
| 256 | # In particular a GPT partition name that can contain spaces |
| 257 | # like 'EFI System Partition' cf. https://github.com/rear/rear/issues/1563 |
| 258 | # so that when calling parted on command line it must be done like |
| 259 | # parted -s /dev/sdb unit MiB mkpart "'partition name'" 12 34 |
| 260 | # where the outer quoting "..." is for bash so that |
| 261 | # the inner quoting '...' is preserved for parted's internal parser: |
| 262 | cat >> "$LAYOUT_CODE" <<EOF |
| 263 | my_udevsettle |
| 264 | parted -s $device mkpart "'$name'" $start_mb $end_mb >&2 |
| 265 | my_udevsettle |
| 266 | EOF |
| 267 | fi |
| 268 | |
| 269 | # Only if resizing all partitions is explicitly wanted |
| 270 | # the start of the next partition is where this one ends. |
| 271 | # We can't use $end for extended partitions |
| 272 | # extended partitions have a small actual size as reported by sysfs |
| 273 | # but this issue is meanwhile fixed via https://github.com/rear/rear/pull/1733 by |
| 274 | # https://github.com/rear/rear/pull/1733/commits/6efb681d8b4c6a4d9f20b2900bbea79548c624a8 |
| 275 | # Additionally in front of a logical partition should be at least 512B empty space |
| 276 | # which is probably wrong because certain places in the Internet mention a required gap |
| 277 | # of at least 63 sectors (63 * 512 bytes) between extended partition and logical partition |
| 278 | # e.g. cf. the German Wikipedia article about Master Boot Record that reads (excerpts): |
| 279 | # Primaere und erweiterte Partitionstabelle |
| 280 | # ... |
| 281 | # Alte Betriebssysteme erwarten den Start einer Partition immer an den Zylindergrenzen. |
| 282 | # Daher ergibt sich auch heute noch bei verbreiteten Betriebssystemen eine Luecke |
| 283 | # von 63 Sektoren zwischen erweiterter Partitionstabelle und dem Startsektor |
| 284 | # der entsprechenden logischen Partition. |
| 285 | if is_true "$autoresize_partitions" && test "$name" = "logical" ; then |
| 286 | # Without analysis I <[email protected]> think by plain looking at the code |
| 287 | # that this '+ $block_size' results bad alignment because it usually adds 512B |
| 288 | # to the 'small actual size as reported by sysfs' which is e.g. 2 * 512B |
| 289 | # so that the result is the original start of disklayout.conf + 3 * 512B |
| 290 | # i.e. a new partition alignment to '3 * 512B' units: |
| 291 | start=$( mathlib_calculate "$start + ${size%B} + $block_size" ) |
| 292 | else |
| 293 | start=$( mathlib_calculate "$start + ${size%B}" ) |
| 294 | fi |
| 295 | |
| 296 | # round starting size to next multiple of 4096 |
| 297 | # 4096 is a good match for most device's block size |
| 298 | # only if resizing all partitions is explicitly wanted: |
| 299 | if is_true "$autoresize_partitions" ; then |
| 300 | start=$(( $start + 4096 - ( $start % 4096 ) )) |
| 301 | fi |
| 302 | |
| 303 | local flags="$( echo $flags | tr ',' ' ' )" |
| 304 | local flag |
| 305 | for flag in $flags ; do |
| 306 | if [[ "$flag" = "none" ]] ; then |
| 307 | continue |
| 308 | fi |
| 309 | ( |
| 310 | echo "my_udevsettle" |
| 311 | echo "parted -s $device set $number $flag on >&2" |
| 312 | echo "my_udevsettle" |
| 313 | ) >> $LAYOUT_CODE |
| 314 | done |
| 315 | |
| 316 | # Explicitly name GPT partitions. |
| 317 | # For the SUSE specific gpt_sync_mbr partitioning scheme |
| 318 | # see https://github.com/rear/rear/issues/544 |
| 319 | # The quoted duplicated quoting \"'$name'\" is there because |
| 320 | # parted's internal parser needs single quotes for values with blanks. |
| 321 | # In particular a GPT partition name that can contain spaces |
| 322 | # like 'EFI System Partition' cf. https://github.com/rear/rear/issues/1563 |
| 323 | # so that when calling parted on command line it must be done like |
| 324 | # parted -s /dev/sdb unit MiB mkpart "'partition name'" 12 34 |
| 325 | # where the outer quoting "..." is for bash which needs to be quoted \"...\" here |
| 326 | # because there is a outermost quoting "..." of the echo command |
| 327 | # and the inner quoting '...' is preserved for parted's internal parser: |
| 328 | if [[ "$label" = "gpt" || "$label" == "gpt_sync_mbr" ]] && [[ "$name" != "rear-noname" ]] ; then |
| 329 | ( |
| 330 | echo "my_udevsettle" |
| 331 | echo "parted -s $device name $number \"'$name'\" >&2" |
| 332 | echo "my_udevsettle" |
| 333 | ) >> $LAYOUT_CODE |
| 334 | fi |
| 335 | done < <(grep "^part $device " $LAYOUT_FILE) |
| 336 | |
| 337 | # This will override all partition setup previously made, |
| 338 | # and create exact copy of original disk layout |
| 339 | # (ugly, ugly, ugly, but works) |
| 340 | # TODO: add code for GPT |
| 341 | if is_true "$BLOCKCLONE_STRICT_PARTITIONING" && [ -n "$BLOCKCLONE_SAVE_MBR_DEV" ]; then |
| 342 | ( |
| 343 | echo "" |
| 344 | echo "# WARNING:" |
| 345 | echo "# This code will overwrite all partition changes previously made." |
| 346 | echo "# If you want avoid this, set BLOCKCLONE_STRICT_PARTITIONING=\"no\"" |
| 347 | echo "sfdisk $device < $VAR_DIR/layout/$BLOCKCLONE_PARTITIONS_CONF_FILE" |
| 348 | echo "dd if=$VAR_DIR/layout/$BLOCKCLONE_MBR_FILE of=$device bs=446 count=1" |
| 349 | echo "" |
| 350 | ) >> "$LAYOUT_CODE" |
| 351 | fi |
| 352 | |
| 353 | # Try to ensure the kernel uses the new partitioning |
| 354 | # see https://github.com/rear/rear/issues/793 |
| 355 | # First do a hardcoded sleep of 1 second so that |
| 356 | # the kernel and udev get a bit of time to process |
| 357 | # automated "read partition table changes" triggers |
| 358 | # of nowadays parted. |
| 359 | # Then to be backward compatible with traditional parted |
| 360 | # call partprobe explicitly to trigger the kernel |
| 361 | # to "read partition table changes" and if that fails |
| 362 | # wait 10 seconds before a first retry and if that fails |
| 363 | # wait 60 seconds before a final retry and if that fails |
| 364 | # ignore that failure and proceed "bona fide" because |
| 365 | # nowadays it should "just work" regardless of partprobe. |
| 366 | ( |
| 367 | echo "sleep 1" |
| 368 | echo "if ! partprobe -s $device >&2 ; then" |
| 369 | echo " LogPrint 'retrying partprobe $device after 10 seconds' " |
| 370 | echo " sleep 10" |
| 371 | echo " if ! partprobe -s $device >&2 ; then" |
| 372 | echo " LogPrint 'retrying partprobe $device after 1 minute' " |
| 373 | echo " sleep 60" |
| 374 | echo " if ! partprobe -s $device >&2 ; then" |
| 375 | echo " LogPrint 'partprobe $device failed, proceeding bona fide' " |
| 376 | echo " fi" |
| 377 | echo " fi" |
| 378 | echo "fi" |
| 379 | ) >> "$LAYOUT_CODE" |
| 380 | } |
| 381 | |
| 382 | # vim: set et ts=4 sw=4: |