2015-04-19 23:25:52 +02:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <libzfs.h>
|
|
|
|
// #include "zpool.h"
|
|
|
|
// #include "zfs.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2015-12-04 23:05:19 +01:00
|
|
|
"time"
|
2015-04-19 23:25:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
msgPoolIsNil = "Pool handle not initialized or its closed"
|
|
|
|
)
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolProperties type is map of pool properties name -> value
|
|
|
|
type PoolProperties map[Prop]string
|
2015-04-19 23:25:52 +02:00
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
// VDevTree ZFS virtual device tree
|
|
|
|
type VDevTree struct {
|
|
|
|
Type VDevType
|
|
|
|
Devices []VDevTree // groups other devices (e.g. mirror)
|
|
|
|
Parity uint
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Pool object represents handler to single ZFS pool
|
2015-04-19 23:25:52 +02:00
|
|
|
//
|
|
|
|
/* Pool.Properties map[string]Property
|
|
|
|
*/
|
|
|
|
// Map of all ZFS pool properties, changing any of this will not affect ZFS
|
|
|
|
// pool, for that use SetProperty( name, value string) method of the pool
|
|
|
|
// object. This map is initial loaded when ever you open or create pool to
|
|
|
|
// give easy access to listing all available properties. It can be refreshed
|
|
|
|
// with up to date values with call to (*Pool) ReloadProperties
|
|
|
|
type Pool struct {
|
|
|
|
list *C.zpool_list_t
|
|
|
|
Properties []Property
|
|
|
|
Features map[string]string
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolOpen open ZFS pool handler by name.
|
2015-04-19 23:25:52 +02:00
|
|
|
// Returns Pool object, requires Pool.Close() to be called explicitly
|
|
|
|
// for memory cleanup after object is not needed anymore.
|
|
|
|
func PoolOpen(name string) (pool Pool, err error) {
|
2015-12-04 23:05:19 +01:00
|
|
|
pool.list = C.zpool_list_open(libzfsHandle, C.CString(name))
|
2015-04-19 23:25:52 +02:00
|
|
|
if pool.list != nil {
|
|
|
|
err = pool.ReloadProperties()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
func poolSearchImport(q string, searchpaths []string, guid bool) (name string,
|
|
|
|
err error) {
|
|
|
|
var config *C.nvlist_t
|
|
|
|
var cname *C.char
|
|
|
|
config = nil
|
2015-04-19 23:25:52 +02:00
|
|
|
errPoolList := errors.New("Failed to list pools")
|
|
|
|
var elem *C.nvpair_t
|
|
|
|
numofp := len(searchpaths)
|
|
|
|
cpaths := C.alloc_strings(C.int(numofp))
|
|
|
|
for i, path := range searchpaths {
|
|
|
|
C.strings_setat(cpaths, C.int(i), C.CString(path))
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
pools := C.zpool_find_import(libzfsHandle, C.int(numofp), cpaths)
|
2015-04-19 23:25:52 +02:00
|
|
|
defer C.nvlist_free(pools)
|
|
|
|
|
|
|
|
elem = C.nvlist_next_nvpair(pools, elem)
|
|
|
|
for ; elem != nil; elem = C.nvlist_next_nvpair(pools, elem) {
|
2015-12-06 21:54:43 +01:00
|
|
|
var cq *C.char
|
2015-04-19 23:25:52 +02:00
|
|
|
var tconfig *C.nvlist_t
|
|
|
|
retcode := C.nvpair_value_nvlist(elem, &tconfig)
|
|
|
|
if retcode != 0 {
|
|
|
|
err = errPoolList
|
|
|
|
return
|
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
if guid {
|
|
|
|
var iguid C.uint64_t
|
|
|
|
if retcode = C.nvlist_lookup_uint64(tconfig,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_POOL_GUID), &iguid); retcode != 0 {
|
|
|
|
err = errPoolList
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sguid := fmt.Sprint(iguid)
|
|
|
|
if q == sguid {
|
|
|
|
config = tconfig
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if retcode = C.nvlist_lookup_string(tconfig,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_POOL_NAME), &cq); retcode != 0 {
|
|
|
|
err = errPoolList
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cname = cq
|
|
|
|
name = C.GoString(cq)
|
|
|
|
if q == name {
|
|
|
|
config = tconfig
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if config == nil {
|
|
|
|
err = fmt.Errorf("No pool found %s", q)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if guid {
|
|
|
|
// We need to get name so we can open pool by name
|
|
|
|
if retcode := C.nvlist_lookup_string(config,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_POOL_NAME), &cname); retcode != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = errPoolList
|
|
|
|
return
|
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
name = C.GoString(cname)
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
if retcode := C.zpool_import(libzfsHandle, config, cname,
|
|
|
|
nil); retcode != 0 {
|
|
|
|
err = LastError()
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
return
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
// PoolImport given a list of directories to search, find and import pool with matching
|
|
|
|
// name stored on disk.
|
|
|
|
func PoolImport(name string, searchpaths []string) (pool Pool, err error) {
|
|
|
|
_, err = poolSearchImport(name, searchpaths, false)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pool, err = PoolOpen(name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// PoolImportByGUID given a list of directories to search, find and import pool
|
|
|
|
// with matching GUID stored on disk.
|
|
|
|
func PoolImportByGUID(guid string, searchpaths []string) (pool Pool, err error) {
|
|
|
|
var name string
|
|
|
|
name, err = poolSearchImport(guid, searchpaths, true)
|
|
|
|
if err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
pool, err = PoolOpen(name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
// func PoolList(paths []string, cache string) (pools []Pool, err error) {
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolOpenAll open all active ZFS pools on current system.
|
2015-04-19 23:25:52 +02:00
|
|
|
// Returns array of Pool handlers, each have to be closed after not needed
|
|
|
|
// anymore. Call Pool.Close() method.
|
|
|
|
func PoolOpenAll() (pools []Pool, err error) {
|
|
|
|
var pool Pool
|
2015-12-04 23:05:19 +01:00
|
|
|
errcode := C.zpool_list(libzfsHandle, &pool.list)
|
2015-04-19 23:25:52 +02:00
|
|
|
for pool.list != nil {
|
|
|
|
err = pool.ReloadProperties()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pools = append(pools, pool)
|
|
|
|
pool.list = C.zpool_next(pool.list)
|
|
|
|
}
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolCloseAll close all pools in given slice
|
2015-04-19 23:25:52 +02:00
|
|
|
func PoolCloseAll(pools []Pool) {
|
|
|
|
for _, p := range pools {
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolPropertyToName convert property to name
|
2015-04-19 23:25:52 +02:00
|
|
|
// ( returns built in string representation of property name).
|
|
|
|
// This is optional, you can represent each property with string
|
|
|
|
// name of choice.
|
2015-12-04 23:05:19 +01:00
|
|
|
func PoolPropertyToName(p Prop) (name string) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if p == PoolNumProps {
|
|
|
|
return "numofprops"
|
|
|
|
}
|
|
|
|
prop := C.zpool_prop_t(p)
|
|
|
|
name = C.GoString(C.zpool_prop_to_name(prop))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolStateToName maps POOL STATE to string.
|
2015-06-11 00:51:46 +02:00
|
|
|
func PoolStateToName(state PoolState) (name string) {
|
|
|
|
ps := C.pool_state_t(state)
|
|
|
|
name = C.GoString(C.zpool_pool_state_to_name(ps))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// ReloadProperties re-read ZFS pool properties and features, refresh
|
|
|
|
// Pool.Properties and Pool.Features map
|
2015-04-19 23:25:52 +02:00
|
|
|
func (pool *Pool) ReloadProperties() (err error) {
|
|
|
|
propList := C.read_zpool_properties(pool.list.zph)
|
|
|
|
if propList == nil {
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.Properties = make([]Property, PoolNumProps+1)
|
|
|
|
next := propList
|
|
|
|
for next != nil {
|
|
|
|
pool.Properties[next.property] = Property{Value: C.GoString(&(next.value[0])), Source: C.GoString(&(next.source[0]))}
|
|
|
|
next = C.next_property(next)
|
|
|
|
}
|
|
|
|
C.free_properties(propList)
|
|
|
|
|
|
|
|
// read features
|
|
|
|
pool.Features = map[string]string{
|
2015-12-06 21:54:43 +01:00
|
|
|
"async_destroy": "disabled",
|
|
|
|
"empty_bpobj": "disabled",
|
|
|
|
"lz4_compress": "disabled",
|
|
|
|
"spacemap_histogram": "disabled",
|
|
|
|
"enabled_txg": "disabled",
|
|
|
|
"hole_birth": "disabled",
|
|
|
|
"extensible_dataset": "disabled",
|
|
|
|
"embedded_data": "disabled",
|
|
|
|
"bookmarks": "disabled",
|
|
|
|
"filesystem_limits": "disabled",
|
|
|
|
"large_blocks": "disabled"}
|
2015-12-04 23:05:19 +01:00
|
|
|
for name := range pool.Features {
|
2015-04-19 23:25:52 +02:00
|
|
|
pool.GetFeature(name)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// GetProperty reload and return single specified property. This also reloads requested
|
2015-04-19 23:25:52 +02:00
|
|
|
// property in Properties map.
|
2015-12-04 23:05:19 +01:00
|
|
|
func (pool *Pool) GetProperty(p Prop) (prop Property, err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if pool.list != nil {
|
|
|
|
// First check if property exist at all
|
|
|
|
if p < PoolPropName || p > PoolNumProps {
|
|
|
|
err = errors.New(fmt.Sprint("Unknown zpool property: ",
|
2015-06-09 14:26:35 +02:00
|
|
|
PoolPropertyToName(p)))
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
var list C.property_list_t
|
|
|
|
r := C.read_zpool_property(pool.list.zph, &list, C.int(p))
|
|
|
|
if r != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
prop.Value = C.GoString(&(list.value[0]))
|
|
|
|
prop.Source = C.GoString(&(list.source[0]))
|
|
|
|
pool.Properties[p] = prop
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return prop, errors.New(msgPoolIsNil)
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// GetFeature reload and return single specified feature. This also reloads requested
|
2015-04-19 23:25:52 +02:00
|
|
|
// feature in Features map.
|
|
|
|
func (pool *Pool) GetFeature(name string) (value string, err error) {
|
|
|
|
var fvalue [512]C.char
|
|
|
|
sname := fmt.Sprint("feature@", name)
|
|
|
|
r := C.zpool_prop_get_feature(pool.list.zph, C.CString(sname), &(fvalue[0]), 512)
|
|
|
|
if r != 0 {
|
|
|
|
err = errors.New(fmt.Sprint("Unknown zpool feature: ", name))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
value = C.GoString(&(fvalue[0]))
|
|
|
|
pool.Features[name] = value
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// SetProperty set ZFS pool property to value. Not all properties can be set,
|
2015-04-19 23:25:52 +02:00
|
|
|
// some can be set only at creation time and some are read only.
|
|
|
|
// Always check if returned error and its description.
|
2015-12-04 23:05:19 +01:00
|
|
|
func (pool *Pool) SetProperty(p Prop, value string) (err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if pool.list != nil {
|
|
|
|
// First check if property exist at all
|
|
|
|
if p < PoolPropName || p > PoolNumProps {
|
|
|
|
err = errors.New(fmt.Sprint("Unknown zpool property: ",
|
2015-06-09 14:26:35 +02:00
|
|
|
PoolPropertyToName(p)))
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
2015-06-09 14:26:35 +02:00
|
|
|
r := C.zpool_set_prop(pool.list.zph, C.CString(PoolPropertyToName(p)), C.CString(value))
|
2015-04-19 23:25:52 +02:00
|
|
|
if r != 0 {
|
|
|
|
err = LastError()
|
|
|
|
} else {
|
|
|
|
// Update Properties member with change made
|
2015-06-09 14:26:35 +02:00
|
|
|
if _, err = pool.GetProperty(p); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return errors.New(msgPoolIsNil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close ZFS pool handler and release associated memory.
|
|
|
|
// Do not use Pool object after this.
|
|
|
|
func (pool *Pool) Close() {
|
|
|
|
C.zpool_list_close(pool.list)
|
|
|
|
pool.list = nil
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Name get (re-read) ZFS pool name property
|
2015-04-19 23:25:52 +02:00
|
|
|
func (pool *Pool) Name() (name string, err error) {
|
|
|
|
if pool.list == nil {
|
|
|
|
err = errors.New(msgPoolIsNil)
|
|
|
|
} else {
|
|
|
|
name = C.GoString(C.zpool_get_name(pool.list.zph))
|
|
|
|
pool.Properties[PoolPropName] = Property{Value: name, Source: "none"}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// State get ZFS pool state
|
2015-04-19 23:25:52 +02:00
|
|
|
// Return the state of the pool (ACTIVE or UNAVAILABLE)
|
|
|
|
func (pool *Pool) State() (state PoolState, err error) {
|
|
|
|
if pool.list == nil {
|
|
|
|
err = errors.New(msgPoolIsNil)
|
|
|
|
} else {
|
|
|
|
state = PoolState(C.zpool_read_state(pool.list.zph))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
func (vdev *VDevTree) isGrouping() (grouping bool, mindevs, maxdevs int) {
|
2015-04-19 23:25:52 +02:00
|
|
|
maxdevs = int(^uint(0) >> 1)
|
2015-12-04 23:05:19 +01:00
|
|
|
if vdev.Type == VDevTypeRaidz {
|
2015-04-19 23:25:52 +02:00
|
|
|
grouping = true
|
2015-12-04 23:05:19 +01:00
|
|
|
if vdev.Parity == 0 {
|
|
|
|
vdev.Parity = 1
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
if vdev.Parity > 254 {
|
|
|
|
vdev.Parity = 254
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
mindevs = int(vdev.Parity) + 1
|
2015-04-19 23:25:52 +02:00
|
|
|
maxdevs = 255
|
2015-12-04 23:05:19 +01:00
|
|
|
} else if vdev.Type == VDevTypeMirror {
|
2015-04-19 23:25:52 +02:00
|
|
|
grouping = true
|
|
|
|
mindevs = 2
|
2015-12-04 23:05:19 +01:00
|
|
|
} else if vdev.Type == VDevTypeLog || vdev.Type == VDevTypeSpare || vdev.Type == VDevTypeL2cache {
|
2015-04-19 23:25:52 +02:00
|
|
|
grouping = true
|
|
|
|
mindevs = 1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
func (vdev *VDevTree) isLog() (r C.uint64_t) {
|
2015-04-19 23:25:52 +02:00
|
|
|
r = 0
|
2015-12-04 23:05:19 +01:00
|
|
|
if vdev.Type == VDevTypeLog {
|
2015-04-19 23:25:52 +02:00
|
|
|
r = 1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func toCPoolProperties(props PoolProperties) (cprops *C.nvlist_t) {
|
|
|
|
cprops = nil
|
|
|
|
for prop, value := range props {
|
|
|
|
name := C.zpool_prop_to_name(C.zpool_prop_t(prop))
|
|
|
|
r := C.add_prop_list(name, C.CString(value), &cprops, C.boolean_t(1))
|
|
|
|
if r != 0 {
|
|
|
|
if cprops != nil {
|
|
|
|
C.nvlist_free(cprops)
|
|
|
|
cprops = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
func toCDatasetProperties(props DatasetProperties) (cprops *C.nvlist_t) {
|
2015-04-19 23:25:52 +02:00
|
|
|
cprops = nil
|
|
|
|
for prop, value := range props {
|
|
|
|
name := C.zfs_prop_to_name(C.zfs_prop_t(prop))
|
|
|
|
r := C.add_prop_list(name, C.CString(value), &cprops, C.boolean_t(0))
|
|
|
|
if r != 0 {
|
|
|
|
if cprops != nil {
|
|
|
|
C.nvlist_free(cprops)
|
|
|
|
cprops = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
func buildVDevTree(root *C.nvlist_t, rtype VDevType, vdevs []VDevTree,
|
2015-04-19 23:25:52 +02:00
|
|
|
props PoolProperties) (err error) {
|
|
|
|
count := len(vdevs)
|
|
|
|
if count == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
childrens := C.nvlist_alloc_array(C.int(count))
|
|
|
|
if childrens == nil {
|
|
|
|
err = errors.New("No enough memory")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.nvlist_free_array(childrens)
|
|
|
|
spares := C.nvlist_alloc_array(C.int(count))
|
|
|
|
if childrens == nil {
|
|
|
|
err = errors.New("No enough memory")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nspares := 0
|
|
|
|
defer C.nvlist_free_array(spares)
|
|
|
|
l2cache := C.nvlist_alloc_array(C.int(count))
|
|
|
|
if childrens == nil {
|
|
|
|
err = errors.New("No enough memory")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
nl2cache := 0
|
|
|
|
defer C.nvlist_free_array(l2cache)
|
|
|
|
for i, vdev := range vdevs {
|
|
|
|
grouping, mindevs, maxdevs := vdev.isGrouping()
|
2015-12-04 23:05:19 +01:00
|
|
|
var child *C.nvlist_t
|
2015-04-19 23:25:52 +02:00
|
|
|
// fmt.Println(vdev.Type)
|
|
|
|
if r := C.nvlist_alloc(&child, C.NV_UNIQUE_NAME, 0); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vcount := len(vdev.Devices)
|
|
|
|
if vcount < mindevs || vcount > maxdevs {
|
2015-12-04 23:05:19 +01:00
|
|
|
err = fmt.Errorf(
|
|
|
|
"Invalid vdev specification: %s supports no less than %d or more than %d devices",
|
|
|
|
vdev.Type, mindevs, maxdevs)
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if r := C.nvlist_add_string(child, C.CString(C.ZPOOL_CONFIG_TYPE),
|
|
|
|
C.CString(string(vdev.Type))); r != 0 {
|
|
|
|
err = errors.New("Failed to set vdev type")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r := C.nvlist_add_uint64(child, C.CString(C.ZPOOL_CONFIG_IS_LOG),
|
|
|
|
vdev.isLog()); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev (is_log)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if grouping {
|
|
|
|
if vdev.Type == VDevTypeRaidz {
|
|
|
|
r := C.nvlist_add_uint64(child,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_NPARITY),
|
|
|
|
C.uint64_t(mindevs-1))
|
|
|
|
if r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev (parity)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
if err = buildVDevTree(child, vdev.Type, vdev.Devices,
|
2015-04-19 23:25:52 +02:00
|
|
|
props); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// if vdev.Type == VDevTypeDisk {
|
|
|
|
if r := C.nvlist_add_uint64(child,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_WHOLE_DISK), 1); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev child (whdisk)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
if len(vdev.Path) > 0 {
|
|
|
|
if r := C.nvlist_add_string(
|
|
|
|
child, C.CString(C.ZPOOL_CONFIG_PATH),
|
|
|
|
C.CString(vdev.Path)); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev child (type)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ashift, _ := strconv.Atoi(props[PoolPropAshift])
|
|
|
|
if ashift > 0 {
|
|
|
|
if r := C.nvlist_add_uint64(child,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_ASHIFT),
|
|
|
|
C.uint64_t(ashift)); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev child (ashift)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if vdev.Type == VDevTypeSpare {
|
|
|
|
C.nvlist_array_set(spares, C.int(nspares), child)
|
|
|
|
nspares++
|
|
|
|
count--
|
|
|
|
continue
|
|
|
|
} else if vdev.Type == VDevTypeL2cache {
|
|
|
|
C.nvlist_array_set(l2cache, C.int(nl2cache), child)
|
|
|
|
nl2cache++
|
|
|
|
count--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
C.nvlist_array_set(childrens, C.int(i), child)
|
|
|
|
}
|
|
|
|
if count > 0 {
|
|
|
|
if r := C.nvlist_add_nvlist_array(root,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_CHILDREN), childrens,
|
|
|
|
C.uint_t(count)); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev children")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// fmt.Println("childs", root, count, rtype)
|
|
|
|
// debug.PrintStack()
|
|
|
|
}
|
|
|
|
if nl2cache > 0 {
|
|
|
|
if r := C.nvlist_add_nvlist_array(root,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_L2CACHE), l2cache,
|
|
|
|
C.uint_t(nl2cache)); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev cache")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if nspares > 0 {
|
|
|
|
if r := C.nvlist_add_nvlist_array(root,
|
|
|
|
C.CString(C.ZPOOL_CONFIG_SPARES), spares,
|
|
|
|
C.uint_t(nspares)); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate vdev spare")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// fmt.Println("spares", root, count)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// PoolCreate create ZFS pool per specs, features and properties of pool and root dataset
|
2015-12-06 21:54:43 +01:00
|
|
|
func PoolCreate(name string, vdevs []VDevTree, features map[string]string,
|
2015-12-04 23:05:19 +01:00
|
|
|
props PoolProperties, fsprops DatasetProperties) (pool Pool, err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
// create root vdev nvroot
|
2015-12-04 23:05:19 +01:00
|
|
|
var nvroot *C.nvlist_t
|
2015-04-19 23:25:52 +02:00
|
|
|
if r := C.nvlist_alloc(&nvroot, C.NV_UNIQUE_NAME, 0); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate root vdev")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r := C.nvlist_add_string(nvroot, C.CString(C.ZPOOL_CONFIG_TYPE),
|
|
|
|
C.CString(string(VDevTypeRoot))); r != 0 {
|
|
|
|
err = errors.New("Failed to allocate root vdev")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.nvlist_free(nvroot)
|
|
|
|
|
|
|
|
// Now we need to build specs (vdev hierarchy)
|
2015-12-06 21:54:43 +01:00
|
|
|
if err = buildVDevTree(nvroot, VDevTypeRoot, vdevs, props); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert properties
|
|
|
|
cprops := toCPoolProperties(props)
|
|
|
|
if cprops != nil {
|
|
|
|
defer C.nvlist_free(cprops)
|
|
|
|
} else if len(props) > 0 {
|
|
|
|
err = errors.New("Failed to allocate pool properties")
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
cfsprops := toCDatasetProperties(fsprops)
|
2015-04-19 23:25:52 +02:00
|
|
|
if cfsprops != nil {
|
|
|
|
defer C.nvlist_free(cfsprops)
|
|
|
|
} else if len(fsprops) > 0 {
|
|
|
|
err = errors.New("Failed to allocate FS properties")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for fname, fval := range features {
|
|
|
|
sfname := fmt.Sprintf("feature@%s", fname)
|
|
|
|
r := C.add_prop_list(C.CString(sfname), C.CString(fval), &cprops,
|
|
|
|
C.boolean_t(1))
|
|
|
|
if r != 0 {
|
|
|
|
if cprops != nil {
|
|
|
|
C.nvlist_free(cprops)
|
|
|
|
cprops = nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create actual pool then open
|
2015-12-04 23:05:19 +01:00
|
|
|
if r := C.zpool_create(libzfsHandle, C.CString(name), nvroot,
|
2015-04-19 23:25:52 +02:00
|
|
|
cprops, cfsprops); r != 0 {
|
|
|
|
err = LastError()
|
2015-12-04 23:05:19 +01:00
|
|
|
err = errors.New(err.Error() + " (zpool_create)")
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
|
|
|
|
// It can happen that pool is not immediately available,
|
|
|
|
// we know we just created it with success so lets wait and retry
|
|
|
|
// but only in case EZFS_NOENT error
|
|
|
|
retr := 0
|
|
|
|
for pool, err = PoolOpen(name); err != nil && retr < 3; retr++ {
|
|
|
|
errno := C.libzfs_errno(libzfsHandle)
|
|
|
|
if errno == ENoent {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
err = errors.New(err.Error() + " (PoolOpen)")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pool, err = PoolOpen(name)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
err = errors.New(err.Error() + " (PoolOpen)")
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Status get pool status. Let you check if pool healthy.
|
2015-04-19 23:25:52 +02:00
|
|
|
func (pool *Pool) Status() (status PoolStatus, err error) {
|
|
|
|
var msgid *C.char
|
|
|
|
var reason C.zpool_status_t
|
|
|
|
var errata C.zpool_errata_t
|
|
|
|
if pool.list == nil {
|
|
|
|
err = errors.New(msgPoolIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reason = C.zpool_get_status(pool.list.zph, &msgid, &errata)
|
|
|
|
status = PoolStatus(reason)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the pool. It is up to the caller to ensure that there are no
|
|
|
|
// datasets left in the pool. logStr is optional if specified it is
|
|
|
|
// appended to ZFS history
|
|
|
|
func (pool *Pool) Destroy(logStr string) (err error) {
|
|
|
|
if pool.list == nil {
|
|
|
|
err = errors.New(msgPoolIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
retcode := C.zpool_destroy(pool.list.zph, C.CString(logStr))
|
|
|
|
if retcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Export exports the pool from the system.
|
2015-04-19 23:25:52 +02:00
|
|
|
// Before exporting the pool, all datasets within the pool are unmounted.
|
|
|
|
// A pool can not be exported if it has a shared spare that is currently
|
|
|
|
// being used.
|
2015-06-08 23:41:22 +02:00
|
|
|
func (pool *Pool) Export(force bool, log string) (err error) {
|
2015-12-04 23:05:19 +01:00
|
|
|
var forcet C.boolean_t
|
2015-04-19 23:25:52 +02:00
|
|
|
if force {
|
2015-12-04 23:05:19 +01:00
|
|
|
forcet = 1
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
if rc := C.zpool_export(pool.list.zph, forcet, C.CString(log)); rc != 0 {
|
2015-06-08 23:41:22 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// ExportForce hard force export of the pool from the system.
|
2015-06-08 23:41:22 +02:00
|
|
|
func (pool *Pool) ExportForce(log string) (err error) {
|
|
|
|
if rc := C.zpool_export_force(pool.list.zph, C.CString(log)); rc != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|