2
# corrupt a GPT table; ensure parted takes notice
4
# Copyright (C) 2009-2012 Free Software Foundation, Inc.
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9
# (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
16
# You should have received a copy of the GNU General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
. "${srcdir=.}/init.sh"; path_prepend_ ../parted
26
dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
29
parted -s $dev mklabel gpt > empty 2>&1 || fail=1
30
compare /dev/null empty || fail=1
32
# print the empty table
33
parted -m -s $dev unit s print > t 2>&1 || fail=1
34
sed "s,.*/$dev:,$dev:," t > out || fail=1
36
# check for expected output
37
printf "BYT;\n$dev:${n_sectors}s:file:$sector_size_:$sector_size_:gpt::;\n" \
39
compare exp out || fail=1
42
parted -s $dev mkpart sw linux-swap 2048s 4095s > empty 2>&1 || fail=1
43
compare /dev/null empty || fail=1
45
# We're going to change the name of the first partition,
46
# thus invalidating the PartitionEntryArrayCRC32 checksum.
47
orig_byte=$(gpt_corrupt_primary_table_ $dev $ss) || fail=1
49
# printing the table must succeed, but with a scary diagnostic.
50
parted -s $dev print > err 2>&1 || fail=1
51
grep Error: err > k && mv k err || fail=1
53
# check for expected diagnostic
54
echo 'Error: The primary GPT table is corrupt, but the backup appears OK,' \
55
'so that will be used.' > exp || fail=1
56
compare exp err || fail=1
58
# ----------------------------------------------------------
59
# Now, restore things, and corrupt the MyLBA in the alternate GUID table.
61
# Restore original byte
62
gpt_restore_primary_table_ $dev $ss "$orig_byte" || fail=1
65
parted -s $dev print > out 2> err || fail=1
66
compare /dev/null err || fail=1
68
# The MyLBA member of the alternate table is in the last sector,
69
# $n_sectors, 8-byte field starting at offset 24.
70
alt_my_lba_offset=$(expr $n_sectors \* $ss - $ss + 24)
71
# get the first byte of MyLBA
72
byte=$(peek_ $dev $alt_my_lba_offset) || fail=1
75
test x"$byte" = xA && new_byte=B || new_byte=A
77
# Replace good byte with the bad one.
78
poke_ $dev $alt_my_lba_offset "$new_byte" || fail=1
80
# attempting to set partition name must print a diagnostic
81
parted -m -s $dev name 1 foo > err 2>&1 || fail=1
83
# check for expected diagnostic
84
echo 'Error: The backup GPT table is corrupt, but the primary appears OK,' \
85
'so that will be used.' > exp || fail=1
86
compare exp err || fail=1
88
# corruption is fixed; printing the table now elicits no diagnostic
89
parted -m -s $dev u s print > out 2>&1 || fail=1
91
# check for expected output
92
printf "BYT;\nfile\n1:2048s:4095s:2048s::foo:;\n" > exp || fail=1
93
sed "s/.*gpt::;/file/" out > k && mv k out || fail=1
94
compare exp out || fail=1