[parted-devel] [PATCH v3 2/2] parted: Fix resizepart and rm command

Sebastian Parschauer sparschauer at suse.de
Tue Nov 7 16:23:56 UTC 2017


In script mode the resizepart command fails when shrinking and
if the partition is busy. Also the warnings printed in this
case are only applicable to interactive mode. A similar problem
exists with the rm command.
So print different warnings in script mode and continue if growing
a busy partition. Require the '--force' option to be set in order
to shrink or remove a busy partition. Shrinking cannot be more
dangerous in script mode than removing. So allow shrinking a
partition in script mode which is not busy.

In interactive mode there is a problem if providing the partition
number and the end of the partition as arguments to the resizepart
command directly with a busy partition. The warning is shown and
after continuing anyway parted asks for the partition end although
it has already been provided. So count the number of words on
command line and warn after processing all of them or after getting
the partition number.

NEWS: Mention the bugfix.

Fixes: 21c58e17c473 ("parted: add resizepart command")
Reported-by: Arvin Schnell <aschnell at suse.com>
Signed-off-by: Sebastian Parschauer <sparschauer at suse.de>
---
 NEWS            |  3 +++
 parted/parted.c | 39 ++++++++++++++++++++++++++++++++-------
 2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/NEWS b/NEWS
index bb15212b245e..627ff30f2122 100644
--- a/NEWS
+++ b/NEWS
@@ -6,6 +6,9 @@ GNU parted NEWS                                    -*- outline -*-
 
 ** Bug Fixes
 
+  Fix resizepart command with busy partitions and in script mode.
+  Allow enforcing shrinking or removing busy partitions in script mode.
+
   Fix a udev cookie leak when using resizepart on device-mapper devices.
 
   Fix a gettext crash/error sometimes when using localized languages.
diff --git a/parted/parted.c b/parted/parted.c
index cf6b68b2a72d..fbaf87af9f83 100644
--- a/parted/parted.c
+++ b/parted/parted.c
@@ -222,13 +222,19 @@ _timer_handler (PedTimer* timer, void* context)
 }
 
 static int
-_partition_warn_busy (PedPartition* part)
+_partition_warn_busy (PedPartition* part, bool dangerous)
 {
         char* path;
 
         if (ped_partition_is_busy (part)) {
                 path = ped_partition_get_path (part);
-                if (ped_exception_throw (
+                if (opt_script_mode && (!dangerous || force)) {
+                        ped_exception_throw (
+                            PED_EXCEPTION_WARNING,
+                            PED_EXCEPTION_UNHANDLED,
+                            _("Partition %s is being used, continuing anyway."),
+                            path);
+                } else if (ped_exception_throw (
                             PED_EXCEPTION_WARNING,
                             PED_EXCEPTION_YES_NO,
                             _("Partition %s is being used. Are you sure you " \
@@ -1548,6 +1554,11 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
         PedSector               start, end, oldend;
         PedGeometry             *range_end = NULL;
         PedConstraint*          constraint;
+        int                     cmdline_words = command_line_get_word_count();
+        /* update this if adding/removing arguments to/from this command */
+        const int               part_idx = 1;
+        const int               end_idx = 2;
+        const bool              danger_if_busy = false;
         int rc = 0;
 
         if (!disk) {
@@ -1564,26 +1575,39 @@ do_resizepart (PedDevice** dev, PedDisk** diskp)
 
         if (!command_line_get_partition (_("Partition number?"), disk, &part))
                 goto error;
-        if (!_partition_warn_busy (part))
+        /* warn early if the partition end is not provided on cmdline */
+        if (cmdline_words <= part_idx && !_partition_warn_busy (part, danger_if_busy))
                 goto error;
-
         start = part->geom.start;
         end = oldend = part->geom.end;
         if (!command_line_get_sector (_("End?"), *dev, &end, &range_end, NULL))
                 goto error;
+        if (cmdline_words >= end_idx && !_partition_warn_busy (part, danger_if_busy))
+                goto error;
+
         /* Do not move start of the partition */
         constraint = constraint_from_start_end_fixed_start (*dev, start, range_end);
         if (!ped_disk_set_partition_geom (disk, part, constraint,
                                           start, end))
                 goto error_destroy_constraint;
         /* warn when shrinking partition - might lose data */
-        if (part->geom.end < oldend)
-                if (ped_exception_throw (
+        if (part->geom.end < oldend) {
+                if (opt_script_mode && (!ped_partition_is_busy (part) || force)) {
+                        char *path = ped_partition_get_path (part);
+                        ped_exception_throw (
+                            PED_EXCEPTION_WARNING,
+                            PED_EXCEPTION_UNHANDLED,
+                            _("Shrinking partition %s, data loss possible."), path);
+                        free(path);
+                } else if (ped_exception_throw (
                             PED_EXCEPTION_WARNING,
                             PED_EXCEPTION_YES_NO,
                             _("Shrinking a partition can cause data loss, " \
                               "are you sure you want to continue?")) != PED_EXCEPTION_YES)
+                {
                         goto error_destroy_constraint;
+                }
+        }
         ped_disk_commit (disk);
 
         if ((*dev)->type != PED_DEVICE_FILE)
@@ -1604,6 +1628,7 @@ static int
 do_rm (PedDevice** dev, PedDisk** diskp)
 {
         PedPartition*           part = NULL;
+        const bool              danger_if_busy = true;
 
         if (!*diskp)
                 *diskp = ped_disk_new (*dev);
@@ -1612,7 +1637,7 @@ do_rm (PedDevice** dev, PedDisk** diskp)
 
         if (!command_line_get_partition (_("Partition number?"), *diskp, &part))
                 goto error;
-        if (!_partition_warn_busy (part))
+        if (!_partition_warn_busy (part, danger_if_busy))
                 goto error;
 
         if (!ped_disk_delete_partition (*diskp, part))
-- 
2.13.6




More information about the parted-devel mailing list