private void createAndLaunchVirtualMachine(int i) throws InterruptedException {
    String instanceName = vmName + "_" + i;
    IMachine adminNode = manager.getVBox().findMachine(adminNodeName);

    IMachine clonedVM =
        manager.getVBox().createMachine(settingsFile, instanceName, osTypeId, vmId, forceOverwrite);
    List<CloneOptions> options = new ArrayList<CloneOptions>();
    options.add(CloneOptions.Link);
    IProgress progress =
        adminNode
            .getCurrentSnapshot()
            .getMachine()
            .cloneTo(clonedVM, CloneMode.MachineState, options);
    if (progress.getCompleted()) logger().debug("clone done");

    manager.getVBox().registerMachine(clonedVM);

    System.out.println("\nLaunching VM named " + clonedVM.getName() + " ...");
    launchVMProcess(clonedVM, manager.getSessionObject());
    String ipAddress = null;
    while (ipAddress == null || ipAddress.equals("")) {
      try {
        ipAddress = clonedVM.getGuestPropertyValue("/VirtualBox/GuestInfo/Net/0/V4/IP");
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println(ipAddress + " is the IP address of " + clonedVM.getName());

    // TODO
    // IPSocket socket = new IPSocket(ipAddress, 22);
    // checkSSH(IPSocket socket)

  }
  @Test(dependsOnMethods = "testStartAndValidateVirtualMachines")
  public void testStopVirtualMachines() {
    for (int i = 1; i < numberOfVirtualMachine + 1; i++) {
      String instanceName = vmName + "_" + i;
      IMachine machine = manager.getVBox().findMachine(instanceName);

      try {
        ISession machineSession = manager.openMachineSession(machine);
        IProgress progress = machineSession.getConsole().powerDown();
        progress.waitForCompletion(-1);
        machineSession.unlockMachine();

        while (!machine.getSessionState().equals(SessionState.Unlocked)) {
          try {
            System.out.println(
                "waiting for unlocking session - session state: " + machine.getSessionState());
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
        assertEquals(machine.getState(), MachineState.PoweredOff);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  @Override
  public Hardware apply(@Nullable IMachine vm) {
    String osTypeId = vm.getOSTypeId();

    IGuestOSType guestOSType = virtualBoxManager.getVBox().getGuestOSType(osTypeId);
    Boolean is64Bit = guestOSType.getIs64Bit();
    HardwareBuilder hardwareBuilder = new HardwareBuilder();
    hardwareBuilder.ids(vm.getId());
    hardwareBuilder.supportsImage(ImagePredicates.idEquals(vm.getId()));
    hardwareBuilder.is64Bit(is64Bit);
    hardwareBuilder.supportsImage(ImagePredicates.idEquals(vm.getId()));
    return hardwareBuilder.build();
  }
 private IMachine getMasterNode(
     VirtualBoxManager manager, ComputeServiceContext localHostContext) {
   try {
     Predicate<IPSocket> socketTester =
         new RetryablePredicate<IPSocket>(new InetSocketAddressConnect(), 10, 1, TimeUnit.SECONDS);
     String workingDir = PropertyUtils.getWorkingDirFromProperty();
     StorageController ideController =
         StorageController.builder()
             .name(controllerIDE)
             .bus(StorageBus.IDE)
             .attachISO(0, 0, workingDir + "/ubuntu-11.04-server-i386.iso")
             .attachHardDisk(
                 HardDisk.builder()
                     .diskpath(workingDir + "/testadmin.vdi")
                     .controllerPort(0)
                     .deviceSlot(1)
                     .build())
             .attachISO(1, 1, workingDir + "/VBoxGuestAdditions_4.1.2.iso")
             .build();
     VmSpec vmSpecification =
         VmSpec.builder()
             .id(vmId)
             .name(vmName)
             .osTypeId(osTypeId)
             .memoryMB(512)
             .cleanUpMode(CleanupMode.Full)
             .controller(ideController)
             .forceOverwrite(true)
             .build();
     return new CreateAndInstallVm(
             manager, guestId, localHostContext, hostId, socketTester, "127.0.0.1", 8080, HEADLESS)
         .apply(vmSpecification);
   } catch (IllegalStateException e) {
     // already created
     return manager.getVBox().findMachine(vmName);
   }
 }