~youscribe/parted/3.1

« back to all changes in this revision

Viewing changes to tests/t0280-gpt-corrupt.sh

  • Committer: Guilhem Lettron
  • Date: 2012-10-22 14:37:59 UTC
  • Revision ID: guilhem+ubuntu@lettron.fr-20121022143759-m403kecgz13sknvp
3.1 from tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# corrupt a GPT table; ensure parted takes notice
 
3
 
 
4
# Copyright (C) 2009-2012 Free Software Foundation, Inc.
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
. "${srcdir=.}/init.sh"; path_prepend_ ../parted
 
20
 
 
21
dev=loop-file
 
22
 
 
23
ss=$sector_size_
 
24
n_sectors=5000
 
25
 
 
26
dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
 
27
 
 
28
# create gpt label
 
29
parted -s $dev mklabel gpt > empty 2>&1 || fail=1
 
30
compare /dev/null empty || fail=1
 
31
 
 
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
 
35
 
 
36
# check for expected output
 
37
printf "BYT;\n$dev:${n_sectors}s:file:$sector_size_:$sector_size_:gpt::;\n" \
 
38
  > exp || fail=1
 
39
compare exp out || fail=1
 
40
 
 
41
# create a partition
 
42
parted -s $dev mkpart sw linux-swap 2048s 4095s > empty 2>&1 || fail=1
 
43
compare /dev/null empty || fail=1
 
44
 
 
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
 
48
 
 
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
 
52
 
 
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
 
57
 
 
58
# ----------------------------------------------------------
 
59
# Now, restore things, and corrupt the MyLBA in the alternate GUID table.
 
60
 
 
61
# Restore original byte
 
62
gpt_restore_primary_table_ $dev $ss "$orig_byte" || fail=1
 
63
 
 
64
# print the table
 
65
parted -s $dev print > out 2> err || fail=1
 
66
compare /dev/null err || fail=1
 
67
 
 
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
 
73
 
 
74
# Perturb it.
 
75
test x"$byte" = xA && new_byte=B || new_byte=A
 
76
 
 
77
# Replace good byte with the bad one.
 
78
poke_ $dev $alt_my_lba_offset "$new_byte" || fail=1
 
79
 
 
80
# attempting to set partition name must print a diagnostic
 
81
parted -m -s $dev name 1 foo > err 2>&1 || fail=1
 
82
 
 
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
 
87
 
 
88
# corruption is fixed; printing the table now elicits no diagnostic
 
89
parted -m -s $dev u s print > out 2>&1 || fail=1
 
90
 
 
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
 
95
 
 
96
Exit $fail