我正在使用SDK版本“VirtualBoxSDK-5.1.22-115126”(vboxjws.jar)中的VirtualBox Java API。
我想获取属于我正在使用的 IMachine 对象(代表一个虚拟机的对象)的所有快照的列表。
IMachine 的方法findSnapshot(String nameOrId)返回给定名称或UUID的快照。但我想要一张机器所有快照列表......
命令行界面 vboxmanage 能够使用以下命令返回所有快照的列表:
vboxmanage snapshot <uuid|vmname> list
(来源:https://www.virtualbox.org/manual/ch08.html#idm4900)
API中是否缺少这种方法,或者让Oracle的开发人员忘记实现它? (证明他们也只是人类;))
答案 0 :(得分:1)
快照是具有根快照的树结构,所有其他快照都来自该快照。你可以说它是设计API调用不存在,但你可以直接通过树来实现它。
此示例将执行此操作:
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();
}
}
}
我假设您知道如何配置WS登录&amp;密码变量或禁用WebService进程的身份验证。
IMachine::findSnapshot()的文档解释了null可用于获取根快照,您可以从中快速处理子快照:
使用给定的UUID返回此计算机的快照。空 参数可用于获取对此进行的第一个快照 机。从。开始遍历整个快照树 root,检查根快照的ISnapshot :: children属性和 递绝那些孩子。