Vijava 学习笔记之(VirtualMachine 更改虚拟机系统磁盘大小)解析

访客 阅读:241 2021-03-31 22:27:56 评论: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/6/27. 
 */ 
public class UpdateVMDisk { 
    public static void main(String[] args)throws  Exception{ 
        ServiceInstance serviceInstance = null; 
        String vmname = "6699VV"; 
        //ServiceInstance----服务实例 
        serviceInstance = Session.getInstance("192.168.0.22","administrator@vsphere.local","Vixuan12#"); 
        //rootFolder-------根文件夹 
        Folder rootFolder = serviceInstance.getRootFolder(); 
        //vm---------------指定虚拟机 
        VirtualMachine vm = (VirtualMachine) new  InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", vmname); 
        //vmci--------------虚拟机配置信息 
        VirtualMachineConfigInfo vmci = vm.getConfig(); 
        //devices-----------虚拟机关联设备 
        VirtualDevice [] devices = vmci.getHardware().getDevice(); 
        //theDisk-----------虚拟机系统磁盘 
        VirtualDisk theDisk = null; 
        for ( int i=0; devices!=null && i<devices.length; i++) { 
            if ( devices[i] instanceof  VirtualDisk) { 
                theDisk = (VirtualDisk)devices[i]; 
                break; 
            } 
        } 
        //更改系统磁盘大小 
        theDisk.setCapacityInKB(Long.parseLong("60")*1024*1024); 
        VirtualDeviceConfigSpec vdcs = new VirtualDeviceConfigSpec(); 
        vdcs.setDevice(theDisk); 
        vdcs.setOperation(VirtualDeviceConfigSpecOperation.edit); 
        VirtualMachineConfigSpec vmcs = new VirtualMachineConfigSpec(); 
        vmcs.setDeviceChange(new VirtualDeviceConfigSpec[]{vdcs}); 
        //执行虚拟机系统磁盘更改任务 
        Task task = vm.reconfigVM_Task(vmcs); 
        String result = task.waitForMe(); 
        if (result.equals(Task.SUCCESS)) { 
            System.out.println("success"); 
        } else { 
            System.out.println("failed"); 
        } 
    } 
} 


声明

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

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

KIKK导航

关注我们