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

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