Thursday, October 24, 2013

Clone a VM from the Command Line in VMWare ESXi 5.1

This post will go over how to clone a vm from the command line (and thus, make it easily scriptable).

Step 1:
Decide which vm you will be cloning.  The "CentOS 6.4 PXE" server has not been touched since creation, so will make a good base system.

Step 2:
Create the vm config file.  The local datastore for vm's on this esxi server is /vmfs/volumes/datastore1 which is a soft link to /vmfs/volumes/524734d7-f389d00a-4f68-b870f4dd73cf.

The directory contains all of the current vm's on the local datastore.

cd /vmfs/volumes/datastore1
ls -1
CentOS 6.4
CentOS 6.4 PXE
FreeBSD 9.1
Security Onion 12.04.3
Test 1
Ubuntu 10.0.4 x32

Create a directory for a new vm.

mkdir "Base CentOS 6.4"

Change to the new directory and copy the "PXE" config file:

cd Base\ CentOS\ 6.4
cp ../CentOS\ 6.4\ PXE/CentOS\ 6.4\ PXE.vmx "Base CentOS 6.4.vmx"

Make any necessary changes to the configuration file for the new system:

cat Base\ CentOS\ 6.4.vmx 
.encoding = "UTF-8"
config.version = "8"
virtualHW.version = "8"
displayName = "Base CentOS 6.4"
floppy0.present = "TRUE"
scsi0.present = "TRUE"
scsi0.sharedBus = "none"
scsi0.virtualDev = "pvscsi"
memsize = "512"
scsi0:0.present = "TRUE"
scsi0:0.fileName = "Base CentOS 6.4.vmdk"
scsi0:0.deviceType = "scsi-hardDisk"
ide1:0.present = "TRUE"
ide1:0.clientDevice = "TRUE"
ide1:0.deviceType = "cdrom-raw"
ide1:0.startConnected = "FALSE"
floppy0.startConnected = "FALSE"
floppy0.fileName = ""
floppy0.clientDevice = "TRUE"
ethernet0.present = "TRUE"
ethernet0.virtualDev = "vmxnet3"
ethernet0.networkName = "Management"
ethernet0.addressType = "generated"
ethernet1.present = "TRUE"
ethernet1.virtualDev = "vmxnet3"
ethernet1.networkName = "Production"
ethernet1.addressType = "generated"
guestOS = "rhel6"

Step 3:
Copy the hard disk (make sure the vm is powered off).

vmkfstools -i "/vmfs/volumes/datastore1/CentOS 6.4/CentOS 6.4.vmdk" \
"/vmfs/volumes/datastore1/Base CentOS 6.4/Base CentOS 6.4.vmdk"

Step 4:
Register the new machine with esxi. 

vim-cmd solo/registervm "/vmfs/volumes/524734d7-f389d00a-4f68-b870f4dd73cf/Base CentOS 6.4/Base CentOS 6.4.vmx"

Verify the new machine is registered

vim-cmd vmsvc/getallvms
...trim...
17     Base CentOS 6.4          [datastore1] Base CentOS 6.4/Base CentOS 6.4.vmx                 rhel6Guest     vmx-08            
...trim...  

Step 5:
Power on the vm.  Perform any post installation configuration.  Most notably, new ip addresses will need to be specified.  If necessary, use the dhcp server to get a temporary ip, and watch the server to see what leases are given out, and connect to the new lease.  The mac address can be verified in the vmx configuration file.

vim-cmd vmsvc/power.on 17



No comments:

Post a Comment