Pino Toscano
2016-Nov-25 10:22 UTC
[Libguestfs] [PATCH 1/2] daemon: allow to change the labels of swap partitions
--- daemon/daemon.h | 1 + daemon/labels.c | 3 +++ daemon/swap.c | 21 +++++++++++++++++++++ generator/actions.ml | 4 ++++ 4 files changed, 29 insertions(+) diff --git a/daemon/daemon.h b/daemon/daemon.h index 79a5288..2379e31 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -254,6 +254,7 @@ extern int64_t ntfs_minimum_size (const char *device); /*-- in swap.c --*/ extern int swap_set_uuid (const char *device, const char *uuid); +extern int swap_set_label (const char *device, const char *label); /* ordinary daemon functions use these to indicate errors * NB: you don't need to prefix the string with the current command, diff --git a/daemon/labels.c b/daemon/labels.c index 20f27cb..aaa3eaf 100644 --- a/daemon/labels.c +++ b/daemon/labels.c @@ -85,6 +85,9 @@ do_set_label (const mountable_t *mountable, const char *label) else if (STREQ (vfs_type, "xfs")) r = xfslabel (mountable->device, label); + else if (STREQ (vfs_type, "swap")) + r = swap_set_label (mountable->device, label); + else NOT_SUPPORTED (-1, "don't know how to set the label for '%s' filesystems", vfs_type); diff --git a/daemon/swap.c b/daemon/swap.c index 9d7839e..028bc1e 100644 --- a/daemon/swap.c +++ b/daemon/swap.c @@ -239,3 +239,24 @@ swap_set_uuid (const char *device, const char *uuid) return 0; } + +int +swap_set_label (const char *device, const char *label) +{ + int r; + CLEANUP_FREE char *err = NULL; + + if (strlen (label) > SWAP_LABEL_MAX) { + reply_with_error ("%s: Linux swap labels are limited to %d bytes", + label, SWAP_LABEL_MAX); + return -1; + } + + r = command (NULL, &err, str_swaplabel, "-L", label, device, NULL); + if (r == -1) { + reply_with_error ("%s", err); + return -1; + } + + return 0; +} diff --git a/generator/actions.ml b/generator/actions.ml index 43de38b..5e0356f 100644 --- a/generator/actions.ml +++ b/generator/actions.ml @@ -10304,6 +10304,10 @@ when trying to set the label. The label is limited to 11 bytes. +=item swap + +The label is limited to 16 bytes. + =back If there is no support for changing the label -- 2.7.4
Pino Toscano
2016-Nov-25 10:22 UTC
[Libguestfs] [PATCH 2/2] resize: shrink/expand swap partitions
Handle the swap partition on their own, rebuilding them using the existing UUID and label. --- resize/resize.ml | 35 ++++++++++++++++++++++++++++------- resize/virt-resize.pod | 8 ++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/resize/resize.ml b/resize/resize.ml index 7d06f18..59ee5bf 100644 --- a/resize/resize.ml +++ b/resize/resize.ml @@ -60,6 +60,7 @@ and partition_content | ContentPV of int64 (* physical volume (size of PV) *) | ContentFS of string * int64 (* mountable filesystem (FS type, FS size) *) | ContentExtendedPartition (* MBR extended partition *) + | ContentSwap (* Swap partition *) and partition_operation | OpCopy (* copy it as-is, no resizing *) | OpIgnore (* ignore it (create on target, but don't @@ -104,11 +105,13 @@ and string_of_partition_content = function | ContentPV sz -> sprintf "LVM PV (%Ld bytes)" sz | ContentFS (fs, sz) -> sprintf "filesystem %s (%Ld bytes)" fs sz | ContentExtendedPartition -> "extended partition" + | ContentSwap -> "swap" and string_of_partition_content_no_size = function | ContentUnknown -> "unknown data" | ContentPV _ -> "LVM PV" | ContentFS (fs, _) -> sprintf "filesystem %s" fs | ContentExtendedPartition -> "extended partition" + | ContentSwap -> "swap" (* Data structure describing LVs on the source disk. This is only * used if the user gave the --lv-expand option. @@ -130,6 +133,7 @@ let debug_logvol lv type expand_content_method | PVResize | Resize2fs | NTFSResize | BtrfsFilesystemResize | XFSGrowFS + | Mkswap let string_of_expand_content_method = function | PVResize -> s_"pvresize" @@ -137,6 +141,7 @@ let string_of_expand_content_method = function | NTFSResize -> s_"ntfsresize" | BtrfsFilesystemResize -> s_"btrfs-filesystem-resize" | XFSGrowFS -> s_"xfs_growfs" + | Mkswap -> s_"mkswap" type unknown_filesystems_mode | UnknownFsIgnore @@ -414,6 +419,8 @@ read the man page virt-resize(1). let fs = g#vfs_type dev in if fs = "unknown" then ContentUnknown + else if fs = "swap" then + ContentSwap else if fs = "LVM2_member" then ( let rec loop = function | [] -> @@ -531,7 +538,7 @@ read the man page virt-resize(1). assert ( match typ with | ContentPV _ | ContentExtendedPartition -> false - | ContentUnknown | ContentFS _ -> true + | ContentUnknown | ContentFS _ | ContentSwap -> true ); { lv_name = name; lv_type = typ; lv_operation = LVOpNone } @@ -558,6 +565,7 @@ read the man page virt-resize(1). | ContentFS (("xfs"), _) when !xfs_available -> true | ContentFS _ -> false | ContentExtendedPartition -> false + | ContentSwap -> true else fun _ -> false @@ -572,6 +580,7 @@ read the man page virt-resize(1). | ContentFS (("xfs"), _) when !xfs_available -> XFSGrowFS | ContentFS _ -> assert false | ContentExtendedPartition -> assert false + | ContentSwap -> Mkswap else fun _ -> assert false in @@ -665,6 +674,7 @@ read the man page virt-resize(1). | ContentExtendedPartition -> error (f_"%s: This extended partition contains logical partitions which might be damaged by shrinking it. If you want to shrink this partition, you need to use the '--resize-force' option, but that could destroy logical partitions within this partition. (This error came from '%s' option on the command line.)") name option + | ContentSwap -> () ); p.p_operation <- OpResize newsize @@ -831,7 +841,8 @@ read the man page virt-resize(1). (match p.p_type with | ContentUnknown | ContentPV _ - | ContentExtendedPartition -> () + | ContentExtendedPartition + | ContentSwap -> () | ContentFS (fs, _) -> error (f_"unknown/unavailable method for expanding the %s filesystem on %s") fs p.p_name @@ -848,7 +859,8 @@ read the man page virt-resize(1). (match lv.lv_type with | ContentUnknown | ContentPV _ - | ContentExtendedPartition -> () + | ContentExtendedPartition + | ContentSwap -> () | ContentFS (fs, _) -> error (f_"unknown/unavailable method for expanding the %s filesystem on %s") fs lv.lv_name; @@ -886,7 +898,8 @@ read the man page virt-resize(1). (match p.p_type with | ContentUnknown | ContentPV _ - | ContentExtendedPartition -> () + | ContentExtendedPartition + | ContentSwap -> () | ContentFS (fs, _) -> warning (f_"unknown/unavailable method for expanding the %s filesystem on %s") fs p.p_name; @@ -916,7 +929,8 @@ read the man page virt-resize(1). (match lv.lv_type with | ContentUnknown | ContentPV _ - | ContentExtendedPartition -> () + | ContentExtendedPartition + | ContentSwap -> () | ContentFS (fs, _) -> warning (f_"unknown/unavailable method for expanding the %s filesystem on %s") fs name; @@ -1199,7 +1213,7 @@ read the man page virt-resize(1). message (f_"Copying %s") source; (match p.p_type with - | ContentUnknown | ContentPV _ | ContentFS _ -> + | ContentUnknown | ContentPV _ | ContentFS _ | ContentSwap -> g#copy_device_to_device ~size:copysize ~sparse source target | ContentExtendedPartition -> @@ -1255,7 +1269,7 @@ read the man page virt-resize(1). | { p_type (ContentFS _|ContentUnknown|ContentPV _ - |ContentExtendedPartition) } :: _ + |ContentExtendedPartition|ContentSwap) } :: _ | [] -> () ); @@ -1312,6 +1326,13 @@ read the man page virt-resize(1). | NTFSResize -> g#ntfsresize ~force:ntfsresize_force target | BtrfsFilesystemResize -> with_mounted target g#btrfs_filesystem_resize | XFSGrowFS -> with_mounted target g#xfs_growfs + | Mkswap -> + (* Rebuild the swap using the UUID and label of the existing + * swap partition. + *) + let uuid = g#vfs_uuid target in + let label = g#vfs_label target in + g#mkswap ~uuid ~label target in (* Expand partition content as required. *) diff --git a/resize/virt-resize.pod b/resize/virt-resize.pod index aa01849..2344056 100644 --- a/resize/virt-resize.pod +++ b/resize/virt-resize.pod @@ -398,6 +398,14 @@ Btrfs filesystems, if libguestfs was compiled with support for btrfs. XFS filesystems, if libguestfs was compiled with support for XFS. +=item * + +Linux swap partitions. + +Please note that libguestfs I<destroys> the existing swap content +by recreating it with C<mkswap>, so this should not be used when +the guest is suspended. + =back Note that you cannot use I<--expand> and I<--shrink> together. -- 2.7.4
Richard W.M. Jones
2016-Dec-07 11:54 UTC
Re: [Libguestfs] [PATCH 1/2] daemon: allow to change the labels of swap partitions
On Fri, Nov 25, 2016 at 11:22:26AM +0100, Pino Toscano wrote:> --- > daemon/daemon.h | 1 + > daemon/labels.c | 3 +++ > daemon/swap.c | 21 +++++++++++++++++++++ > generator/actions.ml | 4 ++++ > 4 files changed, 29 insertions(+) > > diff --git a/daemon/daemon.h b/daemon/daemon.h > index 79a5288..2379e31 100644 > --- a/daemon/daemon.h > +++ b/daemon/daemon.h > @@ -254,6 +254,7 @@ extern int64_t ntfs_minimum_size (const char *device); > > /*-- in swap.c --*/ > extern int swap_set_uuid (const char *device, const char *uuid); > +extern int swap_set_label (const char *device, const char *label); > > /* ordinary daemon functions use these to indicate errors > * NB: you don't need to prefix the string with the current command, > diff --git a/daemon/labels.c b/daemon/labels.c > index 20f27cb..aaa3eaf 100644 > --- a/daemon/labels.c > +++ b/daemon/labels.c > @@ -85,6 +85,9 @@ do_set_label (const mountable_t *mountable, const char *label) > else if (STREQ (vfs_type, "xfs")) > r = xfslabel (mountable->device, label); > > + else if (STREQ (vfs_type, "swap")) > + r = swap_set_label (mountable->device, label); > + > else > NOT_SUPPORTED (-1, "don't know how to set the label for '%s' filesystems", > vfs_type); > diff --git a/daemon/swap.c b/daemon/swap.c > index 9d7839e..028bc1e 100644 > --- a/daemon/swap.c > +++ b/daemon/swap.c > @@ -239,3 +239,24 @@ swap_set_uuid (const char *device, const char *uuid) > > return 0; > } > + > +int > +swap_set_label (const char *device, const char *label) > +{ > + int r; > + CLEANUP_FREE char *err = NULL; > + > + if (strlen (label) > SWAP_LABEL_MAX) { > + reply_with_error ("%s: Linux swap labels are limited to %d bytes", > + label, SWAP_LABEL_MAX); > + return -1; > + } > + > + r = command (NULL, &err, str_swaplabel, "-L", label, device, NULL); > + if (r == -1) { > + reply_with_error ("%s", err); > + return -1; > + } > + > + return 0; > +} > diff --git a/generator/actions.ml b/generator/actions.ml > index 43de38b..5e0356f 100644 > --- a/generator/actions.ml > +++ b/generator/actions.ml > @@ -10304,6 +10304,10 @@ when trying to set the label. > > The label is limited to 11 bytes. > > +=item swap > + > +The label is limited to 16 bytes. > + > =back > > If there is no support for changing the label > -- > 2.7.4 > > _______________________________________________ > Libguestfs mailing list > Libguestfs@redhat.com > https://www.redhat.com/mailman/listinfo/libguestfsObvious improvement, ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Richard W.M. Jones
2016-Dec-07 11:55 UTC
Re: [Libguestfs] [PATCH 2/2] resize: shrink/expand swap partitions
On Fri, Nov 25, 2016 at 11:22:27AM +0100, Pino Toscano wrote:> Handle the swap partition on their own, rebuilding them using the > existing UUID and label. > > resize/resize.ml | 35 ++++++++++++++++++++++++++++------- > resize/virt-resize.pod | 8 ++++++++ > 2 files changed, 36 insertions(+), 7 deletions(-) > > diff --git a/resize/resize.ml b/resize/resize.ml > index 7d06f18..59ee5bf 100644 > --- a/resize/resize.ml > +++ b/resize/resize.ml > @@ -60,6 +60,7 @@ and partition_content > | ContentPV of int64 (* physical volume (size of PV) *) > | ContentFS of string * int64 (* mountable filesystem (FS type, FS size) *) > | ContentExtendedPartition (* MBR extended partition *) > + | ContentSwap (* Swap partition *) > and partition_operation > | OpCopy (* copy it as-is, no resizing *) > | OpIgnore (* ignore it (create on target, but don't > @@ -104,11 +105,13 @@ and string_of_partition_content = function > | ContentPV sz -> sprintf "LVM PV (%Ld bytes)" sz > | ContentFS (fs, sz) -> sprintf "filesystem %s (%Ld bytes)" fs sz > | ContentExtendedPartition -> "extended partition" > + | ContentSwap -> "swap" > and string_of_partition_content_no_size = function > | ContentUnknown -> "unknown data" > | ContentPV _ -> "LVM PV" > | ContentFS (fs, _) -> sprintf "filesystem %s" fs > | ContentExtendedPartition -> "extended partition" > + | ContentSwap -> "swap" > > (* Data structure describing LVs on the source disk. This is only > * used if the user gave the --lv-expand option. > @@ -130,6 +133,7 @@ let debug_logvol lv > > type expand_content_method > | PVResize | Resize2fs | NTFSResize | BtrfsFilesystemResize | XFSGrowFS > + | Mkswap > > let string_of_expand_content_method = function > | PVResize -> s_"pvresize" > @@ -137,6 +141,7 @@ let string_of_expand_content_method = function > | NTFSResize -> s_"ntfsresize" > | BtrfsFilesystemResize -> s_"btrfs-filesystem-resize" > | XFSGrowFS -> s_"xfs_growfs" > + | Mkswap -> s_"mkswap" > > type unknown_filesystems_mode > | UnknownFsIgnore > @@ -414,6 +419,8 @@ read the man page virt-resize(1). > let fs = g#vfs_type dev in > if fs = "unknown" then > ContentUnknown > + else if fs = "swap" then > + ContentSwap > else if fs = "LVM2_member" then ( > let rec loop = function > | [] -> > @@ -531,7 +538,7 @@ read the man page virt-resize(1). > assert ( > match typ with > | ContentPV _ | ContentExtendedPartition -> false > - | ContentUnknown | ContentFS _ -> true > + | ContentUnknown | ContentFS _ | ContentSwap -> true > ); > > { lv_name = name; lv_type = typ; lv_operation = LVOpNone } > @@ -558,6 +565,7 @@ read the man page virt-resize(1). > | ContentFS (("xfs"), _) when !xfs_available -> true > | ContentFS _ -> false > | ContentExtendedPartition -> false > + | ContentSwap -> true > else > fun _ -> false > > @@ -572,6 +580,7 @@ read the man page virt-resize(1). > | ContentFS (("xfs"), _) when !xfs_available -> XFSGrowFS > | ContentFS _ -> assert false > | ContentExtendedPartition -> assert false > + | ContentSwap -> Mkswap > else > fun _ -> assert false > in > @@ -665,6 +674,7 @@ read the man page virt-resize(1). > | ContentExtendedPartition -> > error (f_"%s: This extended partition contains logical partitions which might be damaged by shrinking it. If you want to shrink this partition, you need to use the '--resize-force' option, but that could destroy logical partitions within this partition. (This error came from '%s' option on the command line.)") > name option > + | ContentSwap -> () > ); > > p.p_operation <- OpResize newsize > @@ -831,7 +841,8 @@ read the man page virt-resize(1). > (match p.p_type with > | ContentUnknown > | ContentPV _ > - | ContentExtendedPartition -> () > + | ContentExtendedPartition > + | ContentSwap -> () > | ContentFS (fs, _) -> > error (f_"unknown/unavailable method for expanding the %s filesystem on %s") > fs p.p_name > @@ -848,7 +859,8 @@ read the man page virt-resize(1). > (match lv.lv_type with > | ContentUnknown > | ContentPV _ > - | ContentExtendedPartition -> () > + | ContentExtendedPartition > + | ContentSwap -> () > | ContentFS (fs, _) -> > error (f_"unknown/unavailable method for expanding the %s filesystem on %s") > fs lv.lv_name; > @@ -886,7 +898,8 @@ read the man page virt-resize(1). > (match p.p_type with > | ContentUnknown > | ContentPV _ > - | ContentExtendedPartition -> () > + | ContentExtendedPartition > + | ContentSwap -> () > | ContentFS (fs, _) -> > warning (f_"unknown/unavailable method for expanding the %s filesystem on %s") > fs p.p_name; > @@ -916,7 +929,8 @@ read the man page virt-resize(1). > (match lv.lv_type with > | ContentUnknown > | ContentPV _ > - | ContentExtendedPartition -> () > + | ContentExtendedPartition > + | ContentSwap -> () > | ContentFS (fs, _) -> > warning (f_"unknown/unavailable method for expanding the %s filesystem on %s") > fs name; > @@ -1199,7 +1213,7 @@ read the man page virt-resize(1). > message (f_"Copying %s") source; > > (match p.p_type with > - | ContentUnknown | ContentPV _ | ContentFS _ -> > + | ContentUnknown | ContentPV _ | ContentFS _ | ContentSwap -> > g#copy_device_to_device ~size:copysize ~sparse source target > > | ContentExtendedPartition -> > @@ -1255,7 +1269,7 @@ read the man page virt-resize(1). > > | { p_type > (ContentFS _|ContentUnknown|ContentPV _ > - |ContentExtendedPartition) } :: _ > + |ContentExtendedPartition|ContentSwap) } :: _ > | [] -> () > ); > > @@ -1312,6 +1326,13 @@ read the man page virt-resize(1). > | NTFSResize -> g#ntfsresize ~force:ntfsresize_force target > | BtrfsFilesystemResize -> with_mounted target g#btrfs_filesystem_resize > | XFSGrowFS -> with_mounted target g#xfs_growfs > + | Mkswap -> > + (* Rebuild the swap using the UUID and label of the existing > + * swap partition. > + *) > + let uuid = g#vfs_uuid target in > + let label = g#vfs_label target in > + g#mkswap ~uuid ~label target > in > > (* Expand partition content as required. *) > diff --git a/resize/virt-resize.pod b/resize/virt-resize.pod > index aa01849..2344056 100644 > --- a/resize/virt-resize.pod > +++ b/resize/virt-resize.pod > @@ -398,6 +398,14 @@ Btrfs filesystems, if libguestfs was compiled with support for btrfs. > > XFS filesystems, if libguestfs was compiled with support for XFS. > > +=item * > + > +Linux swap partitions. > + > +Please note that libguestfs I<destroys> the existing swap content > +by recreating it with C<mkswap>, so this should not be used when > +the guest is suspended. > + > =back > > Note that you cannot use I<--expand> and I<--shrink> together.ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v
Seemingly Similar Threads
- [PATCH 0/2] Support for expanding f2fs partitions
- [PATCH 2/2] resize: shrink/expand swap partitions
- [PATCH 0/2] resize: Split out the command line parsing into Cmdline
- [PATCH v2 2/2] resize: expand f2fs partitions
- Re: virt-resize: support to MBR logical partitions and some question