summaryrefslogtreecommitdiff
path: root/tests/t0211-gpt-rewrite-header.sh
blob: d0f286fa020970ea1e1ef4653a1de1e125ba2dae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
# exercise parted's gpt-rewriting code
# When it encounters a GPT device with backup not at the end,
# parted (without -s) offers to correct that by moving the backup
# header to the end of the device.  Before parted-3.1, when it attempted
# to do that, starting with a 9-PTE array, it would render the result invalid.

# Copyright (C) 2012-2014, 2019-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. "${srcdir=.}/init.sh"; path_prepend_ ../parted $srcdir
require_perl_digest_crc_

# gpt-header-munge won't work on 32bit systems
require_64bit_

ss=$sector_size_

ns=100         # Initial number of sectors.
ns2=$((ns+64)) # Some larger number of sectors.
dev=loop-file
# create a file large enough to hold a GPT partition table
dd if=/dev/null of=$dev bs=$ss seek=$ns || framework_failure

# create a GPT partition table with 9 partitions in a standard 128-entry table.
parted -a min -s $dev mklabel gpt \
    mkpart p1 34s 34s \
    mkpart p2 35s 35s \
    mkpart p3 36s 36s \
    mkpart p4 37s 37s \
    mkpart p5 38s 38s \
    mkpart p6 39s 39s \
    mkpart p7 40s 40s \
    mkpart p8 41s 41s \
    mkpart p9 42s 42s \
  > out 2>&1 || fail=1
# expect no output
compare /dev/null out || fail=1

# Adjust the GPT table to have only 9 PTEs.
gpt-header-munge --sector-size=$ss --n=9 $dev || fail=1

# Extend size of the device.
dd if=/dev/null of=$dev bs=$ss seek=$ns2 || fail=1

printf '%s\n' f f > in || fail=1

# This would always succeed, even before.
parted ---pretend-input-tty $dev u s p < in || fail=1

printf '%s\n' \
    'BYT;' \
    "...:${ns2}s:file:$ss:$ss:gpt::;" \
    '1:34s:34s:1s::p1:;' \
    '2:35s:35s:1s::p2:;' \
    '3:36s:36s:1s::p3:;' \
    '4:37s:37s:1s::p4:;' \
    '5:38s:38s:1s::p5:;' \
    '6:39s:39s:1s::p6:;' \
    '7:40s:40s:1s::p7:;' \
    '8:41s:41s:1s::p8:;' \
    '9:42s:42s:1s::p9:;' \
  > exp || fail=1

# Before parted-3.1, this would fail, reporting that both tables are corrupt.
parted -m -s $dev u s p > out 2>&1 || fail=1
sed '2s/^[^:]*:/...:/' out > k && mv k out
compare exp out || fail=1

Exit $fail