VMware Cloud Community
mahmn
Enthusiast
Enthusiast

Clonning a vm via command line

Hi

I have created a windows machine as a template and for every user, I just clone that to another machine.   In the clone wizard, I customize the hardware (cpu, mem, disk) and then boot the new machine.

I would like to know if I can use any command in the terminal (vcenter, esxi, ...) to automate that. For example, similar to qemu, I am looking for a command like this

 

0 Kudos
11 Replies
scott28tt
VMware Employee
VMware Employee

@mahmn 

Moderator: Moved to vSphere Discussions, although I suspect the answer lies with PowerCLI...


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos
mahmn
Enthusiast
Enthusiast

I see some CLI guides for ESXi interface.

However, since one ESXi node can not see other's datastore, I would like to use vcenter/vsphere to select datastores and use them.

0 Kudos
depping
Leadership
Leadership

Do you already have vCenter Server? If so, for automation PowerCLI is probably your best bet.

0 Kudos
mahmn
Enthusiast
Enthusiast

Yes, I have the vcenter. However it uses linux like commands.

Moreover, I didn't find scripts for clonning in vcenter.

0 Kudos
depping
Leadership
Leadership

that is not how vCenter is supposed to be used. you are not supposed to shell in to vcenter and run scripts. ESXi does have a shell, and there are plenty of different "esxcli" command, but these are typically not used for VM management.

For automation of VM provisioning management it is recommended to use PowerCLI, or a different language if you prefer. 

0 Kudos
depping
Leadership
Leadership

We have SDKs for Perl, Python, Ruby etc. William has some great articles:

continuum
Immortal
Immortal

Sure - you can do that via ssh

### create empty new dir with a command like

mkdir /vmfs/volumes/home-of-the-clones/vmdir

### then cd to /vmfs/volumes/home-of-the-clones/vmdir

cd /vmfs/volumes/home-of-the-clones/vmdir

### create a new vmx-file as you like
### keep in mind that at least 1/3 of the lines in a vmx - will be autogenerated on first boot
### so just concentrate on the lines you really need

echo "echo the content of the vmx-file into clone.vmx"

echo >> clone.vmx .encoding = "UTF-8"
echo >> clone.vmx config.version = "8"
echo >> clone.vmx virtualHW.version = "8"
echo >> clone.vmx nvram = "server2016.nvram"
echo >> clone.vmx pciBridge0.present = "TRUE"
echo >> clone.vmx svga.present = "TRUE"
echo >> clone.vmx pciBridge4.present = "TRUE"
echo >> clone.vmx pciBridge4.virtualDev = "pcieRootPort"
echo >> clone.vmx pciBridge4.functions = "8"
echo >> clone.vmx pciBridge5.present = "TRUE"
echo >> clone.vmx pciBridge5.virtualDev = "pcieRootPort"
echo >> clone.vmx pciBridge5.functions = "8"
echo >> clone.vmx pciBridge6.present = "TRUE"
echo >> clone.vmx pciBridge6.virtualDev = "pcieRootPort"
echo >> clone.vmx pciBridge6.functions = "8"
echo >> clone.vmx pciBridge7.present = "TRUE"
echo >> clone.vmx pciBridge7.virtualDev = "pcieRootPort"
echo >> clone.vmx pciBridge7.functions = "8"
echo >> clone.vmx vmci0.present = "TRUE"
echo >> clone.vmx hpet0.present = "TRUE"
echo >> clone.vmx displayName = "server2016"
echo >> clone.vmx numvcpus = "2"
echo >> clone.vmx cpuid.coresPerSocket = "2"
echo >> clone.vmx memSize = "4680"
echo >> clone.vmx ide1:0.deviceType = "cdrom-image"
echo >> clone.vmx ide1:0.fileName = "win-install.iso"
echo >> clone.vmx ide1:0.present = "TRUE"
echo >> clone.vmx ethernet0.virtualDev = "e1000e"
echo >> clone.vmx ethernet0.networkName = "VM Network"
echo >> clone.vmx ethernet0.addressType = "generated"
echo >> clone.vmx ethernet0.present = "TRUE"
echo >> clone.vmx guestOS = "windows8srv-64"
echo >> clone.vmx disk.EnableUUID = "TRUE"
echo >> clone.vmx pciBridge0.pciSlotNumber = "17"
echo >> clone.vmx pciBridge4.pciSlotNumber = "21"
echo >> clone.vmx pciBridge5.pciSlotNumber = "22"
echo >> clone.vmx pciBridge6.pciSlotNumber = "23"
echo >> clone.vmx pciBridge7.pciSlotNumber = "24"
echo >> clone.vmx ethernet0.pciSlotNumber = "192"
echo >> clone.vmx vmci0.pciSlotNumber = "32"
echo >> clone.vmx scsi0.virtualDev = "lsisas1068"
echo >> clone.vmx scsi0.present = "TRUE"
echo >> clone.vmx scsi0:0.deviceType = "scsi-hardDisk"
echo >> clone.vmx scsi0:0.fileName = "server2016.vmdk"
echo >> clone.vmx scsi0:0.present = "TRUE"
echo >> clone.vmx scsi0.pciSlotNumber = "160"

