Blame SOURCES/0001-growpart-fix-bug-occurring-if-start-sector-and-size-.patch

9e6b0a
From 827ca9237044f4821eb442fee1eef07ec7c3448c Mon Sep 17 00:00:00 2001
9e6b0a
From: Lars Kellogg-Stedman <lars@redhat.com>
9e6b0a
Date: Thu, 6 Dec 2018 15:32:35 -0500
9e6b0a
Subject: [PATCH] growpart: fix bug occurring if start sector and size were the
9e6b0a
 same.
9e6b0a
9e6b0a
The existing sed expression would erroneously change the start sector
9e6b0a
of a partition, rather than the size, if the start sector and size
9e6b0a
were identical.  This commit modifies the sed expression so that it
9e6b0a
will only operate on the final match in the line.
9e6b0a
9e6b0a
bzr-revno: 338.1.1
9e6b0a
(cherry picked from commit 7b11ac4d3abe16525639cff9198f5e7f8303280b)
9e6b0a
---
9e6b0a
 bin/growpart                          |  2 +-
9e6b0a
 test/test-growpart-start-matches-size | 75 +++++++++++++++++++++++++++
9e6b0a
 2 files changed, 76 insertions(+), 1 deletion(-)
9e6b0a
 create mode 100755 test/test-growpart-start-matches-size
9e6b0a
9e6b0a
diff --git a/bin/growpart b/bin/growpart
9e6b0a
index 13eda6e..4069fd4 100755
9e6b0a
--- a/bin/growpart
9e6b0a
+++ b/bin/growpart
9e6b0a
@@ -314,7 +314,7 @@ resize_sfdisk() {
9e6b0a
 	# now, change the size for this partition in ${dump_out} to be the
9e6b0a
 	# new size
9e6b0a
 	new_size=$((${max_end}-${pt_start}))
9e6b0a
-	sed "\|^\s*${dpart} |s/${pt_size},/${new_size},/" "${dump_out}" \
9e6b0a
+	sed "\|^\s*${dpart} |s/\(.*\)${pt_size},/\1${new_size},/" "${dump_out}" \
9e6b0a
 		>"${new_out}" ||
9e6b0a
 		fail "failed to change size in output"
9e6b0a
 
9e6b0a
diff --git a/test/test-growpart-start-matches-size b/test/test-growpart-start-matches-size
9e6b0a
new file mode 100755
9e6b0a
index 0000000..9967827
9e6b0a
--- /dev/null
9e6b0a
+++ b/test/test-growpart-start-matches-size
9e6b0a
@@ -0,0 +1,75 @@
9e6b0a
+#!/bin/bash
9e6b0a
+#
9e6b0a
+# Create a disk image where there exists a partition whose sizes matches the
9e6b0a
+# start sector.
9e6b0a
+# brought up under bug 1807171, which describes an error in the sed expression
9e6b0a
+# used to generate the replacement partition map
9e6b0a
+
9e6b0a
+set -e
9e6b0a
+
9e6b0a
+TEMP_D=""
9e6b0a
+
9e6b0a
+rq() {
9e6b0a
+	local out="${TEMP_D}/out"
9e6b0a
+	"$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
9e6b0a
+}
9e6b0a
+fail() { echo "FAILED:" "$@" 1>&2; exit 1; }
9e6b0a
+
9e6b0a
+setup_img() {
9e6b0a
+	local img_fp="$1" img=""
9e6b0a
+	img=$(basename "$img_fp")
9e6b0a
+	sfdisk "${img_fp}" <
9e6b0a
+label: gpt
9e6b0a
+label-id: db24000c-6ef3-4a17-b71c-1064baa29514
9e6b0a
+device: ${img}
9e6b0a
+unit: sectors
9e6b0a
+first-lba: 2048
9e6b0a
+last-lba: 4194270
9e6b0a
+
9e6b0a
+${img}1 : start=        2048, size=     1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5bc16165-bfc0-4e13-94eb-b898dc0bca41
9e6b0a
+${img}2 : start=     1026048, size=     1026048, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=a0e1636e-b759-4e7a-bd14-6f3d6c04745d
9e6b0a
+EOF
9e6b0a
+}
9e6b0a
+
9e6b0a
+cleanup() {
9e6b0a
+	[ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
9e6b0a
+}
9e6b0a
+TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
9e6b0a
+trap cleanup EXIT
9e6b0a
+
9e6b0a
+expected_sfdisk="CHANGED: partition=2 start=1026048 old: size=1026048 end=2052096 new: size=3168223 end=4194271"
9e6b0a
+expected_sgdisk="CHANGED: partition=2 start=1026048 old: size=1026048 end=2052096 new: size=3166208 end=4192256"
9e6b0a
+CR='
9e6b0a
+'
9e6b0a
+for resizer in sfdisk sgdisk; do
9e6b0a
+    expected_var_name="expected_$resizer"
9e6b0a
+	expected="${!expected_var_name}"
9e6b0a
+
9e6b0a
+	img="${TEMP_D}/disk-$resizer.img"
9e6b0a
+	echo "====== Testing with resizer=$resizer ====="
9e6b0a
+	rq truncate "--size=2G" "$img"
9e6b0a
+	( cd ${TEMP_D} && rq setup_img "${img##*/}" ) || fail "setup image $img"
9e6b0a
+	echo "==== before ===="
9e6b0a
+	( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
9e6b0a
+	err="${TEMP_D}/gp.err"
9e6b0a
+	out="${TEMP_D}/gp.out"
9e6b0a
+	if ! GROWPART_RESIZER=$resizer \
9e6b0a
+			growpart -v -v "$img" 2 2>"$err" > "$out"; then
9e6b0a
+		cat "$err" "$out"
9e6b0a
+		fail "[resizer=$resizer] growpart failed"
9e6b0a
+	fi
9e6b0a
+	echo "==== after ===="
9e6b0a
+	( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
9e6b0a
+	echo
9e6b0a
+    echo "==== after sgdisk ==="
9e6b0a
+	( cd "${TEMP_D}" && sgdisk --print "${img##*/}" )
9e6b0a
+	echo "==== growpart-stderr ==="
9e6b0a
+	cat "$err"
9e6b0a
+	echo "==== growpart-stdout ===="
9e6b0a
+	cat "$out"
9e6b0a
+	[ "$(cat $out)" = "$expected" ] || {
9e6b0a
+        fail "[resizer=$resizer] output ^^^ did not match expected vvv:${CR}$expected"
9e6b0a
+	}
9e6b0a
+done
9e6b0a
+
9e6b0a
+# vi: ts=4 noexpandtab
9e6b0a
-- 
9e6b0a
2.17.2
9e6b0a