Using Vagrant to clone an existing VM instead of using a box

398 views
Skip to first unread message

j...@van-veldhuizen.nl

unread,
Oct 25, 2018, 1:08:29 AM10/25/18
to Vagrant
I would like to use a Vagrantfile to clone an existing VM instead of using a box.

The current vb.linked_clone = true works, but it still creates a base machine for the defined box.
I want to create VM's based on other linked clones.

Of course, I could just use the Virtualbox gui to do that, but I like to use the other features that Vagrant has. Like handling port collisions and provisioning the machine.


Alvaro Miranda Aguilera

unread,
Oct 25, 2018, 2:40:50 PM10/25/18
to vagra...@googlegroups.com
Hello Jan.

Vagrant uses a base box (sort of base template) to create a guest.

I don't think vagrant as today scan start from a linked clone.

Can you share more information on the use case you have or what particular need this will solve?

Thanks
Alvaro.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/2da22943-7b14-4c43-8f56-8e766b69ac2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Alvaro

j...@van-veldhuizen.nl

unread,
Oct 25, 2018, 2:54:25 PM10/25/18
to vagra...@googlegroups.com

Yes, I can.

The use case is:

 

A base windows VM, that I create as a box. This will be the starting point.

Then I can create a Vagrantfile like this:

  • Box: windowsbase
  • Linked Clone
  • Provisioning: install Visual Studio 2015

Another one:

  • Box: windowsbase
  • Linked Clone
  • Provisioning: install Visual Studio 2017

Now I have two linked clones sharing the same windows base VM.

Now the fun starts.

I want to use these VMs, and create linked clones to create different development environment for the projects I am working on.

The Visual Studio 2017 machine takes about 25Gb, so if this can be a base box for other VM’s I will save a lot of disk space.

 

I can achieve exactly that by using VirtualBox to create the linked clones and then put my dev stuff on those machines.

The thing is that I want to make use of the features that vagrant offers to provision and handle all the port collisions for me.

So instead of using a box as a starting point for a new VM, it would be great if Vagrant can use another VM as the starting point.

Alvaro Miranda Aguilera

unread,
Oct 25, 2018, 3:52:16 PM10/25/18
to vagra...@googlegroups.com
Hello.

I think is a valid use case however not sure will be implemented in vagrant core since clones are a bit sensitive.

You could either have a script that does this for you and then try (to build) a plugin.. i think could address this.

Here is a proof of concept on linux that shows how could be done.

-- Dont try this in prod, and YMMV, etc.


1. create a base VM.


0  $ mkdir n1

0  $ cd n1

0  $ vagrant init -m alvaro/xenial64

0  $ vagrant up --provider virtualbox


2. do some work


< here is where you install SW, etc>


3. grab uuid and create a named snapshot.



0  $ cat .vagrant/machines/default/virtualbox/id 

bf944f9e-cd87-43ba-8da6-21e51f0409e4

0  $ VBoxManage snapshot bf944f9e-cd87-43ba-8da6-21e51f0409e4 take forclone --live

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Snapshot taken. UUID: d6b5dcf4-4212-4456-896a-03b2d4e57613

0  $ 


4. create a new project directory



0  $ cd ..

0  $ cp -ap n1 n2

0  $ cd n2

0  $ 


5. create a clone from the snap and update the new project box.


0  $ VBoxManage clonevm bf944f9e-cd87-43ba-8da6-21e51f0409e4 --snapshot forclone --options link --name n2 --register

0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

Machine has been successfully cloned as "n2"

0  $ echo 35117e02-e226-43e7-b5b6-cfec1f0c8338 > .vagrant/machines/default/virtualbox/id

0  $ 


sizes that shows is a reduced size VM. and not a full copy



0  $ du -sh n1*/ n2/

2.7G n1_default_1540496025122_57328/

118M n2/

0  $ 




6. vagrant reload will work from here.


0  $ vagrant reload

==> default: This machine used to live in /Users/alvaro/Downloads/clone/n1 but it's now at /Users/alvaro/Downloads/clone/n2.

==> default: Depending on your current provider you may need to change the name of

==> default: the machine to run it as a different machine.

==> default: Discarding saved state of VM...

==> default: Checking if box 'alvaro/xenial64' is up to date...

==> default: Clearing any previously set forwarded ports...

==> default: Fixed port collision for 22 => 2222. Now on port 2200.

==> default: Clearing any previously set network interfaces...

==> default: Preparing network interfaces based on configuration...

    default: Adapter 1: nat

==> default: Forwarding ports...

    default: 22 (guest) => 2200 (host) (adapter 1)

==> default: Booting VM...

==> default: Waiting for machine to boot. This may take a few minutes...

    default: SSH address: 127.0.0.1:2200

    default: SSH username: vagrant

    default: SSH auth method: private key

==> default: Machine booted and ready!

==> default: Checking for guest additions in VM...

==> default: Mounting shared folders...

    default: /vagrant => /Users/alvaro/Downloads/clone/n2

0  $ 


Screenshot 2018-10-25 21.42.01.png

alvaro


For more options, visit https://groups.google.com/d/optout.


--
Alvaro

j...@van-veldhuizen.nl

unread,
Nov 8, 2018, 4:49:20 PM11/8/18
to Vagrant
I forked the Vagrant Github repository and discovered that it is written in Ruby.
I have never seen Ruby before, but it was surprisingly simple to create a version that does exactly what I want.

Just check my github repo and see the changes in my latest commit: https://github.com/janvanveldhuizen/vagrant
Reply all
Reply to author
Forward
0 new messages