[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vbox-dev
Subject:    Re: [vbox-dev] trying to clone a vm
From:       Carlo Bongiovanni <carlo.bongiovanni () gmail ! com>
Date:       2013-07-23 14:43:57
Message-ID: CABccLP=aavGNTwS1=3TRzSabUYk0iLeyTsiF9N6Bf=1e+vpeOQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


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@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@virtualbox.org
> https://www.virtualbox.org/mailman/listinfo/vbox-dev
>



-- 
Carlo Bongiovanni

Tel. +49 173 9714421
callto://pacman81
gtalk: carlo.bongiovanni

[Attachment #5 (text/html)]

<div dir="ltr"><div><div>Hi Klaus, <br><br></div>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:<br>

<br>            // find the machine to clone<br>            IMachine m_clone = \
vbox.findMachine(vmuid_clone);<br>            String m_clone_os = \
m_clone.getOSTypeId();<br>            // create a new machine with the provided \
name<br>

            IMachine machine = vbox.createMachine(null, vmname, null, m_clone_os, \
null);<br><br>            machine.saveSettings();<br>            \
vbox.registerMachine(machine);<br><br>            // lock the src machine<br>

            session = mgr.getSessionObject();<br>            \
m_clone.lockMachine(session, LockType.Write);<br>            IMachine mutable = \
session.getMachine();<br><br>            // clone options<br>            CloneMode cm \
= CloneMode.MachineState;<br>

            List&lt;CloneOptions&gt; cloneOptionsList = new \
ArrayList&lt;CloneOptions&gt;();<br>            \
cloneOptionsList.add(CloneOptions.KeepAllMACs);<br><br>            // clone the \
machine<br>            IProgress p = machine.cloneTo(mutable, cm, \
cloneOptionsList);<br>

<br>            // get percent of cloning option<br>            \
log(p.getPercent());<br><br>            // wait<br>            \
p.waitForCompletion(-1);<br><br>            // get percent of cloning option<br>      \
log(p.getPercent());<br>

<br>            // unlock the src machine<br>            \
session.unlockMachine();<br><br></div><div>After the execution of this code, the \
p.getPercent() returns &quot;50&quot;, and the new machine is registered, but no \
disks are linked to it. Am I missing something important?<br>

<br></div><div>Thanks for any feedback. \
<br></div><div>Best<br>Carlo<br></div><div><br><br></div></div><div \
class="gmail_extra"><br><br><div class="gmail_quote">2013/7/15 Klaus Espenlaub <span \
dir="ltr">&lt;<a href="mailto:klaus.espenlaub@oracle.com" \
target="_blank">klaus.espenlaub@oracle.com</a>&gt;</span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Hi Carlo,<br> <div><div class="h5"><br>
On 15.07.2013 10:01, Carlo Bongiovanni wrote:<br>
&gt; Hi All,<br>
&gt;<br>
&gt; I am running VirtualBox 4.2.10 on Ubuntu 12.04.<br>
&gt;<br>
&gt; I am trying to clone a vm with the Java API. Doing this operation from<br>
&gt; the console, I would issue the following command:<br>
&gt; VBoxManage clonevm vmuid_src --name vmname_dest --register<br>
&gt;<br>
&gt; With Java I am doing the following:<br>
&gt;<br>
&gt;          IMachine m_clone = vbox.findMachine(vmuid_src);<br>
&gt;          String m_clone_os = m_clone.getOSTypeId();<br>
&gt;          IMachine machine = vbox.createMachine(null, vmname_dest, null,<br>
&gt; m_clone_os, null);<br>
&gt;          machine.saveSettings();<br>
&gt;          vbox.registerMachine(machine);<br>
&gt;<br>
&gt;          CloneMode cm = CloneMode.MachineState;<br>
&gt;          List&lt;CloneOptions&gt; cloneOptionsList = new<br>
&gt; ArrayList&lt;CloneOptions&gt;();<br>
&gt;          cloneOptionsList.add(CloneOptions.KeepAllMACs);<br>
&gt;<br>
&gt;          machine.cloneTo(m_clone, cm, cloneOptionsList);<br>
&gt;<br>
&gt; After the execution of this code, I found a new vm in the list, but<br>
&gt; without any storage attached. What am I missing? Can you direct me to<br>
&gt; the right way to do it? I couldn&#39;t understand it from the sdkref.<br>
<br>
</div></div>Your code has to wait until the cloning has completed. The IProgress<br>
object isn&#39;t returned for fun, it serves an important purpose. Without<br>
it there&#39;s no good way to tell when the cloning has finished. It can<br>
take a very long time, as your code creates a full clone which<br>
potentially means copying many GBytes of virtual disk contents. Only at<br>
the end of the VM cloning the settings are all in place...<br>
<br>
The best &quot;sample&quot; is always the source of VBoxManage - it&#39;s C++, but \
the<br> API works in principle the same in all languages, with minor differences<br>
in the error handling and thus details of the parameter list.<br>
<br>
Klaus<br>
&gt;<br>
&gt; Thanks, best<br>
&gt; Carlo<br>
<br>
_______________________________________________<br>
vbox-dev mailing list<br>
<a href="mailto:vbox-dev@virtualbox.org">vbox-dev@virtualbox.org</a><br>
<a href="https://www.virtualbox.org/mailman/listinfo/vbox-dev" \
target="_blank">https://www.virtualbox.org/mailman/listinfo/vbox-dev</a><br> \
</blockquote></div><br><br clear="all"><br>-- <br><div dir="ltr"><span \
style="font-family:arial;font-size:small">Carlo Bongiovanni</span><br \
style="font-family:arial;font-size:small"><br \
style="font-family:arial;font-size:small">

<span style="font-family:arial;font-size:small">Tel. +49</span><span \
style="font-family:arial;font-size:small"> </span><span style="font-size:small">173 \
9714421</span><br style="font-family:arial;font-size:small"><span \
style="font-family:arial;font-size:small">callto://pacman81</span><br \
style="font-family:arial;font-size:small">

<span style="font-family:arial;font-size:small">gtalk: \
carlo.bongiovanni</span><br></div> </div>



_______________________________________________
vbox-dev mailing list
vbox-dev@virtualbox.org
https://www.virtualbox.org/mailman/listinfo/vbox-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic