Blame SOURCES/0002-growpart-fix-bug-when-resizing-a-middle-partition-wi.patch

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