2015-06-03 22:42:48 +02:00
|
|
|
package zfs_test
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-06-03 22:42:48 +02:00
|
|
|
"github.com/bicomsystems/go-libzfs"
|
2015-04-19 23:25:52 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
// HELPERS:
|
|
|
|
|
|
|
|
var TST_POOL_NAME = "TESTPOOL"
|
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 {
|
|
|
|
p, err := zfs.PoolOpen(TST_POOL_NAME)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
p.Close()
|
|
|
|
TST_POOL_NAME += "0"
|
|
|
|
}
|
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}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
var vdevs, mdevs, sdevs []zfs.VDevSpec
|
2015-04-19 23:25:52 +02:00
|
|
|
for _, d := range disks {
|
|
|
|
mdevs = append(mdevs,
|
2015-06-03 22:42:48 +02:00
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeFile, Path: d})
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
2015-06-03 22:42:48 +02:00
|
|
|
sdevs = []zfs.VDevSpec{
|
|
|
|
{Type: zfs.VDevTypeFile, Path: s3path}}
|
|
|
|
vdevs = []zfs.VDevSpec{
|
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeMirror, Devices: mdevs},
|
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeSpare, Devices: sdevs},
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
props := make(map[zfs.PoolProp]string)
|
|
|
|
fsprops := make(map[zfs.ZFSProp]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
features := make(map[string]string)
|
2015-06-03 22:42:48 +02:00
|
|
|
fsprops[zfs.ZFSPropMountpoint] = "none"
|
2015-04-19 23:25:52 +02:00
|
|
|
features["async_destroy"] = "enabled"
|
|
|
|
features["empty_bpobj"] = "enabled"
|
|
|
|
features["lz4_compress"] = "enabled"
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
pool, err := zfs.PoolCreate(TST_POOL_NAME, vdevs, 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-06-03 22:42:48 +02:00
|
|
|
println("PASS\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-06-03 22:42:48 +02:00
|
|
|
println("PASS\n")
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
func zpoolTestPoolDestroy(t *testing.T) {
|
|
|
|
println("TEST POOL Destroy( ", TST_POOL_NAME, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TST_POOL_NAME)
|
2015-04-19 23:25:52 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
if err = p.Destroy("Test of pool destroy (" + TST_POOL_NAME + ")"); err != nil {
|
|
|
|
t.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
2015-06-03 22:42:48 +02:00
|
|
|
println("PASS\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-06-03 22:42:48 +02:00
|
|
|
println("PASS\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) {
|
|
|
|
println("TEST POOL Export( ", TST_POOL_NAME, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TST_POOL_NAME)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Export(false, "Test exporting pool")
|
|
|
|
defer p.Close()
|
|
|
|
println("PASS\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestExportForce(t *testing.T) {
|
|
|
|
println("TEST POOL ExportForce( ", TST_POOL_NAME, " ) ... ")
|
|
|
|
p, err := zfs.PoolOpen(TST_POOL_NAME)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.ExportForce("Test force exporting pool")
|
|
|
|
defer p.Close()
|
|
|
|
println("PASS\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func zpoolTestImport(t *testing.T) {
|
|
|
|
println("TEST POOL Import( ", TST_POOL_NAME, " ) ... ")
|
|
|
|
p, err := zfs.PoolImport(TST_POOL_NAME, []string{"/tmp"})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer p.Close()
|
|
|
|
println("PASS\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
// 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-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-06-03 22:42:48 +02:00
|
|
|
pkey := zfs.PoolProp(key)
|
|
|
|
if pkey == zfs.PoolPropName {
|
2015-04-19 23:25:52 +02:00
|
|
|
continue // Skip name its already printed above
|
|
|
|
}
|
|
|
|
fmt.Printf("|%14s | %20s | %15s |\n", p.PropertyToName(pkey),
|
|
|
|
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"}
|
|
|
|
|
2015-06-03 22:42:48 +02:00
|
|
|
var vdevs, mdevs, sdevs []zfs.VDevSpec
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
// build mirror devices specs
|
|
|
|
for _, d := range disks {
|
|
|
|
mdevs = append(mdevs,
|
2015-06-03 22:42:48 +02:00
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeDisk, Path: d})
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// spare device specs
|
2015-06-03 22:42:48 +02:00
|
|
|
sdevs = []zfs.VDevSpec{
|
|
|
|
{Type: zfs.VDevTypeDisk, Path: "/dev/disk/by-id/ATA-789"}}
|
2015-04-19 23:25:52 +02:00
|
|
|
|
|
|
|
// pool specs
|
2015-06-03 22:42:48 +02:00
|
|
|
vdevs = []zfs.VDevSpec{
|
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeMirror, Devices: mdevs},
|
|
|
|
zfs.VDevSpec{Type: zfs.VDevTypeSpare, Devices: sdevs},
|
2015-04-19 23:25:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// pool properties
|
2015-06-03 22:42:48 +02:00
|
|
|
props := make(map[zfs.PoolProp]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
// root dataset filesystem properties
|
2015-06-03 22:42:48 +02:00
|
|
|
fsprops := make(map[zfs.ZFSProp]string)
|
2015-04-19 23:25:52 +02:00
|
|
|
// pool features
|
|
|
|
features := make(map[string]string)
|
|
|
|
|
|
|
|
// Turn off auto mounting by ZFS
|
2015-06-03 22:42:48 +02:00
|
|
|
fsprops[zfs.ZFSPropMountpoint] = "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
|
2015-06-03 22:42:48 +02:00
|
|
|
pool, err := zfs.PoolCreate("TESTPOOL", vdevs, 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)
|
|
|
|
}
|
|
|
|
}
|