利用VMware Infrastructure SDK编程控制虚拟机集群(2)
接上回,继续整理针对虚拟机的各种操作。5、创建与恢复快照
[*]/// <summary>
[*]/// 创建快照
[*]/// </summary>
[*]public void CreateSnapshot()
[*]{
[*] //虚拟机的资源路径,同前,本例是针对vm100打快照
[*] string path = "DataCenter/vm/vm100";
[*]
[*] //根据虚拟机的资源路径获取资源的引用
[*] ManagedObjectReference vmRef = m_Service.FindByInventoryPath(m_Content.searchIndex, path);
[*] if(vmRef != null)
[*] {
[*] //调用服务实例上的CreateSnapshot_Task来创建虚拟机快照(异步),此处没有等待任务完成
[*] ManagedObjectReference taskRef = m_Service.CreateSnapshot_Task(vmRef, "SnapshotName", "Description", true, true);
[*] }
[*]}
[*]
[*]/// <summary>
[*]/// 恢复当前快照
[*]/// </summary>
[*]public void RevertToCurrentSnapshot()
[*]{
[*] //虚拟机的资源路径,同前,本例是针对vm100恢复当前
[*] string path = "DataCenter/vm/vm100";
[*]
[*] //根据虚拟机的资源路径获取资源的引用
[*] ManagedObjectReference vmRef = m_Service.FindByInventoryPath(m_Content.searchIndex, path);
[*] if(vmRef != null)
[*] {
[*] //调用服务实例上的RevertToCurrentSnapshot_Task来恢复虚拟机当前快照(异步),此处没有等待任务完成
[*] ManagedObjectReference taskRef = m_Service.RevertToCurrentSnapshot_Task(vmRef, null);
[*] }
[*]}
6、在同一台主机上从模板复制虚拟机
先要指出,模板本身也是虚拟机,以VI系统中它的类型就是VirtualMachine,只是有特殊属性标明它是模板,因此不能运行。
注意此方法要求源与目标在同一台主机上,后面会讲到如何在不同主机间复制。
[*]/// <summary>
[*]/// 从模板部署虚拟机
[*]/// </summary>
[*]public void Deploy()
[*]{
[*] //模板路径,与虚拟机相同,此例中模板名叫template100
[*] string templatePath = "DataCenter/vm/template100";
[*] //根据路径获取此模板的引用
[*] ManagedObjectReference templateRef = m_Service.FindByInventoryPath(m_Content.searchIndex, templatePath);
[*] //folder的引用,即逻辑上存储新虚拟机的位置,固定是这样的
[*] ManagedObjectReference folderRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/vm");
[*] //资源池的引用,格式是“中心/host/主机名/Resources”,此例中我们将虚拟机部署到host10这台主机上
[*] ManagedObjectReference poolRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/host/host10/Resources");
[*]
[*] //设置部署位置相关的参数
[*] VirtualMachineRelocateSpec vmRelocSpec = new VirtualMachineRelocateSpec();
[*] vmRelocSpec.transform = VirtualMachineRelocateTransformation.sparse;
[*] vmRelocSpec.pool = poolRef;
[*]
[*] //设定复制相关参数
[*] VirtualMachineCloneSpec vmCloneSpec = new VirtualMachineCloneSpec();
[*] vmCloneSpec.template = false; //新虚拟机不是模板
[*] vmCloneSpec.location = vmRelocSpec;//存放位置
[*] vmCloneSpec.powerOn = true; //部署完成后是否开机
[*]
[*] //调用CloneVM_Task来克隆虚拟机(异步),此处没有等待任务完成
[*] ManagedObjectReference taskRef = m_Service.CloneVM_Task(templateRef, folderRef, vmName, vmCloneSpec);
[*]}
未完待续……
页:
[1]