From b28a068b430d430d08b7b72fdc2ffc2deb58fa4c Mon Sep 17 00:00:00 2001 From: Justin Clift Date: Wed, 9 Jun 2010 02:38:32 +1000 Subject: [PATCH] virsh: add snapshot backing store support to vol-create-as This patch adds two new parameters to the vol-create-as command: --backing-vol --backing-vol-format virsh # vol-create-as guest_images_lvm snapvol1 5G --backing-vol \ rhel6vm1lun1 Vol snapvol1 created virsh # vol-create-as image_dir qcow2snap2 5G --format qcow2 \ --backing-vol imagevol1.qcow2 \ --backing-vol-format qcow2 Vol qcow2snap2 created Additionally, the virsh man page update fixes incorrect snapshot parameters that were included in my prior bulk volume command patch. --- tools/virsh.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++- tools/virsh.pod | 10 ++++---- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index d60c27b18b..7c43acb193 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -5279,6 +5279,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")}, + {"backing-vol", VSH_OT_STRING, 0, N_("the backing volume if taking a snapshot")}, + {"backing-vol-format", VSH_OT_STRING, 0, N_("format of backing volume if taking a snapshot")}, {NULL, 0, 0, NULL} }; @@ -5318,6 +5320,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; @@ -5344,6 +5347,8 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) vshError(ctl, _("Malformed size %s"), allocationStr); format = vshCommandOptString(cmd, "format", &found); + snapshotStrVol = vshCommandOptString(cmd, "backing-vol", &found); + snapshotStrFormat = vshCommandOptString(cmd, "backing-vol-format", &found); virBufferAddLit(&buf, "\n"); virBufferVSprintf(&buf, " %s\n", name); @@ -5356,8 +5361,62 @@ cmdVolCreateAs(vshControl *ctl, const vshCmd *cmd) virBufferVSprintf(&buf, " \n",format); virBufferAddLit(&buf, " \n"); } - virBufferAddLit(&buf, "\n"); + /* Convert the snapshot parameters into backingStore XML */ + if (snapshotStrVol) { + /* Lookup snapshot backing volume. Try the backing-vol + * parameter as a name */ + vshDebug(ctl, 5, "%s: Look up backing store volume '%s' as name\n", + cmd->def->name, snapshotStrVol); + virStorageVolPtr snapVol = virStorageVolLookupByName(pool, snapshotStrVol); + if (snapVol) + vshDebug(ctl, 5, "%s: Backing store volume found using '%s' as name\n", + cmd->def->name, snapshotStrVol); + + if (snapVol == NULL) { + /* Snapshot backing volume not found by name. Try the + * backing-vol parameter as a key */ + vshDebug(ctl, 5, "%s: Look up backing store volume '%s' as key\n", + cmd->def->name, snapshotStrVol); + snapVol = virStorageVolLookupByKey(ctl->conn, snapshotStrVol); + if (snapVol) + vshDebug(ctl, 5, "%s: Backing store volume found using '%s' as key\n", + cmd->def->name, snapshotStrVol); + } + if (snapVol == NULL) { + /* Snapshot backing volume not found by key. Try the + * backing-vol parameter as a path */ + vshDebug(ctl, 5, "%s: Look up backing store volume '%s' as path\n", + cmd->def->name, snapshotStrVol); + snapVol = virStorageVolLookupByPath(ctl->conn, snapshotStrVol); + if (snapVol) + vshDebug(ctl, 5, "%s: Backing store volume found using '%s' as path\n", + cmd->def->name, snapshotStrVol); + } + if (snapVol == NULL) { + vshError(ctl, _("failed to get vol '%s'"), snapshotStrVol); + return FALSE; + } + + char *snapshotStrVolPath; + if ((snapshotStrVolPath = virStorageVolGetPath(snapVol)) == NULL) { + virStorageVolFree(snapVol); + return FALSE; + } + + /* Create XML for the backing store */ + virBufferAddLit(&buf, " \n"); + virBufferVSprintf(&buf, " %s\n",snapshotStrVolPath); + if (snapshotStrFormat) + virBufferVSprintf(&buf, " \n",snapshotStrFormat); + virBufferAddLit(&buf, " \n"); + + /* Cleanup snapshot allocations */ + VIR_FREE(snapshotStrVolPath); + virStorageVolFree(snapVol); + } + + virBufferAddLit(&buf, "\n"); if (virBufferError(&buf)) { vshPrint(ctl, "%s", _("Failed to allocate XML buffer")); diff --git a/tools/virsh.pod b/tools/virsh.pod index b54003c3f9..f376649c60 100644 --- a/tools/virsh.pod +++ b/tools/virsh.pod @@ -784,8 +784,8 @@ source volume is in. I is the name or key or path of the source volume. =item B I I I optional -I<--allocation> I I<--format> I I<--snapshot-source-vol> -I I<--snapshot-source-format> I +I<--allocation> I I<--format> I I<--backing-vol> +I I<--backing-vol-format> I Create a volume from a set of arguments. I is the name or UUID of the storage pool to create the volume @@ -797,10 +797,10 @@ I<--allocation> I is the initial size to be allocated in the volume, with optional k, M, G, or T suffix. I<--format> I 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 is the source backing +I<--backing-vol> I is the source backing volume to be used if taking a snapshot of an existing volume. -I<--snapshot-source-format> I is the format of the snapshot backing volume; -raw, bochs, qcow, qcow2, vmdk. +I<--backing-vol-format> I is the format of the snapshot backing volume; +raw, bochs, qcow, qcow2, vmdk, host_device. =item B [optional I<--pool> I] I I