0
votes

I'm using the Java API for VirtualBox from the SDK version "VirtualBoxSDK-5.1.22-115126" (vboxjws.jar).

I want to get a List of all snapshots belonging to the IMachine object (object representing one virtual machine) that I'm working with.

IMachine has the method findSnapshot(String nameOrId) which returns a snapshot for the given name or UUID. But I want a list of ALL snapshots the machine has...

The command line interface vboxmanage is able to return a list of all snapshots with the command:

vboxmanage snapshot <uuid|vmname> list

(source: https://www.virtualbox.org/manual/ch08.html#idm4900)

So is this method missing in the API by design or have the developers from Oracle just forgotten to implement it? (would proof that they are just human beings too ;))

1

1 Answers

1
votes

Snapshots is a tree structure with a root snapshot, from which all other snapshots originate. You could say it is by design the API call does not exists, but you can implement it yourself directly by going through the tree.

This sample will do just that:

import org.virtualbox_5_1.IMachine;
import org.virtualbox_5_1.ISnapshot;
import org.virtualbox_5_1.IVirtualBox;
import org.virtualbox_5_1.VirtualBoxManager;

public class SnapshotList {

    private static void printChilds(ISnapshot snapshot) {
        System.out.println("\"" + snapshot.getName() + "\" {" + snapshot.getId() + "}");
        for (ISnapshot snapChild : snapshot.getChildren()) {
            printChilds(snapChild);
        }
    }

    public static void main(String[] args) {
        /*
         * WebServices info
         */
        String wsHost = "http://localhost:18083";
        String wsUser = "user";
        String wsPass = "password";

        if (args.length < 1 || args[0] == null || args[0].length() < 1) {
            System.err.println("Specify the VM name/UUID as first parameter");
            System.exit(1);
        }

        String vmName = args[0];

        VirtualBoxManager vboxManager = VirtualBoxManager.createInstance(null);
        vboxManager.connect(wsHost, wsUser, wsPass);

        try {
            IVirtualBox vbox = vboxManager.getVBox();
            IMachine vm = vbox.findMachine(vmName);
            if (vm.getSnapshotCount() < 1) {
                System.out.println("The machine + " + vmName + " has no snapshot");
                System.exit(0);
            }

            // The magic is here: null will give you the root snapshot
            printChilds(vm.findSnapshot(null));
        } finally {
            vboxManager.disconnect();
            vboxManager.cleanup();
        }
    }

}

I assume you know how to configure the WS login & password variables or disable authentication on the WebService process.

The doc of IMachine::findSnapshot() explains that null can be used to fetch the root snapshot, from which you can just process the childs:

Returns a snapshot of this machine with the given UUID. A null argument can be used to obtain the first snapshot taken on this machine. To traverse the whole tree of snapshots starting from the root, inspect the root snapshot's ISnapshot::children attribute and recurse over those children.