Blame SOURCES/0048-parted-add-resizepart-command.patch

fc4a62
From 109a5a35d8482f43c7d779df3b7d10bf16d5f31b Mon Sep 17 00:00:00 2001
fc4a62
From: Petr Uzel <petr.uzel@suse.cz>
fc4a62
Date: Mon, 26 Sep 2011 17:21:01 +0200
fc4a62
Subject: [PATCH 48/48] parted: add resizepart command
fc4a62
fc4a62
Add resizepart command to resize ( change the end position ) an existing
fc4a62
partition.  Note that it does nothing to a filesystem in the partition.
fc4a62
fc4a62
(cherry picked from commit 21c58e17c473ea8ef31a18d03348eb2381c0f36c)
fc4a62
fc4a62
Resolves: rhbz#1423357
fc4a62
---
fc4a62
 NEWS            |  1 +
fc4a62
 parted/parted.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
fc4a62
 2 files changed, 77 insertions(+), 1 deletion(-)
fc4a62
fc4a62
diff --git a/NEWS b/NEWS
fc4a62
index d1a6f58..62d6381 100644
fc4a62
--- a/NEWS
fc4a62
+++ b/NEWS
fc4a62
@@ -3,6 +3,7 @@ GNU parted NEWS                                    -*- outline -*-
fc4a62
 * Noteworthy changes in release 3.1-29 (2017-04-11) [RHEL7.4]
fc4a62
 
fc4a62
   Parted now recognizes NVMe devices
fc4a62
+  Add resizepart command to resize a partition
fc4a62
 
fc4a62
 * Noteworthy changes in release 3.1-18 (2014-08-12) [RHEL7.1]
fc4a62
 
fc4a62
diff --git a/parted/parted.c b/parted/parted.c
fc4a62
index d4a397b..d64b0b8 100644
fc4a62
--- a/parted/parted.c
fc4a62
+++ b/parted/parted.c
fc4a62
@@ -152,6 +152,9 @@ static const char* fs_type_msg_start = N_("FS-TYPE is one of: ");
fc4a62
 static const char* start_end_msg =    N_("START and END are disk locations, such as "
fc4a62
                 "4GB or 10%.  Negative values count from the end of the disk.  "
fc4a62
                 "For example, -1s specifies exactly the last sector.\n");
fc4a62
+static const char* end_msg =          N_("END is disk location, such as "
fc4a62
+                "4GB or 10%.  Negative value counts from the end of the disk.  "
fc4a62
+                "For example, -1s specifies exactly the last sector.\n");
fc4a62
 static const char* state_msg =        N_("STATE is one of: on, off\n");
fc4a62
 static const char* device_msg =       N_("DEVICE is usually /dev/hda or /dev/sda\n");
fc4a62
 static const char* name_msg =         N_("NAME is any word you want\n");
fc4a62
@@ -435,6 +438,21 @@ constraint_from_start_end (PedDevice* dev, PedGeometry* range_start,
fc4a62
                 range_start, range_end, 1, dev->length);
fc4a62
 }
fc4a62
 
fc4a62
+
fc4a62
+static PedConstraint*
fc4a62
+constraint_from_start_end_fixed_start (PedDevice* dev, PedSector start_sector,
fc4a62
+                           PedGeometry* range_end)
fc4a62
+{
fc4a62
+        PedGeometry range_start;
fc4a62
+        range_start.dev = dev;
fc4a62
+        range_start.start = start_sector;
fc4a62
+        range_start.end = start_sector;
fc4a62
+        range_start.length = 1;
fc4a62
+
fc4a62
+        return ped_constraint_new (ped_alignment_any, ped_alignment_any,
fc4a62
+                &range_start, range_end, 1, dev->length);
fc4a62
+}
fc4a62
+
fc4a62
 void
fc4a62
 help_on (char* topic)
fc4a62
 {
fc4a62
@@ -1478,7 +1496,7 @@ error:
fc4a62
 }
fc4a62
 
fc4a62
 static int
fc4a62
-do_resize (PedDevice **dev, PedDisk** diskp)
fc4a62
+do_resize (PedDevice **dev)
fc4a62
 {
fc4a62
         ped_exception_throw (
fc4a62
                 PED_EXCEPTION_ERROR,
fc4a62
@@ -1488,6 +1506,63 @@ do_resize (PedDevice **dev, PedDisk** diskp)
fc4a62
 }
fc4a62
 
fc4a62
 static int
fc4a62
+do_resizepart (PedDevice** dev)
fc4a62
+{
fc4a62
+        PedDisk                 *disk;
fc4a62
+        PedPartition            *part = NULL;
fc4a62
+        PedSector               start, end, oldend;
fc4a62
+        PedGeometry             *range_end = NULL;
fc4a62
+        PedConstraint*          constraint;
fc4a62
+        int rc = 0;
fc4a62
+
fc4a62
+        disk = ped_disk_new (*dev);
fc4a62
+        if (!disk)
fc4a62
+                goto error;
fc4a62
+
fc4a62
+        if (ped_disk_is_flag_available(disk, PED_DISK_CYLINDER_ALIGNMENT))
fc4a62
+                if (!ped_disk_set_flag(disk, PED_DISK_CYLINDER_ALIGNMENT,
fc4a62
+                                       alignment == ALIGNMENT_CYLINDER))
fc4a62
+                        goto error;
fc4a62
+
fc4a62
+        if (!command_line_get_partition (_("Partition number?"), disk, &part))
fc4a62
+                goto error;
fc4a62
+        if (!_partition_warn_busy (part))
fc4a62
+                goto error;
fc4a62
+
fc4a62
+        start = part->geom.start;
fc4a62
+        end = oldend = part->geom.end;
fc4a62
+        if (!command_line_get_sector (_("End?"), *dev, &end, &range_end, NULL))
fc4a62
+                goto error;
fc4a62
+        /* Do not move start of the partition */
fc4a62
+        constraint = constraint_from_start_end_fixed_start (*dev, start, range_end);
fc4a62
+        if (!ped_disk_set_partition_geom (disk, part, constraint,
fc4a62
+                                          start, end))
fc4a62
+                goto error_destroy_constraint;
fc4a62
+        /* warn when shrinking partition - might lose data */
fc4a62
+        if (part->geom.end < oldend)
fc4a62
+                if (ped_exception_throw (
fc4a62
+                            PED_EXCEPTION_WARNING,
fc4a62
+                            PED_EXCEPTION_YES_NO,
fc4a62
+                            _("Shrinking a partition can cause data loss, " \
fc4a62
+                              "are you sure you want to continue?")) != PED_EXCEPTION_YES)
fc4a62
+                        goto error_destroy_constraint;
fc4a62
+        ped_disk_commit (disk);
fc4a62
+
fc4a62
+        if ((*dev)->type != PED_DEVICE_FILE)
fc4a62
+                disk_is_modified = 1;
fc4a62
+
fc4a62
+        rc = 1;
fc4a62
+
fc4a62
+error_destroy_constraint:
fc4a62
+        ped_constraint_destroy (constraint);
fc4a62
+error:
fc4a62
+        if (range_end != NULL)
fc4a62
+                ped_geometry_destroy (range_end);
fc4a62
+        return rc;
fc4a62
+}
fc4a62
+
fc4a62
+
fc4a62
+static int
fc4a62
 do_rm (PedDevice** dev)
fc4a62
 {
fc4a62
         PedDisk*                disk;
fc4a62
-- 
fc4a62
2.9.4
fc4a62