Hans de Goede wrote:
> The new --align commandline option can have the following values:
> none:  Use the minimum alignment allowed by the disk type
> cyl:   Align partitions to cylinders (the default)
> min:   Use minimum alignment as given by the disk topology information
> opt:   Use optimum alignment as given by the disk topology information
>
> Note the min and opt values will use layout information provided by the
> disk to align the logical partition table addresses to actual physical
> blocks on the disks. The min value is the minimum aligment needed to
> align the partition properly to physical blocks, which avoids
> performance degradation. Where as the optimum aligment align's to a
> multiple of the physical block size in a way that guarantees optimal
> performance.
>
> The min and opt values will only work when compiled with
> libblkid >= 2.17 and running on a kernel >= 2.6.31, otherwise they will
> behave as the none --align value.
>
> * parted/parted.c(ALIGNMENT_ enum values): New enum.
> * parted/parted.c(options, options_help): Add --align option.
> * parted/parted.c(alignment): New global variable.
> * parted/parted.c(do_mkpart): Honor aligment variable.
> * parted/parted.c(_parse_options): handle --align option.

Here's an incremental patch (not including required update to
po/POTFILES.in).  Complete patch below.

diff --git a/bootstrap.conf b/bootstrap.conf
index 09870cd..b382bd7 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -26,6 +26,7 @@ avoided_gnulib_modules='
 gnulib_modules="
        $avoided_gnulib_modules
        alloca announce-gen assert
+       argmatch
        calloc config-h configmake
        canonicalize-lgpl
        close
diff --git a/parted/parted.c b/parted/parted.c
index f1c5cf0..7d910a4 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -19,6 +19,7 @@
 #include <config.h>
 #include <stdbool.h>

+#include "argmatch.h"
 #include "closeout.h"
 #include "configmake.h"
 #include "version-etc.h"
@@ -76,12 +77,30 @@ enum

 enum
 {
-        ALIGNMENT_NONE,
+        ALIGNMENT_NONE = 2,
         ALIGNMENT_CYLINDER,
         ALIGNMENT_MINIMAL,
-        ALIGNMENT_OPTIMAL,
+        ALIGNMENT_OPTIMAL
 };

+static char const *const align_args[] =
+{
+  "none",
+  "cylinder",
+  "minimal",
+  "optimal",
+  NULL
+};
+
+static int const align_types[] =
+{
+  ALIGNMENT_NONE,
+  ALIGNMENT_CYLINDER,
+  ALIGNMENT_MINIMAL,
+  ALIGNMENT_OPTIMAL
+};
+ARGMATCH_VERIFY (align_args, align_types);
+
 typedef struct {
         time_t  last_update;
         time_t  predicted_time_left;
@@ -2485,25 +2505,15 @@ while (1)
                 case 's': opt_script_mode = 1; break;
                 case 'v': version = 1; break;
                 case 'a':
-                        if (!strcmp(optarg, "none"))
-                                alignment = ALIGNMENT_NONE;
-                        else if (!strcmp(optarg, "cyl"))
-                                alignment = ALIGNMENT_CYLINDER;
-                        else if (!strcmp(optarg, "min"))
-                                alignment = ALIGNMENT_MINIMAL;
-                        else if (!strcmp(optarg, "opt"))
-                                alignment = ALIGNMENT_OPTIMAL;
-                        else {
-                                fprintf(stderr,
-                                        "%s: Invalid alignment value: %s\n",
-                                        program_name, optarg);
-                                wrong = 1;
-                        }
-                        break;
+                  alignment = XARGMATCH ("--align", optarg,
+                                         align_args, align_types);
+                  break;
                 case PRETEND_INPUT_TTY:
                   pretend_input_tty = 1;
                   break;
-                default:  wrong = 1; break;
+                default:
+                  wrong = 1;
+                  break;
         }
 }

Here's the complete change, including adjusted log entry:

>From 19108cb96271f6a19f656db0ed2e9016b3e8d615 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdego...@redhat.com>
Date: Thu, 10 Dec 2009 11:33:58 +0100
Subject: [PATCH 1/4] parted: add --align command-line option to specify mkpart 
alignment

The new --align command-line option can have the following values:
none:     Use the minimum alignment allowed by the disk type
cylinder: Align partitions to cylinders (the default)
minimal: Use minimum alignment as given by the disk topology information
optimal: Use optimum alignment as given by the disk topology information

Note the "minimal" and "optimal" values will use layout information
provided by the disk to align the logical partition table addresses
to actual physical blocks on the disks.  The "minimal" value is the
minimum aligment needed to align the partition properly to physical
blocks, which avoids performance degradation.  Whereas the "optimum"
alignment align's to a multiple of the physical block size in a way
that guarantees optimal performance.

The "minimal" and "optimal" values are useful only when parted is
compiled with libblkid >= 2.17 and running on a kernel >= 2.6.31,
otherwise they are equivalent to --align=none.

* parted/parted.c (ALIGNMENT_ enum values): New enum.
(options, options_help): Add --align option.
(align_args, align_types, alignment): New global variables.
(do_mkpart): Honor aligment variable.
(_parse_options): handle --align option.
* bootstrap.conf (gnulib_modules): Add argmatch.
---
 bootstrap.conf  |    1 +
 parted/parted.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++----
 po/POTFILES.in  |    1 +
 3 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/bootstrap.conf b/bootstrap.conf
index 09870cd..b382bd7 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -26,6 +26,7 @@ avoided_gnulib_modules='
 gnulib_modules="
        $avoided_gnulib_modules
        alloca announce-gen assert
