OSDN Git Service

parted: change default alignment to "optimal"
[android-x86/external-parted.git] / tests / t0280-gpt-corrupt.sh
1 #!/bin/sh
2 # corrupt a GPT table; ensure parted takes notice
3
4 # Copyright (C) 2009-2010 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 if test "$VERBOSE" = yes; then
20   set -x
21   parted --version
22 fi
23
24 : ${srcdir=.}
25 . $srcdir/t-lib.sh
26
27 peek()
28 {
29   case $# in 2) ;; *) echo "usage: peek FILE 0_BASED_OFFSET" >&2; exit 1;; esac
30   case $2 in *[^0-9]*) echo "peek: invalid offset: $2"; exit 1 ;; esac
31   dd if="$1" bs=1 skip="$2" count=1
32 }
33
34 poke()
35 {
36   case $# in 3) ;; *) echo "usage: poke FILE 0_BASED_OFFSET BYTE" >&2; exit 1;;
37     esac
38   case $2 in *[^0-9]*) echo "poke: invalid offset: $2"; exit 1 ;; esac
39   case $3 in ?) ;; *) echo "poke: invalid byte: '$3'"; exit 1 ;; esac
40   printf %s "$3" | dd of="$1" bs=1 seek="$2" count=1 conv=notrunc
41 }
42
43 dev=loop-file
44
45 ss=$sector_size_
46 n_sectors=5000
47
48 fail=0
49 dd if=/dev/null of=$dev bs=$ss seek=$n_sectors || fail=1
50
51 # create gpt label
52 parted -s $dev mklabel gpt > empty 2>&1 || fail=1
53 compare /dev/null empty || fail=1
54
55 # print the empty table
56 parted -m -s $dev unit s print > t 2>&1 || fail=1
57 sed "s,.*/$dev:,$dev:," t > out || fail=1
58
59 # check for expected output
60 printf "BYT;\n$dev:${n_sectors}s:file:$sector_size_:$sector_size_:gpt:;\n" \
61   > exp || fail=1
62 compare exp out || fail=1
63
64 # create a partition
65 parted -s $dev mkpart sw linux-swap 2048s 4095s > empty 2>&1 || fail=1
66 compare /dev/null empty || fail=1
67
68 # We're going to change the name of the first partition,
69 # thus invalidating the PartitionEntryArrayCRC32 checksum.
70
71 # byte 56 of the partition entry is the first byte of its 72-byte name field
72 pte_offset=$(expr $ss \* 2 + 56)
73
74 # get the first byte of the name
75 pte_byte=$(peek $dev $pte_offset) || fail=1
76
77 test x"$pte_byte" = xA && new_byte=B || new_byte=A
78
79 # Replace with a different byte
80 poke $dev $pte_offset "$new_byte" || fail=1
81
82 # printing the table must succeed, but with a scary diagnostic.
83 parted -s $dev print > err 2>&1 || fail=1
84 grep Error: err > k && mv k err || fail=1
85
86 # check for expected diagnostic
87 echo 'Error: The primary GPT table is corrupt, but the backup appears OK,' \
88     'so that will be used.' > exp || fail=1
89 compare exp err || fail=1
90
91 # ----------------------------------------------------------
92 # Now, restore things, and corrupt the MyLBA in the alternate GUID table.
93
94 orig_byte=s
95 # Restore original byte
96 poke $dev $pte_offset "$orig_byte" || fail=1
97
98 # print the table
99 parted -s $dev print > out 2> err || fail=1
100 compare /dev/null err || fail=1
101
102 # The MyLBA member of the alternate table is in the last sector,
103 # $n_sectors, 8-byte field starting at offset 24.
104 alt_my_lba_offset=$(expr $n_sectors \* $ss - $ss + 24)
105 # get the first byte of MyLBA
106 byte=$(peek $dev $alt_my_lba_offset) || fail=1
107
108 # Perturb it.
109 test x"$byte" = xA && new_byte=B || new_byte=A
110
111 # Replace good byte with the bad one.
112 poke $dev $alt_my_lba_offset "$new_byte" || fail=1
113
114 # attempting to set partition name must print a diagnostic
115 parted -m -s $dev name 1 foo > err 2>&1 || fail=1
116
117 # check for expected diagnostic
118 echo 'Error: The backup GPT table is corrupt, but the primary appears OK,' \
119     'so that will be used.' > exp || fail=1
120 compare exp err || fail=1
121
122 # corruption is fixed; printing the table now elicits no diagnostic
123 parted -m -s $dev u s print > out 2>&1 || fail=1
124
125 # check for expected output
126 printf "BYT;\nfile\n1:2048s:4095s:2048s::foo:;\n" > exp || fail=1
127 sed "s/.*gpt:;/file/" out > k && mv k out || fail=1
128 compare exp out || fail=1
129
130 Exit $fail