Clone VMs with govc
Hi,
I recently started looking at govc as a replacement to Vagrant for bringing up VMs in an easy and reliable way. Almost all of the functionality I need is baked into govc without the need to write my own additional Go applications, however, I am unable to find a way to clone a VM either from a template or from another VM via govc, is this possible and I've missed the arguments somewhere?
If this functionality does not yet exist, then I think this issue is a feature request :)
Thanks! zc
Thanks for your interest!
This is not possible yet. The only way to do something that smells like cloning is to pass the -link flag to vm.create to create a linked clone of an existing disk. It is not possible to clone a VM directly yet.
Thanks Peiter, I will try out using the link flag. One thing I noticed is:
% govc vm.create --help
Usage: govc vm.create [OPTIONS]
[ ... ]
-link=true Link specified disk
[ ... ]
If -link only pertains to whether to link the disk or not (and based on my reading of https://www.vmware.com/support/ws5/doc/ws_clone_overview.html it looks like this is exactly what I'm after) how do I actually specify the disk I'm linking (as this flag just appears to be a Boolean value)? Can you perhaps provide an example usage of just creating a VM using this option so I can wrap my head around which flags are required?
edit: I apologize for my dumb questions, I'm still getting acquainted with how things are laid out in this project.
Thanks for your help! zc
You specify the disk with the -disk flag. Specify the datastore of the disk with the -ds flag.
If used, all VMs created with these flags will share the same underlying disk.
That makes sense, thank you! One final question (I promise!) I think I'm running into this issue when trying to specify datastores: https://github.com/vmware/govmomi/issues/255
My datastore layout is as follows:
`-- DATACENTER
|-- LOCAL [folder]
`-- NETAPP [folder]
|-- DS_NFS01 [data store]
|-- DS_NFS02 [data store]
`-- DS_NFS03 [data store]
However, running govc datastore.ls dc=[my data center] returns only two VMs which were incorrectly created on the local disk. Is there any way I can get to those nested data stores? (If this is related to issue 285 do you want me to just move this description of the problem there?)
edit: I should mention that this appears to only be a problem with datastore.ls, and I can see the path down to the NetApp devices which are mounted over NFS with just a govc ls /[DATACENTER]/datastore/NETAPP/ but I was under the impression I will have issues if datastore.ls can't find them.
Thanks again! zc
I looked at this a little bit more today, and was running govc datastore.ls dc=[my dc] -debug to get the XML back. I found the XML response contained the local datastore as well as a folder for NetApp:
<RetrievePropertiesResponse xmlns="urn:vim25">
<returnval>
<obj type="Folder">group-s32138</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">Local</val>
</propSet>
</returnval>
<returnval>
<obj type="Folder">group-s32139</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">NetApp</val>
</propSet>
</returnval>
<returnval>
<obj type="Datastore">datastore-43243</obj>
<propSet>
<name>name</name>
<val xsi:type="xsd:string">[(Another) Root Datastore]</val>
</propSet>
</returnval>
</RetrievePropertiesResponse>
So what I'm trying to figure out how to do is how I can specify things under the NetApp folder using the -ds flag to specify the datastore the disk I want to link is on. We have >50 data stores, most under the folder "local" (which is the local storage for each ESXi host).
Hopefully this clarifies my last comment some. From a casual reading of the code, I suspect the problem is simply that folder objects are not expanded and searched.
Perhaps I am totally misunderstanding, or perhaps I need to place each of our NetApp datastores at the root level as there might not be an easy fix.
Being unsure of how to fix this, I opted to just move my datastores to the root directory so that they are no longer folder objects. I can browse them, however, link appears to fail still with the error Error: Invalid configuration for device '0'..
I have been unable to get linked disks to work in conjunction with vm.create using the following arguments:
% govc vm.create
-dc=[my DC]
-ds=[my netapp device]
-disk=[my existing VM]
-link=true
-g=centos64Guest
-host.ip=[destination blade]
-net=[vlan]
-net.adapter=vmxnet3
link-clone-test
I noticed that the resulting VM is also missing a network device (though my guess is that if the disk issue was resolved, that issue may be as well).
At this point I think I'm going to work with the Python API as I am more comfortable in Python than Go, but figured I would update this with what I had discovered so far in case it's useful in tracking down problems or adding new features.
Thanks!
This is also a feature I would like to have. :)
+1 Definitely needs this feature...
+1 utterly agreed. being a vsphere admin... i can't imagine no cloning... also from and to templates.
We'd also like to ensure support for the vStorage APIs for Array Integration during clones -
- https://www.vmware.com/resources/techresources/10337
- https://blogs.vmware.com/vsphere/2014/04/effect-vaai-cloning-flash-arrays.html
Surely, altought it wouldn't be the problem of a client library but more of ESXi itself. You either ask an ESXi or vcenter (who will design the working ESXi) to clone a vm via cloneSpec. The ESXi (since ESXi 4.5 if not mistaken) will use VAAI if available.
Advanced usage as Change block traking and vadp are rarerly used directly by client software (backup), VADP is not even publicly documented.
So no need to complicate there, just ask a clone and you shall get one. (concerning the sphere webservice api)
Adding to the list of people looking for clone options. I would really like to see the standard full clone options and ideally the new instant clone tech as well.
Last is checked, instant clone is in a private API... I would realy doubt seeing implemented in an open project like govmomi.
Even if finding the appy call is possible by looking at the call made (soap/onyx). It would be 'unsuported' and undocumented (API calls)
We all agree, it is a shame.
I would like to sketch what a clone function would be.
Basicaly this is would be calling the clone_vm function with a proper clone spec. https://www.vmware.com/support/developer/vc-sdk/visdk25pubs/ReferenceGuide/vim.vm.customization.Specification.html
Clone spec includes a few options to: -change the config while cloning -set a relocation spec to select datastore(s), provisioning type (thik or thin) as well as resource pool. -define or use customization spec (either manual or by name) -power on the vm (or not) -make it a template
Change the config
While cloning i don't change too many things as if done to wildly it can inflence the clone so this i use it mainly for cpu, mem and network. i would the include the following arguments: -c -m -net
Relocation spec
most often i change datastore and eventual provisioning type. The following arguments would be used -ds -datastore-cluster -disk.provisionning (thick|thin)
Customisation spec
i mostly use saved customization template so only a name is required. Moreover the specific configuration would be tedious (lightweight kickstart) so i wouldn't go further than the template -customisation
Templating and starting up the vm are already handled in the create
Any more requirements?
got a basic vm.clone command on my forked repo branch clone. I still would like to add
- storagepod management
- adapt cpu and memory
- customisationspec (by name) management
- templating
+1
Implemented in #486
I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.