Vijava 学习笔记之(VirtualMachine 移除指定虚拟磁盘)解析

阿里 阅读:214 2021-03-31 22:31:10 评论:0

源代码:

package com.vmware.client; 
 
 
import com.vmware.util.Session; 
import com.vmware.vim25.*; 
import com.vmware.vim25.mo.*; 
 
/** 
 * Created by vixuan-008 on 2015/5/27. 
 */ 
public class RemoveVmDisk { 
    public static void main(String[] args){ 
        try{ 
            String vmname="vm7";  //指定虚拟机名称 
            String diskname="硬盘 1";  //指定虚拟磁盘 
            //获取 HostSystem 
            ServiceInstance serviceInstance= Session.getInstance("*****", "[email protected]", "Vixuan12#");  //获取连接 
 
            Folder rootFolder = serviceInstance.getRootFolder(); 
            VirtualMachine vm=(VirtualMachine)new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname);  //获取指定虚拟机 
 
 
                if(vm==null) 
                { 
                    System.out.println("No VM " + vmname + " found"); 
                    serviceInstance.getServerConnection().logout(); 
                    return; 
                } 
                VirtualMachineConfigSpec vmConfigSpec = 
                        new VirtualMachineConfigSpec();    //虚拟机配置文件 
                VirtualDeviceConfigSpec vdiskSpec = 
                        createRemoveDiskConfigSpec(vm.getConfig(), diskname);  //虚拟机设备配置文件 
                vmConfigSpec.setDeviceChange( 
                        new VirtualDeviceConfigSpec[]{vdiskSpec} ); 
                Task task = vm.reconfigVM_Task(vmConfigSpec);    //指定虚拟机配置信息更改 
                if(task.waitForMe()==Task.SUCCESS) 
                { 
                    System.out.println("Disk removed."); 
                } 
                else 
                { 
                    System.out.println("Error while removing disk"); 
                } 
                serviceInstance.getServerConnection().logout(); 
 
 
        }catch (Exception e){ 
 
        } 
    } 
    private static VirtualDeviceConfigSpec createRemoveDiskConfigSpec( VirtualMachineConfigInfo vmConfig, String diskName)    //查询指定虚拟磁盘的设备配置信息 
            throws Exception{ 
        VirtualDeviceConfigSpec diskSpec =new VirtualDeviceConfigSpec(); 
        VirtualDisk disk = (VirtualDisk) findVirtualDevice(vmConfig, diskName); 
 
        if(disk != null) 
        { 
            diskSpec.setOperation(VirtualDeviceConfigSpecOperation.remove); 
            // removing the following line can keep the disk file 
            diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.destroy); 
            diskSpec.setDevice(disk); 
            return diskSpec; 
        } 
        else 
        { 
            throw new Exception("No device found:"  + diskName); 
        } 
    } 
 
    private static VirtualDevice findVirtualDevice(    //寻找指定名称的虚拟设备 
            VirtualMachineConfigInfo cfg, String name) 
    { 
        VirtualDevice [] devices = cfg.getHardware().getDevice(); 
        for(int i=0;devices!=null && i<devices.length; i++) 
        { 
             return devices[12]; 
//            if(devices[i].getDeviceInfo().getLabel().equals(name)) 
//            { 
//                return devices[i]; 
//            } 
        } 
        return null; 
    } 
} 


声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们