Merge lp:~larsks/cloud-utils/bug-1807171 into lp:cloud-utils

Proposed by Lars Kellogg-Stedman
Status: Merged
Merged at revision: 339
Proposed branch: lp:~larsks/cloud-utils/bug-1807171
Merge into: lp:cloud-utils
Diff against target: 112 lines (+96/-1)
2 files modified
bin/growpart (+1/-1)
test/test-growpart-start-matches-size (+95/-0)
To merge this branch: bzr merge lp:~larsks/cloud-utils/bug-1807171
Reviewer Review Type Date Requested Status
Scott Moser (community) Approve
Review via email: mp+360264@code.launchpad.net

Commit message

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.

To post a comment you must log in.
Revision history for this message
Lars Kellogg-Stedman (larsks) wrote :

Ugh, so, the first stab a test is invalid because it only checks the output of growpart, and growpart lies. I'll submit an update in a moment.

Revision history for this message
Scott Moser (smoser) wrote :

2 problems with the test case:
a.) it has some tab/spaces indentation issues. for better or worse (probably worse), current expectation in those files is tabs.
b.) running it with current growpart does not catch the failure.

heres some modifications that catch the failure.
http://paste.ubuntu.com/p/BX5BkX7XFB/

integrate those , commit and i'll be happy.

thanks!

lp:~larsks/cloud-utils/bug-1807171 updated
340. By Lars Kellogg-Stedman

Fix test for bug #1807171

The test in the previous commit did not actually verify that the
final partition table matched the desired configuration.

Revision history for this message
Lars Kellogg-Stedman (larsks) wrote :

I fixed the test, but in a completely different fashion. I've tested the test case against both the fix and the previous commit to verify that it breaks when expected.

Revision history for this message
Scott Moser (smoser) wrote :

larsks,
this looks good. thank you.

review: Approve
Revision history for this message
Scott Moser (smoser) wrote :

Lars,

Looking at it a bit more I'mk going to ake a small change in how we read back
the content from the sfdisk dump. My initial suggestion was trying to be
more forgiving in sfdisk output. sfdisk --dump probably doesn't guarantee
that its output is identical across different versions. For the simplist
case imagine that they add a new field to the header. That would cause
the test to fail.

So what I've done here is add a 'read_pt_info' that tries to read
a specific key for a specific partition in --dump output in a relatively
forgiving way.

Basically... I just don't want to babysit test failures that are not
test failures in the event of sfdisk changing its output.

Revision history for this message
Lars Kellogg-Stedman (larsks) wrote :

It seems that we need to trust the output of sfdisk --dump because we rely on it in growpart. The simplest fix would be to just discard the headers and only look at the partition lines. In any case, I'm happy as long as you're happy :).

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/growpart'
2--- bin/growpart 2018-10-19 16:46:40 +0000
3+++ bin/growpart 2018-12-06 22:40:58 +0000
4@@ -323,7 +323,7 @@
5 # now, change the size for this partition in ${dump_out} to be the
6 # new size
7 new_size=$((${max_end}-${pt_start}))
8- sed "\|^\s*${dpart} |s/${pt_size},/${new_size},/" "${dump_out}" \
9+ sed "\|^\s*${dpart} |s/\(.*\)${pt_size},/\1${new_size},/" "${dump_out}" \
10 >"${new_out}" ||
11 fail "failed to change size in output"
12
13
14=== added file 'test/test-growpart-start-matches-size'
15--- test/test-growpart-start-matches-size 1970-01-01 00:00:00 +0000
16+++ test/test-growpart-start-matches-size 2018-12-06 22:40:58 +0000
17@@ -0,0 +1,95 @@
18+#!/bin/bash
19+#
20+# Create a disk image where there exists a partition whose sizes matches the
21+# start sector.
22+# brought up under bug 1807171, which describes an error in the sed expression
23+# used to generate the replacement partition map
24+
25+set -e
26+
27+TEMP_D=""
28+
29+rq() {
30+ local out="${TEMP_D}/out"
31+ "$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; }
32+}
33+fail() { echo "FAILED:" "$@" 1>&2; exit 1; }
34+
35+setup_image() {
36+ sfdisk disk.img <<EOF
37+label: gpt
38+label-id: db24000c-6ef3-4a17-b71c-1064baa29514
39+device: disk.img
40+unit: sectors
41+first-lba: 2048
42+last-lba: 4194270
43+
44+disk.img1 : start= 2048, size= 1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5bc16165-bfc0-4e13-94eb-b898dc0bca41
45+disk.img2 : start= 1026048, size= 1026048, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=a0e1636e-b759-4e7a-bd14-6f3d6c04745d
46+EOF
47+}
48+
49+cleanup() {
50+ rm -rf "${TEMP_D}"
51+}
52+
53+TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX)
54+trap cleanup EXIT
55+
56+# the sfdisk and sgdisk resizers result in slightly different output, because
57+# of course they do.
58+
59+expected_sfdisk='label: gpt
60+label-id: DB24000C-6EF3-4A17-B71C-1064BAA29514
61+device: disk.img
62+unit: sectors
63+first-lba: 2048
64+last-lba: 4194270
65+
66+disk.img1 : start= 2048, size= 1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5BC16165-BFC0-4E13-94EB-B898DC0BCA41
67+disk.img2 : start= 1026048, size= 3168223, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=A0E1636E-B759-4E7A-BD14-6F3D6C04745D'
68+
69+expected_sgdisk='label: gpt
70+label-id: DB24000C-6EF3-4A17-B71C-1064BAA29514
71+device: disk.img
72+unit: sectors
73+first-lba: 2048
74+last-lba: 4192256
75+
76+disk.img1 : start= 2048, size= 1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5BC16165-BFC0-4E13-94EB-B898DC0BCA41
77+disk.img2 : start= 1026048, size= 3166208, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=A0E1636E-B759-4E7A-BD14-6F3D6C04745D'
78+
79+
80+CR='
81+'
82+
83+test_resize () {
84+ local resizer=$1
85+ local expected=$2
86+ echo "====== Testing with resizer=$resizer ====="
87+
88+ (
89+ cd ${TEMP_D}
90+ echo "$expected" > partitions.expected
91+
92+ rq truncate "--size=2G" disk.img
93+ rq setup_image || fail "setup image $img"
94+
95+ sfdisk --dump disk.img > partitions.before
96+
97+ if ! GROWPART_RESIZER=$resizer \
98+ growpart -v -v disk.img 2 2>"stderr" > "stdout"; then
99+ cat "stderr" "stdout"
100+ fail "[resizer=$resizer] growpart failed"
101+ fi
102+
103+ sfdisk --dump disk.img > partitions.after
104+ if ! diff -U0 partitions.expected partitions.after; then
105+ fail "[resizer=$resizer] partition table is incorrect"
106+ fi
107+ )
108+}
109+
110+set -e
111+test_resize sfdisk "$expected_sfdisk"
112+test_resize sgdisk "$expected_sgdisk"

Subscribers

People subscribed via source and target branches