[vbox-dev] trying to clone a vm
Carlo Bongiovanni
carlo.bongiovanni at gmail.com
Tue Jul 23 07:43:57 PDT 2013
Hi Klaus,
I elaborated your last answer and tried a lot to find what was wrong in my
code. I also discovered that probably I also needed to lock the session
before cloning the machine and I tried doing it.. I finally got this code,
surrounded with try/catch that I avoid to paste here for semplicity:
// find the machine to clone
IMachine m_clone = vbox.findMachine(vmuid_clone);
String m_clone_os = m_clone.getOSTypeId();
// create a new machine with the provided name
IMachine machine = vbox.createMachine(null, vmname, null,
m_clone_os, null);
machine.saveSettings();
vbox.registerMachine(machine);
// lock the src machine
session = mgr.getSessionObject();
m_clone.lockMachine(session, LockType.Write);
IMachine mutable = session.getMachine();
// clone options
CloneMode cm = CloneMode.MachineState;
List<CloneOptions> cloneOptionsList = new
ArrayList<CloneOptions>();
cloneOptionsList.add(CloneOptions.KeepAllMACs);
// clone the machine
IProgress p = machine.cloneTo(mutable, cm, cloneOptionsList);
// get percent of cloning option
log(p.getPercent());
// wait
p.waitForCompletion(-1);
// get percent of cloning option
log(p.getPercent());
// unlock the src machine
session.unlockMachine();
After the execution of this code, the p.getPercent() returns "50", and the
new machine is registered, but no disks are linked to it. Am I missing
something important?
Thanks for any feedback.
Best
Carlo
2013/7/15 Klaus Espenlaub <klaus.espenlaub at oracle.com>
> Hi Carlo,
>
> On 15.07.2013 10:01, Carlo Bongiovanni wrote:
> > Hi All,
> >
> > I am running VirtualBox 4.2.10 on Ubuntu 12.04.
> >
> > I am trying to clone a vm with the Java API. Doing this operation from
> > the console, I would issue the following command:
> > VBoxManage clonevm vmuid_src --name vmname_dest --register
> >
> > With Java I am doing the following:
> >
> > IMachine m_clone = vbox.findMachine(vmuid_src);
> > String m_clone_os = m_clone.getOSTypeId();
> > IMachine machine = vbox.createMachine(null, vmname_dest, null,
> > m_clone_os, null);
> > machine.saveSettings();
> > vbox.registerMachine(machine);
> >
> > CloneMode cm = CloneMode.MachineState;
> > List<CloneOptions> cloneOptionsList = new
> > ArrayList<CloneOptions>();
> > cloneOptionsList.add(CloneOptions.KeepAllMACs);
> >
> > machine.cloneTo(m_clone, cm, cloneOptionsList);
> >
> > After the execution of this code, I found a new vm in the list, but
> > without any storage attached. What am I missing? Can you direct me to
> > the right way to do it? I couldn't understand it from the sdkref.
>
> Your code has to wait until the cloning has completed. The IProgress
> object isn't returned for fun, it serves an important purpose. Without
> it there's no good way to tell when the cloning has finished. It can
> take a very long time, as your code creates a full clone which
> potentially means copying many GBytes of virtual disk contents. Only at
> the end of the VM cloning the settings are all in place...
>
> The best "sample" is always the source of VBoxManage - it's C++, but the
> API works in principle the same in all languages, with minor differences
> in the error handling and thus details of the parameter list.
>
> Klaus
> >
> > Thanks, best
> > Carlo
>
> _______________________________________________
> vbox-dev mailing list
> vbox-dev at virtualbox.org
> https://www.virtualbox.org/mailman/listinfo/vbox-dev
>
--
Carlo Bongiovanni
Tel. +49 173 9714421
callto://pacman81
gtalk: carlo.bongiovanni
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.virtualbox.org/pipermail/vbox-dev/attachments/20130723/f6933324/attachment.html
More information about the vbox-dev
mailing list