+       argmatch
        calloc config-h configmake
        canonicalize-lgpl
        close
diff --git a/parted/parted.c b/parted/parted.c
index 099bc94..dba376d 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -19,6 +19,7 @@
 #include <config.h>
 #include <stdbool.h>

+#include "argmatch.h"
 #include "closeout.h"
 #include "configmake.h"
 #include "version-etc.h"
@@ -74,6 +75,31 @@ enum
   PRETEND_INPUT_TTY = CHAR_MAX + 1,
 };

+enum
+{
+        ALIGNMENT_NONE = 2,
+        ALIGNMENT_CYLINDER,
+        ALIGNMENT_MINIMAL,
+        ALIGNMENT_OPTIMAL
+};
+
+static char const *const align_args[] =
+{
+  "none",
+  "cylinder",
+  "minimal",
+  "optimal",
+  NULL
+};
+
+static int const align_types[] =
+{
+  ALIGNMENT_NONE,
+  ALIGNMENT_CYLINDER,
+  ALIGNMENT_MINIMAL,
+  ALIGNMENT_OPTIMAL
+};
+ARGMATCH_VERIFY (align_args, align_types);

 typedef struct {
         time_t  last_update;
@@ -87,6 +113,7 @@ static struct option const options[] = {
         {"machine",     0, NULL, 'm'},
         {"script",      0, NULL, 's'},
         {"version",     0, NULL, 'v'},
+        {"align",       required_argument, NULL, 'a'},
         {"-pretend-input-tty", 0, NULL, PRETEND_INPUT_TTY},
         {NULL,          0, NULL, 0}
 };
@@ -97,6 +124,7 @@ static const char *const options_help [][2] = {
         {"machine",     N_("displays machine parseable output")},
         {"script",      N_("never prompts for user intervention")},
         {"version",     N_("displays the version")},
+        {"align=[none|cyl|min|opt]", N_("alignment for new partitions")},
         {NULL,          NULL}
 };

@@ -105,6 +133,7 @@ int     pretend_input_tty = 0;
 int     opt_machine_mode = 0;
 int     disk_is_modified = 0;
 int     is_toggle_mode = 0;
+int     alignment = ALIGNMENT_CYLINDER;

 static const char* number_msg = N_(
 "NUMBER is the partition number used by Linux.  On MS-DOS disk labels, the "
@@ -587,7 +616,7 @@ print_options_help ()
         int             i;

         for (i=0; options_help [i][0]; i++) {
-                printf ("  -%c, --%-23.23s %s\n",
+                printf ("  -%c, --%-25.25s %s\n",
                         options_help [i][0][0],
                         options_help [i][0],
                         _(options_help [i][1]));
@@ -719,6 +748,11 @@ do_mkpart (PedDevice** dev)
         if (!disk)
                 goto error;

+        if (ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
+                if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
+                                       alignment == ALIGNMENT_CYLINDER))
+                        goto error_destroy_disk;
+
         if (!ped_disk_type_check_feature (disk->type, PED_DISK_TYPE_EXTENDED)) 
{
                 part_type = PED_PARTITION_NORMAL;
         } else {
@@ -771,7 +805,14 @@ do_mkpart (PedDevice** dev)
                         range_end);
         PED_ASSERT (user_constraint != NULL, return 0);

-        dev_constraint = ped_device_get_constraint (*dev);
+        if (alignment == ALIGNMENT_OPTIMAL)
+                dev_constraint =
+                        ped_device_get_optimal_aligned_constraint(*dev);
+        else if (alignment == ALIGNMENT_MINIMAL)
+                dev_constraint =
+                        ped_device_get_minimal_aligned_constraint(*dev);
+        else
+                dev_constraint = ped_device_get_constraint(*dev);
         PED_ASSERT (dev_constraint != NULL, return 0);

         final_constraint = ped_constraint_intersect (user_constraint,
@@ -2451,7 +2492,7 @@ int     opt, help = 0, list = 0, version = 0, wrong = 0;

 while (1)
 {
-        opt = getopt_long (*argc_ptr, *argv_ptr, "hlmsv",
+        opt = getopt_long (*argc_ptr, *argv_ptr, "hlmsva:",
                            options, NULL);
         if (opt == -1)
                 break;
@@ -2462,16 +2503,22 @@ while (1)
                 case 'm': opt_machine_mode = 1; break;
                 case 's': opt_script_mode = 1; break;
                 case 'v': version = 1; break;
+                case 'a':
+                  alignment = XARGMATCH ("--align", optarg,
+                                         align_args, align_types);
+                  break;
                 case PRETEND_INPUT_TTY:
                   pretend_input_tty = 1;
                   break;
-                default:  wrong = 1; break;
+                default:
+                  wrong = 1;
+                  break;
         }
 }

 if (wrong == 1) {
         fprintf (stderr,
-                 _("Usage: %s [-hlmsv] [DEVICE [COMMAND [PARAMETERS]]...]\n"),
+                 _("Usage: %s [-hlmsv] [-a<align>] [DEVICE [COMMAND 
[PARAMETERS]]...]\n"),
                  program_name);
         return 0;
 }
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 203252e..d1fcbdd 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -5,6 +5,7 @@
 # list of files containing translatable strings

 # lib
+lib/argmatch.c
 lib/closeout.c
 lib/error.c
 lib/getopt.c
--
1.6.6.rc1.319.g9b57d


_______________________________________________
bug-parted mailing list
bug-parted@gnu.org
http://lists.gnu.org/mailman/listinfo/bug-parted

Reply via email to