Easy Cloning

Here you can provide suggestions on how to improve the product, website, etc.
CHAUVIERE
Posts: 6
Joined: 3. Oct 2008, 14:21

Script to clone a VM

Post by CHAUVIERE »

I made a script to clone vm under VB.
If it can help....

http://www.chauviere.org/jr/pages/Tech/shell/
TerryE
Volunteer
Posts: 3572
Joined: 28. May 2008, 08:40
Primary OS: Ubuntu other
VBox Version: PUEL
Guest OSses: Ubuntu 10.04 & 11.10, both Svr&Wstn, Debian, CentOS
Contact:

Post by TerryE »

Chauviere, I just tried to download this and got a "404 page not found".
Read the Forum Posting Guide
Google your Q site:VirtualBox.org or search for the answer before posting.
right-or-am-i-wrong
Posts: 28
Joined: 2. Jan 2009, 00:29

Post by right-or-am-i-wrong »

please check this out:
http://forums.virtualbox.org/viewtopic. ... f3f0a37084

eFloh has created this for newer VBox versions with the new sun install path.
Please post to the thread above.

Kind regards
eohrnberger
Posts: 18
Joined: 17. Feb 2009, 02:34

Is a clone a clone without snapshots?

Post by eohrnberger »

I too have to admit that the addition of cloning VMs in the VirtualBox management GUI would be a great addition.

From a machine state perspective, since the source VM would have a hard disk VDI associated with it as well as a set of snapshots, these would have to be carried over to the copy. Would it really be a clone if the snapshots were not carried along?

On the other hand, selecting a particular snapshot from the history, it could be cloned to base state of a new VM.

So it's a valid choice either way for the user to make.

Lastly, wouldn't it also be good feature to 'collapse' all or a set of snapshots into the base VDI? If you choose a later snapshot, all previous snapshots could be applied into the base to become the new base.

Just my two cents worth. Best wishes. Erik.
SivEd
Posts: 5
Joined: 22. Jun 2011, 16:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: XP, 7, Ubuntu 11.04

Re: Easy Cloning

Post by SivEd »

Hi there,

I have updated this script a bit. It now works with VB version 4. I also wanted to be able to specify the path to the source and destination drives so there are now two more parameters for the source drive path and the clone drive path.

An example:

Code: Select all

./clonevm ubuntu-template ubuntu1 /media/raid/ubuntu-template.vdi /media/raid/ubuntu1.vdi
Hope it is useful to someone, I use it all the time and it works as a "one-click" solution for me - well one line, anyway. If I run this in a shell, the resulting VM shows up in phpVirtualBox on another client without even refreshing.

This is working on Ubuntu server 11.04.

Code: Select all

#!/bin/bash

############## Clone VirtualBox Machine #############################################
# Written By: Samy Antoun                                                           #
# Usage:                                                                            #
# ./clonevm.sh "Source Machine" "Clone Machine" "Soure Disk Path" "Clone Disk Path" #
# Limitation:                                                                       #
# Clone only a VM that has ONE Hard Drive                                           #
#####################################################################################

############## Functions

function ChkUtility {
    res=`which $1 | grep "no $1"`
    if [ "$res" != "" ]; then
        echo "\"$1\" is missing";
        exit 2;
    fi
}

function GetValue {
    Line=`VBoxManage -nologo showvminfo "$1" -machinereadable | grep "^$2="`;
    Raw=`echo $Line | cut -d= -f2`;
    Fixed=`echo $Raw | sed 's/"//g'`;
    echo $Fixed;
}

function CheckUUID {
    CheckID=`echo "$1" | sed -e "s/\w\{8\}-\w\{4\}-\w\{4\}-\w\{4\}-\w\{12\}/ok/"`;
    if [ "$CheckID" != "ok" ]; then
        echo "Source Disk \"$1\" UUID Not Valid";
        exit 2;
    fi
}

############## Sanity Check
# Make Sure that we have the required software
ChkUtility "grep";
ChkUtility "cut";
ChkUtility "sed";
ChkUtility "uuidgen";
ChkUtility "VBoxManage";

