Esempio n. 1
0
// ListStoragePools collects the list of storage pools, and allocates an array
// to store those objects.
// Normally, all storage pools are returned; however, "flags" can be used to
// filter the results for a smaller list of targeted pools. The valid flags are
// divided into groups, where each group contains bits that describe mutually
// exclusive attributes of a pool, and where all bits within a group describe
// all possible pools.
// The first group of "flags" is PoolListActive (online) and PoolListInactive
// (offline) to filter the pools by state.
// The second group of "flags" is PoolListPersistent (defined) and
// PoolListTransient (running but not defined), to filter the pools by whether
// they have persistent config or not.
// The third group of "flags" is PoolListAutostart and PoolListNoAutostart, to
// filter the pools by whether they are marked as autostart or not.
// The last group of "flags" is provided to filter the pools by the types, the
// flags include: PoolListDir, PoolListFS, PoolListNetFS, PoolListLogical,
// PoolListDisk, PoolListISCSI, PoolListSCSI, PoolListMPath, PoolListRBD,
// PoolListSheepdog.
func (conn Connection) ListStoragePools(flags StoragePoolListFlag) ([]StoragePool, error) {
	var cStoragePools []C.virStoragePoolPtr
	cStoragePoolsSH := (*reflect.SliceHeader)(unsafe.Pointer(&cStoragePools))

	conn.log.Printf("reading storage pools (flags = %v)...\n", flags)
	cRet := C.virConnectListAllStoragePools(conn.virConnect, (**C.virStoragePoolPtr)(unsafe.Pointer(&cStoragePoolsSH.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(cStoragePoolsSH.Data))

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

	storagePools := make([]StoragePool, ret)
	for i, cPool := range cStoragePools {
		storagePools[i] = StoragePool{
			log:            conn.log,
			virStoragePool: cPool,
		}
	}

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

	return storagePools, nil
}
Esempio n. 2
0
func (h *Hypervisor) ListStoragePools(flags uint) ([]*StoragePool, error) {
	var cpools *C.virStoragePoolPtr
	result := C.virConnectListAllStoragePools(h.cptr, &cpools, C.uint(flags))
	if result == -1 {
		return nil, GetLastError()
	}

	var pools = make([]*StoragePool, result)
	p := (*[1 << 30]C.virStoragePoolPtr)(unsafe.Pointer(cpools))

	for i := 0; i < int(result); i++ {
		pools[i] = newStoragePool(p[i])
	}
	defer C.free(unsafe.Pointer(cpools))

	return pools, nil
}
Esempio n. 3
0
func (c *VirConnection) ListAllStoragePools(flags uint32) ([]VirStoragePool, error) {
	var cList *C.virStoragePoolPtr
	numPools := C.virConnectListAllStoragePools(c.ptr, (**C.virStoragePoolPtr)(&cList), C.uint(flags))
	if numPools == -1 {
		return nil, GetLastError()
	}
	hdr := reflect.SliceHeader{
		Data: uintptr(unsafe.Pointer(cList)),
		Len:  int(numPools),
		Cap:  int(numPools),
	}
	var pools []VirStoragePool
	slice := *(*[]C.virStoragePoolPtr)(unsafe.Pointer(&hdr))
	for _, ptr := range slice {
		pools = append(pools, VirStoragePool{ptr})
	}
	C.free(unsafe.Pointer(cList))
	return pools, nil
}