Exemple #1
0
func NewVirConnectionReadOnly(uri string) (VirConnection, error) {
	cUri := C.CString(uri)
	defer C.free(unsafe.Pointer(cUri))
	ptr := C.virConnectOpenReadOnly(cUri)
	if ptr == nil {
		return VirConnection{}, errors.New(GetLastError())
	}
	obj := VirConnection{ptr: ptr}
	return obj, nil
}
Exemple #2
0
func NewVirConnectionReadOnly(uri string) (VirConnection, error) {
	var cUri *C.char
	if uri != "" {
		cUri = C.CString(uri)
		defer C.free(unsafe.Pointer(cUri))
	}
	ptr := C.virConnectOpenReadOnly(cUri)
	if ptr == nil {
		return VirConnection{}, GetLastError()
	}
	obj := VirConnection{ptr: ptr}
	return obj, nil
}
Exemple #3
0
func NewVirConnectionReadOnly(uri string) (VirConnection, error) {
	var cUri *C.char
	if uri != "" {
		cUri = C.CString(uri)
		defer C.free(unsafe.Pointer(cUri))
	}
	ptr := C.virConnectOpenReadOnly(cUri)
	if ptr == nil {
		return VirConnection{}, GetLastError()
	}
	obj := VirConnection{ptr: ptr, event_cbs: make(map[int]*domainCallbackContext)}
	return obj, nil
}
// Open creates a new libvirt connection to the Hypervisor. The
// connection mode specifies whether the connection will be read-write
// or read-only. The URIs are documented at http://libvirt.org/uri.html.
func Open(uri string, mode ConnectionMode, logOutput io.Writer) (Connection, error) {
	cUri := C.CString(uri)
	defer C.free(unsafe.Pointer(cUri))

	logger := newLogger(logOutput)

	if uri == DefaultURI {
		logger.Printf("opening connection (mode = %v) to the default URI...\n", mode)
	} else {
		logger.Printf("opening connection (mode = %v) to %v...\n", mode, uri)
	}

	var cConn C.virConnectPtr
	switch mode {
	case ReadWrite:
		cConn = C.virConnectOpen(cUri)
	case ReadOnly:
		cConn = C.virConnectOpenReadOnly(cUri)
	default:
		return Connection{}, ErrInvalidConnectionMode
	}

	if cConn == nil {
		err := LastError()
		logger.Printf("an error occurred: %v\n", err)
		return Connection{}, err
	}

	logger.Println("connection established")

	conn := Connection{
		log:        logger,
		virConnect: cConn,
	}

	return conn, nil
}