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

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