Posted On January 15, 2021

How to Clone VMWare Virtual Machine using CLI

kimconnect 0 comments
blog.KimConnect.com >> Virtualization >> How to Clone VMWare Virtual Machine using CLI
  • 1. SSH into the ESXi host to perform VM cloning via Command Line Interface (CLI)
# Set variables
sourceDataStore=/vmfs/volumes/ESX2-500GB
sourceVmName=UbuntuServerTemplate
destinationVmName=linux03
destinationDataStore=/vmfs/volumes/ESX2-1TB

# Turn off template VM
sourceVmid=$(vim-cmd vmsvc/getallvms | grep $sourceVmName|sed -e 's/\s.*$//')
vim-cmd vmsvc/power.off $sourceVmid

# Create a directory at the destination prior to cloning
mkdir "$destinationDataStore/$destinationVmName"
# Perform the clone
sourceDisk="$sourceDataStore/$sourceVmName/$sourceVmName.vmdk"
destinationDisk="$destinationDataStore/$destinationVmName/$destinationVmName.vmdk"
vmkfstools -i $sourceDisk $destinationDisk -d thin -a buslogic

# Create a new VM
vmxFile="$destinationDataStore/$destinationVmName/$destinationVmName.vmx"
vmid=$(vim-cmd vmsvc/createdummyvm $destinationVmName $vmxFile)

# If vmx file already exists, use this command
# vim-cmd solo/registervm $vmxFile

# Configure cloned machine:
#
# Obtain source machine's configurations
sourceVmx=$sourceDataStore/$sourceVmName/$sourceVmName.vmx
ethernet0=$(grep "ethernet0.pciSlotNumber" $sourceVmx | grep -Eo '[[:digit:]]{2,}')
ethernet0Network=$(grep "ethernet0.networkName" $sourceVmx | grep -Eo '\"(.*)\"')
osType=$(grep "guestOS =" $sourceVmx | grep -Eo '\"(.*)\"')
memSize=$(grep "memSize =" $sourceVmx | grep -Eo '[[:digit:]]+')
numvcpus=$(grep "numvcpus =" $sourceVmx | grep -Eo '[[:digit:]]+')
virtualHwVersion=$(grep "virtualHW.version" $sourceVmx | grep -Eo '[[:digit:]]{2,}')
#
# Edit dummy file to update these values
sed '/scsi0:0.fileName/ c\scsi0:0.fileName = \"'"$destinationVmName"'.vmdk\"' -i $vmxFile
sed '/guestOS/ c\guestOS = '"$osType"'' -i $vmxFile
sed '/virtualHW.version/ c\virtualHW.version = \"'"$virtualHwVersion"'\"' -i $vmxFile
cat <<appendEOF >> $vmxFile
#### custom contents ####
sched.scsi0:0.shares = "normal"
memSize = "$memSize"
numvcpus = "$numvcpus"
ethernet0.pciSlotNumber="$ethernet0"
ethernet0.virtualDev="vmxnet3"
ethernet0.networkName = $ethernet0Network
ethernet0.addressType = "generated"
ethernet0.uptCompatibility = "TRUE"
ethernet0.present = "TRUE"
appendEOF
  
# How to remove a VM
# destinationVmid=$(vim-cmd vmsvc/getallvms | grep $destinationVmName|sed -e 's/\s.*$//')
# vim-cmd vmsvc/power.off $destinationVmid
# vim-cmd /vmsvc/unregister $destinationVmid
 
# Remove any unwanted files that may remain from the createdummyvm command
# Remove junk files: removing files with an underscore it their names
ls $destinationDataStore/$destinationVmName/ # scan for the existence of files before running the next command
find $destinationDataStore/$destinationVmName/ -type f -name "$destinationVmName*_*" | xargs rm
ls $destinationDataStore/$destinationVmName/ # verify the the extraneous files were removed

# List all existing guest VMs
vim-cmd vmsvc/getallvms
  
# Power on VM
# vmid=14 # this variable should have been assigned previously via 'vim-cmd vmsvc/createdummyvm' command
vim-cmd vmsvc/power.on $vmid
  
# Check power state
vim-cmd vmsvc/power.getstate $vmid
  
# Power off VM
vmid=14
vim-cmd vmsvc/power.off $vmid
  
# Optional: create a new disk and add it to existing guest VM
vmid=14
controllerNumber=0 # SCSI controller id
diskNumber=1 # disk id
$newdisk="$destinationDataStore/$destinationVmName/$destinationVmName_1.vmdk"
vmkfstools --createvirtualdisk 100G --diskformat thin $newdisk
vim-cmd vmsvc/device.diskaddexisting $vmid $newdisk $controllerNumber $diskNumber
  • 2. Configure cloned VM
# Example: Ubuntu Server 20.04 LTS

# Configure networking
sudo vim /etc/netplan/*.yaml
### Sample content ###
network:
  version: 2
  renderer: networkd
  ethernets:
    ens192:
      dhcp4: false
      addresses: [10.10.10.99/24]
      gateway4: 10.10.10.1
      nameservers:
        addresses: [8.8.8.8,1.1.1.1]
#######################
sudo netplan apply

# Change the hostname
hostname=linux03
sudo hostnamectl set-hostname $hostname 
 
# Update the /etc/hosts file
sed -i '/127.0.1.1/ c\127.0.1.1 '"$hostname"'' /etc/hosts
 
# Update cloud.cfg
sed -i '/preserve_hostname:/ c\preserve_hostname: true' /etc/cloud/cloud.cfg

Leave a Reply

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

Related Post

Windows Domain Controller restore Procedure

1. Terminate instance on AWS or Delete VM from VMWare 2. Perform these steps on…

How To Upgrade NextCloud 22.1.1 to 22.2.0 When Deployed with Kubernetes & Helm

Step 1: Navigate to nextcloud > html > edit version.php <?php $OC_Version = array(22,1,1,2); $OC_VersionString…

PowerShell: List All Hyper-V Snapshots of All VMs in All Clusters in Domain

# listHyperVSnapshots.ps1 $clusterName='*' function listAllHyperVSnapshots([string[]]$clusterName){ function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709…