# Command line Parameters must be 4
if [ $# -lt 4 ]; then
    echo "Parameter Missing";
    echo "Usage: ./clonevm SrcVM DestVM SrcDrivePath DestDrivePath";
    exit 2;
fi

# Get Command line Parameters
SrcVM=$1;
CloneVM=$2;
SrcDrive=$3;
CloneDrive=$4;

# Set Clone Machine Directory
MachinesDirLine=`VBoxManage -nologo list systemproperties | grep "Default machine folder:"`;
MachinesDirRaw=`echo $MachinesDirLine | cut -d: -f2`;
MachinesDir=`echo $MachinesDirRaw | sed -e "s/^ *//g"`;
CloneMachineDir=$MachinesDir/$CloneVM;

# Make Sure that the Source VM is Registered
res=`VBoxManage -nologo showvminfo "$SrcVM" | grep "[!]"`
if [ "$res" != "" ]; then
    echo "Source VM \"$SrcVM\" Not Registered";
    exit 2;
fi

# Make Sure that the Clone VM NOT Registered
# res=`VBoxManage -nologo showvminfo "$CloneVM" | grep "[!]"`
# if [ "$res" = "" ]; then
#     echo "Clone VM \"$CloneVM\" Already Registered";
#     exit 2;
# fi

# Make Sure that the Clone VM Directory Doesn't Exsits
if [ -d "$CloneMachineDir" ]; then
   echo "The Directory \"$CloneMachineDir\" Already Exists";
   exit 2;
fi

# Make Sure that the Source Disk UUID is Valid
SrcDiskID=`GetValue "$SrcVM" "HdaImageUUID"`;
CheckDiskID=`CheckUUID "$SrcDiskID"`;

# Make Sure that the Source Machine UUID is Valid
SrcVMid=`GetValue "$SrcVM" "UUID"`;
CheckDiskID=`CheckUUID "$SrcVMid"`;

# Make Sure that the Source Machine Config File Exists
SrcVMcfg=`GetValue "$SrcVM" "CfgFile"`;
if [ ! -f "$SrcVMcfg" ]; then
    echo "Source Config File \"$SrcVMcfg\" Doesn't Exist";
    exit 2;
fi

############## Make Clone Machine Directory and Copy Machine Config File
mkdir "$CloneMachineDir";
echo "Created Clone Machine Directory \"$CloneMachineDir\""
cp -f "$SrcVMcfg" "$CloneMachineDir/$CloneVM.xml";
echo "Copied Source Machine Config File \"$SrcVMcfg\" to \"$CloneMachineDir/$CloneVM.xml\""

############## Clone Disk
echo "Cloning Disk ..."
## VBoxManage -nologo clonevdi "$SrcDiskID" "/media/raid/$CloneVM.vdi";
VBoxManage -nologo clonevdi "$SrcDrive" "$CloneDrive";
echo "Source Disk has been Cloned to \"$CloneDrive\""

############## Create a new Machine UUID and replace the original
sed -i "s/<Machine uuid=\"{$SrcVMid}\" name=\"$SrcVM\"/<Machine uuid=\"{`uuidgen`}\" name=\"$CloneVM\"/" "$CloneMachineDir/$CloneVM.xml";
echo "Clone Machine UUID has been Updated"

############## Register the New Machine
VBoxManage -nologo registervm "$CloneMachineDir/$CloneVM.xml";
echo "Clone Machine has been Registered"

############## Remove Hard Disk Description
## sed -i 's/<HardDiskAttachment .*\/>//' "$CloneMachineDir/$CloneVM.xml";
VBoxManage storageattach "$CloneVM" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium none
echo "Hard Disk Description has been Removed"

############## Register the New Disk
## VBoxManage -nologo modifyvm "$CloneVM" -hda "$CloneVM.vdi";
VBoxManage storageattach "$CloneVM" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "/media/raid/$CloneVM.vdi"
echo "Clone Machine Hard Disk has been Registered"

############## Done
echo -e "*** DONE ***\n*** The Clone Machine \"$CloneVM\" Created Succesfully ***"
Note that I have left the original commands in place for reference commented with "##"

Regards,
Pete
Anupambrt
Posts: 16
Joined: 4. Sep 2011, 09:47
Primary OS: Ubuntu other
VBox Version: OSE other
Guest OSses: Ubuntu 11.04, OpenSuse 11.04

Re: Easy Cloning

Post by Anupambrt »

Hello friends,
Will this script work twice on a VM? Means can it be used to make 2 duplicates of a VM(original VM+2 clones)? plus how can I provide this vm to some user distantly located (rdesktop <ip>:port)? if yes how can i get ip and port number?
Thank you in advance....
SivEd
Posts: 5
Joined: 22. Jun 2011, 16:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: XP, 7, Ubuntu 11.04

Re: Easy Cloning

Post by SivEd »

Yes, it works as many times as you need. It creates a new virtual machine with new drives every time. I keep a "template" XP Virtual machine which I clone over and over. Takes about a minute each time :)

Pete
Anupambrt
Posts: 16
Joined: 4. Sep 2011, 09:47
Primary OS: Ubuntu other
VBox Version: OSE other
Guest OSses: Ubuntu 11.04, OpenSuse 11.04

Re: Easy Cloning

Post by Anupambrt »

Hey thanks for replying,
I did run the command but I am getting some errors I am posting terminal output in code block below... please convey me my mistakes...
thank you in advance...

Code: Select all

