Cloning Operations in vSphere

Speed and automation is a high priority for many IT organizations. This is especially true when you think of your customers (employees or customers that consume your IT services.) Expectations have changed from months to years, to days and weeks and sometimes even seconds. If I can download an app on my phone and get instant information, why can’t IT deliver me a certain service immediately? This is especially true when it comes to Continuous Integration and Continuous Development pipelines (CI/CD) and developer operations as a whole. While containers are certainly becoming the primary choice for speedy resources, there are still many organizations that require an independent virtual machine. One of the most common I can think of, are the security companies that need to test malware and other threats amongst many individual machines. Virtual machine cloning is a common tool to accomplish this, and we will dive deeper into how you can do that with native vSphere API’s. This will be especially important if you are on a VxRail/vSAN cluster where UI driven cloning is largely absent.

Full Clones, Linked Clones, and Instant Clones

By far the best reading on this subject is going to be on William Lam’s blog. Most of what I learned on cloning was from him. He has several scripts to help you get started. My hope in this blog is to water this down to a 101/201 level, why you would choose one or the other, and a basic script to get you started. I’m also focusing on what is available native in vSphere. Many cloning operations can be done with enterprise-class storage arrays. Since their operations vary by vendor, I will not cover them in-depth here.  

Let’s start with the basics of what each clone gives you, starting with Full Clones. A Full Clone is a completely independent copy of a virtual machine that does not share any shared virtual resources (like VMDK’s.) This operation is in the vSphere client UI, so it is simple to do. However, since this is an entire new copy, the process does take a while. How long it will take will largely depend on several factors. I’m not going to list out specific times because I don’t want to set an improper expectation. But I will compare the relative time it took for each operation to complete in vCenter.

Next, we are going to discuss Linked Clones.  Linked Clones are cloned virtual machines off of a source vSphere snapshot. It uses the data off of that original snapshot and creates a new delta disk for any changes on its own VM resources. Linked Clones are not in the UI. You will need to access it via the vSphere API through a tool of your choice. The most common I see in the field is through VMware’s PowerCLI library for PowerShell. To call a linked clone, we will use the New-VM cmdlet and make sure that Linkedclone:$true is in our syntax. You will need to make sure you have a snapshot of the VM to execute the command. 

If you are looking to make multiple virtual machines by using Linked Clones. You can use this script I’ve made here. 

#Basic cloning script with prompts.  You must be connected to vCenter first before you begin the operation.  

$CloneBaseName = Read-Host -Prompt "Input clone base name"
$CloneTotalCount = Read-Host -Prompt "Input the number of clones you want to make"
$sourcevm = Read-Host -Prompt "What is the source VM?"
$snap = Read-Host -Prompt "What is the snapshot name?"
$custspec = Read-Host -Prompt "What is the Guest Customization Spec?"
$RP = Read-Host -Prompt "Which resource pool do you want to use?"

    Foreach ($i in 1..$CloneTotalCount) {
        $CloneName = $CloneBaseName+$i
        
        New-VM -VM $sourcevm -ResourcePool $RP -Name $CloneName -Datastore vSandatastore -LinkedClone:$true -ReferenceSnapshot $snap -OSCustomizationSpec $custspec
        
        Write-Host "$CloneName created, initiating boot"

        Start-VM -VM $CloneName
    }

Start-Sleep -seconds 60

Get-VM $CloneBaseName* | select name, Powerstate, @{N="IPAddress"; E={$_.Guest.IPAddress[0]}}, @{N="DnsName"; E={$_.ExtensionData.Guest.Hostname}}

It’s also possible to promote a linked clone to a full independent copy. I’m not going to go into detail here, but its good to know that it is possible.

The last clone option I’m going to cover is probably the most exciting. An Instant Clone is a clone operation where the disk as well as the memory and CPU information is forked. This means that not only does the creation of these virtual machines happen very fast, but the machine is already on and does not need to boot! Instant Clones were introduced in vSphere 6.0 (under the name VMfork,) mostly utilized by Horizon View for VDI.  Just like the Linked Clone script, I’ve made an Instant Clone script with prompts. Instant Clones are more complex because it requires a in guest script as well as the external API call. However, There are some great existing scripts out there today to get you started. I used Lam’s Ubuntu script to initiate the in guest portion, and an amended external script to spin up 100 virtual machines in my lab!

Just spinning up 100 Nginx web servers in my lab, no big deal

The speed difference between a Full Clone and an Instant Clone or Linked Clone is massive. It does make to where using a Full Clone is for one-off situations where you just need a full independent copy. You typically would not integrate a Full Clone into a use case that requires fast copies to be available. When it comes to speed of Instant Clones or Linked Clones, the actual task in vCenter is pretty close to the same time. However, the difference will be how fast that machine is accessible and usable. This is why Horizon View largely uses Instant Clones.

The time it took the vCenter task to complete. Doesn’t necessarily reflect the time to a usable machine.

Which Clone Operation do I Use?

Now let’s bring all these options together to understand which would be best for a given use case.  

Full Clone

Benefits – Full independent copy of a virtual machine with no dependencies.

Drawbacks – Slow operation because data is fully copied.  

Linked Clone

Benefits – Quick creation. You can add guest customization specs to add it to a domain among other customization properties. Storage space is saved because it uses the snapshot data.  

Drawbacks – No UI workflow as of writing this. Dependant on a snapshot.

Instant Clone

Benefits – Quick creation and does not have to go through a boot cycle. Fastest from clone to the usable machine. Can save storage, memory, and CPU resources since all are forked.  

Drawbacks – Instant-on functionality doesn’t allow some property changes that require a reboot. For example, joining a domain.  

Wrap Up!

As companies understand that technology and software development is an important part of their business, you may find yourself in a situation where you need to choose the proper cloning strategies. Whether it be for advanced CI/CD pipelines, or rudimentary early stage testing processes, understanding your cloning option in vSphere will ensure you get off to the right start. Do you use Linked Clones or Instant Clones? Leave a comment with your use cases to help others see the potential benefit for them!

Leave a Reply

Your email address will not be published. Required fields are marked *