From: Sebastian Parschauer Date: Tue, 7 Nov 2017 14:09:46 +0100 Subject: parted: Fix resizepart and rm command References: bsc#1058667 Patch-mainline: not yet, based on v3 submitted on 2017-11-07 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 '--ignore-busy' 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. Fixes: 21c58e17c473 ("parted: add resizepart command") Reported-by: Arvin Schnell Signed-off-by: Sebastian Parschauer --- parted/parted.c | 39 ++++++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 7 deletions(-) Index: parted-3.3/parted/parted.c =================================================================== --- parted-3.3.orig/parted/parted.c +++ parted-3.3/parted/parted.c @@ -229,13 +229,19 @@ _timer_handler (PedTimer* timer, void* c } 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 || ignore_busy)) { + 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 " \ @@ -1655,6 +1661,11 @@ do_resizepart (PedDevice** dev, PedDisk* 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) { @@ -1671,7 +1682,8 @@ do_resizepart (PedDevice** dev, PedDisk* 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; @@ -1681,19 +1693,32 @@ do_resizepart (PedDevice** dev, PedDisk* goto error; _adjust_end_if_iec(&start, &end, range_end, end_input); free(end_input); + 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) || ignore_busy)) { + 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; + { + goto error_destroy_constraint; + } + } ped_disk_commit (disk); if ((*dev)->type != PED_DEVICE_FILE) @@ -1714,6 +1739,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); @@ -1722,7 +1748,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))