|
|
4a4c91 |
From 099bdf30b9c73c6924e632e174a5e988972301df Mon Sep 17 00:00:00 2001
|
|
|
4a4c91 |
From: Scott Moser <smoser@ubuntu.com>
|
|
|
4a4c91 |
Date: Wed, 5 Sep 2018 22:32:39 -0400
|
|
|
4a4c91 |
Subject: [PATCH] growpart: fix bug when resizing a middle partition with
|
|
|
4a4c91 |
sgdisk.
|
|
|
4a4c91 |
|
|
|
4a4c91 |
Added a test and made growpart output consistent between sgdisk
|
|
|
4a4c91 |
and sfdisk resizers.
|
|
|
4a4c91 |
Thanks to Fred De Backer.
|
|
|
4a4c91 |
|
|
|
4a4c91 |
LP: #1706751
|
|
|
4a4c91 |
bzr-revno: 331
|
|
|
4a4c91 |
|
|
|
4a4c91 |
diff --git a/bin/growpart b/bin/growpart
|
|
|
4a4c91 |
index 4947ede..2700365 100755
|
|
|
4a4c91 |
--- a/bin/growpart
|
|
|
4a4c91 |
+++ b/bin/growpart
|
|
|
4a4c91 |
@@ -327,7 +327,9 @@ resize_sfdisk() {
|
|
|
4a4c91 |
>"${new_out}" ||
|
|
|
4a4c91 |
fail "failed to change size in output"
|
|
|
4a4c91 |
|
|
|
4a4c91 |
- change_info="partition=${PART} start=${pt_start} old: size=${pt_size} end=${pt_end} new: size=${new_size},end=${max_end}"
|
|
|
4a4c91 |
+ change_info="partition=${PART} start=${pt_start}"
|
|
|
4a4c91 |
+ change_info="${change_info} old: size=${pt_size} end=${pt_end}"
|
|
|
4a4c91 |
+ change_info="${change_info} new: size=${new_size} end=${max_end}"
|
|
|
4a4c91 |
if [ ${DRY_RUN} -ne 0 ]; then
|
|
|
4a4c91 |
echo "CHANGE: ${change_info}"
|
|
|
4a4c91 |
{
|
|
|
4a4c91 |
@@ -436,6 +438,8 @@ resize_sgdisk() {
|
|
|
4a4c91 |
pt_end=$(awk '$1 == '"${PART}"' { print $3 }' "${pt_data}") &&
|
|
|
4a4c91 |
[ -n "${pt_end}" ] ||
|
|
|
4a4c91 |
fail "${dev}: failed to get end sector"
|
|
|
4a4c91 |
+ # sgdisk start and end are inclusive. start 2048 length 10 ends at 2057.
|
|
|
4a4c91 |
+ pt_end=$((pt_end+1))
|
|
|
4a4c91 |
pt_size="$((${pt_end} - ${pt_start}))"
|
|
|
4a4c91 |
|
|
|
4a4c91 |
# Get the last usable sector
|
|
|
4a4c91 |
@@ -475,9 +479,9 @@ resize_sgdisk() {
|
|
|
4a4c91 |
|
|
|
4a4c91 |
# Calculate the new size of the partition
|
|
|
4a4c91 |
new_size=$((${pt_max} - ${pt_start}))
|
|
|
4a4c91 |
- old="old: size=${pt_size},end=${pt_end}"
|
|
|
4a4c91 |
- new="new: size=${new_size},end=${pt_max}"
|
|
|
4a4c91 |
- change_info="${dev}: start=${pt_start} ${old} ${new}"
|
|
|
4a4c91 |
+ change_info="partition=${PART} start=${pt_start}"
|
|
|
4a4c91 |
+ change_info="${change_info} old: size=${pt_size} end=${pt_end}"
|
|
|
4a4c91 |
+ change_info="${change_info} new: size=${new_size} end=${pt_max}"
|
|
|
4a4c91 |
|
|
|
4a4c91 |
# Backup the current partition table, we're about to modify it
|
|
|
4a4c91 |
rq sgd_backup $wouldrun sgdisk "--backup=${GPT_BACKUP}" "${DISK}" ||
|
|
|
4a4c91 |
@@ -492,7 +496,7 @@ resize_sgdisk() {
|
|
|
4a4c91 |
# - set the partition GUID
|
|
|
4a4c91 |
# - set the partition name
|
|
|
4a4c91 |
rq sgdisk_mod $wouldrun sgdisk --move-second-header "--delete=${PART}" \
|
|
|
4a4c91 |
- "--new=${PART}:${pt_start}:${pt_max}" \
|
|
|
4a4c91 |
+ "--new=${PART}:${pt_start}:$((pt_max-1))" \
|
|
|
4a4c91 |
"--typecode=${PART}:${code}" \
|
|
|
4a4c91 |
"--partition-guid=${PART}:${guid}" \
|
|
|
4a4c91 |
"--change-name=${PART}:${name}" "${DISK}" &&
|
|
|
4a4c91 |
diff --git a/test/test-growpart-fsimage-middle b/test/test-growpart-fsimage-middle
|
|
|
4a4c91 |
new file mode 100755
|
|
|
4a4c91 |
index 0000000..4f2dfbf
|
|
|
4a4c91 |
--- /dev/null
|
|
|
4a4c91 |
+++ b/test/test-growpart-fsimage-middle
|
|
|
4a4c91 |
@@ -0,0 +1,74 @@
|
|
|
4a4c91 |
+#!/bin/bash
|
|
|
4a4c91 |
+#
|
|
|
4a4c91 |
+# Create a disk image where a partition other than the last is grown.
|
|
|
4a4c91 |
+# brought up under bug 1706751, where we had an off-by-one error
|
|
|
4a4c91 |
+# when resizing with sgdisk.
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+set -e
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+TEMP_D=""
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+rq() {
|
|
|
4a4c91 |
+ local out="${TEMP_D}/out"
|
|
|
4a4c91 |
+ "$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
|
|
|
4a4c91 |
+}
|
|
|
4a4c91 |
+fail() { echo "FAILED:" "$@" 1>&2; exit 1; }
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+setup_img() {
|
|
|
4a4c91 |
+ local img_fp="$1" img=""
|
|
|
4a4c91 |
+ img=$(basename "$img_fp")
|
|
|
4a4c91 |
+ sfdisk "${img_fp}" <
|
|
|
4a4c91 |
+label: gpt
|
|
|
4a4c91 |
+label-id: 67CCB7DB-DEE7-4A7F-8F67-4DAC2B2F09A9
|
|
|
4a4c91 |
+device: ${img}
|
|
|
4a4c91 |
+unit: sectors
|
|
|
4a4c91 |
+first-lba: 34
|
|
|
4a4c91 |
+last-lba: 4194270
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+${img}1 : start= 2048, size= 204800, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=5ADB0402-936C-4AF3-8E56-74C161BCF925, name="misc fs"
|
|
|
4a4c91 |
+${img}2 : start= 206848, size= 524288, type=0657FD6D-A4AB-43C4-84E5-0933C84B4F4F, uuid=72174002-4428-427C-9DED-92479F1CDB4A, name="my swap"
|
|
|
4a4c91 |
+${img}3 : start= 731136, size= 819200, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=EE4FB792-DA02-4E1E-90DA-961D3A603225, name="root filesystem"
|
|
|
4a4c91 |
+${img}4 : start= 34, size= 2014, type=21686148-6449-6E6F-744E-656564454649, uuid=AC2AF951-6AD2-4B14-818F-BF457A4386AD, name="BIOS boot partition"
|
|
|
4a4c91 |
+${img}5 : start= 4061184, size= 133087, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4, uuid=FC3742E9-53E2-410C-8036-8B64DD9F2ED6, name="config drive"
|
|
|
4a4c91 |
+EOF
|
|
|
4a4c91 |
+}
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+cleanup() {
|
|
|
4a4c91 |
+ [ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
|
|
|
4a4c91 |
+}
|
|
|
4a4c91 |
+TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
|
|
|
4a4c91 |
+trap cleanup EXIT
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+expected="CHANGED: partition=3 start=731136 old: size=819200 end=1550336"
|
|
|
4a4c91 |
+expected="${expected} new: size=3330048 end=4061184"
|
|
|
4a4c91 |
+CR='
|
|
|
4a4c91 |
+'
|
|
|
4a4c91 |
+for resizer in sfdisk sgdisk; do
|
|
|
4a4c91 |
+ img="${TEMP_D}/disk-$resizer.img"
|
|
|
4a4c91 |
+ echo "====== Testing with resizer=$resizer ====="
|
|
|
4a4c91 |
+ rq truncate "--size=2G" "$img"
|
|
|
4a4c91 |
+ ( cd ${TEMP_D} && rq setup_img "${img##*/}" ) || fail "setup image $img"
|
|
|
4a4c91 |
+ echo "==== before ===="
|
|
|
4a4c91 |
+ ( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
|
|
|
4a4c91 |
+ err="${TEMP_D}/gp.err"
|
|
|
4a4c91 |
+ out="${TEMP_D}/gp.out"
|
|
|
4a4c91 |
+ if ! GROWPART_RESIZER=$resizer \
|
|
|
4a4c91 |
+ growpart -v -v "$img" 3 2>"$err" > "$out"; then
|
|
|
4a4c91 |
+ cat "$err" "$out"
|
|
|
4a4c91 |
+ fail "[resizer=$resizer] growpart failed"
|
|
|
4a4c91 |
+ fi
|
|
|
4a4c91 |
+ echo "==== after ===="
|
|
|
4a4c91 |
+ ( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
|
|
|
4a4c91 |
+ echo
|
|
|
4a4c91 |
+ echo "==== after sgdisk ==="
|
|
|
4a4c91 |
+ ( cd "${TEMP_D}" && sgdisk --print "${img##*/}" )
|
|
|
4a4c91 |
+ echo "==== growpart-stderr ==="
|
|
|
4a4c91 |
+ cat "$err"
|
|
|
4a4c91 |
+ echo "==== growpart-stdout ===="
|
|
|
4a4c91 |
+ cat "$out"
|
|
|
4a4c91 |
+ [ "$(cat $out)" = "$expected" ] || {
|
|
|
4a4c91 |
+ fail "[resizer=$resizer] output ^^^ did not match expected vvv:${CR}$expected"
|
|
|
4a4c91 |
+ }
|
|
|
4a4c91 |
+done
|
|
|
4a4c91 |
+
|
|
|
4a4c91 |
+# vi: ts=4 noexpandtab
|
|
|
4a4c91 |
--
|
|
|
4a4c91 |
2.17.2
|
|
|
4a4c91 |
|