[libvirt] [PATCH] Added snapshot backing store parameters to virsh.

--- tools/virsh.c | 32 +++++++++++++++++++++++++++++++- tools/virsh.pod | 7 ++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 1279f41..04d38b7 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -5274,6 +5274,8 @@ static const vshCmdOptDef opts_vol_create_as[] = { {"capacity", VSH_OT_DATA, VSH_OFLAG_REQ, N_("size of the vol with optional k,M,G,T suffix")}, {"allocation", VSH_OT_STRING, 0, N_("initial allocation size with optional k,M,G,T suffix")}, {"format", VSH_OT_STRING, 0, N_("file format type raw,bochs,qcow,qcow2,vmdk")}, + {"snapshot-source-vol", VSH_OT_STRING, 0, N_("the source volume if taking a snapshot")}, + {"snapshot-source-format", VSH_OT_STRING, 0, N_("format of the source volume if taking a snapshot")}, {NULL, 0, 0, NULL} }; @@ -5313,6 +5315,7 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) int found; char *xml; char *name, *capacityStr, *allocationStr, *format; + char *snapshotStrVol, *snapshotStrFormat; unsigned long long capacity, allocation = 0; virBuffer buf = VIR_BUFFER_INITIALIZER; @@ -5339,6 +5342,8 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) vshError(ctl, _("Malformed size %s"), allocationStr); format = vshCommandOptString(cmd, "format", &found); + snapshotStrVol = vshCommandOptString(cmd, "snapshot-source-vol", &found); + snapshotStrFormat = vshCommandOptString(cmd, "snapshot-source-format", &found); virBufferAddLit(&buf, "<volume>\n"); virBufferVSprintf(&buf, " <name>%s</name>\n", name); @@ -5351,8 +5356,33 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) virBufferVSprintf(&buf, " <format type='%s'/>\n",format); virBufferAddLit(&buf, " </target>\n"); } - virBufferAddLit(&buf, "</volume>\n"); + // Convert the snapshot parameters into backingStore XML + if (snapshotStrVol) { + // Use the name of the snapshot source volume to retrieve its device path + virStorageVolPtr snapVol = virStorageVolLookupByName(pool, snapshotStrVol); + if (snapVol == NULL) { + return FALSE; + } + char *snapshotStrVolPath; + if ((snapshotStrVolPath = virStorageVolGetPath(snapVol)) == NULL) { + virStorageVolFree(snapVol); + return FALSE; + } + + // Create the XML for the backing store + virBufferAddLit(&buf, " <backingStore>\n"); + virBufferVSprintf(&buf, " <path>%s</path>\n",snapshotStrVolPath); + if (snapshotStrFormat) + virBufferVSprintf(&buf, " <format type='%s'/>\n",snapshotStrFormat); + virBufferAddLit(&buf, " </backingStore>\n"); + + // Cleanup snapshot allocations + VIR_FREE(snapshotStrVolPath); + virStorageVolFree(snapVol); + } + + virBufferAddLit(&buf, "</volume>\n"); if (virBufferError(&buf)) { vshPrint(ctl, "%s", _("Failed to allocate XML buffer")); diff --git a/tools/virsh.pod b/tools/virsh.pod index cab931c..b1a8544 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -782,7 +782,8 @@ source volume is in. I<vol-name-or-key> is the name or UUID of the source volume. =item B<vol-create-as> I<pool-or-uuid> I<name> I<capacity> optional -I<--allocation> I<size> I<--format> I<string> +I<--allocation> I<size> I<--format> I<string> I<--snapshot-source-vol> I<vol-name-or-key> +I<--snapshot-source-format> I<string> Create a volume from a set of arguments. I<pool-or-uuid> is the name or UUID of the storage pool to create the volume @@ -794,6 +795,10 @@ I<--allocation> I<size> is the initial size to be allocated in the volume, with optional k, M, G, or T suffix. I<--format> I<string> is used in file based storage pools to specify the volume file format to use; raw, bochs, qcow, qcow2, vmdk. +I<--snapshot-source-vol> I<vol-name-or-key> is the source backing volume to be used if +taking a snapshot of an existing volume. +I<--snapshot-source-format> I<string> is the format of the snapshot backing volume; +raw, bochs, qcow, qcow2, vmdk. =item B<vol-clone> [optional I<--pool> I<pool-or-uuid>] I<vol-name-or-key> I<name> -- 1.7.0.1

