Agaetis
VMWare API

1.0.0.8

com.agaetis.vmware
Class VixVM

java.lang.Object
  extended by com.agaetis.vmware.VixVM

public class VixVM
extends Object

VixVM is instantiated when a VMHost runs an "open" method. VixVM is the wrapper for a Virtual Machine that includes every methods to power it on, off, suspend it or revert it to the last snapshot.

Author:
Franck MARCHAND

Field Summary
private  int handle
           
(package private)  boolean loggedIn
           
(package private)  VixLibrary.VixLibraryInterface vLibraryInterfaceInstance
           
private  String vmxFilePathName
           
 
Constructor Summary
VixVM()
           
 
Method Summary
 void copyFileFromGuestToHost(String guestPathName, String hostPathName)
          copy a file from guest to host file system OS
 void copyFileFromHostToGuest(String hostPathName, String guestPathName)
          copy a file from host to the virtual machine guest OS
 void createSnapshot(String name, String description)
          create a snapshot on the server with the current running machine.
 void deleteDirectoryInGuest(String guestPathName)
          This function deletes a directory in the guest operating system.
 void deleteFileInGuest(String guestPathName)
          This function deletes a file in the guest operating system.
 boolean directoryExistsInGuest(String guestPathName)
          This function tests the existence of a directory in the guest operating system.
 boolean fileExistsInGuest(String guestPathName)
          This function tests the existence of a file in the guest operating system.
 int getHandle()
           
 int getPowerState()
          It allows to know the state of the current vm.
private  int getRootSnapshot(int index)
           
 String getVmxFilePathName()
           
 boolean isOff()
          To know if the vm is off.
 boolean isOn()
          To know if the vm is running with a power state matching with Vix.PowerState.POWERED_ON
 boolean isSuspended()
          To know if the vm is in a suspended mode.
 void loginInGuest(String username, String password)
          Allows to login on the guest system.
 void loginInGuest(String username, String password, boolean interactive)
          Allows to login on the guest system.
 void logoutFromGuest()
          Logout from the previous login for this VM.
private  void power(boolean p)
          Private method for power actions.
 void powerOff()
          Power off the VM.
 void powerOn()
          Power on the VM.
 void revertToSnapshot(int index)
          Revert the vm to the snapshot specified with index.
 ExitProperties runProgramInGuest(String program, String args, int flag)
          Run a command in the virtual machine.
 ExitProperties runScriptInGuest(String interpreter, String scriptText, int flag)
          Allow to run a script in an interpreter installed on the guest machine.
 void setHandle(int handle)
           
 void setVmxFilePathName(String vmxFilePathName)
           
 void shutdownUnixSystem(String login, String password, String shutdownOptions)
          If the guest system is a linux, it shutdowns the machine.
 void suspend()
          Put the virtual machine in a suspended mode.
 void unpause()
          Unpause the machine whe it was in a paused state.
 void waitForToolsInGuest(int timeoutInSeconds)
          waitForToolsInGuest wait for VMWare tools to be launched.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

vmxFilePathName

private String vmxFilePathName

handle

private int handle

loggedIn

boolean loggedIn

vLibraryInterfaceInstance

VixLibrary.VixLibraryInterface vLibraryInterfaceInstance
Constructor Detail

VixVM

public VixVM()
Method Detail

copyFileFromGuestToHost

public void copyFileFromGuestToHost(String guestPathName,
                                    String hostPathName)
copy a file from guest to host file system OS

Parameters:
guestPathName - The path name of a file on the guest file system
hostPathName - The path name of a file on the host file system

copyFileFromHostToGuest

public void copyFileFromHostToGuest(String hostPathName,
                                    String guestPathName)
copy a file from host to the virtual machine guest OS

Parameters:
hostPathName - The path name of a file on the host file system
guestPathName - The path name of a file on the guest file system

createSnapshot

public void createSnapshot(String name,
                           String description)
create a snapshot on the server with the current running machine. VMWare Server can only support one snapshot at a time. Therefore the method revertToSnapshot will only take 0 as possible index. For other VMWare product it can take any value from 0 to n-1 (n : number of snapshots).

Parameters:
name - unique name for the snapshot
description - description of the snapshot

deleteDirectoryInGuest

public void deleteDirectoryInGuest(String guestPathName)
This function deletes a directory in the guest operating system. Any files or subdirectories in the specified directory will also be deleted.

Parameters:
guestPathName - the path name of the directory to delete in the guest operating system.

deleteFileInGuest

public void deleteFileInGuest(String guestPathName)
This function deletes a file in the guest operating system.

Parameters:
guestPathName - the path name of the file to delete in the guest operating system.

directoryExistsInGuest

public boolean directoryExistsInGuest(String guestPathName)
This function tests the existence of a directory in the guest operating system.

Parameters:
guestPathName - The path to the directory to be tested.
Returns:
true if the directory exists

fileExistsInGuest

public boolean fileExistsInGuest(String guestPathName)
This function tests the existence of a file in the guest operating system. If it is a directory and even if it exists then it will return false.

Parameters:
guestPathName - The path to the file to be tested.
Returns:
true if the file exists

getHandle

public int getHandle()

getPowerState

public int getPowerState()
                  throws VMWareRuntimeException
It allows to know the state of the current vm.

Returns:
state of the current machine in the form of the combination of different flag
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException
See Also:
Vix.PowerState

getRootSnapshot

private int getRootSnapshot(int index)

getVmxFilePathName

public String getVmxFilePathName()

isOff

public boolean isOff()
To know if the vm is off.

Returns:
true if the vm is off or false if it's on.

isOn

public boolean isOn()
To know if the vm is running with a power state matching with Vix.PowerState.POWERED_ON

Returns:
true if the vm is on or false if it isn't.

isSuspended

