利用VMware Infrastructure SDK编程控制虚拟机集群(3)

简介:

接上回,继续整理针对虚拟机的各种操作。


7、跨主机克隆虚拟机

网上资料比较少,当时费了很大劲才成功的,与同一台主机上的虚拟机克隆有区别。

 
  1. /// <summary>   
  2. /// 从模板部署虚拟机  
  3. /// </summary>   
  4. public void Deploy()   
  5. {   
  6.     //模板路径,我们假设template100这个模板在另一台主机上 
  7.     //后续的几步都与同一台主机上克隆没有区别 
  8.     string templatePath = "DataCenter/vm/template100"
  9.     ManagedObjectReference templateRef = m_Service.FindByInventoryPath(m_Content.searchIndex, templatePath); 
  10.     ManagedObjectReference folderRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/vm"); 
  11.     ManagedObjectReference poolRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/host/host10/Resources"); 
  12.  
  13.     //此处开始是关键 
  14.     //获取目标host的引用,路径是“数据中心/host/主机名/资源名”,默认资源名与主机名相同 
  15.     ManagedObjectReference hostRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/host/host10/host10"); 
  16.  
  17.     //还需要获取目标主机存储的引用,用到的RetrievePropertiesForSingleObject方法是自定义的,主要是封装一下服务上的RetrieveProperties方法 
  18.     //获取hostRef上的datastore属性,最终的dataRef就是存储对象的引用 
  19.     ObjectContent[] contents = RetrievePropertiesForSingleObject(hostRef, new string[] { "datastore" }); 
  20.     ManagedObjectReference[] datastores = (ManagedObjectReference[])contents[0].propSet[0].val; 
  21.     ManagedObjectReference dataRef = datastores[0]; 
  22.  
  23.     //指定位置参数 
  24.     VirtualMachineRelocateSpec vmRelocSpec = new VirtualMachineRelocateSpec(); 
  25.     vmRelocSpec.pool = poolRef; 
  26.     vmRelocSpec.datastore = dataRef;   //这个很重要 
  27.     vmRelocSpec.transform = VirtualMachineRelocateTransformation.sparse; 
  28.  
  29.     //指定复制相关参数,与同主机复制相同 
  30.     VirtualMachineCloneSpec vmCloneSpec = new VirtualMachineCloneSpec(); 
  31.     vmCloneSpec.template = true
  32.     vmCloneSpec.location = vmRelocSpec; 
  33.     vmCloneSpec.powerOn = true
  34.  
  35.     //还是调用CloneVM_Task来克隆虚拟机(异步),此处没有等待任务完成 
  36.     ManagedObjectReference taskRef = m_Service.CloneVM_Task(templateRef, folderRef, destName, vmCloneSpec); 
  37. }   
  38.  
  39. /// <summary> 
  40. /// 获取单一对象的属性  
  41. /// </summary> 
  42. /// <param name="obj">对象</param> 
  43. /// <param name="properties">属性集合</param> 
  44. /// <returns>内容集合</returns> 
  45. private ObjectContent[] RetrievePropertiesForSingleObject(ManagedObjectReference obj, string[] properties) 
  46.     PropertySpec[] pSpec = new PropertySpec[] { new PropertySpec() }; 
  47.     pSpec[0].type = obj.type; 
  48.     pSpec[0].all = false
  49.     pSpec[0].pathSet = properties; 
  50.  
  51.     ObjectSpec[] obSpec = new ObjectSpec[] { new ObjectSpec() }; 
  52.     obSpec[0].obj = obj; 
  53.     obSpec[0].skip = true
  54.  
  55.     PropertyFilterSpec spec = new PropertyFilterSpec(); 
  56.     spec.propSet = pSpec; 
  57.     spec.objectSet = obSpec; 
  58.  
  59.     ObjectContent[] contents = m_Service.RetrieveProperties(m_Collector, new PropertyFilterSpec[] { spec }); 
  60.  
  61.     return contents; 


未完待续……




     本文转自 BoyTNT 51CTO博客,原文链接:http://blog.51cto.com/boytnt/1031743,如需转载请自行联系原作者


相关文章
|
1月前
|
Linux 虚拟化 iOS开发
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
337 0
VMware Remote Console 13.0.1 for macOS, Linux, Windows - vSphere 虚拟机控制台的桌面客户端
|
1月前
|
Linux 虚拟化 iOS开发
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
375 0
VMware Fusion 25H2 OEM BIOS 2.7 - 在 macOS 中运行 Windows 虚拟机的最佳方式
|
1月前
|
存储 Linux 文件存储
VMware vSphere Replication 9.0.4 - 虚拟机复制和数据保护
VMware vSphere Replication 9.0.4 - 虚拟机复制和数据保护
140 2
|
2月前
|
监控 Linux 网络安全
VMware vCenter Converter 9.0 - P2V 虚拟机转换工具
VMware vCenter Converter 9.0 - P2V 虚拟机转换工具
671 0
VMware vCenter Converter 9.0 - P2V 虚拟机转换工具
|
2月前
|
Oracle 关系型数据库 虚拟化
在VMware的Win10虚拟机中安装使用ENSP
本文介绍了在Windows 10虚拟机上安装ENSP及相关软件的全过程,包括VirtualBox、WinPcap、Wireshark、VLC和ENSP的安装步骤,并提供图文演示,帮助用户顺利完成配置与测试。
838 134
|
5月前
|
虚拟化 数据中心
VMware vSphere Replication 9.0.3 - 虚拟机复制和数据保护
VMware vSphere Replication 9.0.3 - 虚拟机复制和数据保护
243 0
|
3月前
|
Linux 虚拟化 iOS开发
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
385 3
Windows Server 2025 OVF (2025 年 8 月更新) - VMware 虚拟机模板
|
4月前
|
编解码 网络协议 网络安全
OpenWrt x86 虚拟机VMware ESXi安装
OpenWrt x86 虚拟机VMware ESXi安装
|
3月前
|
Linux 虚拟化 iOS开发
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
300 0
Debian 13 x86_64 OVF (sysin) - VMware 虚拟机模板
|
5月前
|
Linux 虚拟化 iOS开发
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板
123 9
Windows Server 2016 OVF (2025 年 6 月更新) - VMware 虚拟机模板