示例#1
0
// ListDomains collects a possibly-filtered list of all domains, and return an
// array of information for each.
func (conn Connection) ListDomains(flags DomainListFlag) ([]Domain, error) {
	var cDomains []C.virDomainPtr
	domainsSH := (*reflect.SliceHeader)(unsafe.Pointer(&cDomains))

	conn.log.Printf("reading domains (flags = %v)...\n", flags)
	cRet := C.virConnectListAllDomains(conn.virConnect, (**C.virDomainPtr)(unsafe.Pointer(&domainsSH.Data)), C.uint(flags))
	ret := int32(cRet)

	if ret == -1 {
		err := LastError()
		conn.log.Printf("an error occurred: %v\n", err)
		return nil, err
	}
	defer C.free(unsafe.Pointer(domainsSH.Data))

	domainsSH.Cap = int(ret)
	domainsSH.Len = int(ret)

	domains := make([]Domain, ret)
	for i := range domains {
		domains[i] = Domain{
			log:       conn.log,
			virDomain: cDomains[i],
		}
	}

	conn.log.Printf("domains count: %v\n", ret)

	return domains, nil
}
示例#2
0
//virConnectList functions
func (h *Hypervisor) ListDomains(flags uint) ([]*Domain, error) {
	var cdomains *C.virDomainPtr
	result := C.virConnectListAllDomains(h.cptr, &cdomains, C.uint(flags))
	if result == -1 {
		return nil, GetLastError()
	}

	var domains = make([]*Domain, result)
	p := (*[1 << 30]C.virDomainPtr)(unsafe.Pointer(cdomains))

	for i := 0; i < int(result); i++ {
		domains[i] = newDomain(p[i])
	}
	defer C.free(unsafe.Pointer(cdomains))

	return domains, nil
}
示例#3
0
func (c *VirConnection) ListAllDomains(flags uint32) ([]VirDomain, error) {
	var cList *C.virDomainPtr
	numDomains := C.virConnectListAllDomains(c.ptr, (**C.virDomainPtr)(&cList), C.uint(flags))
	if numDomains == -1 {
		return nil, GetLastError()
	}
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(cList)),
		Len:  int(numDomains),
		Cap:  int(numDomains),
	}
	var domains []VirDomain
	slice := *(*[]C.virDomainPtr)(unsafe.Pointer(&hdr))
	for _, ptr := range slice {
		domains = append(domains, VirDomain{ptr})
	}
	C.free(unsafe.Pointer(cList))
	return domains, nil
}
示例#4
0
func (c *VirConnection) ListAllDomains() ([]VirDomain, error) {
	var cList *C.virDomainPtr
	/* 3 ==  VIR_CONNECT_LIST_DOMAINS_ACTIVE | VIR_CONNECT_LIST_DOMAINS_INACTIVE */
	numDomains := C.virConnectListAllDomains(c.ptr, (**C.virDomainPtr)(&cList), 3)

	if numDomains == -1 {
		return nil, errors.New(GetLastError())
	}
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(cList)),
		Len:  int(numDomains),
		Cap:  int(numDomains),
	}
	var domains []VirDomain
	slice := *(*[]C.virDomainPtr)(unsafe.Pointer(&hdr))
	for _, ptr := range slice {
		domains = append(domains, VirDomain{ptr})
	}
	C.free(unsafe.Pointer(cList))
	return domains, nil
}