OSDN Git Service

libparted: gpt: a smaller device need not render both headers invalid
authorJim Meyering <meyering@redhat.com>
Mon, 9 Jan 2012 08:21:09 +0000 (09:21 +0100)
committerJim Meyering <meyering@redhat.com>
Mon, 9 Jan 2012 11:02:14 +0000 (12:02 +0100)
Putting a valid GPT-labeled image on a smaller device, (i.e., one that
truncates the backup GPT header, but that does not impact the primary
one) should not render the primary header invalid.
* libparted/labels/gpt.c (_header_is_valid): Do *not* reject a header
when its last_usable value is larger than the device length.
This reverts part of commit v3.0-10-g99f9c6a, "gpt: don't abort for a
truncated GPT-formatted device".  With this change, running parted in
interactive mode with such an image/device combination will recognize
that only the backup header is corrupt, and will offer to correct it.
Before, it would report that both headers were corrupt.
* tests/t0203-gpt-shortened-device-primary-valid.sh: New file.
* tests/Makefile.am (TESTS): Add it.

libparted/labels/gpt.c
tests/Makefile.am
tests/t0203-gpt-shortened-device-primary-valid.sh [new file with mode: 0755]

index b812c4a..3fdd672 100644 (file)
@@ -4,7 +4,7 @@
     original version by Matt Domsch <Matt_Domsch@dell.com>
     Disclaimed into the Public Domain
 
-    Portions Copyright (C) 2001-2003, 2005-2011 Free Software Foundation, Inc.
+    Portions Copyright (C) 2001-2003, 2005-2012 Free Software Foundation, Inc.
 
     EFI GUID Partition Table handling
     Per Intel EFI Specification v1.02
@@ -670,10 +670,6 @@ _header_is_valid (PedDisk const *disk, GuidPartitionTableHeader_t *gpt,
   if (first_usable < 3)
     return 0;
 
-  PedSector last_usable = PED_LE64_TO_CPU (gpt->LastUsableLBA);
-  if (disk->dev->length < last_usable)
-    return 0;
-
   origcrc = gpt->HeaderCRC32;
   gpt->HeaderCRC32 = 0;
   if (pth_crc32 (dev, gpt, &crc) != 0)
index 89162ba..f442204 100644 (file)
@@ -15,6 +15,7 @@ TESTS = \
   t0201-gpt.sh \
   t0202-gpt-pmbr.sh \
   t0203-gpt-tiny-device-abort.sh \
+  t0203-gpt-shortened-device-primary-valid.sh \
   t0205-gpt-list-clobbers-pmbr.sh \
   t0206-gpt-print-with-corrupt-primary-clobbers-pmbr.sh \
   t0207-IEC-binary-notation.sh \
diff --git a/tests/t0203-gpt-shortened-device-primary-valid.sh b/tests/t0203-gpt-shortened-device-primary-valid.sh
new file mode 100755 (executable)
index 0000000..27f2c5f
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/sh
+# Demonstrate that placing a valid gpt-labeled image on a shorter device
+# does not invalidate the primary GPT header.
+
+# Copyright (C) 2012 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
+ss=$sector_size_
+
+dev=dev-file
+dd if=/dev/null of=$dev bs=$ss seek=100 || fail=1
+parted -s -- $dev mklabel gpt || fail=1
+
+# Chop off the last two sectors.
+dd if=/dev/null of=$dev bs=$ss seek=98 || fail=1
+printf 'ignore\nok\n' > in
+parted -m ---pretend-input-tty $dev u s p < in > out 2> err || fail=1
+
+# Remove abs name of $dev_file.
+sed "s, [^ ]*/$dev, $dev," err > k && mv k err || fail=1
+# Compare only the last line, to avoid control chars of interactive mode.
+tail -1 out > k && mv k out || fail=1
+sed "s,.*/$dev:,$dev:," out > k && mv k out || fail=1
+
+emit_superuser_warning > err.exp || fail=1
+cat <<EOF >> err.exp || fail=1
+Error: end of file while reading $dev
+Error: The backup GPT table is corrupt, but the primary appears OK, so that will be used.
+EOF
+
+echo "$dev:98s:file:$ss:$ss:gpt:;" > out.exp || fail=1
+
+compare err.exp err || fail=1
+compare out.exp out || fail=1
+
+Exit $fail