anupam@anupam-Inspiron-N5010:~/Desktop/untitled folder$ ./clonevm.sh Ubuntu_ Ubuntu_43 '/home/anupam/VirtualBox VMs/Ubuntu_/Ubuntu_.vdi' '/home/anupam/VirtualBox VMs/Ubuntu_43/Ubuntu_43.vdi'
Created Clone Machine Directory "/home/anupam/VirtualBox VMs/Ubuntu_43"
Copied Source Machine Config File "/home/anupam/VirtualBox VMs/Ubuntu_/Ubuntu_.vbox" to "/home/anupam/VirtualBox VMs/Ubuntu_43/Ubuntu_43.xml"
Cloning Disk ...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: 0318f86d-21cf-4543-8dc3-bcd2d9736bdd
Source Disk has been Cloned to "/home/anupam/VirtualBox VMs/Ubuntu_43/Ubuntu_43.vdi"
Clone Machine UUID has been Updated
VBoxManage: error: Cannot register the hard disk '/home/anupam/VirtualBox VMs/Ubuntu_43/Ubuntu_.vdi' {c8c3c06d-c1c8-4d0e-aa84-e02321c749d9} because a hard disk '/home/anupam/VirtualBox VMs/Ubuntu_/Ubuntu_.vdi' with UUID {c8c3c06d-c1c8-4d0e-aa84-e02321c749d9} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "OpenMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 88 of file VBoxManageMisc.cpp
Clone Machine has been Registered
VBoxManage: error: Could not find a registered machine named 'Ubuntu_43'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 250 of file VBoxManageStorageController.cpp
Hard Disk Description has been Removed
VBoxManage: error: Could not find a registered machine named 'Ubuntu_43'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 250 of file VBoxManageStorageController.cpp
Clone Machine Hard Disk has been Registered
*** DONE ***
*** The Clone Machine "Ubuntu_43" Created Succesfully ***
And will i be able to use those vms just by startvm or need i do something before dat?
SivEd
Posts: 5
Joined: 22. Jun 2011, 16:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: XP, 7, Ubuntu 11.04

Re: Easy Cloning

Post by SivEd »

OK, few things to check...

Are you using VirtualBox 4?

If so, don't use the script in the original post as it won't work. It was written for VirtualBox 3. Use my updated script in the post above yours.

If you are using VirtualBox 3, of course use the original script :)

Here's output on my machine...

Code: Select all

pete@vhost:~$ VBoxManage -v
4.0.8r71778

pete@vhost:~$ ./clonevm vm-template vm-example /media/raid/vm-template.vdi /media/raid/vm-example.vdi
Copied Source Machine Config File "/home/pete/VirtualBox VMs/vm-template/vm-template.xml" to "/home/pete/VirtualBox VMs/vm-example/vm-example.xml"
Cloning Disk ...
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
Clone hard disk created in format 'VDI'. UUID: e89a6327-e75b-4d73-ad33-8e2f1ec31dc4
Source Disk has been Cloned to "/media/raid/vm-example.vdi"
Clone Machine UUID has been Updated
Clone Machine has been Registered
Hard Disk Description has been Removed
Clone Machine Hard Disk has been Registered
*** DONE ***
*** The Clone Machine "vm-example" Created Succesfully ***
pete@vhost:~$
You don't need to put the full path on the virtual machine names (the script asks VBoxManage where to look)? Maybe that's it...

Otherwise, maybe some kind of permissions error?

The VMs are just normal VMs, so start them like any other :)

Cheers,
Pete
Anupambrt
Posts: 16
Joined: 4. Sep 2011, 09:47
Primary OS: Ubuntu other
VBox Version: OSE other
Guest OSses: Ubuntu 11.04, OpenSuse 11.04

Re: Easy Cloning

Post by Anupambrt »

Hey thanks for replying :) ,
I am using virtualbox 4.0.4.
when i use

Code: Select all

./clonevm.sh Ubuntu_template ubuntu_example /media/raid/Ubuntu_template.vdi /media/raid/ubuntu_example.vdi
this to run script then clone disk doesnot get created I am posting terminal output. Are there any pre-requisites for this script which you have performed. I have done

Code: Select all

chmod 777 clonecv.sh


so I don't think its permissions that's causing probs....

Code: Select all

