| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# Create a disk image where there exists a partition whose sizes matches the |
| 4 |
# start sector. |
| 5 |
# brought up under bug 1807171, which describes an error in the sed expression |
| 6 |
# used to generate the replacement partition map |
| 7 |
|
| 8 |
set -e |
| 9 |
|
| 10 |
TEMP_D="" |
| 11 |
|
| 12 |
rq() { |
| 13 |
local out="${TEMP_D}/out" |
| 14 |
"$@" > "$out" 2>&1 || { echo "FAILED:" "$@"; cat "$out"; return 1; } |
| 15 |
} |
| 16 |
fail() { echo "FAILED:" "$@" 1>&2; exit 1; } |
| 17 |
|
| 18 |
read_pt_info() { |
| 19 |
# read_pt_info(dump, <file><ptnum>, keyname) |
| 20 |
# parse sfdisk --dump output returning the value for keyname. |
| 21 |
# expecting to parse a single line like: |
| 22 |
# disk.img2 : start= 1026048, size= 1026048, |
| 23 |
# type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, |
| 24 |
# uuid=a0e1636e-b759-4e7a-bd14-6f3d6c04745d |
| 25 |
local file="$1" fpart="$2" key="$3" |
| 26 |
local cmd="" out="" |
| 27 |
cmd=( |
| 28 |
sed |
| 29 |
# delete any other line. |
| 30 |
-e "/^${fpart}[ ]*:/!d" |
| 31 |
# match key=<spaces><value>, |
| 32 |
-e "s/.* ${key}[ ]*=[ ]*\([^, ]*\).*/\1/" |
| 33 |
) |
| 34 |
out=$("${cmd[@]}" "$file") || |
| 35 |
{ fail "failed command: ${cmd[*]}"; return 1; } |
| 36 |
_RET="$out" |
| 37 |
} |
| 38 |
|
| 39 |
setup_image() { |
| 40 |
sfdisk disk.img <<EOF |
| 41 |
label: gpt |
| 42 |
label-id: db24000c-6ef3-4a17-b71c-1064baa29514 |
| 43 |
device: disk.img |
| 44 |
unit: sectors |
| 45 |
first-lba: 2048 |
| 46 |
last-lba: 4194270 |
| 47 |
|
| 48 |
disk.img1 : start= 2048, size= 1024000, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B, uuid=5bc16165-bfc0-4e13-94eb-b898dc0bca41 |
| 49 |
disk.img2 : start= 1026048, size= 1026048, type=4F68BCE3-E8CD-4DB1-96E7-FBCAF984B709, uuid=a0e1636e-b759-4e7a-bd14-6f3d6c04745d |
| 50 |
EOF |
| 51 |
} |
| 52 |
|
| 53 |
cleanup() { |
| 54 |
rm -rf "${TEMP_D}" |
| 55 |
} |
| 56 |
|
| 57 |
test_resize () { |
| 58 |
local resizer="$1" expected_start="$2" expected_size="$3" |
| 59 |
echo "====== Testing with resizer=$resizer =====" |
| 60 |
|
| 61 |
( |
| 62 |
cd ${TEMP_D} |
| 63 |
echo "$expected" > partitions.expected |
| 64 |
|
| 65 |
rq truncate -s 2G disk.img |
| 66 |
rq setup_image || fail "setup image $img" |
| 67 |
|
| 68 |
sfdisk --dump disk.img > partitions.before |
| 69 |
|
| 70 |
if ! GROWPART_RESIZER=$resizer \ |
| 71 |
growpart -v -v disk.img 2 2>"stderr" > "stdout"; then |
| 72 |
cat "stderr" "stdout" |
| 73 |
fail "[resizer=$resizer] growpart failed" |
| 74 |
fi |
| 75 |
|
| 76 |
sfdisk --dump disk.img > partitions.after |
| 77 |
read_pt_info partitions.after disk.img2 start |
| 78 |
start=$_RET |
| 79 |
read_pt_info partitions.after disk.img2 size |
| 80 |
size=$_RET |
| 81 |
[ "$expected_start" = "$start" ] && |
| 82 |
[ "$expected_size" = "$size" ] || { |
| 83 |
fail "$resizer failed. expected start/size != found start/size" \ |
| 84 |
"($expected_start/$expected_size != $start/$size)" |
| 85 |
} |
| 86 |
) |
| 87 |
} |
| 88 |
|
| 89 |
TEMP_D=$(mktemp -d ${TMPDIR:-/tmp}/${0##*/}.XXXXXX) |
| 90 |
trap cleanup EXIT |
| 91 |
|
| 92 |
test_resize sfdisk 1026048 3166208 |
| 93 |
test_resize sgdisk 1026048 3166208 |