[PATCH] sun: initialize basic label fields

Karel Zak kzak at redhat.com
Fri Feb 19 15:12:52 UTC 2010


libparted completely ignores basic fields (version, nparts and sanity)
in the sun VTOC, then for example fdisk prints warnings for such
labels:

	# fdisk -lu /dev/sdb
	Detected sun disklabel with wrong version [0x00000000].
	Detected sun disklabel with wrong sanity [0x00000000].
	Detected sun disklabel with wrong num_partitions [0].

It seems that the current libparted sun support follows old Linux
kernel code, but the code was changed 3 years ago (see kernel commit
3961bae0ac030a70ae2e0578270203889021f1a1).

Note that the number of partitions (nparts) is optional for Linux
kernel, but fdisk(8) strictly requires SUN_DISK_MAXPARTITIONS (=8)
there. This patch sets nparts to SUN_DISK_MAXPARTITIONS rather then
use a real number of partitions.

The patch includes a new regression test for Sun label.

Signed-off-by: Karel Zak <kzak at redhat.com>
---
 NEWS                    |    6 +++++
 libparted/labels/sun.c  |   20 ++++++++++++++++-
 tests/t4001-sun-vtoc.sh |   52 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 76 insertions(+), 2 deletions(-)
 create mode 100755 tests/t4001-sun-vtoc.sh

diff --git a/NEWS b/NEWS
index 30ca181..97b27d5 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,12 @@ GNU parted NEWS                                    -*- outline -*-
   found to be corrupt.
   [bug introduced prior to parted-1.8.0]
 
+  sun: the version, sanity and nparts VTOC fields were ignored by libparted.
+  Those fields are properly initialized now. The nparts (number of partitions)
+  field is initialized to 8 (max. number of sun partitions) rather that to a
+  real number of partitions. This solution is compatible with Linux kernel 
+  and Linux fdisk.
+
   "make install" no longer installs tests programs named disk and label
 
   libparted: try harder to inform kernel of partition changes.
diff --git a/libparted/labels/sun.c b/libparted/labels/sun.c
index 9aeddcf..177a47c 100644
--- a/libparted/labels/sun.c
+++ b/libparted/labels/sun.c
@@ -44,6 +44,9 @@
 #define SUN_DISK_MAGIC		0xDABE	/* Disk magic number */
 #define SUN_DISK_MAXPARTITIONS	8
 
+#define SUN_VTOC_VERSION	1
+#define SUN_VTOC_SANITY		0x600DDEEE
+
 #define WHOLE_DISK_ID		0x05
 #define WHOLE_DISK_PART		2	/* as in 0, 1, 2 (3rd partition) */
 #define LINUX_SWAP_ID		0x82
@@ -68,9 +71,18 @@ struct __attribute__ ((packed)) _SunPartitionInfo {
 
 struct __attribute__ ((packed)) _SunRawLabel {
 	char 		info[128];	/* Informative text string */
-	u_int8_t	spare0[14];
+	u_int32_t	version;	/* Layout version */
+	u_int8_t	volume[8];	/* Volume name */
+	u_int16_t	nparts;		/* Number of partitions */
 	SunPartitionInfo infos[SUN_DISK_MAXPARTITIONS];
-	u_int8_t	spare1[246];	/* Boot information etc. */
+	u_int16_t	padding;	/* Alignment padding */
+	u_int32_t	bootinfo[3];	/* Info needed by mboot */
+	u_int32_t	sanity;		/* To verify vtoc sanity */
+	u_int32_t	reserved[10];	/* Free space */
+	u_int32_t	timestamp[8];	/* Partition timestamp */
+	u_int32_t	write_reinstruct; /* sectors to skip, writes */
+	u_int32_t	read_reinstruct; /* sectors to skip, reads */
+	u_int8_t	spare1[148];	/* Padding */
 	u_int16_t	rspeed;		/* Disk rotational speed */
 	u_int16_t	pcylcount;	/* Physical cylinder count */
 	u_int16_t	sparecyl;	/* extra sects per cylinder */
@@ -195,6 +207,10 @@ sun_alloc (const PedDevice* dev)
 	label->nsect	= PED_CPU_TO_BE16 (bios_geom->sectors);
 	label->ncyl	= PED_CPU_TO_BE16 (dev->length / cyl_size);
 
+	label->sanity   = PED_CPU_TO_BE32 (SUN_VTOC_SANITY);
+	label->version  = PED_CPU_TO_BE32 (SUN_VTOC_VERSION);
+	label->nparts   = PED_CPU_TO_BE16 (SUN_DISK_MAXPARTITIONS);
+
 	/* Add a whole disk partition at a minimum */
 	label->infos[WHOLE_DISK_PART].id = WHOLE_DISK_ID;
 	label->partitions[WHOLE_DISK_PART].start_cylinder = 0;
diff --git a/tests/t4001-sun-vtoc.sh b/tests/t4001-sun-vtoc.sh
new file mode 100755
index 0000000..ca92c3f
--- /dev/null
+++ b/tests/t4001-sun-vtoc.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# Ensure that Sun VTOC is properly initialized.
+
+# Copyright (C) 2009-2010 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/>.
+
+# Written by Karel Zak <kzak at redhat.com>
+
+test_description='test Sun VTOC initialization'
+
+: ${srcdir=.}
+. $srcdir/test-lib.sh
+
+N=2M
+dev=loop-file
+test_expect_success \
+    'create a file to simulate the underlying device' \
+    'dd if=/dev/null of=$dev bs=1 seek=$N 2> /dev/null'
+
+test_expect_success \
+    'label the test disk' \
+    'parted -s $dev mklabel sun > out 2>&1'
+test_expect_success 'expect no output' 'compare out /dev/null'
+
+test_expect_success \
+    'extract version' \
+    'od -t x1 -An -j128 -N4 $dev > out && echo " 00 00 00 01" > exp'
+test_expect_success 'expect it to be 00 00 00 01, not 00 00 00 00' 'compare out exp'
+
+test_expect_success \
+    'extract nparts' \
+    'od -t x1 -An -j140 -N2 $dev > out && echo " 00 08" > exp'
+test_expect_success 'expect it to be 00 08, not 00 00' 'compare out exp'
+
+test_expect_success \
+    'extract sanity magic' \
+    'od -t x1 -An -j188 -N4 $dev > out && echo " 60 0d de ee" > exp'
+test_expect_success 'expect it to be 60 0d de ee, not 00 00 00 00' 'compare out exp'
+
+test_done
-- 
1.6.6




More information about the parted-devel mailing list