anupam@anupam-Inspiron-N5010:~/Desktop/untitled folder$ ./clonevm.sh Ubuntu_template ubuntu_example /media/raid/Ubuntu_template.vdi /media/raid/ubuntu_example.vdi
Created Clone Machine Directory "/home/anupam/VirtualBox VMs/ubuntu_example"
Copied Source Machine Config File "/home/anupam/VirtualBox VMs/Ubuntu_template/Ubuntu_template.vbox" to "/home/anupam/VirtualBox VMs/ubuntu_example/ubuntu_example.xml"
Cloning Disk ...
VBoxManage: error: Could not find file for the medium '/media/raid/Ubuntu_template.vdi' (VERR_FILE_NOT_FOUND)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component Medium, interface IMedium, callee nsISupports
Context: "OpenMedium(Bstr(pszFilenameOrUuid).raw(), enmDevType, AccessMode_ReadWrite, pMedium.asOutParam())" at line 209 of file VBoxManageDisk.cpp
Source Disk has been Cloned to "/media/raid/ubuntu_example.vdi"
Clone Machine UUID has been Updated
VBoxManage: error: Cannot register the hard disk '/home/anupam/VirtualBox VMs/ubuntu_example/Ubuntu_.vdi' {c8c3c06d-c1c8-4d0e-aa84-e02321c749d9} because a hard disk '/home/anupam/VirtualBox VMs/Ubuntu_template/Ubuntu_.vdi' with UUID {c8c3c06d-c1c8-4d0e-aa84-e02321c749d9} already exists
VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "OpenMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 88 of file VBoxManageMisc.cpp
Clone Machine has been Registered
VBoxManage: error: Could not find a registered machine named 'ubuntu_example'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 250 of file VBoxManageStorageController.cpp
Hard Disk Description has been Removed
VBoxManage: error: Could not find a registered machine named 'ubuntu_example'
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBox, interface IVirtualBox, callee nsISupports
Context: "FindMachine(Bstr(a->argv[0]).raw(), machine.asOutParam())" at line 250 of file VBoxManageStorageController.cpp
Clone Machine Hard Disk has been Registered
*** DONE ***
*** The Clone Machine "ubuntu_example" Created Succesfully ***

Im using 64 bit Ubuntu 11.04 host and 32 bit ubuntu 11.04 guest os
SivEd
Posts: 5
Joined: 22. Jun 2011, 16:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: XP, 7, Ubuntu 11.04

Re: Easy Cloning

Post by SivEd »

OK different errors, that's good ;)

I can see: -

Code: Select all

Cloning Disk ...
VBoxManage: error: Could not find file for the medium '/media/raid/Ubuntu_template.vdi' (VERR_FILE_NOT_FOUND)
So do you have a /media/raid mountpoint?

The format of the command is: -

Code: Select all

./clonevm.sh <source machine NAME> <dest machine NAME> <source drive PATH> <dest drive PATH>
So I am guessing from your first post and your second post: -

Code: Select all

./clonevm.sh Ubuntu_ ubuntu_43 /home/anupam/VirtualBox\ VMs/Ubuntu_/Ubuntu_.vdi /home/anupam/VirtualBox\ VMs/Ubuntu_43/Ubuntu_43.vdi
Bottom line: Check your machine names and check your drive paths? Remember your case (I am seeing Ubuntu and ubuntu in different places?).

This script is in no way finished and the errors/successes it reports are probably spurious! If it goes through OK (i.e. all the paramterers are right) then you'll get good output, but any errors will throw it off and maybe give you strange messages.

Pete :)
Anupambrt
Posts: 16
Joined: 4. Sep 2011, 09:47
Primary OS: Ubuntu other
VBox Version: OSE other
Guest OSses: Ubuntu 11.04, OpenSuse 11.04

Re: Easy Cloning

Post by Anupambrt »

Hey thanks for replying,
Please tell me what is the significance of

Code: Select all

 /media/raid/Ubuntu_.vdi 
path

cause you can access vdi file from

Code: Select all


'/home/anupam/VirtualBox VMs/Ubuntu32/Ubuntu32.vdi'
path too, right?
I think that might be my problem.... Sorry for troubling so much...
SivEd
Posts: 5
Joined: 22. Jun 2011, 16:24
Primary OS: Ubuntu other
VBox Version: OSE Debian
Guest OSses: XP, 7, Ubuntu 11.04

Re: Easy Cloning

Post by SivEd »

Yes, you are right, the source drive path should be '/home/anupam/VirtualBox VMs/Ubuntu32/Ubuntu32.vdi'

I guess then that your dest path (keeping your conventions) would be: -

Code: Select all

/home/anupam/VirtualBox VMs/<destVMName>/<destVMName>.vdi
Where <destVMName> is what you want to create your VM as...

So to clone Ubuntu32 as Ubuntu32-copy

Code: Select all

./clonevm.sh Ubuntu32 Ubuntu32-copy '/home/anupam/VirtualBox VMs/Ubuntu32/Ubuntu32.vdi' '/home/anupam/VirtualBox VMs/Ubuntu32-copy/Ubuntu32-copy.vdi'
Cheers,
Pete
Anupambrt
Posts: 16
Joined: 4. Sep 2011, 09:47
Primary OS: Ubuntu other
VBox Version: OSE other
Guest OSses: Ubuntu 11.04, OpenSuse 11.04

Re: Easy Cloning

Post by Anupambrt »

Thank you SivEd...
Post Reply