Esempio n. 1
0
// This function opens a virtual machine on the host
// and returns a VM instance.
//
// Parameters:
//
//   VmxFile: The path name of the virtual machine configuration file on the
//   local host.
//
//   Password: If VM is encrypted, this is the password for VIX to be able to
//   open it.
//
// Remarks:
//
//   * This function opens a virtual machine on the host instance
//     The virtual machine is identified by vmxFile, which is a path name to the
//     configuration file (.VMX file) for that virtual machine.
//
//   * The format of the path name depends on the host operating system.
//     For example, a path name for a Windows host requires backslash as a
//     directory separator, whereas a Linux host requires a forward slash. If the
//     path name includes backslash characters, you need to precede each one with
//     an escape character. For VMware Server 2.x, the path contains a preceeding
//     data store, for example [storage1] vm/vm.vmx.
//
//   * For VMware Server hosts, a virtual machine must be registered before you
//     can open it. You can register a virtual machine by opening it with the
//     VMware Server Console, through the vmware-cmd command with the register
//     parameter, or with Host.RegisterVM().
//
//   * For vSphere, the virtual machine opened may not be the one desired if more
//     than one Datacenter contains VmxFile.
//
//   * To open an encrypted virtual machine, pass its correspondent password.
//
// Since VMware Workstation 7.0
func (h *Host) OpenVM(vmxFile, password string) (*VM, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var propertyHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var vmHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	defer C.Vix_ReleaseHandle(propertyHandle)
	defer C.Vix_ReleaseHandle(jobHandle)

	if password != "" {
		cpassword := C.CString(password)
		defer C.free(unsafe.Pointer(cpassword))

		err = C.alloc_vm_pwd_proplist(h.handle,
			&propertyHandle,
			cpassword)

		if C.VIX_OK != err {
			return nil, &Error{
				Operation: "host.OpenVM",
				Code:      int(err & 0xFFFF),
				Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
			}
		}
	}

	cVmxFile := C.CString(vmxFile)
	defer C.free(unsafe.Pointer(cVmxFile))

	jobHandle = C.VixHost_OpenVM(h.handle,
		cVmxFile,
		C.VIX_VMOPEN_NORMAL,
		propertyHandle,
		nil, // callbackProc
		nil) // clientData

	err = C.get_vix_handle(jobHandle,
		C.VIX_PROPERTY_JOB_RESULT_HANDLE,
		&vmHandle,
		C.VIX_PROPERTY_NONE)

	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "host.OpenVM.get_vix_handle",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return NewVirtualMachine(vmHandle, vmxFile)
}
Esempio n. 2
0
// Ls lists a directory in the guest operating system.
//
// Parameters:
//
//   dir: The path name of a directory to be listed.
//
// Remarks:
//
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) Ls(dir string) ([]*GuestFile, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var files []*GuestFile

	guestdir := C.CString(dir)
	defer C.free(unsafe.Pointer(guestdir))

	jobHandle = C.VixVM_ListDirectoryInGuest(g.handle, guestdir, 0, nil, nil)
	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "guest.Ls.ListDirectoryInGuest",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	num := C.VixJob_GetNumProperties(jobHandle, C.VIX_PROPERTY_JOB_RESULT_ITEM_NAME)
	for i := 0; i < int(num); i++ {
		var name *C.char
		var size *C.int64
		var modtime *C.int64
		var attrs *C.int

		gfile := &GuestFile{}

		err = C.get_guest_file(jobHandle, C.int(i), name, size, modtime, attrs)
		if C.VIX_OK != err {
			return nil, &Error{
				Operation: "guest.Ls.get_guest_file",
				Code:      int(err & 0xFFFF),
				Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
			}
		}

		gfile.Path = C.GoString(name)
		C.Vix_FreeBuffer(unsafe.Pointer(name))

		gfile.Size = int64(*size)
		gfile.Modtime = int64(*modtime)
		gfile.Attrs = FileAttr(*attrs)

		files = append(files, gfile)
	}

	return files, nil
}
Esempio n. 3
0
// This function finds Vix objects. For example, when used to find all
// running virtual machines, Host.FindItems() returns a series of virtual
// machine file path names.
func (h *Host) FindItems(options SearchType) ([]string, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var items []string

	callback := func(item *C.char) {
		items = append(items, C.GoString(item))
	}

	jobHandle = C.VixHost_FindItems(h.handle,
		C.VixFindItemType(options), //searchType
		C.VIX_INVALID_HANDLE,       //searchCriteria
		-1,                         //timeout
		(*C.VixEventProc)(C.find_items_callback), //callbackProc
		unsafe.Pointer(&callback))                //clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "host.FindItems",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return items, nil
}
Esempio n. 4
0
// Copies a file or directory from the local system (where the Vix client is
// running) to the guest operating system.
//
// Parameters:
//
//   src: The path name of a file on a file system available to the Vix client.
//   guest: Guest instance where the file is going to be copied to
//   dest: The path name of a file on a file system available to the guest.
//
// Remarks:
//
//   * The virtual machine must be running while the file is copied from the Vix
//     client machine to the guest operating system.
//
//   * Existing files of the same name are overwritten, and folder contents are
//     merged.
//
//   * The copy operation requires VMware Tools to be installed and running in
//     the guest operating system.
//
//   * You must call VM.LoginInGuest() before calling this function in order
//     to get a Guest instance.
//
//   * The format of the file name depends on the guest or local operating system.
//     For example, a path name for a Microsoft Windows guest or host requires
//     backslash as a directory separator, whereas a Linux guest or host requires
//     a forward slash. If the path name includes backslash characters,
//     you need to precede each one with an escape character.
//
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
//   * If any file fails to be copied, Vix aborts the operation, does not attempt
//     to copy the remaining files, and returns an error.
//
//   * In order to copy a file to a mapped network drive in a Windows guest
//     operating system, it is necessary to call VixVM_LoginInGuest() with the
//     LOGIN_IN_GUEST_REQUIRE_INTERACTIVE_ENVIRONMENT flag set.
//     Using the interactive session option incurs an overhead in file transfer
//     speed.
//
// Since VMware Server 1.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
//
func (h *Host) CopyFileToGuest(src string, guest *Guest, dest string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	csrc := C.CString(src)
	cdest := C.CString(dest)
	defer C.free(unsafe.Pointer(csrc))
	defer C.free(unsafe.Pointer(cdest))

	jobHandle = C.VixVM_CopyFileFromHostToGuest(
		guest.handle,         // VM handle
		csrc,                 // src name
		cdest,                // dest name
		C.int(0),             // options
		C.VIX_INVALID_HANDLE, // propertyListHandle
		nil,                  // callbackProc
		nil)                  // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "host.CopyFileToGuest",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 5
0
// Mv renames a file or directory in the guest operating system.
//
// Parameters:
//
//   path1: The path to the file to be renamed.
//   path2: The path to the new file.
//
// Remarks:
//
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
//   * On Windows guests, it fails on directory moves when the destination is on a
//     different volume.
//
//   * Because of the differences in how various operating systems handle
//     filenames, Vix may return either VIX_E_INVALID_ARG or
//     VIX_E_FILE_NAME_TOO_LONG for filenames longer than 255 characters.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) Mv(path1, path2 string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	cpath1 := C.CString(path1)
	cpath2 := C.CString(path2)
	defer C.free(unsafe.Pointer(cpath1))
	defer C.free(unsafe.Pointer(cpath2))

	jobHandle = C.VixVM_RenameFileInGuest(g.handle,
		cpath1,               //oldName
		cpath2,               //newName
		0,                    //options
		C.VIX_INVALID_HANDLE, //propertyListHandle
		nil,                  //callbackProc
		nil)                  //clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.Mv",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 6
0
// IsFile tests the existence of a file in the guest operating system.
//
// Parameters:
//
//   filepath: The path to the file to be tested.
//
// Remarks:
//
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
//   * If filepath exists as a file system object, but is not a normal file (e.g.
//     it is a directory, device, UNIX domain socket, etc),
//     then VIX_OK is returned, and VIX_PROPERTY_JOB_RESULT_GUEST_OBJECT_EXISTS
//     is set to FALSE.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) IsFile(filepath string) (bool, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var result C.int

	fpath := C.CString(filepath)
	defer C.free(unsafe.Pointer(fpath))

	jobHandle = C.VixVM_FileExistsInGuest(g.handle,
		fpath, // dir path name
		nil,   // callbackProc
		nil)   // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.is_file_or_dir(jobHandle, &result)
	if C.VIX_OK != err {
		return false, &Error{
			Operation: "guest.IsFile",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	if int(result) == C.FALSE {
		return false, nil
	}

	return true, nil
}
Esempio n. 7
0
// RmFile deletes a file in the guest operating system.
//
// Parameters:
//
//   filepath: file path to be deleted in the guest OS
//
// Remarks:
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) RmFile(filepath string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	fpath := C.CString(filepath)
	defer C.free(unsafe.Pointer(fpath))

	jobHandle = C.VixVM_DeleteFileInGuest(g.handle,
		fpath, // file path name
		nil,   // callbackProc
		nil)   // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.RmFile",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 8
0
// RunScript runs a script in the guest operating system.
//
// Parameters:
//
//   shell: The path to the script interpreter, or NULL to use cmd.exe as
//   the interpreter on Windows.
//
//   script: The text of the script.
//
//   options: Run options for the program. See the remarks below.
//
// Remarks:
//
//   * This function runs the script in the guest operating system.
//
//   * The current working directory for the script executed in the guest is
//     not defined. Absolute paths should be used for files in the guest,
//     including the path to the shell or interpreter, and any files referenced
//     in the script text.
//
//   * If the options parameter is RUNPROGRAM_WAIT, this function will block and
//     return only when the program exits in the guest operating system.
//     Alternatively, you can pass RUNPROGRAM_RETURN_IMMEDIATELY as the value of
//     the options parameter, and this makes the function to return as soon as the
//     program starts in the guest.
//
//   * The following properties will be returned:
//     PROCESS_ID: the process id; however, if the guest has an older version of
//                 Tools (those released with Workstation 6 and earlier) and
//                 the RUNPROGRAM_RETURN_IMMEDIATELY flag is used, then the
//                 process ID will not be returned from the guest and this
//                 property will return 0.
//     ELAPSED_TIME: the process elapsed time;
//     PROGRAM_EXIT_CODE: the process exit code.
//
//   * If the option parameter is RUNPROGRAM_RETURN_IMMEDIATELY, the latter two
//     will both be 0.
//
//   * Depending on the behavior of the guest operating system, there may be a
//     short delay after the function returns before the process is visible in the
//     guest operating system.
//
//   * If the total size of the specified interpreter and the script text is
//     larger than 60536 bytes, then the error VIX_E_ARGUMENT_TOO_BIG is returned.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) RunScript(shell, args string, options RunProgramOption) (uint64, int, int, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var pid *C.uint64
	var elapsedtime *C.int
	var exitCode *C.int

	cshell := C.CString(shell)
	cargs := C.CString(args)
	defer C.free(unsafe.Pointer(cshell))
	defer C.free(unsafe.Pointer(cargs))

	jobHandle = C.VixVM_RunProgramInGuest(g.handle,
		cshell, //guestProgramName
		cargs,  //commandLineArgs
		C.VixRunProgramOptions(options), //options
		C.VIX_INVALID_HANDLE,            //propertyListHandle
		nil,                             // callbackProc
		nil)                             // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.get_program_output(jobHandle, pid, elapsedtime, exitCode)

	if C.VIX_OK != err {
		return 0, 0, 0, &Error{
			Operation: "guest.RunScript.get_program_output",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return uint64(*pid), int(*elapsedtime), int(*exitCode), nil
}
Esempio n. 9
0
// FileInfo returns information about a file in the guest operating system.
//
// Parameters:
//
//   filepath: The path name of the file in the guest.
//
// Remarks:
//   * Only absolute paths should be used for files in the guest;
//     the resolution of relative paths is not specified.
//
// Since VMware Workstation 6.5
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) FileInfo(filepath string) (*GuestFile, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var fsize *C.int64
	var flags *C.int
	var modtime *C.int64

	fpath := C.CString(filepath)
	defer C.free(unsafe.Pointer(fpath))

	jobHandle = C.VixVM_GetFileInfoInGuest(g.handle,
		fpath, // file path name
		nil,   // callbackProc
		nil)   // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.get_file_info(jobHandle, fsize, flags, modtime)
	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "guest.FileInfo",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return &GuestFile{
		Path:    filepath,
		Size:    int64(*fsize),
		Attrs:   FileAttr(*flags),
		Modtime: int64(*modtime),
	}, nil
}
Esempio n. 10
0
// CopyFileToHost copies a file or directory from the guest operating system to the local
// system (where the VIX client is running).
//
// Parameters:
//
//   guestpath: The path name of a file on a file system available to the guest.
//   hostpath: The path name of a file on a file system available to the Vix
//   client.
//
func (g *Guest) CopyFileToHost(guestpath, hostpath string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	gpath := C.CString(guestpath)
	hpath := C.CString(hostpath)
	defer C.free(unsafe.Pointer(gpath))
	defer C.free(unsafe.Pointer(hpath))

	jobHandle = C.VixVM_CopyFileFromGuestToHost(g.handle,
		gpath,                // src name
		hpath,                // dest name
		0,                    // options
		C.VIX_INVALID_HANDLE, // propertyListHandle
		nil,                  // callbackProc
		nil)                  // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.CopyFileToHost",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 11
0
// This function removes a virtual machine from the host's inventory.
//
// Parameters:
//
//   VmxFile: The path name of the .vmx file on the host.
//
// Remarks:
//
//   * This function unregisters the virtual machine identified by vmxFile,
//     which is a storage path to the configuration file (.vmx) for that virtual
//     machine. A virtual machine must be powered off to unregister it.
//   * The format of the storage path depends on the host operating system.
//     If the storage path includes backslash characters, you need to precede each
//     one with an escape character. Path to storage [standard] or [storage1] may
//     vary.
//   * For VMware Server 1.x, supply the full path name instead of storage path,
//     and specify VMWARE_SERVER provider to connect.
//   * This function has no effect on Workstation or Player, which lack a virtual
//     machine inventory.
//   * It is not a Vix error to unregister an already-unregistered virtual machine,
//     nor is it a Vix error to unregister a non-existent virtual machine.
//
// Since VMware Server 1.0
func (h *Host) UnregisterVM(vmxFile string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	cVmxFile := C.CString(vmxFile)
	defer C.free(unsafe.Pointer(cVmxFile))

	jobHandle = C.VixHost_UnregisterVM(h.handle,
		cVmxFile,
		nil, // callbackProc
		nil) // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "host.UnregisterVM",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 12
0
// MkTemp creates a temporary file in the guest operating system.
// The user is responsible for removing the file when it is no longer needed.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) MkTemp() (string, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var tempFilePath *C.char

	jobHandle = C.VixVM_CreateTempFileInGuest(g.handle,
		0,                    // options
		C.VIX_INVALID_HANDLE, // propertyListHandle
		nil,                  // callbackProc
		nil)                  // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.get_temp_filepath(jobHandle, tempFilePath)
	defer C.Vix_FreeBuffer(unsafe.Pointer(tempFilePath))

	if C.VIX_OK != err {
		return "", &Error{
			Operation: "guest.MkTemp",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return C.GoString(tempFilePath), nil
}
Esempio n. 13
0
// MkDir creates a directory in the guest operating system.
//
// Parameters:
//
//   path: Directory path to be created in the guest OS
//
// Remarks:
//
//   * If the parent directories for the specified path do not exist, this
//     function will create them.
//
//   * If the directory already exists, the error will be set to
//     VIX_E_FILE_ALREADY_EXISTS.
//
//   * Only absolute paths should be used for files in the guest; the resolution
//     of relative paths is not specified.
//
// Since Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) MkDir(path string) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	cpath := C.CString(path)
	defer C.free(unsafe.Pointer(cpath))

	jobHandle = C.VixVM_CreateDirectoryInGuest(g.handle,
		cpath,                // path name
		C.VIX_INVALID_HANDLE, // propertyListHandle
		nil,                  // callbackProc
		nil)                  // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.MkDir",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 14
0
// NumChildren returns the number of child snapshots of a specified snapshot.
//
// Remarks:
//
//   * This function is not supported when using the VMWARE_PLAYER provider.
//
// Since VMware Workstation 6.0.
func (s *Snapshot) NumChildren() (int, error) {
	var err C.VixError = C.VIX_OK
	var numChildren *C.int

	err = C.VixSnapshot_GetNumChildren(s.handle, numChildren)

	if C.VIX_OK != err {
		return 0, &Error{
			Operation: "snapshot.NumChildren",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return int(*numChildren), nil
}
Esempio n. 15
0
// Connects to a Provider
//
// Parameters:
//
//  config: See type ConnectConfig documentation for details
//
// Remarks:
//   * To specify the local host (where the API client runs) with VMware
//     Workstation and VMware Player, pass empty values for the hostname, port,
//     login, and password parameters or just don't set them.
//
//   * With vCenter Server, ESX/ESXi hosts, and VMware Server 2.0, the URL for
//     the hostname argument may specify the port.
//     Otherwise a HTTPS connection is attempted on port 443. HTTPS is strongly
//     recommended.
//     Port numbers are set during installation of Server 2.0. The installer's
//     default HTTP and HTTPS values are 8222 and 8333 for Server on Windows, or
//     (if not already in use) 80 and 443 for Server on Linux, and 902 for the
//     automation socket, authd. If connecting to a virtual machine through a
//     firewall, port 902 and the communicating port must be opened to allow
//     guest operations.
//
//   * If a VMware ESX host is being managed by a VMware VCenter Server, you
//     should call VixHost_Connect with the hostname or IP address of the VCenter
//     server, not the ESX host.
//     Connecting directly to an ESX host while bypassing its VCenter Server can
//     cause state inconsistency.
//
//   * On Windows, this function should not be called multiple times with
//     different service providers in the same process; doing so will result in
//     a VIX_E_WRAPPER_MULTIPLE_SERVICEPROVIDERS error.
//     A single client process can connect to multiple hosts as long as it
//     connects using the same service provider type.
//
//   * To enable SSL certificate verification, set the value of the options
//     parameter to include the bit flag specified by VERIFY_SSL_CERT.
//     This option can also be set in the VMware config file by assigning
//     vix.enableSslCertificateCheck as TRUE or FALSE.
//
//     The vix.sslCertificateFile config option specifies the path to a file
//     containing CA certificates in PEM format.
//
//     The vix.sslCertificateDirectory config option can specify a directory
//     containing files that each contain a CA certificate.
//     Upon encountering a SSL validation error, the host handle is not created
//     with a resulting error code of E_NET_HTTP_SSL_SECURITY.
//
//   * With VMware vCenter Server and ESX/ESXi 4.0 hosts, an existing VI API
//     session can be used instead of the username/password pair to authenticate
//     when connecting. To use an existing VI API session, a VI "clone ticket"
//     is required; call the VI API AcquireCloneTicket() method of the
//     SessionManager object to get this ticket.
//     Using the ticket string returned by this method, call vix.Connect()
//     with "" as the 'username' and the ticket as the 'password'.
//
// Since VMware Server 1.0
func Connect(config ConnectConfig) (*Host, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var hostHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	chostname := C.CString(config.Hostname)
	cusername := C.CString(config.Username)
	cpassword := C.CString(config.Password)
	defer C.free(unsafe.Pointer(chostname))
	defer C.free(unsafe.Pointer(cusername))
	defer C.free(unsafe.Pointer(cpassword))

	jobHandle = C.VixHost_Connect(C.VIX_API_VERSION,
		C.VixServiceProvider(config.Provider),
		chostname,
		C.int(config.Port),
		cusername,
		cpassword,
		C.VixHostOptions(config.Options),
		C.VIX_INVALID_HANDLE, // propertyListHandle
		nil,                  // callbackProc
		nil)                  // clientData

	err = C.get_vix_handle(jobHandle,
		C.VIX_PROPERTY_JOB_RESULT_HANDLE,
		&hostHandle,
		C.VIX_PROPERTY_NONE)

	defer C.Vix_ReleaseHandle(jobHandle)

	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "vix.Connect",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	host := &Host{
		handle:   hostHandle,
		Provider: config.Provider,
	}

	runtime.SetFinalizer(host, cleanupHost)

	return host, nil
}
Esempio n. 16
0
// Name returns user defined name for the snapshot.
func (s *Snapshot) Name() (string, error) {
	var err C.VixError = C.VIX_OK
	var name *C.char

	err = C.get_property(s.handle,
		C.VIX_PROPERTY_SNAPSHOT_DISPLAYNAME,
		unsafe.Pointer(&name))

	defer C.Vix_FreeBuffer(unsafe.Pointer(name))

	if C.VIX_OK != err {
		return "", &Error{
			Operation: "snapshot.Name",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return C.GoString(name), nil
}
Esempio n. 17
0
// SharedFoldersParentDir returns the parent dir for share folders in the Guest.
func (g *Guest) SharedFoldersParentDir() (string, error) {
	var err C.VixError = C.VIX_OK
	var path *C.char

	err = C.get_property(g.handle,
		C.VIX_PROPERTY_GUEST_SHAREDFOLDERS_SHARES_PATH,
		unsafe.Pointer(&path))

	defer C.Vix_FreeBuffer(unsafe.Pointer(path))

	if C.VIX_OK != err {
		return "", &Error{
			Operation: "guest.SharedFoldersParentDir",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return C.GoString(path), nil
}
Esempio n. 18
0
// Description returns user defined description for the snapshot.
func (s *Snapshot) Description() (string, error) {
	var err C.VixError = C.VIX_OK
	var desc *C.char

	err = C.get_property(s.handle,
		C.VIX_PROPERTY_SNAPSHOT_DESCRIPTION,
		unsafe.Pointer(&desc))

	defer C.Vix_FreeBuffer(unsafe.Pointer(desc))

	if C.VIX_OK != err {
		return "", &Error{
			Operation: "snapshot.Description",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return C.GoString(desc), nil
}
Esempio n. 19
0
// Logout removes any guest operating system authentication
// context created by a previous call to VM.LoginInGuest().
//
// Remarks:
//   * This function has no effect and returns success if VM.LoginInGuest()
//     has not been called.
//   * If you call this function while guest operations are in progress,
//     subsequent operations may fail with a permissions error.
//     It is best to wait for guest operations to complete before logging out.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) Logout() error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	jobHandle = C.VixVM_LogoutFromGuest(g.handle,
		nil, // callbackProc
		nil) // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.Logout",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 20
0
// Parent returns the parent of a snapshot.
//
// Remarks:
//
//   * This function is not supported when using the VMWARE_PLAYER provider
//
// Since VMware Workstation 6.0
func (s *Snapshot) Parent() (*Snapshot, error) {
	var snapshotHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	err = C.VixSnapshot_GetParent(s.handle,
		&snapshotHandle) //(output) A handle to the child snapshot.

	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "snapshot.Parent",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	snapshot := &Snapshot{
		handle: snapshotHandle,
	}

	runtime.SetFinalizer(snapshot, cleanupSnapshot)

	return snapshot, nil
}
Esempio n. 21
0
// Kill terminates a process in the guest operating system.
//
// Parameters:
//
//   pid: The ID of the process to be killed.
//
// Remarks:
//
//   * Depending on the behavior of the guest operating system, there may be a
//     short delay after the job completes before the process truly disappears.
//
//   * Because of differences in how various Operating Systems handle process IDs,
//     Vix may return either VIX_E_INVALID_ARG or VIX_E_NO_SUCH_PROCESS
//     for invalid process IDs.
//
// Since VMware Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) Kill(pid uint64) error {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK

	jobHandle = C.VixVM_KillProcessInGuest(g.handle,
		C.uint64(pid), // file path name
		0,             // options
		nil,           // callbackProc
		nil)           // clientData

	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return &Error{
			Operation: "guest.Kill",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	return nil
}
Esempio n. 22
0
// Ps lists the running processes in the guest operating system.
//
// Since Workstation 6.0
// Minimum Supported Guest OS: Microsoft Windows NT Series, Linux
func (g *Guest) Ps() ([]*GuestProcess, error) {
	var jobHandle C.VixHandle = C.VIX_INVALID_HANDLE
	var err C.VixError = C.VIX_OK
	var processes []*GuestProcess

	jobHandle = C.VixVM_ListProcessesInGuest(g.handle, 0, nil, nil)
	defer C.Vix_ReleaseHandle(jobHandle)

	err = C.vix_job_wait(jobHandle)
	if C.VIX_OK != err {
		return nil, &Error{
			Operation: "guest.Ps",
			Code:      int(err & 0xFFFF),
			Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
		}
	}

	num := C.VixJob_GetNumProperties(jobHandle, C.VIX_PROPERTY_JOB_RESULT_ITEM_NAME)
	for i := 0; i < int(num); i++ {
		var name *C.char
		var pid *C.uint64
		var owner *C.char
		var cmdline *C.char
		var isDebugged *C.Bool
		var startTime *C.int

		gprocess := &GuestProcess{}

		err = C.get_guest_process(jobHandle, C.int(i),
			name,
			pid,
			owner,
			cmdline,
			isDebugged,
			startTime)

		if C.VIX_OK != err {
			return nil, &Error{
				Operation: "guest.Ps.get_guest_process",
				Code:      int(err & 0xFFFF),
				Text:      C.GoString(C.Vix_GetErrorText(err, nil)),
			}
		}

		gprocess.Name = C.GoString(name)
		C.Vix_FreeBuffer(unsafe.Pointer(name))

		gprocess.Pid = uint64(*pid)

		gprocess.Owner = C.GoString(owner)
		C.Vix_FreeBuffer(unsafe.Pointer(owner))

		gprocess.Cmdline = C.GoString(cmdline)
		C.Vix_FreeBuffer(unsafe.Pointer(cmdline))

		if *isDebugged == 1 {
			gprocess.IsDebugged = true
		} else {
			gprocess.IsDebugged = false
		}

		gprocess.StartTime = int(*startTime)

		processes = append(processes, gprocess)
	}

	return processes, nil
}