Skip to content

How to clone_from_vm

Jonathan Senkerik edited this page Apr 2, 2018 · 1 revision

How to clone_from_vm. (vagrant-vmware-esxi plugin V 2.1.0+)

  • The vagrant-vmware-esxi plugin can run in two ways. The first method is to download/use boxes on your local pc/laptop, then transfer the box components to esxi. The second method is to clone a VM on the esxi host. This WIKI page is the how-to, to setup and use the clones (the second method).

  • The big advantage of using esxi.clone_from_vm is that doing 'vagrant up' is much faster than transferring from your local pc/laptop to esxi. The disadvantage is that vagrant doesn't manage the box any longer. You cannot update the boxes as easily.

Prepare the source VM

  • If you want to build your own base box from scratch, see https://www.vagrantup.com/docs/boxes/base.html.
  • I recommend putting your source VM's in a resource pool. For example, if you have a resource pool called 'Templates' and a VM called 'centos7'. In your Vagrantfile, you would specify esxi.clone_from_vm = 'Templates/centos7'
  • The VM must be powered off, otherwise ovftool will NOT be able to access the vmdk and vmx files.
  • To use a public box, follow these steps as an example. First create a 'Templates' Resource Pool on your esxi host. Then use vagrant to upload a public box to esxi.
$ mkdir ~/Templates-centos7
$ cd ~/Templates-centos7
$ vagrant init
$ vi Vagrantfile

#
# Vagrant file for Template/centos7 
#
$script = <<SCRIPT
curl -L https://goo.gl/TvhDAq > /home/vagrant/.ssh/authorized_keys 2>/dev/null
chmod 700 /home/vagrant/.ssh
chmod 600 /home/vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant /home/vagrant
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = 'generic/centos7'

  config.vm.synced_folder('.', '/vagrant', type: 'nfs', disabled: true)
  config.vm.synced_folder('.', '/Vagrantfiles', type: 'rsync', disabled: true)

  config.vm.provision "shell", inline: $script

  config.vm.provider :vmware_esxi do |esxi|
    esxi.esxi_hostname = "esxi_host_or_ip"
    esxi.esxi_username = "root"
    esxi.esxi_password = 'prompt:'
    esxi.esxi_virtual_network = ["VM Network"]
    esxi.guest_name = "centos7"
    esxi.guest_memsize = "1024"
    esxi.guest_numvcpus = "1"
    esxi.esxi_resource_pool = "/Templates"
    esxi.local_allow_overwrite = 'True'
  end
end


$ vagrant up
$ vagrant halt

Create and add the dummy box

$ mkdir ~/dummybox
$ cd ~/dummybox
$ cat >metadata.json <<EOF
{"provider":"vmware"}
EOF

$ tar cvf dummy.box metadata.json

$ vagrant box add --name esxi_clone/dummy dummy.box

Create a new Vagrantfile that will clone a VM.

$ mkdir ~/test
$ cd ~/test
$ vagrant init
$ vi Vagrantfile

#
#  Clone_from_vm test
#
Vagrant.configure("2") do |config|
  config.vm.box = 'esxi_clone/dummy'

  config.vm.synced_folder('.', '/vagrant', type: 'nfs', disabled: true)
  config.vm.synced_folder('.', '/Vagrantfiles', type: 'rsync', disabled: true)

  config.vm.provider :vmware_esxi do |esxi|
    esxi.esxi_hostname = "esxi_host_or_ip"
    esxi.esxi_username = "root"
    esxi.esxi_password = 'prompt:'
    esxi.clone_from_vm = 'Templates/centos7'
    esxi.esxi_virtual_network = ["VM Network"]
    esxi.esxi_resource_pool = "/"
    esxi.local_allow_overwrite = 'True'
  end
end


$ vagrant up