Hi all, This patch adds support for creation of snapshot volumes to the virsh "vol-create-as" command, which is presently missing it. Thinking it will be useful, as it's tricky to explain snapshots + hacking XML when writing up docs. Explaining things as parameters on a command line (no XML required) is so much easier. :) The snapshot support was added through two optional parameters to vol-create-as: --snapshot-source-vol backing_volume --snapshot-source-format volume_format The "--snapshot-source-vol" parameter is the name of a volume to use as the snapshot backing store. It has to be in the same storage pool as the volume being created. The "--snapshot-source-format" parameter is only needed for file based backing stores (ie qcow, qcow2, etc), and specifies the format of the backing store volume. In an ideal scenario, the --snapshot-source-format parameter would not be necessary, but there didn't appear to be a way to query the format of an existing volume to determine it automatically. (ie something like a virStorageVolGetFormat()) Example of using the new parameters with an LVM storage pool: ******************************************************** virsh # vol-list guest_images_lvm Name Path ----------------------------------------- rhel6vm1lun1 /dev/guest_images_lvm/rhel6vm1lun1 testvol /dev/guest_images_lvm/testvol virsh # vol-create-as guest_images_lvm snapvol1 5G --snapshot-source-vol rhel6vm1lun1 Vol snapvol1 created virsh # vol-create-as guest_images_lvm snapvol2 5G --snapshot-source-vol rhel6vm1lun1 Vol snapvol2 created virsh # vol-create-as guest_images_lvm snapvol3 5G --snapshot-source-vol rhel6vm1lun1 Vol snapvol3 created virsh # vol-list guest_images_lvm Name Path ----------------------------------------- rhel6vm1lun1 /dev/guest_images_lvm/rhel6vm1lun1 snapvol1 /dev/guest_images_lvm/snapvol1 snapvol2 /dev/guest_images_lvm/snapvol2 snapvol3 /dev/guest_images_lvm/snapvol3 testvol /dev/guest_images_lvm/testvol # virsh # quit # lvs guest_images_lvm LV VG Attr LSize Origin Snap% Move Log Copy% Convert rhel6vm1lun1 guest_images_lvm owi-a- 20.00g snapvol1 guest_images_lvm swi-a- 5.00g rhel6vm1lun1 0.00 snapvol2 guest_images_lvm swi-a- 5.00g rhel6vm1lun1 0.00 snapvol3 guest_images_lvm swi-a- 5.00g rhel6vm1lun1 0.00 testvol guest_images_lvm -wi-a- 20.00g # ******************************************************** Example of using the new parameters with a directory storage pool: ******************************************************** virsh # vol-list image_dir Name Path ----------------------------------------- imagevol1.qcow2 /home/images/imagevol1.qcow2 virsh # vol-create-as image_dir qcow2snap1 5G --format qcow2 --snapshot-source-vol imagevol1.qcow2 --snapshot-source-format qcow2 Vol qcow2snap1 created virsh # vol-create-as image_dir qcow2snap2 5G --format qcow2 --snapshot-source-vol imagevol1.qcow2 --snapshot-source-format qcow2 Vol qcow2snap2 created virsh # vol-create-as image_dir qcow2snap3 5G --format qcow2 --snapshot-source-vol imagevol1.qcow2 --snapshot-source-format qcow2 Vol qcow2snap3 created virsh # vol-list image_dir Name Path ----------------------------------------- imagevol1.qcow2 /home/images/imagevol1.qcow2 qcow2snap1 /home/images/qcow2snap1 qcow2snap2 /home/images/qcow2snap2 qcow2snap3 /home/images/qcow2snap3 virsh # quit # ls -la /home/images/ total 552 drwx------. 2 root root 4096 Jun 5 22:10 . drwxr-xr-x. 7 root root 4096 Jun 5 16:51 .. -rw-------. 1 root root 262144 Jun 5 16:53 imagevol1.qcow2 -rw-------. 1 root root 262144 Jun 5 22:10 qcow2snap1 -rw-------. 1 root root 262144 Jun 5 22:10 qcow2snap2 -rw-------. 1 root root 262144 Jun 5 22:10 qcow2snap3 # ******************************************************** A Red Hat BZ has been created for tracking this, if useful: https://bugzilla.redhat.com/show_bug.cgi?id=600652 Regards and best wishes, Justin Clift On 06/05/2010 10:13 PM, Justin Clift wrote:
--- tools/virsh.c | 32 +++++++++++++++++++++++++++++++- tools/virsh.pod | 7 ++++++- 2 files changed, 37 insertions(+), 2 deletions(-) <snip>
-- Salasaga - Open Source eLearning IDE http://www.salasaga.org

Sorry, forgot to mention that this patch for the virsh man page builds upon the previous patch submitted a few minutes ago. There wasn't an existing vol-create-as command in the man page to build upon otherwise. Regards and best wishes, Justin Clift On 06/05/2010 10:13 PM, Justin Clift wrote: <snip>
diff --git a/tools/virsh.pod b/tools/virsh.pod index cab931c..b1a8544 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -782,7 +782,8 @@ source volume is in. I<vol-name-or-key> is the name or UUID of the source volume. =item B<vol-create-as> I<pool-or-uuid> I<name> I<capacity> optional -I<--allocation> I<size> I<--format> I<string> +I<--allocation> I<size> I<--format> I<string> I<--snapshot-source-vol> I<vol-name-or-key> +I<--snapshot-source-format> I<string> Create a volume from a set of arguments. I<pool-or-uuid> is the name or UUID of the storage pool to create the volume @@ -794,6 +795,10 @@ I<--allocation> I<size> is the initial size to be allocated in the volume, with optional k, M, G, or T suffix. I<--format> I<string> is used in file based storage pools to specify the volume file format to use; raw, bochs, qcow, qcow2, vmdk. +I<--snapshot-source-vol> I<vol-name-or-key> is the source backing volume to be used if +taking a snapshot of an existing volume. +I<--snapshot-source-format> I<string> is the format of the snapshot backing volume; +raw, bochs, qcow, qcow2, vmdk. =item B<vol-clone> [optional I<--pool> I<pool-or-uuid>] I<vol-name-or-key> I<name> -- 1.7.0.1
-- Salasaga - Open Source eLearning IDE http://www.salasaga.org

On 06/05/2010 10:13 PM, Justin Clift wrote: <snip>
+ virStorageVolPtr snapVol = virStorageVolLookupByName(pool, snapshotStrVol); + if (snapVol == NULL) { + return FALSE; + } <snip>
Thought about it more, and this could do with better failure handling here. I'll resubmit this patch so it tests the given volume string as a key or a path if not found as a name, plus alert the user if the backing store volume wasn't found. Regards and best wishes, Justin Clift -- Salasaga - Open Source eLearning IDE http://www.salasaga.org
participants (1)
-
Justin Clift