Re: [Libguestfs] [PATCH 1/1] New partition API: part_resize

Richard W.M. Jones
Sun, 30 Jul 2017 15:42:07 -0000

On Sat, Jul 29, 2017 at 08:51:04PM +0300, Nikos Skalkotos
wrote:
> This can be used to enlarge or shrink an existing partition.
> ---
>  daemon/parted.c           | 31 +++++++++++++++++++++++++++++++
>  generator/actions_core.ml | 22 ++++++++++++++++++++++
>  generator/proc_nr.ml      |  1 +
>  lib/MAX_PROC_NR           |  2 +-
>  4 files changed, 55 insertions(+), 1 deletion(-)
>
> diff --git a/daemon/parted.c b/daemon/parted.c
> index 72e1b8420..f1205cadf 100644
> --- a/daemon/parted.c
> +++ b/daemon/parted.c
> @@ -178,6 +178,37 @@ do_part_del (const char *device, int partnum)
>  }
>
>  int
> +do_part_resize (const char *device, int partnum, int64_t endsect)
> +{
> +  int r;
> +  CLEANUP_FREE char *err = NULL;
> +  char endstr[32];
> +  char partnum_str[16];
> +
> +  if (partnum <= 0) {
> +    reply_with_error ("partition number must be >= 1");
> +    return -1;
> +  }
> +
> +  snprintf (partnum_str, sizeof partnum_str, "%d", partnum);
> +  snprintf (endstr, sizeof endstr, "%" PRIi64 "s",
endsect);
> +
> +  udev_settle ();
> +
> +  r = commandf (NULL, &err, COMMAND_FLAG_FOLD_STDOUT_ON_STDERR,
> +                "parted", "-s", "--",
device, "resizepart", partnum_str,
> +                endstr, NULL);
> +  if (r == -1) {
> +    reply_with_error ("parted: %s: %s:", device, err);
> +    return -1;
> +  }
> +
> +  udev_settle();
> +
> +  return 0;
> +}
> +
> +int
>  do_part_disk (const char *device, const char *parttype)
>  {
>    int r;
> diff --git a/generator/actions_core.ml b/generator/actions_core.ml
> index ea0735676..939aec7ee 100644
> --- a/generator/actions_core.ml
> +++ b/generator/actions_core.ml
> @@ -9646,4 +9646,26 @@ be mountable but require special options.
Filesystems may
>  not all belong to a single logical operating system
>  (use C<guestfs_inspect_os> to look for OSes)." };
>
> +  { defaults with
> +    name = "part_resize"; added = (1, 37, 20);
> +    style = RErr, [String (Device, "device"); Int
"partnum"; Int64 "endsect"], [];
> +    tests = [
> +      InitEmpty, Always, TestRun (
> +        [["part_init"; "/dev/sda"; "mbr"];
> +         ["part_add"; "/dev/sda"; "primary";
"1"; "-1025"];
> +         ["part_resize"; "/dev/sda"; "1";
"-1"]]), []
> +    ];
> +    shortdesc = "resize a partition";
> +    longdesc = "\
> +This command resizes the partition numbered C<partnum> on
C<device>
> +by moving the end position.
> +
> +Note that this does not modify any filesystem present in the partition.
> +If you wish to do this, you will need to use filesystem resizing
> +commands like C<guestfs_resize2fs>.
> +
> +When growing a partition you will want to grow the filesystem
> +afterwards, but when shrinking, you need to shrink the filesystem
> +before the partition." };
> +
>  ]
> diff --git a/generator/proc_nr.ml b/generator/proc_nr.ml
> index dec02f5fa..7895063b6 100644
> --- a/generator/proc_nr.ml
> +++ b/generator/proc_nr.ml
> @@ -484,6 +484,7 @@ let proc_nr = [
>  474, "internal_yara_scan";
>  475, "file_architecture";
>  476, "list_filesystems";
> +477, "part_resize";
>  ]
>
>  (* End of list.  If adding a new entry, add it at the end of the list
> diff --git a/lib/MAX_PROC_NR b/lib/MAX_PROC_NR
> index b86395733..bf2c10d23 100644
> --- a/lib/MAX_PROC_NR
> +++ b/lib/MAX_PROC_NR
> @@ -1 +1 @@
> -476
> +477

Looks fine to me, ACK.

Rich.

--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org