[PATCH] Really duplicate the disk instead of readd every partition
Otavio Salvador
otavio at ossystems.com.br
Thu Aug 30 13:52:03 BST 2007
To avoid possible differences between the original disk layout and the
duplicated one, a raw copy is done. Has been identified a case[1]
where extended partitions had their positions changed due this.
1. http://bugs.debian.org/294520
The recipe[2] to reproduce the problem, on the provided URI, has been
used to produced a test and hence be sure it's not forgotten anymore.
2. http://bugs.debian.org/294520#34
The fix has been produced by Samuel Thibault <samuel.thibault at ens-lyon.org>
---
libparted/disk.c | 17 ++++---
libparted/tests/Makefile.am | 6 ++-
libparted/tests/disk.c | 103 +++++++++++++++++++++++++++++++++++++++++
libparted/tests/t2000-disk.sh | 27 +++++++++++
4 files changed, 144 insertions(+), 9 deletions(-)
create mode 100644 libparted/tests/disk.c
create mode 100644 libparted/tests/t2000-disk.sh
diff --git a/libparted/disk.c b/libparted/disk.c
index e2e55c3..34b1677 100644
--- a/libparted/disk.c
+++ b/libparted/disk.c
@@ -223,23 +223,24 @@ static int
_add_duplicate_part (PedDisk* disk, PedPartition* old_part)
{
PedPartition* new_part;
- PedConstraint* constraint_exact;
+ int ret;
new_part = disk->type->ops->partition_duplicate (old_part);
if (!new_part)
goto error;
new_part->disk = disk;
- constraint_exact = ped_constraint_exact (&new_part->geom);
- if (!constraint_exact)
+ _disk_push_update_mode (disk);
+ ret = _disk_raw_add (disk, new_part);
+ _disk_pop_update_mode (disk);
+ if (!ret)
goto error_destroy_new_part;
- if (!ped_disk_add_partition (disk, new_part, constraint_exact))
- goto error_destroy_constraint_exact;
- ped_constraint_destroy (constraint_exact);
+#ifdef DEBUG
+ if (!_disk_check_sanity (disk))
+ goto error_destroy_new_part;
+#endif
return 1;
-error_destroy_constraint_exact:
- ped_constraint_destroy (constraint_exact);
error_destroy_new_part:
ped_partition_destroy (new_part);
error:
diff --git a/libparted/tests/Makefile.am b/libparted/tests/Makefile.am
index 12ad29f..851dcd5 100644
--- a/libparted/tests/Makefile.am
+++ b/libparted/tests/Makefile.am
@@ -5,11 +5,15 @@
TESTS = t1000-label.sh
EXTRA_DIST = $(TESTS)
-bin_PROGRAMS = label
+bin_PROGRAMS = label disk
label_CFLAGS = $(CHECK_CFLAGS) -I$(top_srcdir)/include
label_LDADD = $(CHECK_LIBS) $(top_builddir)/libparted/libparted.la
label_SOURCES = common.h common.c label.c
+disk_CFLAGS = $(CHECK_CFLAGS) -I$(top_srcdir)/include
+disk_LDADD = $(CHECK_LIBS) $(top_builddir)/libparted/libparted.la
+disk_SOURCES = common.h common.c disk.c
+
MAINTAINERCLEANFILES = Makefile.in
CLEANFILES = init.sh
diff --git a/libparted/tests/disk.c b/libparted/tests/disk.c
new file mode 100644
index 0000000..25ec82a
--- /dev/null
+++ b/libparted/tests/disk.c
@@ -0,0 +1,103 @@
+#include <config.h>
+#include <unistd.h>
+
+#include <check.h>
+
+#include <parted/parted.h>
+
+#include "common.h"
+
+static char* temporary_disk;
+
+static void
+create_disk (void)
+{
+ temporary_disk = _create_disk (20);
+ fail_if (temporary_disk == NULL, "Failed to create temporary disk");
+}
+
+static void
+destroy_disk (void)
+{
+ unlink (temporary_disk);
+ free (temporary_disk);
+}
+
+/* TEST: Create a disklabel on a simple disk image */
+START_TEST (test_duplicate)
+{
+ PedDevice* dev = ped_device_get (temporary_disk);
+ if (dev == NULL)
+ return;
+
+ PedDiskType* type;
+ PedDisk* disk;
+ PedDisk* disk_dup;
+ PedPartition *part;
+ PedPartition *part_dup;
+ PedConstraint *constraint;
+
+ disk = _create_disk_label (dev, ped_disk_type_get ("msdos"));
+
+ constraint = ped_constraint_any (dev);
+
+ /* Primary partition from 10MB to 15MB */
+ part = ped_partition_new (disk, PED_PARTITION_NORMAL,
+ ped_file_system_type_get ("ext2"),
+ 19584, 29311);
+ ped_disk_add_partition (disk, part, constraint);
+
+ /* Primary partition from 16,4kB to 4981kB */
+ part = ped_partition_new (disk, PED_PARTITION_NORMAL,
+ ped_file_system_type_get ("ext2"),
+ 32, 9727);
+ ped_disk_add_partition (disk, part, constraint);
+
+ ped_disk_commit (disk);
+
+ ped_constraint_destroy (constraint);
+
+ disk_dup = ped_disk_duplicate (disk);
+
+ /* Checks if both partitions match */
+ for (int i = 1; i <= 2; i++) {
+ part = ped_disk_get_partition (disk, i);
+ part_dup = ped_disk_get_partition (disk_dup, i);
+
+ fail_if (part->geom.start != part_dup->geom.start ||
+ part->geom.end != part_dup->geom.end,
+ "Duplicated partition %d doesn't match. "
+ "Details are start: %d/%d end: %d/%d\n",
+ i, part->geom.start, part_dup->geom.start,
+ part->geom.end, part_dup->geom.end);
+ }
+
+ ped_disk_destroy (disk);
+ ped_device_destroy (dev);
+}
+END_TEST
+
+int
+main (void)
+{
+ int number_failed;
+ Suite* suite = suite_create ("Disk");
+ TCase* tcase_duplicate = tcase_create ("Duplicate");
+
+ /* Fail when an exception is raised */
+ ped_exception_set_handler (_test_exception_handler);
+
+ tcase_add_checked_fixture (tcase_duplicate, create_disk, destroy_disk);
+ tcase_add_test (tcase_duplicate, test_duplicate);
+ /* Disable timeout for this test */
+ tcase_set_timeout (tcase_duplicate, 0);
+ suite_add_tcase (suite, tcase_duplicate);
+
+ SRunner* srunner = srunner_create (suite);
+ srunner_run_all (srunner, CK_VERBOSE);
+
+ number_failed = srunner_ntests_failed (srunner);
+ srunner_free (srunner);
+
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/libparted/tests/t2000-disk.sh b/libparted/tests/t2000-disk.sh
new file mode 100644
index 0000000..7a85b98
--- /dev/null
+++ b/libparted/tests/t2000-disk.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# Copyright (C) 2007 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='run the disk unit tests in a directory supporting O_DIRECT'
+# This wrapper around the ./label binary is used to find a directory
+# in which one can open a file with the O_DIRECT flag.
+
+. ./init.sh
+
+test_expect_success \
+ 'run the actual tests' 'disk'
+
+test_done
--
1.5.3.rc4
--=-=-=
TIA,
--
O T A V I O S A L V A D O R
---------------------------------------------
E-mail: otavio at debian.org UIN: 5906116
GNU/Linux User: 239058 GPG ID: 49A5F855
Home Page: http://otavio.ossystems.com.br
---------------------------------------------
"Microsoft sells you Windows ... Linux gives
you the whole house."
--=-=-=--
More information about the Parted-maintainers
mailing list