[Parted-commits] GNU Parted Official Repository: Changes to 'master'
Jim Meyering
meyering at alioth.debian.org
Mon Feb 4 07:41:04 UTC 2008
libparted/disk.c | 67 +++++++++++++
libparted/labels/dvh.c | 6 -
parted/parted.c | 20 +++-
parted/ui.c | 4
tests/Makefile.am | 4
tests/t4100-dvh-partition-limits.sh | 169 ++++++++++++++++++++++++++++++++++
tests/t4100-msdos-partition-limits.sh | 168 +++++++++++++++++++++++++++++++++
7 files changed, 428 insertions(+), 10 deletions(-)
New commits:
commit cf282a908ec143e7ec8c8068fdc246b8bfaf8f33
Author: Jim Meyering <meyering at redhat.com>
Date: Mon Feb 4 08:35:17 2008 +0100
Don't write into line[-1] when line starts with a NUL byte.
* parted/ui.c (_readline): Check strlen first.
diff --git a/parted/ui.c b/parted/ui.c
index 370c2be..d9ab107 100644
--- a/parted/ui.c
+++ b/parted/ui.c
@@ -572,7 +572,9 @@ _readline (const char* prompt, const StrList* possibilities)
fputs (line, stdout);
fflush (stdout);
#endif
- line [strlen (line) - 1] = 0; /* kill trailing CR */
+ /* kill trailing NL */
+ if (strlen (line))
+ line [strlen (line) - 1] = 0;
} else {
free (line);
line = NULL;
commit 9f8b6836221804b7b8aa75456f9e38186253e1b4
Author: Jim Meyering <meyering at redhat.com>
Date: Sat Feb 2 17:00:42 2008 +0100
dvh.c (dvh_partition_set_name): Include partition name in diagnostic.
diff --git a/libparted/labels/dvh.c b/libparted/labels/dvh.c
index 9497ad5..3e377ef 100644
--- a/libparted/labels/dvh.c
+++ b/libparted/labels/dvh.c
@@ -1,6 +1,6 @@
/*
libparted - a library for manipulating disk partitions
- Copyright (C) 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
+ Copyright (C) 2001, 2002, 2005, 2007-2008 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
@@ -717,7 +717,9 @@ dvh_partition_set_name (PedPartition* part, const char* name)
ped_exception_throw (
PED_EXCEPTION_ERROR,
PED_EXCEPTION_CANCEL,
- _("Only logical partitions (boot files) have a name."));
+ _("failed to set dvh partition name to %s:\n"
+ "Only logical partitions (boot files) have a name."),
+ name);
#endif
}
}
diff --git a/parted/parted.c b/parted/parted.c
index f7f6dc3..46999f5 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -708,9 +708,9 @@ do_mkpart (PedDevice** dev)
goto error_destroy_disk;
}
-#if 1
- /* This undocumented _feature_, is next to useless, at least with
- a dvh partition table, since it makes the "mkpart" command
+ /* The undocumented feature that mkpart sometimes takes a
+ partition name is next to useless, at least with a dvh
+ partition table, since it makes the "mkpart" command
fail unconditionally for a primary partition. E.g.,
mkpart primary any-name xfs 4096s 5000s
requires the name, yet always fails, saying that only
@@ -723,7 +723,6 @@ do_mkpart (PedDevice** dev)
&& part_type != PED_PARTITION_LOGICAL))
part_name = command_line_get_word (_("Partition name?"),
"", NULL, 1);
-#endif
peek_word = command_line_peek_word ();
if (part_type == PED_PARTITION_EXTENDED
commit 71bbe2995371357c534dccf1eb3d436b399eaf32
Author: Jim Meyering <meyering at redhat.com>
Date: Thu Jan 10 14:51:56 2008 +0100
Enforce inherent limitations of dos and dvh partition table formats.
* libparted/disk.c (_check_partition): Enforce the 32-bit limitation
on a partition's starting sector number and length (in sectors).
With the usual 512-byte sector size, this limits the maximum
partition size to just under 2TB.
(_partition_max_start, _partition_max_len): New functions.
(_check_partition): Use them.
* tests/t4100-msdos-partition-limits.sh: New file. Test vs. msdos.
* tests/t4100-dvh-partition-limits.sh: New file. Test vs. dvh.
* tests/Makefile.am (TESTS): Add t4100-msdos-partition-limits.sh
and t4100-dvh-partition-limits.sh.
diff --git a/libparted/disk.c b/libparted/disk.c
index 087fbbf..ec09996 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -1,6 +1,6 @@
/*
libparted - a library for manipulating disk partitions
- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007
+ Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2007, 2008
Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
@@ -38,6 +38,7 @@
#include <parted/debug.h>
#include "architecture.h"
+#include "intprops.h"
#if ENABLE_NLS
# include <libintl.h>
@@ -1695,6 +1696,31 @@ _check_extended_partition (PedDisk* disk, PedPartition* part)
return 1;
}
+static PedSector
+_partition_max_start (char const *label_type)
+{
+ /* List partition table names (a la disk->type->name) for which
+ the partition length, in sectors, must fit in 32 bytes. */
+ static char const *const max_32[] = {"msdos", "dvh"};
+ unsigned int i;
+
+ for (i = 0; i < sizeof max_32 / sizeof *max_32; i++)
+ if (strcmp (label_type, max_32[i]) == 0)
+ return UINT32_MAX;
+
+ return TYPE_MAXIMUM (PedSector);
+}
+
+static PedSector
+_partition_max_len (char const *label_type)
+{
+ /* NOTE: for now, they happen to be the same, so don't
+ duplicate needlessly. Of course, if there's some format
+ with different length and starting sector limits, then
+ these functions will diverge. */
+ return _partition_max_start (label_type);
+}
+
static int
_check_partition (PedDisk* disk, PedPartition* part)
{
@@ -1735,6 +1761,45 @@ _check_partition (PedDisk* disk, PedPartition* part)
return 0;
}
+ if (!(part->type & PED_PARTITION_METADATA)) {
+ char const *label_type = disk->type->name;
+ /* Enforce some restrictions inherent in the DOS
+ partition table format. Without these, one would be able
+ to create a 2TB partition (or larger), and it would work,
+ but only until the next reboot. This was insidious: the
+ too-large partition would work initially, because with
+ Linux-2.4.x and newer we set the partition start sector
+ and length (in sectors) accurately and directly via the
+ BLKPG ioctl. However, only the last 32 bits of each
+ number would be written to the partition table, and the
+ next time the system would read/use those corrupted numbers
+ it would usually complain about an invalid partition.
+ The same applies to the starting sector number. */
+
+ if (part->geom.length > _partition_max_len (label_type)) {
+ ped_exception_throw (
+ PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("partition length of %jd sectors exceeds the "
+ "%s-partition-table-imposed maximum of %jd"),
+ part->geom.length,
+ label_type,
+ _partition_max_len (label_type));
+ return 0;
+ }
+
+ /* The starting sector number must fit in 32 bytes. */
+ if (part->geom.start > _partition_max_start (label_type)) {
+ ped_exception_throw (
+ PED_EXCEPTION_ERROR, PED_EXCEPTION_CANCEL,
+ _("starting sector number, %jd exceeds the"
+ " %s-partition-table-imposed maximum of %jd"),
+ part->geom.start,
+ label_type,
+ _partition_max_start (label_type));
+ return 0;
+ }
+ }
+
return 1;
}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 3a3020e..e493e46 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -7,7 +7,9 @@ TESTS = \
t2000-mkfs.sh \
t2100-mkswap.sh \
t3000-constraints.sh \
- t3100-resize-ext2-partion.sh
+ t3100-resize-ext2-partion.sh \
+ t4100-msdos-partition-limits.sh \
+ t4100-dvh-partition-limits.sh
EXTRA_DIST = \
$(TESTS) test-lib.sh mkdtemp
diff --git a/tests/t4100-dvh-partition-limits.sh b/tests/t4100-dvh-partition-limits.sh
new file mode 100755
index 0000000..6e0a3ee
--- /dev/null
+++ b/tests/t4100-dvh-partition-limits.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+# Copyright (C) 2008 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/>.
+
+test_description='enforce limits on partition start sector and length'
+
+# Need root privileges to use mount.
+privileges_required_=1
+
+. ./init.sh
+
+####################################################
+# Create and mount a file system capable of dealing with >=2TB files.
+# We must be able to create a file with an apparent length of 2TB or larger.
+# It needn't be a large file system.
+fs=fs_file
+mp=`pwd`/mount-point
+n=4096
+
+test_expect_success \
+ 'create an XFS file system' \
+ '
+ dd if=/dev/zero of=$fs bs=1MB count=2 seek=20 &&
+ mkfs.xfs -q $fs &&
+ mkdir "$mp"
+
+ '
+
+# Unmount upon interrupt, failure, etc., as well as upon normal completion.
+cleanup_() { cd "$test_dir_" && umount "$mp" > /dev/null 2>&1; }
+
+test_expect_success \
+ 'mount it' \
+ '
+ mount -o loop $fs "$mp" &&
+ cd "$mp"
+
+ '
+dev=loop-file
+
+do_mkpart()
+{
+ start_sector=$1
+ end_sector=$2
+ # echo '********' $(echo $end_sector - $start_sector + 1 |bc)
+ dd if=/dev/zero of=$dev bs=1b count=2k seek=$end_sector 2> /dev/null &&
+ parted -s $dev mklabel $table_type &&
+ parted -s $dev mkpart p xfs ${start_sector}s ${end_sector}s
+}
+
+# Specify the starting sector number and length in sectors,
+# rather than start and end.
+do_mkpart_start_and_len()
+{
+ start_sector=$1
+ len=$2
+ end_sector=$(echo $start_sector + $len - 1|bc)
+ do_mkpart $start_sector $end_sector
+}
+
+for table_type in dvh; do
+
+test_expect_success \
+ "$table_type: a partition length of 2^32-1 works." \
+ '
+ end=$(echo $n+2^32-2|bc) &&
+ do_mkpart $n $end
+ '
+
+test_expect_success \
+ 'print the result' \
+ 'parted -s $dev unit s p > out 2>&1 &&
+ sed -n "/^ *1 *$n/s/ */ /gp" out|sed "s/ *\$//" > k && mv k out &&
+ echo " 1 ${n}s ${end}s 4294967295s primary" > exp &&
+ diff -u out exp
+ '
+
+test_expect_failure \
+ "$table_type: a partition length of exactly 2^32 sectors provokes failure." \
+ 'do_mkpart $n $(echo $n+2^32-1|bc) > err 2>&1'
+
+bad_part_length()
+{ echo "Error: partition length of $1 sectors exceeds the"\
+ "$table_type-partition-table-imposed maximum of 4294967295"; }
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# FIXME: investigate this.
+# Unexpectedly to me, both of these failed with this same diagnostic:
+#
+# Error: partition length of 4294967296 sectors exceeds the \
+# DOS-partition-table-imposed maximum of 2^32-1" > exp &&
+#
+# I expected the one below to fail with a length of _4294967297_.
+# Debugging, I see that _check_partition *does* detect this,
+# but the diagnostic doesn't get displayed because of the wonders
+# of parted's exception mechanism.
+
+test_expect_failure \
+ "$table_type: a partition length of 2^32+1 sectors provokes failure." \
+ 'do_mkpart $n $(echo $n+2^32|bc) > err 2>&1'
+
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_part_length 4294967297 > exp && diff -u err exp'
+
+# =========================================================
+# Now consider partition starting sector numbers.
+bad_start_sector()
+{ echo "Error: starting sector number, $1 exceeds the"\
+ "$table_type-partition-table-imposed maximum of 4294967295"; }
+
+test_expect_success \
+ "$table_type: a partition start sector number of 2^32-1 works." \
+ 'do_mkpart_start_and_len $(echo 2^32-1|bc) 1000'
+
+# FIXME: this partition number 9 (not requested!) looks totally bogus
+# FIXME: For now, we just expect what the code produces.
+# FIXME: In the long run, figure out if it's sensible.
+cat > exp <<EOF
+Model: (file)
+Disk: 4294970342s
+Sector size (logical/physical): 512B/512B
+Partition Table: $table_type
+
+Number Start End Size Type File system Name Flags
+ 9 0s 4095s 4096s extended
+ 1 4294967295s 4294968294s 1000s primary
+
+EOF
+
+test_expect_success \
+ 'print the result' \
+ 'parted -s $dev unit s p > out 2>&1 &&
+ sed "s/Disk .*:/Disk:/;s/ *$//" out > k && mv k out &&
+ diff -u out exp
+ '
+
+test_expect_failure \
+ "$table_type: a partition start sector number of 2^32 must fail." \
+ 'do_mkpart_start_and_len $(echo 2^32|bc) 1000 > err 2>&1'
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+test_expect_failure \
+ "$table_type: a partition start sector number of 2^32+1 must fail, too." \
+ 'do_mkpart_start_and_len $(echo 2^32+1|bc) 1000 > err 2>&1'
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_start_sector 4294967297 > exp && diff -u err exp'
+
+done
+
+test_done
diff --git a/tests/t4100-msdos-partition-limits.sh b/tests/t4100-msdos-partition-limits.sh
new file mode 100755
index 0000000..cedbf83
--- /dev/null
+++ b/tests/t4100-msdos-partition-limits.sh
@@ -0,0 +1,168 @@
+#!/bin/sh
+
+# Copyright (C) 2008 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/>.
+
+test_description='enforce limits on partition start sector and length'
+
+# Need root privileges to use mount.
+privileges_required_=1
+
+. ./init.sh
+
+####################################################
+# Create and mount a file system capable of dealing with >=2TB files.
+# We must be able to create a file with an apparent length of 2TB or larger.
+# It needn't be a large file system.
+fs=fs_file
+mp=`pwd`/mount-point
+n=4096
+
+test_expect_success \
+ 'create an XFS file system' \
+ '
+ dd if=/dev/zero of=$fs bs=1MB count=2 seek=20 &&
+ mkfs.xfs -q $fs &&
+ mkdir "$mp"
+
+ '
+
+# Unmount upon interrupt, failure, etc., as well as upon normal completion.
+cleanup_() { cd "$test_dir_" && umount "$mp" > /dev/null 2>&1; }
+
+test_expect_success \
+ 'mount it' \
+ '
+ mount -o loop $fs "$mp" &&
+ cd "$mp"
+
+ '
+dev=loop-file
+
+do_mkpart()
+{
+ start_sector=$1
+ end_sector=$2
+ # echo '********' $(echo $end_sector - $start_sector + 1 |bc)
+ dd if=/dev/zero of=$dev bs=1b count=2k seek=$end_sector 2> /dev/null &&
+ parted -s $dev mklabel $table_type &&
+ parted -s $dev mkpart p xfs ${start_sector}s ${end_sector}s
+}
+
+# Specify the starting sector number and length in sectors,
+# rather than start and end.
+do_mkpart_start_and_len()
+{
+ start_sector=$1
+ len=$2
+ end_sector=$(echo $start_sector + $len - 1|bc)
+ do_mkpart $start_sector $end_sector
+}
+
+for table_type in msdos; do
+
+test_expect_success \
+ "$table_type: a partition length of 2^32-1 works." \
+ '
+ end=$(echo $n+2^32-2|bc) &&
+ do_mkpart $n $end
+ '
+
+test_expect_success \
+ 'print the result' \
+ 'parted -s $dev unit s p > out 2>&1 &&
+ sed -n "/^ *1 *$n/s/ */ /gp" out|sed "s/ *\$//" > k && mv k out &&
+ echo " 1 ${n}s ${end}s 4294967295s primary" > exp &&
+ diff -u out exp
+ '
+
+test_expect_failure \
+ "$table_type: a partition length of exactly 2^32 sectors provokes failure." \
+ 'do_mkpart $n $(echo $n+2^32-1|bc) > err 2>&1'
+
+bad_part_length()
+{ echo "Error: partition length of $1 sectors exceeds the"\
+ "$table_type-partition-table-imposed maximum of 4294967295"; }
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# FIXME: investigate this.
+# Unexpectedly to me, both of these failed with this same diagnostic:
+#
+# Error: partition length of 4294967296 sectors exceeds the \
+# DOS-partition-table-imposed maximum of 2^32-1" > exp &&
+#
+# I expected the one below to fail with a length of _4294967297_.
+# Debugging, I see that _check_partition *does* detect this,
+# but the diagnostic doesn't get displayed because of the wonders
+# of parted's exception mechanism.
+
+test_expect_failure \
+ "$table_type: a partition length of 2^32+1 sectors provokes failure." \
+ 'do_mkpart $n $(echo $n+2^32|bc) > err 2>&1'
+
+# FIXME: odd that we asked for 2^32+1, yet the diagnostic says 2^32
+# FIXME: Probably due to constraints.
+# FIXME: For now, just accept the current output.
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_part_length 4294967296 > exp && diff -u err exp'
+
+# =========================================================
+# Now consider partition starting sector numbers.
+bad_start_sector()
+{ echo "Error: starting sector number, $1 exceeds the"\
+ "$table_type-partition-table-imposed maximum of 4294967295"; }
+
+test_expect_success \
+ "$table_type: a partition start sector number of 2^32-1 works." \
+ 'do_mkpart_start_and_len $(echo 2^32-1|bc) 1000'
+
+cat > exp <<EOF
+Model: (file)
+Disk: 4294970342s
+Sector size (logical/physical): 512B/512B
+Partition Table: $table_type
+
+Number Start End Size Type File system Flags
+ 1 4294967295s 4294968294s 1000s primary
+
+EOF
+
+test_expect_success \
+ 'print the result' \
+ 'parted -s $dev unit s p > out 2>&1 &&
+ sed "s/Disk .*:/Disk:/;s/ *$//" out > k && mv k out &&
+ diff -u out exp
+ '
+
+test_expect_failure \
+ "$table_type: a partition start sector number of 2^32 must fail." \
+ 'do_mkpart_start_and_len $(echo 2^32|bc) 1000 > err 2>&1'
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+test_expect_failure \
+ "$table_type: a partition start sector number of 2^32+1 must fail, too." \
+ 'do_mkpart_start_and_len $(echo 2^32+1|bc) 1000 > err 2>&1'
+test_expect_success \
+ 'check for new diagnostic' \
+ 'bad_start_sector 4294967296 > exp && diff -u err exp'
+
+done
+
+test_done
commit f564981d2e5f4d5daad5a6f704dfa22ffaa9cf94
Author: Jim Meyering <meyering at redhat.com>
Date: Sat Feb 2 20:57:01 2008 +0100
mkpart: Don't require a DVH partition name if it's guaranteed to fail.
The mkpart command has an undocumented feature whereby it prompts for
(interactive) or requires (-s) a partition name, *regardless* of whether
it already knows the partition type (any thing but 'logical') is
incompatible with a name.
At first I was pissed and simply #if-0'd the offending code.
But in case someone is actually relying on it, I've relented, and
merely remove the prompt/requirement when the partition table type
is "dvh" and the type of the partition in question is not "logical".
* parted/parted.c (do_mkpart):
diff --git a/parted/parted.c b/parted/parted.c
index 738151b..f7f6dc3 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -1,7 +1,6 @@
/*
parted - a frontend to libparted
- Copyright (C) 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007
- Free Software Foundation, Inc.
+ Copyright (C) 1999-2003, 2005-2008 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
@@ -709,11 +708,23 @@ do_mkpart (PedDevice** dev)
goto error_destroy_disk;
}
+#if 1
+ /* This undocumented _feature_, is next to useless, at least with
+ a dvh partition table, since it makes the "mkpart" command
+ fail unconditionally for a primary partition. E.g.,
+ mkpart primary any-name xfs 4096s 5000s
+ requires the name, yet always fails, saying that only
+ logical partitions may have names.
+ If you want a name, use parted's separate "name" command. */
+
if (ped_disk_type_check_feature (disk->type,
- PED_DISK_TYPE_PARTITION_NAME))
+ PED_DISK_TYPE_PARTITION_NAME)
+ && ! (strcmp (disk->type->name, "dvh") == 0
+ && part_type != PED_PARTITION_LOGICAL))
part_name = command_line_get_word (_("Partition name?"),
- "", NULL, 1);
-
+ "", NULL, 1);
+#endif
+
peek_word = command_line_peek_word ();
if (part_type == PED_PARTITION_EXTENDED
|| (peek_word && isdigit (peek_word[0]))) {
More information about the Parted-commits
mailing list