你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2012-12-05] 来源:Linux社区  作者:boytnt [字体:大 中 小]

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

7、跨主机克隆虚拟机

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

  1. /// <summary>
  2. /// 从模板部署虚拟机
  3. /// </summary>
  4. publicvoid 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. //获取目标host的引用,路径是“数据中心/host/主机名/资源名”,默认资源名与主机名相同
  14. ManagedObjectReference hostRef = m_Service.FindByInventoryPath(m_Content.searchIndex, "DataCenter/host/host10/host10");
  15. //还需要获取目标主机存储的引用,用到的RetrievePropertiesForSingleObject方法是自定义的,主要是封装一下服务上的RetrieveProperties方法
  16. //获取hostRef上的datastore属性,最终的dataRef就是存储对象的引用
  17. ObjectContent[] contents = RetrievePropertiesForSingleObject(hostRef, newstring[] { "datastore" });
  18. ManagedObjectReference[] datastores = (ManagedObjectReference[])contents[0].propSet[0].val;
  19. ManagedObjectReference dataRef = datastores[0];
  20. //指定位置参数
  21. VirtualMachineRelocateSpec vmRelocSpec = new VirtualMachineRelocateSpec();
  22. vmRelocSpec.pool = poolRef;
  23. vmRelocSpec.datastore = dataRef; //这个很重要
  24. vmRelocSpec.transform = VirtualMachineRelocateTransformation.sparse;
  25. //指定复制相关参数,与同主机复制相同
  26. VirtualMachineCloneSpec vmCloneSpec = new VirtualMachineCloneSpec();
  27. vmCloneSpec.template = true;
  28. vmCloneSpec.location = vmRelocSpec;
  29. vmCloneSpec.powerOn = true;
  30. //还是调用CloneVM_Task来克隆虚拟机(异步),此处没有等待任务完成
  31. ManagedObjectReference taskRef = m_Service.CloneVM_Task(templateRef, folderRef, destName, vmCloneSpec);
  32. }
  33. /// <summary>
  34. /// 获取单一对象的属性
  35. /// </summary>
  36. /// <param name="obj">对象</param>
  37. /// <param name="properties">属性集合</param>
  38. /// <returns>内容集合</returns>
  39. private ObjectContent[] RetrievePropertiesForSingleObject(ManagedObjectReference obj, string[] properties)
  40. {
  41. PropertySpec[] pSpec = new PropertySpec[] { new PropertySpec() };
  42. pSpec[0].type = obj.type;
  43. pSpec[0].all = false;
  44. pSpec[0].pathSet = properties;
  45. ObjectSpec[] obSpec = new ObjectSpec[] { new ObjectSpec() };
  46. obSpec[0].obj = obj;
  47. obSpec[0].skip = true;
  48. PropertyFilterSpec spec = new PropertyFilterSpec();
  49. spec.propSet = pSpec;
  50. spec.objectSet = obSpec;
  51. ObjectContent[] contents = m_Service.RetrieveProperties(m_Collector, new PropertyFilterSpec[] { spec });
  52. return contents;
  53. }
linux
相关资讯       VMware Infrastructure 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款