2015-06-03 22:42:48 +02:00
|
|
|
package zfs_test
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2015-12-04 23:05:19 +01:00
|
|
|
|
|
|
|
"github.com/bicomsystems/go-libzfs"
|
2015-04-19 23:25:52 +02:00
|
|
|
)
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
// HELPERS:
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
var TSTPoolName = "TESTPOOL"
|
2015-12-06 21:54:43 +01:00
|
|
|
var TSTPoolGUID string
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
func CreateTmpSparse(prefix string, size int64) (path string, err error) {
|
|
|
|
sf, err := ioutil.TempFile("/tmp", prefix)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer sf.Close()
|
|
|
|
if err = sf.Truncate(size); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path = sf.Name()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-08 23:41:22 +02:00
|
|
|
var s1path, s2path, s3path string
|
|
|
|
|
|
|
|
// This will create sparse files in tmp directory,
|
|
|
|
// for purpose of creating test pool.
|
|
|
|
func createTestpoolVdisks() (err error) {
|
|
|
|
if s1path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s2path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
|
|
|
|
// try cleanup
|
|
|
|
os.Remove(s1path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if s3path, err = CreateTmpSparse("zfs_test_", 0x140000000); err != nil {
|
|
|
|
// try cleanup
|
|
|
|
os.Remove(s1path)
|
|
|
|
os.Remove(s2path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup sparse files used for tests
|
|
|
|
func cleanupVDisks() {
|
|
|
|
// try cleanup
|
|
|
|
os.Remove(s1path)
|
|
|
|
os.Remove(s2path)
|
|
|
|
os.Remove(s3path)
|
|
|
|
}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
// TESTS:
|
|
|
|
|
|
|
|
// Create 3 sparse file in /tmp directory each 5G size, and use them to create
|
|
|
|
// mirror TESTPOOL with one spare "disk"
|
|
|
|
func zpoolTestPoolCreate(t *testing.T) {
|
|
|
|
println("TEST PoolCreate ... ")
|
|
|
|
// first check if pool with same name already exist
|
|
|
|
// we don't want conflict
|
|
|
|
for {
|
2015-12-04 23:05:19 +01:00
|
|
|
p, err := zfs.PoolOpen(TSTPoolName)
|
2015-06-03 22:42:48 +02:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.Close()
|
2015-12-04 23:05:19 +01:00
|
|
|
TSTPoolName += "0"
|
2015-06-03 22:42:48 +02:00
|
|
|
}
|
2015-04-19 23:25:52 +02:00
|
|
|
var err error
|
2015-06-08 23:41:22 +02:00
|
|
|
|
|
|
|
if err = createTestpoolVdisks(); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2015-06-08 23:41:22 +02:00
|
|
|
|
2015-04-19 23:25:52 +02:00
|
|
|
disks := [2]string{s1path, s2path}
|
|
|
|
|
2018-01-08 15:29:42 +01:00
|
|
|
var vdev zfs.VDevTree
|
2015-12-06 21:54:43 +01:00
|
|
|
var vdevs, mdevs, sdevs []zfs.VDevTree
|
2015-04-19 23:25:52 +02:00
|
|
|
for _, d := range disks {
|
|
|
|
mdevs = append(mdevs,
|
2015-12-06 21:54:43 +01:00
|
|
|
zfs.VDevTree{Type: zfs.VDevTypeFile, Path: d})
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-12-06 21:54:43 +01:00
|
|
|
sdevs = []zfs.VDevTree{
|
2015-06-03 22:42:48 +02:00
|
|
|
{Type: zfs.VDevTypeFile, Path: s3path}}
|
2015-12-06 21:54:43 +01:00
|
|
|
vdevs = []zfs.VDevTree{
|
|
|
|
zfs.VDevTree{Type: zfs.VDevTypeMirror, Devices: mdevs},
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2018-01-08 15:29:42 +01:00
|
|
|
vdev.Devices = vdevs
|
|
|
|
vdev.Spares = sdevs
|
2015-04-19 23:25:52 +02:00
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
props := make(map[zfs.Prop]string)
|
|
|
|
fsprops := make(map[zfs.Prop]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
features := make(map[string]string)
|
2015-12-04 23:05:19 +01:00
|
|
|
fsprops[zfs.DatasetPropMountpoint] = "none"
|
2017-01-16 14:05:04 +01:00
|
|
|
features["async_destroy"] = zfs.FENABLED
|
|
|
|
features["empty_bpobj"] = zfs.FENABLED
|
|
|
|
features["lz4_compress"] = zfs.FENABLED
|
2015-04-19 23:25:52 +02:00
|
|
|
|
2018-01-08 15:29:42 +01:00
|
|
|
pool, err := zfs.PoolCreate(TSTPoolName, vdev, features, props, fsprops)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
// try cleanup
|
|
|
|
os.Remove(s1path)
|
|
|
|
os.Remove(s2path)
|
|
|
|
os.Remove(s3path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer pool.Close()
|
2015-06-08 23:41:22 +02:00
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
pguid, _ := pool.GetProperty(zfs.PoolPropGUID)
|
|
|
|
TSTPoolGUID = pguid.Value
|
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open and list all pools and them state on the system
|
|
|
|
// Then list properties of last pool in the list
|
2015-06-03 22:42:48 +02:00
|
|
|
func zpoolTestPoolOpenAll(t *testing.T) {
|
2015-04-19 23:25:52 +02:00
|
|
|
println("TEST PoolOpenAll() ... ")
|
|
|
|
var pname string
|
2015-06-03 22:42:48 +02:00
|
|
|
pools, err := zfs.PoolOpenAll()
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println("\tThere is ", len(pools), " ZFS pools.")
|
|
|
|
for _, p := range pools {
|
|
|
|
pname, err = p.Name()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
p.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pstate, err := p.State()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
p.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println("\tPool: ", pname, " state: ", pstate)
|
|
|
|
p.Close()
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
func zpoolTestPoolDestroy(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST POOL Destroy( ", TSTPoolName, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TSTPoolName)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.Close()
|
2017-06-02 08:42:14 +02:00
|
|
|
if err = p.Destroy(TSTPoolName); err != nil {
|
2015-04-19 23:25:52 +02:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
func zpoolTestFailPoolOpen(t *testing.T) {
|
|
|
|
println("TEST open of non existing pool ... ")
|
2015-04-19 23:25:52 +02:00
|
|
|
pname := "fail to open this pool"
|
2015-06-03 22:42:48 +02:00
|
|
|
p, err := zfs.PoolOpen(pname)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-04-19 23:25:52 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Error("PoolOpen pass when it should fail")
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
|
2015-06-08 23:41:22 +02:00
|
|
|
func zpoolTestExport(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST POOL Export( ", TSTPoolName, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TSTPoolName)
|
2015-06-08 23:41:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Export(false, "Test exporting pool")
|
|
|
|
defer p.Close()
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-06-08 23:41:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestExportForce(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST POOL ExportForce( ", TSTPoolName, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TSTPoolName)
|
2015-06-08 23:41:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.ExportForce("Test force exporting pool")
|
|
|
|
defer p.Close()
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-06-08 23:41:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestImport(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST POOL Import( ", TSTPoolName, " ) ... ")
|
|
|
|
p, err := zfs.PoolImport(TSTPoolName, []string{"/tmp"})
|
2015-06-08 23:41:22 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.Close()
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-06-08 23:41:22 +02:00
|
|
|
}
|
|
|
|
|
2015-12-06 21:54:43 +01:00
|
|
|
func zpoolTestImportByGUID(t *testing.T) {
|
|
|
|
println("TEST POOL ImportByGUID( ", TSTPoolGUID, " ) ... ")
|
|
|
|
p, err := zfs.PoolImportByGUID(TSTPoolGUID, []string{"/tmp"})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
print("PASS\n\n")
|
|
|
|
}
|
|
|
|
|
2015-12-10 21:29:39 +01:00
|
|
|
func printVDevTree(vt zfs.VDevTree, pref string) {
|
|
|
|
first := pref + vt.Name
|
|
|
|
fmt.Printf("%-30s | %-10s | %-10s | %s\n", first, vt.Type,
|
|
|
|
vt.Stat.State.String(), vt.Path)
|
|
|
|
for _, v := range vt.Devices {
|
|
|
|
printVDevTree(v, " "+pref)
|
|
|
|
}
|
2018-01-08 15:29:42 +01:00
|
|
|
if len(vt.Spares) > 0 {
|
|
|
|
fmt.Println("spares:")
|
|
|
|
for _, v := range vt.Spares {
|
|
|
|
printVDevTree(v, " "+pref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(vt.L2Cache) > 0 {
|
|
|
|
fmt.Println("l2cache:")
|
|
|
|
for _, v := range vt.L2Cache {
|
|
|
|
printVDevTree(v, " "+pref)
|
|
|
|
}
|
|
|
|
}
|
2015-12-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestPoolImportSearch(t *testing.T) {
|
|
|
|
println("TEST PoolImportSearch")
|
|
|
|
pools, err := zfs.PoolImportSearch([]string{"/tmp"})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, p := range pools {
|
|
|
|
println()
|
|
|
|
println("---------------------------------------------------------------")
|
|
|
|
println("pool: ", p.Name)
|
|
|
|
println("guid: ", p.GUID)
|
|
|
|
println("state: ", p.State.String())
|
|
|
|
fmt.Printf("%-30s | %-10s | %-10s | %s\n", "NAME", "TYPE", "STATE", "PATH")
|
|
|
|
println("---------------------------------------------------------------")
|
|
|
|
printVDevTree(p.VDevs, "")
|
|
|
|
}
|
|
|
|
print("PASS\n\n")
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:26:35 +02:00
|
|
|
func zpoolTestPoolProp(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST PoolProp on ", TSTPoolName, " ... ")
|
|
|
|
if pool, err := zfs.PoolOpen(TSTPoolName); err == nil {
|
2015-06-09 14:26:35 +02:00
|
|
|
defer pool.Close()
|
|
|
|
// Turn on snapshot listing for pool
|
2018-04-12 11:27:31 +02:00
|
|
|
pool.SetProperty(zfs.PoolPropListsnaps, "off")
|
2015-06-09 14:26:35 +02:00
|
|
|
// Verify change is succesfull
|
2018-04-12 11:27:31 +02:00
|
|
|
if pool.Properties[zfs.PoolPropListsnaps].Value != "off" {
|
2015-06-09 14:26:35 +02:00
|
|
|
t.Error(fmt.Errorf("Update of pool property failed"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test fetching property
|
2015-12-10 21:29:39 +01:00
|
|
|
propHealth, err := pool.GetProperty(zfs.PoolPropHealth)
|
2015-06-09 14:26:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-10 21:29:39 +01:00
|
|
|
println("Pool property health: ", propHealth.Value)
|
|
|
|
|
|
|
|
propGUID, err := pool.GetProperty(zfs.PoolPropGUID)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
println("Pool property GUID: ", propGUID.Value)
|
2015-06-09 14:26:35 +02:00
|
|
|
|
2015-06-11 00:51:46 +02:00
|
|
|
// this test pool should not be bootable
|
|
|
|
prop, err := pool.GetProperty(zfs.PoolPropBootfs)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if prop.Value != "-" {
|
|
|
|
t.Errorf("Failed at bootable fs property evaluation")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-09 14:26:35 +02:00
|
|
|
// fetch all properties
|
|
|
|
if err = pool.ReloadProperties(); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-06-09 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestPoolStatusAndState(t *testing.T) {
|
2015-12-04 23:05:19 +01:00
|
|
|
println("TEST pool Status/State ( ", TSTPoolName, " ) ... ")
|
|
|
|
pool, err := zfs.PoolOpen(TSTPoolName)
|
2015-06-09 14:26:35 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer pool.Close()
|
|
|
|
|
|
|
|
if _, err = pool.Status(); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-06-11 00:51:46 +02:00
|
|
|
var pstate zfs.PoolState
|
|
|
|
if pstate, err = pool.State(); err != nil {
|
2015-06-09 14:26:35 +02:00
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2015-12-04 23:05:19 +01:00
|
|
|
println("POOL", TSTPoolName, "state:", zfs.PoolStateToName(pstate))
|
2015-06-09 14:26:35 +02:00
|
|
|
|
2015-12-04 23:05:19 +01:00
|
|
|
print("PASS\n\n")
|
2015-06-09 14:26:35 +02:00
|
|
|
}
|
|
|
|
|
2015-12-10 21:29:39 +01:00
|
|
|
func zpoolTestPoolVDevTree(t *testing.T) {
|
|
|
|
var vdevs zfs.VDevTree
|
|
|
|
println("TEST pool VDevTree ( ", TSTPoolName, " ) ... ")
|
|
|
|
pool, err := zfs.PoolOpen(TSTPoolName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer pool.Close()
|
|
|
|
vdevs, err = pool.VDevTree()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("%-30s | %-10s | %-10s | %s\n", "NAME", "TYPE", "STATE", "PATH")
|
|
|
|
println("---------------------------------------------------------------")
|
|
|
|
printVDevTree(vdevs, "")
|
|
|
|
print("PASS\n\n")
|
|
|
|
}
|
|
|
|
|
2015-06-08 23:41:22 +02:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
// EXAMPLES:
|
|
|
|
|
2015-04-19 23:25:52 +02:00
|
|
|
func ExamplePoolProp() {
|
2015-06-03 22:42:48 +02:00
|
|
|
if pool, err := zfs.PoolOpen("SSD"); err == nil {
|
|
|
|
print("Pool size is: ", pool.Properties[zfs.PoolPropSize].Value)
|
2015-04-19 23:25:52 +02:00
|
|
|
// Turn on snapshot listing for pool
|
2015-06-03 22:42:48 +02:00
|
|
|
pool.SetProperty(zfs.PoolPropListsnaps, "on")
|
2015-06-09 14:26:35 +02:00
|
|
|
println("Changed property",
|
|
|
|
zfs.PoolPropertyToName(zfs.PoolPropListsnaps), "to value:",
|
|
|
|
pool.Properties[zfs.PoolPropListsnaps].Value)
|
|
|
|
|
|
|
|
prop, err := pool.GetProperty(zfs.PoolPropHealth)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
println("Update and print out pool health:", prop.Value)
|
2015-04-19 23:25:52 +02:00
|
|
|
} else {
|
|
|
|
print("Error: ", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open and list all pools on system with them properties
|
|
|
|
func ExamplePoolOpenAll() {
|
|
|
|
// Lets open handles to all active pools on system
|
2015-06-03 22:42:48 +02:00
|
|
|
pools, err := zfs.PoolOpenAll()
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print each pool name and properties
|
|
|
|
for _, p := range pools {
|
|
|
|
// Print fancy header
|
|
|
|
fmt.Printf("\n -----------------------------------------------------------\n")
|
2015-06-03 22:42:48 +02:00
|
|
|
fmt.Printf(" POOL: %49s \n", p.Properties[zfs.PoolPropName].Value)
|
2015-04-19 23:25:52 +02:00
|
|
|
fmt.Printf("|-----------------------------------------------------------|\n")
|
|
|
|
fmt.Printf("| PROPERTY | VALUE | SOURCE |\n")
|
|
|
|
fmt.Printf("|-----------------------------------------------------------|\n")
|
|
|
|
|
|
|
|
// Iterate pool properties and print name, value and source
|
|
|
|
for key, prop := range p.Properties {
|
2015-12-04 23:05:19 +01:00
|
|
|
pkey := zfs.Prop(key)
|
2015-06-03 22:42:48 +02:00
|
|
|
if pkey == zfs.PoolPropName {
|
2015-04-19 23:25:52 +02:00
|
|
|
continue // Skip name its already printed above
|
|
|
|
}
|
2015-06-09 14:26:35 +02:00
|
|
|
fmt.Printf("|%14s | %20s | %15s |\n",
|
|
|
|
zfs.PoolPropertyToName(pkey),
|
2015-04-19 23:25:52 +02:00
|
|
|
prop.Value, prop.Source)
|
|
|
|
println("")
|
|
|
|
}
|
|
|
|
println("")
|
|
|
|
|
|
|
|
// Close pool handle and free memory, since it will not be used anymore
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePoolCreate() {
|
|
|
|
disks := [2]string{"/dev/disk/by-id/ATA-123", "/dev/disk/by-id/ATA-456"}
|
|
|
|
|
2018-01-08 15:29:42 +01:00
|
|
|
var vdev zfs.VDevTree
|
2015-12-06 21:54:43 +01:00
|
|
|
var vdevs, mdevs, sdevs []zfs.VDevTree
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
// build mirror devices specs
|
|
|
|
for _, d := range disks {
|
|
|
|
mdevs = append(mdevs,
|
2015-12-06 21:54:43 +01:00
|
|
|
zfs.VDevTree{Type: zfs.VDevTypeDisk, Path: d})
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// spare device specs
|
2015-12-06 21:54:43 +01:00
|
|
|
sdevs = []zfs.VDevTree{
|
2015-06-03 22:42:48 +02:00
|
|
|
{Type: zfs.VDevTypeDisk, Path: "/dev/disk/by-id/ATA-789"}}
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
// pool specs
|
2015-12-06 21:54:43 +01:00
|
|
|
vdevs = []zfs.VDevTree{
|
|
|
|
zfs.VDevTree{Type: zfs.VDevTypeMirror, Devices: mdevs},
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
2018-01-08 15:29:42 +01:00
|
|
|
vdev.Devices = vdevs
|
|
|
|
vdev.Spares = sdevs
|
|
|
|
|
2015-04-19 23:25:52 +02:00
|
|
|
// pool properties
|
2015-12-04 23:05:19 +01:00
|
|
|
props := make(map[zfs.Prop]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
// root dataset filesystem properties
|
2015-12-04 23:05:19 +01:00
|
|
|
fsprops := make(map[zfs.Prop]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
// pool features
|
|
|
|
features := make(map[string]string)
|
|
|
|
|
|
|
|
// Turn off auto mounting by ZFS
|
2015-12-04 23:05:19 +01:00
|
|
|
fsprops[zfs.DatasetPropMountpoint] = "none"
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
// Enable some features
|
|
|
|
features["async_destroy"] = "enabled"
|
|
|
|
features["empty_bpobj"] = "enabled"
|
|
|
|
features["lz4_compress"] = "enabled"
|
|
|
|
|
|
|
|
// Based on specs formed above create test pool as 2 disk mirror and
|
|
|
|
// one spare disk
|
2018-01-08 15:29:42 +01:00
|
|
|
pool, err := zfs.PoolCreate("TESTPOOL", vdev, features, props, fsprops)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
println("Error: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer pool.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePool_Destroy() {
|
|
|
|
pname := "TESTPOOL"
|
|
|
|
|
|
|
|
// Need handle to pool at first place
|
2015-06-03 22:42:48 +02:00
|
|
|
p, err := zfs.PoolOpen(pname)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
println("Error: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure pool handle is free after we are done here
|
|
|
|
defer p.Close()
|
|
|
|
|
|
|
|
if err = p.Destroy("Example of pool destroy (TESTPOOL)"); err != nil {
|
|
|
|
println("Error: ", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2015-06-08 23:53:06 +02:00
|
|
|
|
|
|
|
func ExamplePoolImport() {
|
|
|
|
p, err := zfs.PoolImport("TESTPOOL", []string{"/dev/disk/by-id"})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
p.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePool_Export() {
|
|
|
|
p, err := zfs.PoolOpen("TESTPOOL")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
if err = p.Export(false, "Example exporting pool"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExamplePool_ExportForce() {
|
|
|
|
p, err := zfs.PoolOpen("TESTPOOL")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
if err = p.ExportForce("Example exporting pool"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 00:51:46 +02:00
|
|
|
|
|
|
|
func ExamplePool_State() {
|
|
|
|
p, err := zfs.PoolOpen("TESTPOOL")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
pstate, err := p.State()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
println("POOL TESTPOOL state:", zfs.PoolStateToName(pstate))
|
|
|
|
}
|
2018-03-28 10:24:47 +02:00
|
|
|
|
2018-11-02 11:15:55 +01:00
|
|
|
// func TestPool_VDevTree(t *testing.T) {
|
|
|
|
// type fields struct {
|
|
|
|
// poolName string
|
|
|
|
// }
|
|
|
|
// tests := []struct {
|
|
|
|
// name string
|
|
|
|
// fields fields
|
|
|
|
// wantErr bool
|
|
|
|
// }{
|
|
|
|
// // TODO: Add test cases.
|
|
|
|
// {
|
|
|
|
// name: "test1",
|
|
|
|
// fields: fields{"TESTPOOL"},
|
|
|
|
// wantErr: false,
|
|
|
|
// },
|
|
|
|
// }
|
|
|
|
// for _, tt := range tests {
|
|
|
|
// t.Run(tt.name, func(t *testing.T) {
|
|
|
|
// pool, _ := zfs.PoolOpen(tt.fields.poolName)
|
|
|
|
// defer pool.Close()
|
|
|
|
// gotVdevs, err := pool.VDevTree()
|
|
|
|
// if (err != nil) != tt.wantErr {
|
|
|
|
// t.Errorf("Pool.VDevTree() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// jsonData, _ := json.MarshalIndent(gotVdevs, "", "\t")
|
|
|
|
// t.Logf("gotVdevs: %s", string(jsonData))
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }
|