Add ignore support and logging
[dtbartle/zfs.git] / zfs.py
1 import subprocess
2
3 class ZfsException:
4     def __init__(self, status, err):
5         self.status = status
6         self.err = err.strip("\n")
7
8 def zfs_exec(*args):
9     args = [ "/usr/sbin/zfs" ] + list(args)
10     zfs = subprocess.Popen(args, stdin=subprocess.PIPE,
11         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
12     out, err = zfs.communicate()
13     status = zfs.wait()
14     if status != 0:
15         raise ZfsException(status, err)
16     return out
17
18 def zfs_list(zpool, type):
19     out = zfs_exec("list", "-H", "-r", "-o", "name", "-t", type, zpool)
20     out = out.strip("\n").split("\n")
21     out = map(lambda x: x[len(zpool) + 1:], out)
22     out = filter(len, out)
23     return out
24
25 def zfs_create(name):
26     zfs_exec("create", name)
27
28 def zfs_destroy(name):
29     zfs_exec("destroy", name)
30
31 def zfs_snapshot(name):
32     zfs_exec("snapshot", name)
33
34 def zfs_rename(old, new):
35     zfs_exec("rename", old, new)