### now create the vmdk with a command like vmkfstools -c 60g server2016.vmdk

vmkfstools -c 60g server2016.vmdk

### finally all you need to do is to register the VM - either from cli or using datastore-browser
### if you want - create an interactive script that asks for parameters like displayname , guestOS ....

 

Ulli

 

 


________________________________________________
Do you need support with a VMFS recovery problem ? - send a message via skype "sanbarrow"
I do not support Workstation 16 at this time ...

mahmn
Enthusiast
Enthusiast

@continuum 

I guess the commands you wrote are used for creating a new vm and not clonning. Is that right?

 
0 Kudos
continuum
Immortal
Immortal

Both tasks are quite related.
Basically a VM is assembled by a vmx-file + plus one or more vmdks. That is the part that you control - during initial setup and changes you make later on. ESXi adds some more files to that for its own use: vmsd, vmxf, vswap, nvram and so on.

If you split up the ingredients of a VM into 3 sections:
VM-identity
Virtual hardware
ESXi-autogenerated

and then look into the details inside the VM-identity section - have a look at the next table

 filesvmx-parameters 
VM-identity

associated vmdks

rdms

floppies

isos


displayName = "hugo"
ethernet0.generatedAddress = "00:0c:29:c2:99:05"
ethernet1.generatedAddress = "00:0c:29:c4:99:07"
numvcpus = "2"
cpuid.coresPerSocket = "2"
memSize = "4680"
scsi0:0.fileName = "hugo.vmdk"
scsi0:1.fileName = "hugo-user-data.vmdk"
ide1:0.fileName = "win-install.iso"

You want to change this details for all new VMs:

Virtual hardwarenvram


pciBridge0.pciSlotNumber = "17"
pciBridge0.present = "TRUE"
pciBridge4.functions = "8"
pciBridge4.pciSlotNumber = "21"
pciBridge4.present = "TRUE"
pciBridge4.virtualDev = "pcieRootPort"
pciBridge5.functions = "8"
pciBridge5.pciSlotNumber = "22"
pciBridge5.present = "TRUE"
pciBridge5.virtualDev = "pcieRootPort"
pciBridge6.functions = "8"
pciBridge6.pciSlotNumber = "23"
pciBridge6.present = "TRUE"
pciBridge6.virtualDev = "pcieRootPort"
pciBridge7.functions = "8"
pciBridge7.pciSlotNumber = "24"
pciBridge7.present = "TRUE"
pciBridge7.virtualDev = "pcieRootPort"

....

you reuse this configuration
from a good
template

 

the related
vmx-parameters
can be copied in
block

ESXi autogeneratedvmsd, vmsn, vmss, vswap
uuid.bios = "56 4d 1c 5a ff 86 6b f8-c8 e9 10 02 49 c2 99 05"
uuid.location = "56 4d 1c 5a ff 86 6b f8-c8 e9 10 02 49 c2 99 05"
this is done by ESXi
automatically
vCenter.........


Now consider that you want to modify the green part anyway - you need new mac adresses and new full paths and directories.
Then it no longer is a big difference between creating a new VM and cloneing an existing one.

For the identity details you probably need 5 - 15 vmx-parameters.
For the vmdks you can easily switch between creating a new one or run vmkfstools -i source-target.vmdk target.vmdk.


Now lets clone a VM - master "hugo" will be cloned to "isildur"

sh.cript:

mkdir "isildur"
cat hugo/hugo.vmx | grep -v "hugo" | grep -v "Address" > isildur/isildur.vmx

echo >> isildur/isildur.vmx displayName = "isildur"
echo >> isildur/isildur.vmx ethernet0.generatedAddress = "00:0c:29:c2:11:05"
echo >> isildur/isildur.vmx ethernet1.generatedAddress = "00:0c:29:c4:11:07"
echo >> isildur/isildur.vmx scsi0:0.fileName = "isildur-os.vmdk"
echo >> isildur/isildur.vmx scsi0:1.fileName = "isildur-personal-data.vmdk"

vmkfstools -i hugo/hugo.vmdk isildur/isildur-os.vmdk -d thin
and another command creates a new disk for the personal data of isildur.

one more command to register the vmx and your new clone is ready to use.

If you want linked clones with a protective snapshot for the original "hugo" you need a few more lines - but if you have done this a few times
you realize that all you need is a few vmxlines + one or more vmkfstools commands.


Ulli


________________________________________________
Do you need support with a VMFS recovery problem ? - send a message via skype "sanbarrow"
I do not support Workstation 16 at this time ...

depping
Leadership
Leadership

I still think using something like PowerCLI makes more sense then using these commands and editing a VMX file 🙂

0 Kudos
mahmn
Enthusiast
Enthusiast

Thanks guys.

I see some basic steps by @marco2601  at https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Clone-a-VM-with-a-script/m-p/509192#M1... which are good to start scripting.

0 Kudos