Sure, there are many ways to make a copy of an OS or VM and spin it up in another instance.
But here is the proper way…
with vSphere at least.
I’m assuming you already have basic knowledge of templating and wiping the system’s unique identifiers (sysprep for windows, etc)
When you have a VM crafted in the way that you like, with all the software installed or just simply up-to-date with Windows Updates and of course, past the installation.
The proper way to clone a VM in my opinion is to use the vmkfstool on the ESXi host itself. This can be used through the ESXi shell or SSH to the host.
You will use this tool to make a copy of the virtual hard drive, and later attach that new virtual hard drive to a new virtual machine you configure to spec. You make this the same way you would make any other VM, but instead of adding a new hard disk you attach one that already exists.
Let’s say for Windows Server 2012R2 we want to make a template.
In CMD, change directory to
Execute the following to shut down your Windows Server VM and leave it powered off for cloning:
sysprep /generalize /shutdown /oobe
I will not go into detail about how this behaves other than you will end up with a template-able virtual hard disk.
Now that our VM is off,
we should SSH or console into our ESXi host. I’m running vSphere 6.5 but this should work on most versions.
You’re going to make a new directory on a datastore. Call it what you like:
I was working on a project, so here’s what I did:
For you and your environment, replace what is necessary including the brackets:
Now change directory to your VM that you just powered off, or include that as part of the path in this next bit.
I’m going to clone the 11101-PRNT01.vmdk virtual disk to another file/folder.
This OS on the volume was sysprepped etc.
vmkfstools -i /vmfs/volumes/ULTRA/11101-PRNT01/11101-PRNT01.vmdk /vmfs/volumes/CRUCIAL/11101-RDBRK02/11101-RDBRK02.vmdk -d thin
It’s important to note the last bit
- while optional, is definitely recommended unless you know what you are doing and why.
- If you thin provisioned your disk originally, leaving that part out will cause it to thick provision and you could take up a lot of space quickly.
Here’s what you’ll do:
vmkfstools -i /vmfs/volumes/<your-datastore>/<your-vm-folder>/<your -vm-folder>.vmdk /vmfs/volumes/<your-datastore>/<your--new-vm-folder>/<your-new-vm-folder>.vmdk -d thin
you’ll see an output like this:
Destination disk format: VMFS thin-provisioned Cloning disk '/vmfs/volumes/ULTRA/11101-PRNT01/11101-PRNT01.vmdk'... Clone: 10% ..
And eventually it will finish.
Now you can attach this virtual hard drive to a new virtual machine and boot it directly into the state you cloned it–with new unique identifiers.
For consistency, I ensure I’m creating the new VM on the datastore I cloned to (when I have more than one datastore) so all the files pertinent to that VM end up in one location.
When creating your new VM, do this exactly how you would any normal VM but this time delete the “New Hard Disk” and attach an existing hard disk. Browse your datastore and select the cloned disk.
Alternatively, you can preserve this clone as a template and continue to clone from that source until you make a new template.
In summary, here’s how I copied the hard disk of 11101-PRNT01 to 11101-RDBRK02, maintaining the thin provision, so I could attach it to a new instance.
The ESXi Shell can be disabled by an administrative user. See the vSphere Security documentation for more information. [[email protected]:~] mkdir /vmfs/volumes/CRUCIAL/11101-RDBRK02 [[email protected]:~] vmkfstools -i /vmfs/volumes/ULTRA/11101-PRNT01/11101-PRNT01.vmdk /vmfs/v olumes/CRUCIAL/11101-RDBRK02/11101-RDBRK02.vmdk -d thin Destination disk format: VMFS thin-provisioned Cloning disk '/vmfs/volumes/ULTRA/11101-PRNT01/11101-PRNT01.vmdk'... Clone: 100% done. [[email protected]:~]