| 1 | import org.virtualbox_4_3.*;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.ArrayList;
|
|---|
| 4 | import java.util.List;
|
|---|
| 5 | import java.util.Arrays;
|
|---|
| 6 |
|
|---|
| 7 | public class Main {
|
|---|
| 8 | final static String source = "Ubuntu 1";
|
|---|
| 9 | final static String cloneName = "Ubuntu 1 Clone 1";
|
|---|
| 10 |
|
|---|
| 11 | static void clone(IVirtualBox vbox) {
|
|---|
| 12 | IMachine targetMachine = vbox.createMachine("", cloneName, new ArrayList<String>(), "Ubuntu_64", "");
|
|---|
| 13 | IMachine sourceMachine = vbox.findMachine(source);
|
|---|
| 14 |
|
|---|
| 15 | IProgress progress = sourceMachine.getCurrentSnapshot().getMachine().cloneTo(targetMachine, CloneMode.MachineState, Arrays.asList(CloneOptions.Link));
|
|---|
| 16 |
|
|---|
| 17 | while (!progress.getCompleted() && !progress.getCanceled()) {
|
|---|
| 18 | progress.waitForCompletion(500);
|
|---|
| 19 | System.out.println("Cloning: Remaining time is: " + progress.getTimeRemaining());
|
|---|
| 20 | }
|
|---|
| 21 | System.out.println("Cloning: Completed");
|
|---|
| 22 |
|
|---|
| 23 | targetMachine.setGroups(Arrays.asList("/Clones")); // THIS CAUSES THE PROBLEM in the end
|
|---|
| 24 |
|
|---|
| 25 | targetMachine.saveSettings();
|
|---|
| 26 |
|
|---|
| 27 | vbox.registerMachine(targetMachine);
|
|---|
| 28 |
|
|---|
| 29 | // VERIFY: Files created in 'PATH_TO_VM_FILES/Clones/Ubuntu 1 Clone 1'
|
|---|
| 30 | // instead of in 'PATH_TO_VM_FILES/Ubuntu 1 Clone 1' when not using a group
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | static void delete(IVirtualBox vbox) {
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | IMachine machine = vbox.findMachine(cloneName);
|
|---|
| 37 | List<IMedium> liMedium = machine.unregister(CleanupMode.DetachAllReturnHardDisksOnly);
|
|---|
| 38 |
|
|---|
| 39 | // WORKAROUND: A possible workaround (make sure you start with a proper configuration of VB)
|
|---|
| 40 | // machine.setGroups(Arrays.asList("/"));
|
|---|
| 41 | // machine.saveSettings();
|
|---|
| 42 |
|
|---|
| 43 | IProgress progress = machine.deleteConfig(liMedium);
|
|---|
| 44 | while (!progress.getCompleted() && !progress.getCanceled()) {
|
|---|
| 45 | progress.waitForCompletion(500);
|
|---|
| 46 | System.out.println("Deleting: Remaining time is: " + progress.getTimeRemaining());
|
|---|
| 47 | }
|
|---|
| 48 | System.out.println("Deleting: Completed");
|
|---|
| 49 |
|
|---|
| 50 | // VERIFY: Files and folder are not deleted. 'PATH_TO_VM_FILES/Clones/Ubuntu 1 Clone 1' still exists
|
|---|
| 51 | // BUG! On second execution of this program, execution will fail due to that
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 | public static void main(String[] args) {
|
|---|
| 56 | VirtualBoxManager mgr = VirtualBoxManager.createInstance(null);
|
|---|
| 57 |
|
|---|
| 58 | // WARNING> We assume here that the authentication was manually disabled in VirtualBox using
|
|---|
| 59 | // > VBoxManager.exe VBoxManage setproperty websrvauthlibrary null
|
|---|
| 60 | String url = "http://localhost:18083/";
|
|---|
| 61 | String user = "test";
|
|---|
| 62 | String passwd = "test";
|
|---|
| 63 | try {
|
|---|
| 64 | System.out.println("Connecting to " + url + " as " + user + " with " + passwd);
|
|---|
| 65 | mgr.connect(url, user, passwd);
|
|---|
| 66 | System.out.println("Connected");
|
|---|
| 67 | } catch (VBoxException e) {
|
|---|
| 68 | e.printStackTrace();
|
|---|
| 69 | System.out.println("Cannot connect, start webserver first!");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | try {
|
|---|
| 73 | IVirtualBox vbox = mgr.getVBox();
|
|---|
| 74 | if (vbox != null) {
|
|---|
| 75 | System.out.println("VirtualBox version: " + vbox.getVersion() + "\n");
|
|---|
| 76 |
|
|---|
| 77 | clone(vbox);
|
|---|
| 78 | delete(vbox);
|
|---|
| 79 |
|
|---|
| 80 | System.out.println("done, press Enter...");
|
|---|
| 81 | int ch = System.in.read();
|
|---|
| 82 | }
|
|---|
| 83 | } catch (VBoxException e) {
|
|---|
| 84 | System.out.println("Java stack trace:");
|
|---|
| 85 | e.printStackTrace();
|
|---|
| 86 | } catch (RuntimeException e) {
|
|---|
| 87 | System.out.println("Runtime error: " + e.getMessage());
|
|---|
| 88 | e.printStackTrace();
|
|---|
| 89 | } catch (java.io.IOException e) {
|
|---|
| 90 | e.printStackTrace();
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | try {
|
|---|
| 94 | mgr.disconnect();
|
|---|
| 95 | } catch (VBoxException e) {
|
|---|
| 96 | e.printStackTrace();
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | mgr.cleanup();
|
|---|
| 100 |
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | }
|
|---|