- 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
Categories: