Blob Blame History Raw
From 827ca9237044f4821eb442fee1eef07ec7c3448c Mon Sep 17 00:00:00 2001
From: Lars Kellogg-Stedman <lars@redhat.com>
Date: Thu, 6 Dec 2018 15:32:35 -0500
Subject: [PATCH] growpart: fix bug occurring if start sector and size were the
 same.

The existing sed expression would erroneously change the start sector
of a partition, rather than the size, if the start sector and size
were identical.  This commit modifies the sed expression so that it
will only operate on the final match in the line.

bzr-revno: 338.1.1
(cherry picked from commit 7b11ac4d3abe16525639cff9198f5e7f8303280b)
---
 bin/growpart                          |  2 +-
 test/test-growpart-start-matches-size | 75 +++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100755 test/test-growpart-start-matches-size

diff --git a/bin/growpart b/bin/growpart
index 13eda6e..4069fd4 100755
--- a/bin/growpart
+++ b/bin/growpart
@@ -314,7 +314,7 @@ resize_sfdisk() {
 	# now, change the size for this partition in ${dump_out} to be the
 	# new size
 	new_size=$((${max_end}-${pt_start}))
-	sed "\|^\s*${dpart} |s/${pt_size},/${new_size},/" "${dump_out}" \
+	sed "\|^\s*${dpart} |s/\(.*\)${pt_size},/\1${new_size},/" "${dump_out}" \
 		>"${new_out}" ||
 		fail "failed to change size in output"
 
diff --git a/test/test-growpart-start-matches-size b/test/test-growpart-start-matches-size
new file mode 100755
index 0000000..9967827
--- /dev/null
+++ b/test/test-growpart-start-matches-size
@@ -0,0 +1,75 @@
+#!/bin/bash
+#
+# Create a disk image where there exists a partition whose sizes matches the
+# start sector.
+# brought up under bug 1807171, which describes an error in the sed expression
+# used to generate the replacement partition map
+
+set -e
+
+TEMP_D=""
+
+rq() {
+	local out="${TEMP_D}/out"
+	"$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
+}
+fail() { echo "FAILED:" "$@" 1>&2; exit 1; }
+
+setup_img() {
+	local img_fp="$1" img=""
+	img=$(basename "$img_fp")
+	sfdisk "${img_fp}" <<EOF
+label: gpt
+label-id: db24000c-6ef3-4a17-b71c-1064baa29514
+device: ${img}
+unit: sectors
+first-lba: 2048
+last-lba: 4194270
+
+${img}1 : start=        2048, size=     1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5bc16165-bfc0-4e13-94eb-b898dc0bca41
+${img}2 : start=     1026048, size=     1026048, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=a0e1636e-b759-4e7a-bd14-6f3d6c04745d
+EOF
+}
+
+cleanup() {
+	[ ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
+}
+TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
+trap cleanup EXIT
+
+expected_sfdisk="CHANGED: partition=2 start=1026048 old: size=1026048 end=2052096 new: size=3168223 end=4194271"
+expected_sgdisk="CHANGED: partition=2 start=1026048 old: size=1026048 end=2052096 new: size=3166208 end=4192256"
+CR='
+'
+for resizer in sfdisk sgdisk; do
+    expected_var_name="expected_$resizer"
+	expected="${!expected_var_name}"
+
+	img="${TEMP_D}/disk-$resizer.img"
+	echo "====== Testing with resizer=$resizer ====="
+	rq truncate "--size=2G" "$img"
+	( cd ${TEMP_D} && rq setup_img "${img##*/}" ) || fail "setup image $img"
+	echo "==== before ===="
+	( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
+	err="${TEMP_D}/gp.err"
+	out="${TEMP_D}/gp.out"
+	if ! GROWPART_RESIZER=$resizer \
+			growpart -v -v "$img" 2 2>"$err" > "$out"; then
+		cat "$err" "$out"
+		fail "[resizer=$resizer] growpart failed"
+	fi
+	echo "==== after ===="
+	( cd "${TEMP_D}" && sfdisk --dump "${img##*/}" )
+	echo
+    echo "==== after sgdisk ==="
+	( cd "${TEMP_D}" && sgdisk --print "${img##*/}" )
+	echo "==== growpart-stderr ==="
+	cat "$err"
+	echo "==== growpart-stdout ===="
+	cat "$out"
+	[ "$(cat $out)" = "$expected" ] || {
+        fail "[resizer=$resizer] output ^^^ did not match expected vvv:${CR}$expected"
+	}
+done
+
+# vi: ts=4 noexpandtab
-- 
2.17.2