获取机器的所有快照 (VirtualBox Java API)
Get all Snapshots for a Machine (VirtualBox Java API)
我正在使用来自 SDK 版本 "VirtualBoxSDK-5.1.22-115126" (vboxjws.jar) 的 Java API for VirtualBox。
我想获取属于我正在使用的 IMachine 对象(代表一个虚拟机的对象)的所有快照的列表。
IMachine 具有方法 findSnapshot(String nameOrId),其中 return 是给定名称或 UUID 的快照。但我想要一份机器拥有的所有快照的列表...
命令行界面 vboxmanage 能够 return 使用以下命令列出所有快照:
vboxmanage snapshot <uuid|vmname> list
(来源:https://www.virtualbox.org/manual/ch08.html#idm4900)
这个方法是 API 设计遗漏的,还是 Oracle 的开发人员忘记实施它? (将证明他们也只是人类;))
快照是具有根快照的树结构,所有其他快照都源自该根快照。你可以说这是设计使然 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 登录名和密码变量或在 WebService 进程上禁用身份验证。
IMachine::findSnapshot() 的文档解释说 null 可用于获取根快照,您可以从中处理子快照:
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.
我正在使用来自 SDK 版本 "VirtualBoxSDK-5.1.22-115126" (vboxjws.jar) 的 Java API for VirtualBox。
我想获取属于我正在使用的 IMachine 对象(代表一个虚拟机的对象)的所有快照的列表。
IMachine 具有方法 findSnapshot(String nameOrId),其中 return 是给定名称或 UUID 的快照。但我想要一份机器拥有的所有快照的列表...
命令行界面 vboxmanage 能够 return 使用以下命令列出所有快照:
vboxmanage snapshot <uuid|vmname> list
(来源:https://www.virtualbox.org/manual/ch08.html#idm4900)
这个方法是 API 设计遗漏的,还是 Oracle 的开发人员忘记实施它? (将证明他们也只是人类;))
快照是具有根快照的树结构,所有其他快照都源自该根快照。你可以说这是设计使然 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 登录名和密码变量或在 WebService 进程上禁用身份验证。
IMachine::findSnapshot() 的文档解释说 null 可用于获取根快照,您可以从中处理子快照:
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.