How to Remotely Create and Delete Directories in a VMware Virtual Machine Guest

From Virtuatopia
Revision as of 18:37, 30 October 2008 by Neil (Talk | contribs) (Obtaining a Directory File Listing)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

With the VMware vmrun command line tool, it is possible to remotely create and remove directories on a guest operating system running inside a VMware Server or VMware Workstation virtual machine.

The vmrun command is installed by default with VMware Server and VMware Workstation. In the case of VMware Server 2.0, vmrun is installed in \Program Files\VMware\VMware Server on Windows hosts and /usr/bin on Linux.

Note that in order to perform these tasks, VMware Tools must be installed on the guest operating system. For details on installing VMware Tools refer to the Understanding and Installing VMware Tools chapter of the Virtuatopia VMware Server 2.0 Essentials online book.




Understanding vmrun Command Line Syntax

The basic command line syntax for using vmrun varies slightly between VMware products, but can generally be summarized as follows:

vmrun <host authentication flags> <guest authentication flags> <command> <parameters>

The host authentication flags are required to provide host, login and password information for the host system. These are essentially the same credentials that would be used when accessing the VI Web Access management interface. These flags are required only on VMware Server hosts, and are not needed for VMware Workstation:

Flag

Description

-h

The https URL of the host to which vmrun is required to connect. Must also include the /sdk sub-directory and, optionally the port number (unless the -p flag below is used). For example, https://myhostname:8333/sdk.

-P The port number used by the host for virtual machine management. By default this will be 8333. If the port is specified in the URL (see above) this flag is not required.

-T

The type of VMware product which is running on the host. Options are ws for VMware Workstation, server to VMware Server 2 and server1 for VMware Server 1.

-u The user name on the host to be used to log into the VMware management interface. The same as the user name that would used when logging in using the VI Web Access interface.

-p

The password corresponding to the user name specified with -u as outlined above.

The guest password flags are specified if the vmrun tool is required to log into the guest operating system to perform tasks such as work with guest files or execute commands. If such operations are not required, these flags may be omitted from the command line:

Flag

Description

-gu

A suitable user name via which vmrun may log into the specified guest operating system

-gp The corresponding password for the guest user specified using the -gu flag.

The command argument identifies to vmrun the task to be performed on the specified virtual machine. For example, start, stop and reset are all valid commands. Other commands are available to perform tasks within a guest operating system, such as runProgramInGuest and deleteFileInGuest.

The parameters arguments specify optional information required for a specific command. If, for example, the stop command is specified, the parameters argument is used to reference the .vmx file of the virtual machine which is to be powered off. In the case of running guest commands, both the .vmx file of the target virtual machine and the path to the program to be executed must be provided as parameters.

When specifying the virtual machine on which a command is to be executed, the location of the virtual machine .vmx configuration file must be provided. For VMware Workstation, this involves specifying the full path of the file. For example:

"C:\VMware\VMachines\win2008.vmx"

In the case of VMware Server 2.0, which introduced the concept of datastores, the name of the datastore in which the virtual machines resides must be provided together with the path of the corresponding .vmx file within that datastore. For example, to reference a virtual machine stored in the win2008 sub-directory of the Vol1 datastore, the vmrun path parameter would be defined as follows (note that the datastore name is enclosed in square brackets and separated from the sub-directory by a space character):

"[Vol1] win2008/win2008.vmx"

Bringing all of these different command line arguments together, a typical vmrun command to start a virtual machine on a host named vmhost running VMware Server 2 might appear as follows:

 vmrun -T server -h https://hostname:8333/sdk -u root -p mypassword stop 
                       "[Vol1] win2008/win2008.vmx"

Creating a Directory on a Virtual Machine Guest System

To create a new directory on the guest operating system of a VMware virtual machine, a command similar to the following will need to be run:

vmrun -T server -h https://hostname:8333/sdk -u root -p mypassword -gu fred -gp guestpasswd 
            createDirectoryInGuest "[Vol1] CentOS 5.2/CentOS 5.2.vmx" /home/fred/MyNewDir

Obtaining a Guest Directory Listing

Before remotely working with a directory residing on a virtual guest system, it may be necessary to obtain a listing of the contents of that directory. Fortunately this can be obtained using vmrun with the listDirectoryInGuest command:

vmrun -T server -h https://hostname:8333/sdk -u root -p mypassword -gu fred -gp guestpassword 
            listDirectoryInGuest "[Vol1] CentOS 5.2/CentOS 5.2.vmx" /home/fred
Directory list: 5
.esd_auth
.dmrc
.gconfd
.recently-used.xbel
Desktop

Deleting a Directory from a Virtual Machine Guest

The following example demonstrates how to delete a directory residing on the guest operating system of a VMware Server virtual machine:

vmrun -T server -h https://hostname:8333/sdk -u root -p mypassword -gu fred -gp guestpasswd 
            deleteDirectoryInGuest "[Vol1] CentOS 5.2/CentOS 5.2.vmx" /home/fred/MyNewDir