zfs: refactor error handling
thanks to @calavera Signed-off-by: Jörg Thalheim <joerg@higgsboson.tk>
This commit is contained in:
parent
a25195d86c
commit
dd614b5e34
|
@ -1,10 +1,12 @@
|
||||||
package zfs
|
package zfs
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#include <locale.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <mntent.h>
|
#include <mntent.h>
|
||||||
|
|
||||||
|
const char* PROC_MOUNTS = "/proc/mounts";
|
||||||
|
const char* OPEN_MODE = "r";
|
||||||
*/
|
*/
|
||||||
import "C"
|
import "C"
|
||||||
|
|
||||||
|
@ -43,10 +45,10 @@ func (*Logger) Log(cmd []string) {
|
||||||
func Init(base string, opt []string) (graphdriver.Driver, error) {
|
func Init(base string, opt []string) (graphdriver.Driver, error) {
|
||||||
var err error
|
var err error
|
||||||
options, err := parseOptions(opt)
|
options, err := parseOptions(opt)
|
||||||
options.mountPath = base
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
options.mountPath = base
|
||||||
|
|
||||||
rootdir := path.Dir(base)
|
rootdir := path.Dir(base)
|
||||||
|
|
||||||
|
@ -58,14 +60,14 @@ func Init(base string, opt []string) (graphdriver.Driver, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := exec.LookPath("zfs"); err != nil {
|
if _, err := exec.LookPath("zfs"); err != nil {
|
||||||
return nil, fmt.Errorf("zfs command is not available")
|
return nil, fmt.Errorf("zfs command is not available: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.OpenFile("/dev/zfs", os.O_RDWR, 600)
|
file, err := os.OpenFile("/dev/zfs", os.O_RDWR, 600)
|
||||||
defer file.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to initialize: %v", err)
|
return nil, fmt.Errorf("cannot open /dev/zfs: %v", err)
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
if options.fsName == "" {
|
if options.fsName == "" {
|
||||||
options.fsName, err = lookupZfsDataset(rootdir)
|
options.fsName, err = lookupZfsDataset(rootdir)
|
||||||
|
@ -74,8 +76,7 @@ func Init(base string, opt []string) (graphdriver.Driver, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger := Logger{}
|
zfs.SetLogger(new(Logger))
|
||||||
zfs.SetLogger(&logger)
|
|
||||||
|
|
||||||
dataset, err := zfs.GetDataset(options.fsName)
|
dataset, err := zfs.GetDataset(options.fsName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -102,7 +103,7 @@ func parseOptions(opt []string) (ZfsOptions, error) {
|
||||||
case "zfs.fsname":
|
case "zfs.fsname":
|
||||||
options.fsName = val
|
options.fsName = val
|
||||||
default:
|
default:
|
||||||
return options, fmt.Errorf("Unknown option %s\n", key)
|
return options, fmt.Errorf("Unknown option %s", key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return options, nil
|
return options, nil
|
||||||
|
@ -111,7 +112,7 @@ func parseOptions(opt []string) (ZfsOptions, error) {
|
||||||
func checkRootdirFs(rootdir string) error {
|
func checkRootdirFs(rootdir string) error {
|
||||||
var buf syscall.Statfs_t
|
var buf syscall.Statfs_t
|
||||||
if err := syscall.Statfs(rootdir, &buf); err != nil {
|
if err := syscall.Statfs(rootdir, &buf); err != nil {
|
||||||
fmt.Errorf("Failed to access '%s': %s", rootdir, err)
|
return fmt.Errorf("Failed to access '%s': %s", rootdir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicZfs {
|
if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicZfs {
|
||||||
|
@ -121,27 +122,24 @@ func checkRootdirFs(rootdir string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var CprocMounts = C.CString("/proc/mounts")
|
|
||||||
var CopenMod = C.CString("r")
|
|
||||||
|
|
||||||
func lookupZfsDataset(rootdir string) (string, error) {
|
func lookupZfsDataset(rootdir string) (string, error) {
|
||||||
var stat syscall.Stat_t
|
var stat syscall.Stat_t
|
||||||
var Cmnt C.struct_mntent
|
|
||||||
var Cfp *C.FILE
|
|
||||||
buf := string(make([]byte, 256, 256))
|
|
||||||
Cbuf := C.CString(buf)
|
|
||||||
defer free(Cbuf)
|
|
||||||
|
|
||||||
if err := syscall.Stat(rootdir, &stat); err != nil {
|
if err := syscall.Stat(rootdir, &stat); err != nil {
|
||||||
return "", fmt.Errorf("Failed to access '%s': %s", rootdir, err)
|
return "", fmt.Errorf("Failed to access '%s': %s", rootdir, err)
|
||||||
}
|
}
|
||||||
wantedDev := stat.Dev
|
wantedDev := stat.Dev
|
||||||
|
|
||||||
if Cfp = C.setmntent(CprocMounts, CopenMod); Cfp == nil {
|
Cfp, err := C.setmntent(C.PROC_MOUNTS, C.OPEN_MODE)
|
||||||
return "", fmt.Errorf("Failed to open /proc/mounts")
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("Failed to open /proc/mounts: %v", err)
|
||||||
}
|
}
|
||||||
defer C.endmntent(Cfp)
|
defer C.endmntent(Cfp)
|
||||||
|
|
||||||
|
var Cmnt C.struct_mntent
|
||||||
|
buf := string(make([]byte, 256, 256))
|
||||||
|
Cbuf := C.CString(buf)
|
||||||
|
defer C.free(unsafe.Pointer(Cbuf))
|
||||||
|
|
||||||
for C.getmntent_r(Cfp, &Cmnt, Cbuf, 256) != nil {
|
for C.getmntent_r(Cfp, &Cmnt, Cbuf, 256) != nil {
|
||||||
dir := C.GoString(Cmnt.mnt_dir)
|
dir := C.GoString(Cmnt.mnt_dir)
|
||||||
if err := syscall.Stat(dir, &stat); err != nil {
|
if err := syscall.Stat(dir, &stat); err != nil {
|
||||||
|
@ -154,12 +152,8 @@ func lookupZfsDataset(rootdir string) (string, error) {
|
||||||
return C.GoString(Cmnt.mnt_fsname), nil
|
return C.GoString(Cmnt.mnt_fsname), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// should never happen
|
|
||||||
return "", fmt.Errorf("Failed to find zfs pool in /proc/mounts")
|
|
||||||
}
|
|
||||||
|
|
||||||
func free(p *C.char) {
|
return "", fmt.Errorf("Failed to find zfs dataset mounted on '%s' in /proc/mounts", rootdir)
|
||||||
C.free(unsafe.Pointer(p))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Driver struct {
|
type Driver struct {
|
||||||
|
@ -179,21 +173,23 @@ func (d *Driver) Status() [][2]string {
|
||||||
parts := strings.Split(d.dataset.Name, "/")
|
parts := strings.Split(d.dataset.Name, "/")
|
||||||
pool, err := zfs.GetZpool(parts[0])
|
pool, err := zfs.GetZpool(parts[0])
|
||||||
|
|
||||||
if err != nil {
|
var poolName, poolHealth string
|
||||||
return [][2]string{
|
if err == nil {
|
||||||
{"error while getting pool", fmt.Sprintf("%v", err)},
|
poolName = pool.Name
|
||||||
}
|
poolHealth = pool.Health
|
||||||
}
|
|
||||||
var quota string
|
|
||||||
if d.dataset.Quota == 0 {
|
|
||||||
quota = strconv.FormatUint(d.dataset.Quota, 10)
|
|
||||||
} else {
|
} else {
|
||||||
quota = "no"
|
poolName = fmt.Sprintf("error while getting pool information %v", err)
|
||||||
|
poolHealth = "not available"
|
||||||
|
}
|
||||||
|
|
||||||
|
quota := "no"
|
||||||
|
if d.dataset.Quota != 0 {
|
||||||
|
quota = strconv.FormatUint(d.dataset.Quota, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
return [][2]string{
|
return [][2]string{
|
||||||
{"Zpool", pool.Name},
|
{"Zpool", poolName},
|
||||||
{"Zpool Health", pool.Health},
|
{"Zpool Health", poolHealth},
|
||||||
{"Parent Dataset", d.dataset.Name},
|
{"Parent Dataset", d.dataset.Name},
|
||||||
{"Space Used By Parent", strconv.FormatUint(d.dataset.Used, 10)},
|
{"Space Used By Parent", strconv.FormatUint(d.dataset.Used, 10)},
|
||||||
{"Space Available", strconv.FormatUint(d.dataset.Avail, 10)},
|
{"Space Available", strconv.FormatUint(d.dataset.Avail, 10)},
|
||||||
|
@ -204,12 +200,12 @@ func (d *Driver) Status() [][2]string {
|
||||||
|
|
||||||
func cloneFilesystem(id, parent, mountpoint string) error {
|
func cloneFilesystem(id, parent, mountpoint string) error {
|
||||||
parentDataset, err := zfs.GetDataset(parent)
|
parentDataset, err := zfs.GetDataset(parent)
|
||||||
if parentDataset == nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
snapshotName := fmt.Sprintf("%d", time.Now().Nanosecond())
|
snapshotName := fmt.Sprintf("%d", time.Now().Nanosecond())
|
||||||
snapshot, err := parentDataset.Snapshot(snapshotName /*recursive */, false)
|
snapshot, err := parentDataset.Snapshot(snapshotName /*recursive */, false)
|
||||||
if snapshot == nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -220,8 +216,7 @@ func cloneFilesystem(id, parent, mountpoint string) error {
|
||||||
snapshot.Destroy(zfs.DestroyDeferDeletion)
|
snapshot.Destroy(zfs.DestroyDeferDeletion)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = snapshot.Destroy(zfs.DestroyDeferDeletion)
|
return snapshot.Destroy(zfs.DestroyDeferDeletion)
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) ZfsPath(id string) string {
|
func (d *Driver) ZfsPath(id string) string {
|
||||||
|
@ -229,23 +224,30 @@ func (d *Driver) ZfsPath(id string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Create(id string, parent string) error {
|
func (d *Driver) Create(id string, parent string) error {
|
||||||
mountPoint := path.Join(d.options.mountPath, "graph", id)
|
|
||||||
datasetName := d.ZfsPath(id)
|
datasetName := d.ZfsPath(id)
|
||||||
dataset, err := zfs.GetDataset(datasetName)
|
dataset, err := zfs.GetDataset(datasetName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// cleanup existing dataset from an aborted build
|
// cleanup existing dataset from an aborted build
|
||||||
dataset.Destroy(zfs.DestroyRecursiveClones)
|
err := dataset.Destroy(zfs.DestroyRecursiveClones)
|
||||||
|
if err != nil {
|
||||||
|
log.Warnf("[zfs] failed to destroy dataset '%s': %v", dataset.Name, err)
|
||||||
|
}
|
||||||
|
} else if zfsError, ok := err.(*zfs.Error); ok {
|
||||||
|
if !strings.HasSuffix(zfsError.Stderr, "dataset does not exist\n") {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mountPoint := path.Join(d.options.mountPath, "graph", id)
|
||||||
if parent == "" {
|
if parent == "" {
|
||||||
_, err := zfs.CreateFilesystem(datasetName, map[string]string{
|
_, err := zfs.CreateFilesystem(datasetName, map[string]string{
|
||||||
"mountpoint": mountPoint,
|
"mountpoint": mountPoint,
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
} else {
|
|
||||||
return cloneFilesystem(datasetName, d.ZfsPath(parent), mountPoint)
|
|
||||||
}
|
}
|
||||||
return nil
|
return cloneFilesystem(datasetName, d.ZfsPath(parent), mountPoint)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Remove(id string) error {
|
func (d *Driver) Remove(id string) error {
|
||||||
|
@ -259,11 +261,10 @@ func (d *Driver) Remove(id string) error {
|
||||||
|
|
||||||
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
func (d *Driver) Get(id, mountLabel string) (string, error) {
|
||||||
dataset, err := zfs.GetDataset(d.ZfsPath(id))
|
dataset, err := zfs.GetDataset(d.ZfsPath(id))
|
||||||
if dataset == nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
} else {
|
|
||||||
return dataset.Mountpoint, nil
|
|
||||||
}
|
}
|
||||||
|
return dataset.Mountpoint, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) Put(id string) error {
|
func (d *Driver) Put(id string) error {
|
||||||
|
|
Loading…
Reference in New Issue