예제 #1
0
  /**
   * Creates the virtual disk.
   *
   * @param volName the vol name
   * @param diskCtlrKey the disk ctlr key
   * @param datastoreRef the datastore ref
   * @param diskSizeMB the disk size in mb
   * @return the virtual device config spec object
   */
  VirtualDeviceConfigSpec createVirtualDisk(
      String volName, int diskCtlrKey, ManagedObjectReference datastoreRef, int diskSizeMB) {
    String volumeName = getVolumeName(volName);
    VirtualDeviceConfigSpec diskSpec = new VirtualDeviceConfigSpec();

    diskSpec.setFileOperation(VirtualDeviceConfigSpecFileOperation.CREATE);
    diskSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);

    VirtualDisk disk = new VirtualDisk();
    VirtualDiskFlatVer2BackingInfo diskfileBacking = new VirtualDiskFlatVer2BackingInfo();

    diskfileBacking.setFileName(volumeName);
    diskfileBacking.setDiskMode("persistent");

    disk.setKey(new Integer(0));
    disk.setControllerKey(new Integer(diskCtlrKey));
    disk.setUnitNumber(new Integer(0));
    disk.setBacking(diskfileBacking);
    disk.setCapacityInKB(1024);

    diskSpec.setDevice(disk);

    return diskSpec;
  }
예제 #2
0
  /**
   * Creates the vm config spec object.
   *
   * @param vmName the vm name
   * @param datastoreName the datastore name
   * @param diskSizeMB the disk size in mb
   * @param computeResMor the compute res moref
   * @param hostMor the host mor
   * @return the virtual machine config spec object
   * @throws Exception the exception
   */
  VirtualMachineConfigSpec createVmConfigSpec(
      String vmName,
      String datastoreName,
      int diskSizeMB,
      ManagedObjectReference computeResMor,
      ManagedObjectReference hostMor)
      throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {

    ConfigTarget configTarget = getConfigTargetForHost(computeResMor, hostMor);
    List<VirtualDevice> defaultDevices = getDefaultDevices(computeResMor, hostMor);
    VirtualMachineConfigSpec configSpec = new VirtualMachineConfigSpec();
    String networkName = null;
    if (configTarget.getNetwork() != null) {
      for (int i = 0; i < configTarget.getNetwork().size(); i++) {
        VirtualMachineNetworkInfo netInfo = configTarget.getNetwork().get(i);
        NetworkSummary netSummary = netInfo.getNetwork();
        if (netSummary.isAccessible()) {
          networkName = netSummary.getName();
          break;
        }
      }
    }
    ManagedObjectReference datastoreRef = null;
    if (datastoreName != null) {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.getName().equals(datastoreName)) {
          flag = true;
          if (dsSummary.isAccessible()) {
            datastoreRef = dsSummary.getDatastore();
          } else {
            throw new RuntimeException("Specified Datastore is not accessible");
          }
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("Specified Datastore is not Found");
      }
    } else {
      boolean flag = false;
      for (int i = 0; i < configTarget.getDatastore().size(); i++) {
        VirtualMachineDatastoreInfo vdsInfo = configTarget.getDatastore().get(i);
        DatastoreSummary dsSummary = vdsInfo.getDatastore();
        if (dsSummary.isAccessible()) {
          datastoreName = dsSummary.getName();
          datastoreRef = dsSummary.getDatastore();
          flag = true;
          break;
        }
      }
      if (!flag) {
        throw new RuntimeException("No Datastore found on host");
      }
    }
    String datastoreVolume = getVolumeName(datastoreName);
    VirtualMachineFileInfo vmfi = new VirtualMachineFileInfo();
    vmfi.setVmPathName(datastoreVolume);
    configSpec.setFiles(vmfi);
    // Add a scsi controller
    int diskCtlrKey = 1;
    VirtualDeviceConfigSpec scsiCtrlSpec = new VirtualDeviceConfigSpec();
    scsiCtrlSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setBusNumber(0);
    scsiCtrlSpec.setDevice(scsiCtrl);
    scsiCtrl.setKey(diskCtlrKey);
    scsiCtrl.setSharedBus(VirtualSCSISharing.NO_SHARING);
    String ctlrType = scsiCtrl.getClass().getName();
    ctlrType = ctlrType.substring(ctlrType.lastIndexOf(".") + 1);

    // Find the IDE controller
    VirtualDevice ideCtlr = null;
    for (int di = 0; di < defaultDevices.size(); di++) {
      if (defaultDevices.get(di) instanceof VirtualIDEController) {
        ideCtlr = defaultDevices.get(di);
        break;
      }
    }

    // Add a floppy
    VirtualDeviceConfigSpec floppySpec = new VirtualDeviceConfigSpec();
    floppySpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
    VirtualFloppy floppy = new VirtualFloppy();
    VirtualFloppyDeviceBackingInfo flpBacking = new VirtualFloppyDeviceBackingInfo();
    flpBacking.setDeviceName("/dev/fd0");
    floppy.setBacking(flpBacking);
    floppy.setKey(3);
    floppySpec.setDevice(floppy);

    // Add a cdrom based on a physical device
    VirtualDeviceConfigSpec cdSpec = null;

    if (ideCtlr != null) {
      cdSpec = new VirtualDeviceConfigSpec();
      cdSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualCdrom cdrom = new VirtualCdrom();
      VirtualCdromIsoBackingInfo cdDeviceBacking = new VirtualCdromIsoBackingInfo();
      cdDeviceBacking.setDatastore(datastoreRef);
      cdDeviceBacking.setFileName(datastoreVolume + "testcd.iso");
      cdrom.setBacking(cdDeviceBacking);
      cdrom.setKey(20);
      cdrom.setControllerKey(new Integer(ideCtlr.getKey()));
      cdrom.setUnitNumber(new Integer(0));
      cdSpec.setDevice(cdrom);
    }

    // Create a new disk - file based - for the vm
    VirtualDeviceConfigSpec diskSpec = null;
    diskSpec = createVirtualDisk(datastoreName, diskCtlrKey, datastoreRef, diskSizeMB);

    // Add a NIC. the network Name must be set as the device name to create the NIC.
    VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec();
    if (networkName != null) {
      nicSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
      VirtualEthernetCard nic = new VirtualPCNet32();
      VirtualEthernetCardNetworkBackingInfo nicBacking =
          new VirtualEthernetCardNetworkBackingInfo();
      nicBacking.setDeviceName(networkName);
      nic.setAddressType("generated");
      nic.setBacking(nicBacking);
      nic.setKey(4);
      nicSpec.setDevice(nic);
    }

    List<VirtualDeviceConfigSpec> deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
    deviceConfigSpec.add(scsiCtrlSpec);
    deviceConfigSpec.add(floppySpec);
    deviceConfigSpec.add(diskSpec);
    if (ideCtlr != null) {
      deviceConfigSpec.add(cdSpec);
      deviceConfigSpec.add(nicSpec);
    } else {
      deviceConfigSpec = new ArrayList<VirtualDeviceConfigSpec>();
      deviceConfigSpec.add(nicSpec);
    }
    configSpec.getDeviceChange().addAll(deviceConfigSpec);
    return configSpec;
  }