Skip to content
/ go-zfs Public

Go package that enables ZFS pool and dataset management by wrapping zfs and zpool CLI commands.

License

Notifications You must be signed in to change notification settings

krystal/go-zfs

Repository files navigation

go-zfs

Go package that enables ZFS pool and dataset management by wrapping zfs and zpool CLI commands.

Go Reference GitHub tag (latest SemVer) Actions Status Coverage GitHub last commit GitHub issues GitHub pull requests License Status

Packages

The primary package used for interacting with ZFS, through the *zfs.Manager type.

import "github.com/krystal/go-zfs"

A helper package which defines a long list of string constants for most native properties available, based on OpenZFS' zfsprops manpage.

import "github.com/krystal/go-zfs/zfsprops"

A helper package which defines a long list of string constants for most zpool properties available, based on OpenZFS' zpoolprops manpage.

import "github.com/krystal/go-zfs/zpoolprops"

Usage

Create a new *zfs.Manager instance to manage ZFS pools and datasets with:

z := zfs.New()

Get details for a specific dataset:

ds, err := z.GetDataset(ctx, "tank/my-data")
fmt.Printf("Name: %s\n", ds.Name)
Name: tank/my-data

Get used dataset property via Used() helper method, which returns the value as a uint64 of bytes:

if v, ok := ds.Used(); ok {
	fmt.Printf("Used: %d bytes\n", v)
}
Used: 150470656 bytes

Get used dataset property by directly accessing the Properties map, returning a Property type which has a string Value.

if prop, ok := ds.Properties[zfsprops.Used]; ok {
	fmt.Printf("Used: %s bytes\n", prop.Value)
}
Used: 150470656 bytes

Create a new pool, using both zpoolprops and zfsprops helper packages:

err = z.CreatePool(ctx, &zfs.CreatePoolOptions{
	Name: "scratch",
	Properties: map[string]string{
		zpoolprops.Ashift:                   "12",
		zpoolprops.AutoTrim:                 "on",
		zpoolprops.Feature("async_destroy"): "enabled",
	},
	FilesystemProperties: map[string]string{
		zfsprops.Atime:       "off",
		zfsprops.CanMount:    "on",
		zfsprops.Compression: "lz4",
	},
	Mountpoint: "/mnt/scratch",
	Vdevs:      []string{"mirror", "/dev/sde", "/dev/sdf"},
})

Create and get a new dataset:

err = z.CreateDataset(ctx, &zfs.CreateDatasetOptions{
	Name: "scratch/http/cache",
	Properties: map[string]string{
		zfsprops.Compression: "off",
	},
	CreateParents: true,
})

ds, err = z.GetDataset(ctx, "scratch/http/cache")
fmt.Printf("Name: %s\n", ds.Name)
Name: scratch/http/cache

Set dataset quota to 100 GiB and turn on atime:

err = z.SetDatasetProperties(ctx, "scratch/http/cache", map[string]string{
	zfsprops.Quota: "100G",
	zfsprops.Atime: "on",
})

Documentation

Please see the Go Reference for documentation and examples.

License

MIT

About

Go package that enables ZFS pool and dataset management by wrapping zfs and zpool CLI commands.

Topics

Resources

License

Stars

Watchers

Forks