public boolean isSuspended()
To know if the vm is in a suspended mode.

Returns:
true if it is suspended or false if it's not in suspended mode.

loginInGuest

public void loginInGuest(String username,
                         String password)
                  throws VMWareRuntimeException
Allows to login on the guest system. This method is necessary to run commands in the guest system. This method does not use the VIX_LOGIN_IN_GUEST_REQUIRE_INTERACTIVE_ENVIRONMENT option. Instead it uses the value '0' for this option. If subsequently it needs to run a program within the guest then this option should be passed as parameter. To do that you should rather use loginInGuest(String, String, boolean)

Parameters:
username - the username login in the guest system.
password - the password used for this login
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

loginInGuest

public void loginInGuest(String username,
                         String password,
                         boolean interactive)
                  throws VMWareRuntimeException
Allows to login on the guest system. This method is necessary to run commands in the guest system.

Parameters:
username - the username login in the guest system.
password - the password used for this login
interactive - define if the option for the LoginInGuest should be 0 (false) or VIX_LOGIN_IN_GUEST_REQUIRE_INTERACTIVE_ENVIRONMENT (true).
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

logoutFromGuest

public void logoutFromGuest()
                     throws VMWareRuntimeException
Logout from the previous login for this VM. if there is no current login session, it will throw an exception.

Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

power

private void power(boolean p)
            throws VMWareRuntimeException
Private method for power actions.

Parameters:
p - a boolean that indicated if it should power on or power off.
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

powerOff

public void powerOff()
              throws VMWareRuntimeException
Power off the VM. Power off the virtual machine. This method throws a VMWareRuntimeException to inform why it didn't power off. This method has the same effect as a hardware power down. Don't use it if you are not sure to have a snapshot or something with a saved state of the vm. Instead, to use a safer way, you can get connected in order to run a shutdown command through VMWare.

Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException
See Also:
shutdownUnixSystem(java.lang.String, java.lang.String, java.lang.String)

powerOn

public void powerOn()
             throws VMWareRuntimeException
Power on the VM. Power on the virtual machine. This method throws a VMWareRuntimeException to inform why it didn't power on. This method should be use to resume a virtual machine from suspended mode.

Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

revertToSnapshot

public void revertToSnapshot(int index)
                      throws VMWareRuntimeException
Revert the vm to the snapshot specified with index. With VMWare Server Console v1.0 there is only one snapshot possible.

Parameters:
index - index of the snapshot used to switched to
Throws:
VMWareRuntimeException

runProgramInGuest

public ExitProperties runProgramInGuest(String program,
                                        String args,
                                        int flag)
                                 throws VMWareRuntimeException
Run a command in the virtual machine. Before to be able to use this method, you need to be logged in on the virtual machine using loginInGuest(String username, String password)

Parameters:
program - program path to run. e.g : "/sbin/shutdown"
args - arguments for the program to launch. e.g : "-h now"
flag - for some guest it's possible to tell with this flag to open the program in a window. The guest need to be in a gui environment. usually Vix.RunProgram.RETURN_IMMEDIATELY is used.
Returns:
ExitProperties#
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException
See Also:
Vix.RunProgram

runScriptInGuest

public ExitProperties runScriptInGuest(String interpreter,
                                       String scriptText,
                                       int flag)
                                throws VMWareRuntimeException
Allow to run a script in an interpreter installed on the guest machine. Before to run this method you need to be logged in in the guest system ( loginInGuest(String, String)

Parameters:
interpreter - interpreter path to run the script with. If it is cmd.exe then put null instead. Otherwise it would launch cmd.exe and not the script itself.
scriptText - script content
flag - for some guest it's possible to tell with this flag to open the program in a window. The guest need to be in a gui environment. usually Vix.RunProgram.RETURN_IMMEDIATELY is used.
Returns:
ExitProperties
Throws:
VMWareRuntimeException

setHandle

public void setHandle(int handle)

setVmxFilePathName

public void setVmxFilePathName(String vmxFilePathName)

shutdownUnixSystem

public void shutdownUnixSystem(String login,
                               String password,
                               String shutdownOptions)
                        throws VMWareRuntimeException
If the guest system is a linux, it shutdowns the machine. It uses the loginInGuest(java.lang.String, java.lang.String) and runProgramInGuest(java.lang.String, java.lang.String, int) combined. You just have to provide login and password used by the loginInGuest method, and shutdownOptions string. It uses the shutdown command of unix system with the -h options which says to halt the machine. Usually you use 'now' after the options of the shutdown command as shutdownOptions. but +1 (wait 1 minute before to proceed with the shutting down).

 
  myVM.shutdownUnixSystem("stiger", "1234", "+1");
 
 

This example will shutdown the vm through the unix command. It will wait 1 minute before stopping every process and before halting the machine.

Parameters:
login - the username login in the guest system.
password - the password used for this login.
shutdownOptions - options like 'now' or '+1' to pass to the unix command shutdown.
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

suspend

public void suspend()
             throws VMWareRuntimeException
Put the virtual machine in a suspended mode. To resume the machine just run the powerOn() method on the suspended vm.

Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

unpause

public void unpause()
             throws VMWareRuntimeException
Unpause the machine whe it was in a paused state. Only works with vmware workstation and maybe server 2.0

Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

waitForToolsInGuest

public void waitForToolsInGuest(int timeoutInSeconds)
                         throws VMWareRuntimeException
waitForToolsInGuest wait for VMWare tools to be launched. Normally when it's running, it means the vm finished to boot. There is a timeout value to not wait endlessly.

Parameters:
timeoutInSeconds - Timeout in second.
Throws:
com.agaetis.exceptions.VMWareRuntimeException
VMWareRuntimeException

Agaetis
VMWare API

1.0.0.8

2010 Agaetis