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"
|
2017-02-23 11:29:17 +01:00
|
|
|
"fmt"
|
2016-07-26 21:14:28 +02:00
|
|
|
"unsafe"
|
2015-04-19 23:25:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
msgDatasetIsNil = "Dataset handle not initialized or its closed"
|
|
|
|
)
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetProperties type is map of dataset or volume properties prop -> value
|
|
|
|
type DatasetProperties map[Prop]string
|
|
|
|
|
|
|
|
// DatasetType defines enum of dataset types
|
2015-04-19 23:25:52 +02:00
|
|
|
type DatasetType int32
|
|
|
|
|
|
|
|
const (
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetTypeFilesystem - file system dataset
|
2015-04-19 23:25:52 +02:00
|
|
|
DatasetTypeFilesystem DatasetType = (1 << 0)
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetTypeSnapshot - snapshot of dataset
|
|
|
|
DatasetTypeSnapshot = (1 << 1)
|
|
|
|
// DatasetTypeVolume - volume (virtual block device) dataset
|
|
|
|
DatasetTypeVolume = (1 << 2)
|
|
|
|
// DatasetTypePool - pool dataset
|
|
|
|
DatasetTypePool = (1 << 3)
|
|
|
|
// DatasetTypeBookmark - bookmark dataset
|
|
|
|
DatasetTypeBookmark = (1 << 4)
|
2015-04-19 23:25:52 +02:00
|
|
|
)
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Dataset - ZFS dataset object
|
2015-04-19 23:25:52 +02:00
|
|
|
type Dataset struct {
|
2017-02-23 11:29:17 +01:00
|
|
|
list C.dataset_list_ptr
|
2015-04-19 23:25:52 +02:00
|
|
|
Type DatasetType
|
2015-12-04 23:05:19 +01:00
|
|
|
Properties map[Prop]Property
|
2015-04-19 23:25:52 +02:00
|
|
|
Children []Dataset
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dataset) openChildren() (err error) {
|
2017-02-23 11:29:17 +01:00
|
|
|
var list C.dataset_list_ptr
|
2015-04-19 23:25:52 +02:00
|
|
|
d.Children = make([]Dataset, 0, 5)
|
2017-02-23 11:29:17 +01:00
|
|
|
errcode := C.dataset_list_children(d.list.zh, unsafe.Pointer(&list))
|
|
|
|
for list != nil {
|
|
|
|
dataset := Dataset{list: list}
|
2015-04-19 23:25:52 +02:00
|
|
|
dataset.Type = DatasetType(C.zfs_get_type(dataset.list.zh))
|
2015-12-04 23:05:19 +01:00
|
|
|
dataset.Properties = make(map[Prop]Property)
|
2015-04-19 23:25:52 +02:00
|
|
|
err = dataset.ReloadProperties()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
d.Children = append(d.Children, dataset)
|
2017-02-23 11:29:17 +01:00
|
|
|
list = C.dataset_next(list)
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
for ci := range d.Children {
|
2015-04-19 23:25:52 +02:00
|
|
|
if err = d.Children[ci].openChildren(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetOpenAll recursive get handles to all available datasets on system
|
2015-04-19 23:25:52 +02:00
|
|
|
// (file-systems, volumes or snapshots).
|
|
|
|
func DatasetOpenAll() (datasets []Dataset, err error) {
|
|
|
|
var dataset Dataset
|
2017-02-23 11:29:17 +01:00
|
|
|
errcode := C.dataset_list_root(libzfsHandle, unsafe.Pointer(&dataset.list))
|
2015-04-19 23:25:52 +02:00
|
|
|
for dataset.list != nil {
|
|
|
|
dataset.Type = DatasetType(C.zfs_get_type(dataset.list.zh))
|
|
|
|
err = dataset.ReloadProperties()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
datasets = append(datasets, dataset)
|
|
|
|
dataset.list = C.dataset_next(dataset.list)
|
|
|
|
}
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
for ci := range datasets {
|
2015-04-19 23:25:52 +02:00
|
|
|
if err = datasets[ci].openChildren(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetCloseAll close all datasets in slice and all of its recursive
|
|
|
|
// children datasets
|
2015-04-19 23:25:52 +02:00
|
|
|
func DatasetCloseAll(datasets []Dataset) {
|
|
|
|
for _, d := range datasets {
|
|
|
|
d.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetOpen open dataset and all of its recursive children datasets
|
2015-04-19 23:25:52 +02:00
|
|
|
func DatasetOpen(path string) (d Dataset, err error) {
|
|
|
|
d.list = C.create_dataset_list_item()
|
2016-07-26 21:14:28 +02:00
|
|
|
csPath := C.CString(path)
|
|
|
|
d.list.zh = C.zfs_open(libzfsHandle, csPath, 0xF)
|
|
|
|
C.free(unsafe.Pointer(csPath))
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
if d.list.zh == nil {
|
|
|
|
err = LastError()
|
2017-02-23 11:29:17 +01:00
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("dataset not found.")
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
d.Type = DatasetType(C.zfs_get_type(d.list.zh))
|
2015-12-04 23:05:19 +01:00
|
|
|
d.Properties = make(map[Prop]Property)
|
2015-04-19 23:25:52 +02:00
|
|
|
err = d.ReloadProperties()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = d.openChildren()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
func datasetPropertiesTonvlist(props map[Prop]Property) (
|
2017-02-23 11:29:17 +01:00
|
|
|
cprops C.nvlist_ptr, err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
// convert properties to nvlist C type
|
2017-02-23 11:29:17 +01:00
|
|
|
r := C.nvlist_alloc(unsafe.Pointer(&cprops), C.NV_UNIQUE_NAME, 0)
|
2015-04-19 23:25:52 +02:00
|
|
|
if r != 0 {
|
|
|
|
err = errors.New("Failed to allocate properties")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for prop, value := range props {
|
2016-07-26 21:14:28 +02:00
|
|
|
csValue := C.CString(value.Value)
|
2015-04-19 23:25:52 +02:00
|
|
|
r := C.nvlist_add_string(
|
|
|
|
cprops, C.zfs_prop_to_name(
|
2016-07-26 21:14:28 +02:00
|
|
|
C.zfs_prop_t(prop)), csValue)
|
|
|
|
C.free(unsafe.Pointer(csValue))
|
2015-04-19 23:25:52 +02:00
|
|
|
if r != 0 {
|
|
|
|
err = errors.New("Failed to convert property")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetCreate create a new filesystem or volume on path representing
|
|
|
|
// pool/dataset or pool/parent/dataset
|
2015-04-19 23:25:52 +02:00
|
|
|
func DatasetCreate(path string, dtype DatasetType,
|
2015-12-04 23:05:19 +01:00
|
|
|
props map[Prop]Property) (d Dataset, err error) {
|
2017-02-23 11:29:17 +01:00
|
|
|
var cprops C.nvlist_ptr
|
2015-12-04 23:05:19 +01:00
|
|
|
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.nvlist_free(cprops)
|
|
|
|
|
2016-07-26 21:14:28 +02:00
|
|
|
csPath := C.CString(path)
|
|
|
|
errcode := C.zfs_create(libzfsHandle, csPath,
|
2015-04-19 23:25:52 +02:00
|
|
|
C.zfs_type_t(dtype), cprops)
|
2016-07-26 21:14:28 +02:00
|
|
|
C.free(unsafe.Pointer(csPath))
|
2015-04-19 23:25:52 +02:00
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Close close dataset and all its recursive children datasets (close handle
|
|
|
|
// and cleanup dataset object/s from memory)
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) Close() {
|
|
|
|
if d.list != nil && d.list.zh != nil {
|
|
|
|
C.dataset_list_close(d.list)
|
2017-02-23 11:29:17 +01:00
|
|
|
d.list = nil
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
for _, cd := range d.Children {
|
|
|
|
cd.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Destroy destroys the dataset. The caller must make sure that the filesystem
|
2015-05-07 22:59:44 +02:00
|
|
|
// isn't mounted, and that there are no active dependents. Set Defer argument
|
|
|
|
// to true to defer destruction for when dataset is not in use.
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) Destroy(Defer bool) (err error) {
|
2015-05-07 22:59:44 +02:00
|
|
|
if len(d.Children) > 0 {
|
|
|
|
path, e := d.Path()
|
|
|
|
if e != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
dsType, e := d.GetProperty(DatasetPropType)
|
2016-07-26 21:14:28 +02:00
|
|
|
if e != nil {
|
|
|
|
dsType.Value = err.Error() // just put error (why it didn't fetch property type)
|
|
|
|
}
|
2015-05-07 22:59:44 +02:00
|
|
|
err = errors.New("Cannot destroy dataset " + path +
|
|
|
|
": " + dsType.Value + " has children")
|
|
|
|
return
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list != nil {
|
2015-12-04 23:05:19 +01:00
|
|
|
if ec := C.zfs_destroy(d.list.zh, booleanT(Defer)); ec != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DestroyRecursive recursively destroy children of dataset and dataset.
|
2015-05-07 22:59:44 +02:00
|
|
|
func (d *Dataset) DestroyRecursive() (err error) {
|
|
|
|
if len(d.Children) > 0 {
|
|
|
|
for _, c := range d.Children {
|
|
|
|
if err = c.DestroyRecursive(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// close handle to destroyed child dataset
|
|
|
|
c.Close()
|
|
|
|
}
|
|
|
|
// clear closed children array
|
|
|
|
d.Children = make([]Dataset, 0)
|
|
|
|
}
|
|
|
|
err = d.Destroy(false)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Pool returns pool dataset belongs to
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) Pool() (p Pool, err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.list = C.create_zpool_list_item()
|
|
|
|
p.list.zph = C.zfs_get_pool_handle(d.list.zh)
|
|
|
|
if p.list != nil {
|
|
|
|
err = p.ReloadProperties()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// ReloadProperties re-read dataset's properties
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) ReloadProperties() (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2017-02-23 11:29:17 +01:00
|
|
|
var plist C.property_list_ptr
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
d.Properties = make(map[Prop]Property)
|
|
|
|
for prop := DatasetPropType; prop < DatasetNumProps; prop++ {
|
2017-02-23 11:29:17 +01:00
|
|
|
plist = C.new_property_list()
|
2015-04-19 23:25:52 +02:00
|
|
|
errcode := C.read_dataset_property(d.list.zh, plist, C.int(prop))
|
|
|
|
if errcode != 0 {
|
2017-02-23 11:29:17 +01:00
|
|
|
C.free_properties(plist)
|
2015-04-19 23:25:52 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
d.Properties[prop] = Property{Value: C.GoString(&(*plist).value[0]),
|
|
|
|
Source: C.GoString(&(*plist).source[0])}
|
2017-02-23 11:29:17 +01:00
|
|
|
C.free_properties(plist)
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
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 (d *Dataset) GetProperty(p Prop) (prop Property, err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2017-02-23 11:29:17 +01:00
|
|
|
var plist C.property_list_ptr
|
2015-04-19 23:25:52 +02:00
|
|
|
plist = C.new_property_list()
|
|
|
|
defer C.free_properties(plist)
|
|
|
|
errcode := C.read_dataset_property(d.list.zh, plist, C.int(p))
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prop = Property{Value: C.GoString(&(*plist).value[0]),
|
|
|
|
Source: C.GoString(&(*plist).source[0])}
|
|
|
|
d.Properties[p] = prop
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:07:17 +01:00
|
|
|
func (d *Dataset) GetUserProperty(p string) (prop Property, err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2017-02-23 11:29:17 +01:00
|
|
|
var plist C.property_list_ptr
|
2017-01-16 14:07:17 +01:00
|
|
|
plist = C.new_property_list()
|
|
|
|
defer C.free_properties(plist)
|
|
|
|
csp := C.CString(p)
|
|
|
|
defer C.free(unsafe.Pointer(csp))
|
|
|
|
errcode := C.read_user_property(d.list.zh, plist, csp)
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
prop = Property{Value: C.GoString(&(*plist).value[0]),
|
|
|
|
Source: C.GoString(&(*plist).source[0])}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// SetProperty set ZFS dataset 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 (d *Dataset) SetProperty(p Prop, value string) (err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2016-07-26 21:14:28 +02:00
|
|
|
csValue := C.CString(value)
|
2015-04-19 23:25:52 +02:00
|
|
|
errcode := C.zfs_prop_set(d.list.zh, C.zfs_prop_to_name(
|
2016-07-26 21:14:28 +02:00
|
|
|
C.zfs_prop_t(p)), csValue)
|
|
|
|
C.free(unsafe.Pointer(csValue))
|
2015-04-19 23:25:52 +02:00
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
2015-06-09 14:26:35 +02:00
|
|
|
// Update Properties member with change made
|
|
|
|
if _, err = d.GetProperty(p); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-16 14:07:17 +01:00
|
|
|
func (d *Dataset) SetUserProperty(prop, value string) (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
csValue := C.CString(value)
|
|
|
|
csProp := C.CString(prop)
|
|
|
|
errcode := C.zfs_prop_set(d.list.zh, csProp, csValue)
|
|
|
|
C.free(unsafe.Pointer(csValue))
|
|
|
|
C.free(unsafe.Pointer(csProp))
|
|
|
|
if errcode != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Clone - clones the dataset. The target must be of the same type as
|
2015-04-19 23:25:52 +02:00
|
|
|
// the source.
|
2015-12-04 23:05:19 +01:00
|
|
|
func (d *Dataset) Clone(target string, props map[Prop]Property) (rd Dataset, err error) {
|
2017-02-23 11:29:17 +01:00
|
|
|
var cprops C.nvlist_ptr
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.nvlist_free(cprops)
|
2016-07-26 21:14:28 +02:00
|
|
|
csTarget := C.CString(target)
|
|
|
|
defer C.free(unsafe.Pointer(csTarget))
|
|
|
|
if errc := C.zfs_clone(d.list.zh, csTarget, cprops); errc != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rd, err = DatasetOpen(target)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetSnapshot create dataset snapshot. Set recur to true to snapshot child datasets.
|
|
|
|
func DatasetSnapshot(path string, recur bool, props map[Prop]Property) (rd Dataset, err error) {
|
2017-02-23 11:29:17 +01:00
|
|
|
var cprops C.nvlist_ptr
|
2015-12-04 23:05:19 +01:00
|
|
|
if cprops, err = datasetPropertiesTonvlist(props); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer C.nvlist_free(cprops)
|
2016-07-26 21:14:28 +02:00
|
|
|
csPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(csPath))
|
|
|
|
if errc := C.zfs_snapshot(libzfsHandle, csPath, booleanT(recur), cprops); errc != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rd, err = DatasetOpen(path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Path return zfs dataset path/name
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) Path() (path string, err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
name := C.zfs_get_name(d.list.zh)
|
|
|
|
path = C.GoString(name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// Rollback rollabck's dataset snapshot
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) Rollback(snap *Dataset, force bool) (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if errc := C.zfs_rollback(d.list.zh,
|
2015-12-04 23:05:19 +01:00
|
|
|
snap.list.zh, booleanT(force)); errc != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-17 14:22:17 +01:00
|
|
|
// Promote promotes dataset clone
|
|
|
|
func (d *Dataset) Promote() (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if errc := C.zfs_promote(d.list.zh); errc != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-07 22:59:44 +02:00
|
|
|
// Rename dataset
|
2016-07-26 21:14:28 +02:00
|
|
|
func (d *Dataset) Rename(newName string, recur,
|
2015-12-04 23:05:19 +01:00
|
|
|
forceUnmount bool) (err error) {
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2016-07-26 21:14:28 +02:00
|
|
|
csNewName := C.CString(newName)
|
|
|
|
defer C.free(unsafe.Pointer(csNewName))
|
|
|
|
if errc := C.zfs_rename(d.list.zh, csNewName,
|
2015-12-04 23:05:19 +01:00
|
|
|
booleanT(recur), booleanT(forceUnmount)); errc != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// IsMounted checks to see if the mount is active. If the filesystem is mounted,
|
|
|
|
// sets in 'where' argument the current mountpoint, and returns true. Otherwise,
|
2015-04-19 23:25:52 +02:00
|
|
|
// returns false.
|
|
|
|
func (d *Dataset) IsMounted() (mounted bool, where string) {
|
2017-02-23 11:29:17 +01:00
|
|
|
var cw C.char_ptr
|
2015-04-19 23:25:52 +02:00
|
|
|
if d.list == nil {
|
|
|
|
return false, ""
|
|
|
|
}
|
2017-02-23 11:29:17 +01:00
|
|
|
m := C.zfs_is_mounted(d.list.zh, unsafe.Pointer(&cw))
|
|
|
|
// defer C.free(cw)
|
2015-04-19 23:25:52 +02:00
|
|
|
if m != 0 {
|
|
|
|
return true, C.GoString(cw)
|
|
|
|
}
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mount the given filesystem.
|
|
|
|
func (d *Dataset) Mount(options string, flags int) (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
2016-07-26 21:14:28 +02:00
|
|
|
csOptions := C.CString(options)
|
|
|
|
defer C.free(unsafe.Pointer(csOptions))
|
|
|
|
if ec := C.zfs_mount(d.list.zh, csOptions, C.int(flags)); ec != 0 {
|
2015-04-19 23:25:52 +02:00
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount the given filesystem.
|
|
|
|
func (d *Dataset) Unmount(flags int) (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ec := C.zfs_unmount(d.list.zh, nil, C.int(flags)); ec != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// UnmountAll unmount this filesystem and any children inheriting the
|
|
|
|
// mountpoint property.
|
2015-04-19 23:25:52 +02:00
|
|
|
func (d *Dataset) UnmountAll(flags int) (err error) {
|
|
|
|
if d.list == nil {
|
|
|
|
err = errors.New(msgDatasetIsNil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if ec := C.zfs_unmountall(d.list.zh, C.int(flags)); ec != 0 {
|
|
|
|
err = LastError()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
// DatasetPropertyToName 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 DatasetPropertyToName(p Prop) (name string) {
|
|
|
|
if p == DatasetNumProps {
|
2015-04-19 23:25:52 +02:00
|
|
|
return "numofprops"
|
|
|
|
}
|
|
|
|
prop := C.zfs_prop_t(p)
|
|
|
|
name = C.GoString(C.zfs_prop_to_name(prop))
|
|
|
|
return
|
|
|
|
}
|