1 From a7d850a3b39b160dcc23e12491cb2cc7c056cd01 Mon Sep 17 00:00:00 2001
2 From: "Brian C. Lane" <bcl@redhat.com>
3 Date: Wed, 28 Oct 2015 11:57:22 -0700
4 Subject: [PATCH 35/36] parted: Display details of partition alignment failure
7 When alignment for a new partition fails it isn't always obvious why it
8 failed. This adds printing the reason for the failure, in the form of:
10 start % grain != offset
12 This modifies align-check in interactive mode to print the alignment the
13 error details if it isn't aligned. Script mode behavior is unchanged.
15 Also cleanup pointer usage and handle asprintf failure by using a constant
16 string in the error report - "unknown (malloc failure)".
18 (cherry picked from commit 1726dbb4cd2dc4b19fe8d3c4b94e172fc0bd2c7c)
20 parted/parted.c | 64 +++++++++++++++++++++++++++++++++++++++++++++------------
21 1 file changed, 51 insertions(+), 13 deletions(-)
23 diff --git a/parted/parted.c b/parted/parted.c
24 index 18b778c..06f9971 100644
27 @@ -183,7 +183,8 @@ static TimerContext timer_context;
28 static int _print_list ();
29 static void _done (PedDevice* dev, PedDisk *diskp);
30 static bool partition_align_check (PedDisk const *disk,
31 - PedPartition const *part, enum AlignmentType a_type);
32 + PedPartition const *part, enum AlignmentType a_type,
36 _timer_handler (PedTimer* timer, void* context)
37 @@ -783,21 +784,27 @@ do_mkpart (PedDevice** dev, PedDisk** diskp)
41 + char *align_err = NULL;
42 if ((alignment == ALIGNMENT_OPTIMAL &&
43 - !partition_align_check(disk, part, PA_OPTIMUM)) ||
44 + !partition_align_check(disk, part, PA_OPTIMUM, &align_err)) ||
45 (alignment == ALIGNMENT_MINIMAL &&
46 - !partition_align_check(disk, part, PA_MINIMUM))) {
47 + !partition_align_check(disk, part, PA_MINIMUM, &align_err))) {
48 if (ped_exception_throw(
49 PED_EXCEPTION_WARNING,
52 : PED_EXCEPTION_IGNORE_CANCEL),
53 _("The resulting partition is not properly "
54 - "aligned for best performance.")) ==
55 + "aligned for best performance: %s"),
56 + align_err ? align_err : _("unknown (malloc failed)")) ==
57 PED_EXCEPTION_CANCEL) {
60 /* undo partition addition */
61 goto error_remove_part;
67 ped_exception_leave_all();
68 @@ -1629,10 +1636,18 @@ do_select (PedDevice** dev, PedDisk** diskp)
69 offset and alignment requirements. Also return true if there is
70 insufficient kernel support to determine DISK's alignment requirements.
71 Otherwise, return false. A_TYPE selects whether to check for minimal
72 - or optimal alignment requirements. */
73 + or optimal alignment requirements.
75 + If align_err is not NULL a string describing why the check failed
76 + will be allocated and returned. It is up to the caller to free this.
77 + Pass NULL if no error description is needed.
79 + If allocating the error string fails *align_err will be set to NULL, the
80 + caller should always check for this.
83 partition_align_check (PedDisk const *disk, PedPartition const *part,
84 - enum AlignmentType a_type)
85 + enum AlignmentType a_type, char **align_err)
87 PED_ASSERT (part->disk == disk);
88 PedDevice const *dev = disk->dev;
89 @@ -1641,10 +1656,20 @@ partition_align_check (PedDisk const *disk, PedPartition const *part,
90 ? ped_device_get_minimum_alignment (dev)
91 : ped_device_get_optimum_alignment (dev));
96 PED_ASSERT (pa->grain_size != 0);
97 bool ok = (part->geom.start % pa->grain_size == pa->offset);
99 + /* If it isn't aligned and the caller wants an explanation,
100 + show them the math. */
101 + if (!ok && align_err) {
102 + if (asprintf(align_err,
103 + "%llds %% %llds != %llds",
104 + part->geom.start, pa->grain_size, pa->offset) < 0) {
111 @@ -1665,12 +1690,25 @@ do_align_check (PedDevice **dev, PedDisk** diskp)
112 if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
115 - bool aligned = partition_align_check (*diskp, part, align_type);
116 - if (!opt_script_mode)
117 - printf(aligned ? _("%d aligned\n") : _("%d not aligned\n"), part->num);
119 - if (opt_script_mode)
120 - return aligned ? 1 : 0;
121 + char *align_err = NULL;
122 + bool aligned = partition_align_check (*diskp, part, align_type, &align_err);
124 + /* Don't print the error in script mode */
125 + if (opt_script_mode) {
128 + return aligned ? 1 : 0;
132 + printf(_("%d aligned\n"), part->num);
134 + printf(_("%d not aligned: %s\n"),
136 + align_err ? align_err : _("unknown (malloc failed)"));
141 /* Always return 1 in interactive mode, to be consistent
142 with the other modes. */