Can't clone VM using cloneTo - Java SDK

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
aandis
Posts: 5
Joined: 3. Feb 2015, 19:23

Can't clone VM using cloneTo - Java SDK

Post by aandis »

VirtualBox version - 4.3.10_Ubuntu r93012
VirtualBox API Version - 4.3.10
Connection Type - WebServer
Language - Java
OS - Ubuntu 14.04

The IMachine::cloneTo function does not appear to be working. Here is the code I have written,

Code: Select all

    
     static IMachine copyVM(VirtualBoxManager mgr)
     {
    	IVirtualBox vbox = mgr.getVBox();
        IMachine oldMachine = vbox.findMachine("21e48f31-248b-4f34-b20b-783a0ae27849"); //this is the vm to clone  

    	IMachine new_machine = vbox.createMachine(null, "vmName", null, oldMachine.getOSTypeId(), null);  //the new vm to clone to
    	new_machine.saveSettings();
    	vbox.registerMachine(new_machine);

        List<CloneOptions> options = new ArrayList<CloneOptions>(); //clone options
        options.add(CloneOptions.KeepDiskNames); //just keep the disk name
        
        IProgress clone_progress = oldMachine.cloneTo(newMachine, CloneMode.MachineState, options); //start the clone process
        progressBar(clone_progress); //this function keeps track of the clone percent

        return newMachine;
    }

Code: Select all

    static boolean progressBar(IProgress p)
    {
        while (!p.getCompleted())
        {
        	System.out.println(p.getPercent() + " . ");
        }
        return true;
    }

This function executes successfully, and the clone percent reaches 100 from 0. I can even see the .vdi file for the new machine being created in my directory while the clone process is going on. But as soon as the clone process finishes, the .vdi gets deleted. Nothing gets copied from the old machine to the new machine. All settings are deleted.

Looks like a bug to me, But I might be doing something wrong maybe?

Any suggestions?
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Can't clone VM using cloneTo - Java SDK

Post by noteirak »

Before attempting anything new, and if possible on your side, please update to the latest VirtualBox version (4.3.20 at this time) and use the official version and not the Ubuntu package.
After you've done so, please check the error message in the progress object - you would get an actual text message of what went wrong. It simply looks like something fails midway and the copy is rolled back.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
aandis
Posts: 5
Joined: 3. Feb 2015, 19:23

Re: Can't clone VM using cloneTo - Java SDK

Post by aandis »

noteirak wrote:Before attempting anything new, and if possible on your side, please update to the latest VirtualBox version (4.3.20 at this time) and use the official version and not the Ubuntu package.
After you've done so, please check the error message in the progress object - you would get an actual text message of what went wrong. It simply looks like something fails midway and the copy is rolled back.
Thanks for the response @noteirak. I'll try that and get back to you shortly.
aandis
Posts: 5
Joined: 3. Feb 2015, 19:23

Re: Can't clone VM using cloneTo - Java SDK

Post by aandis »

OK I checked the error reported with my code, and I got
The machine is not mutable (state is PoweredOff)
So I changed the code to get a mutable copy of the new machine. Here's the new code

Code: Select all

    
   static IMachine copyVM(VirtualBoxManager mgr)
     {
        IVirtualBox vbox = mgr.getVBox();
        IMachine oldMachine = vbox.findMachine("21e48f31-248b-4f34-b20b-783a0ae27849"); //this is the vm to clone  

        IMachine new_machine = vbox.createMachine(null, "vmName", null, oldMachine.getOSTypeId(), null);  //the new vm to clone to
        new_machine.saveSettings();
        vbox.registerMachine(new_machine);

        ISession session = mgr.getSessionObject();
        new_machine.lockMachine(session, LockType.Write);
        IMachine mutable = session.getMachine();

        List<CloneOptions> options = new ArrayList<CloneOptions>(); //clone options
        options.add(CloneOptions.KeepDiskNames); //just keep the disk name
        
        IProgress clone_progress = oldMachine.cloneTo(mutable, CloneMode.MachineState, options); //start the clone process
        progressBar(clone_progress); //this function keeps track of the clone percent

        mutable.saveSettings();
        session.unlockMachine();

        return newMachine;
    }

Code: Select all

    static boolean progressBar(VirtualBoxManager mgr, IProgress p, long waitMillis)
    {
        long end = System.currentTimeMillis() + waitMillis;
        while (!p.getCompleted())
        {
        	System.out.println(p.getPercent() + " . ");
        }
        if(p.getCompleted()){
        	System.out.println(p.getResultCode());
        	IVirtualBoxErrorInfo info = p.getErrorInfo();
        	if(info!=null)
        		System.out.println(info.getText());
        }
        return true;
    }

But it didn't work. I don't see a clone of the old machine. While the process is going on, I can see the vdi being created like before, but as soon as it finishes, everything gets undone. No error gets reported.
Last edited by aandis on 4. Feb 2015, 13:08, edited 2 times in total.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Can't clone VM using cloneTo - Java SDK

Post by noteirak »

I'll try to make it work on my side and get back to you tomorrow.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
aandis
Posts: 5
Joined: 3. Feb 2015, 19:23

Re: Can't clone VM using cloneTo - Java SDK

Post by aandis »

Never mind. It worked.

I was creating a new machine, saving settings, registering with vbox and then starting the clone process by obtaining a mutable copy of the new machine. Instead when I create machine, clone to it and then save and register with vbox it works.

Don't know how it makes a difference, but anyways thanks for the help!
Post Reply