Class: VagrantZFS::ZFS

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant_zfs/zfs.rb

Class Method Summary collapse

Class Method Details

.clone!(snapname, clonename) ⇒ Object

Clone snapshot



72
73
74
75
76
77
78
# File 'lib/vagrant_zfs/zfs.rb', line 72

def self.clone!(snapname, clonename)
  #clonename = clone.name if clone.is_a? ZFS
  #raise AlreadyExists if ZFS(clone).exist?

  cmd = "clone #{snapname} #{clonename}"
  self.exec cmd
end

.create(fs_name, mountpoint) ⇒ Object



19
20
21
22
23
# File 'lib/vagrant_zfs/zfs.rb', line 19

def self.create fs_name, mountpoint
  zfs_opts="-o atime=off -o compression=lzjb"
  cmd = "create #{zfs_opts} -o mountpoint=#{mountpoint} #{fs_name}"
  self.exec cmd
end

.destroy(fs_name) ⇒ Object



25
26
27
28
# File 'lib/vagrant_zfs/zfs.rb', line 25

def self.destroy fs_name
  cmd = "destroy #{fs_name}"
  self.exec cmd
end

.destroy_at(mountpoint) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/vagrant_zfs/zfs.rb', line 30

def self.destroy_at mountpoint
    fs = VagrantZFS::ZFS.mounts.select do |mounted_at,fs|
        mounted_at == mountpoint
    end.first[1]
    puts "Will destroy #{fs} mounted at #{mountpoint}"
    self.destroy fs
end

.exec(cmd) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/vagrant_zfs/zfs.rb', line 4

def self.exec cmd
  cmd = "zfs " + cmd
  stdout, stderr, status = Open3.capture3(cmd)

  if status.success? and stderr.empty?
    return stdout
  else
    raise Exception, "ZFS Error: #{cmd}\n #{stdout}\n #{stderr}\n #{status}"
  end
end

.mountsObject



38
39
40
41
42
43
44
45
46
# File 'lib/vagrant_zfs/zfs.rb', line 38

def self.mounts
  cmd = "get -rHp -oname,value mountpoint"
  lines = self.exec(cmd).split(/\n/)
  mounts = lines.collect do |line|
    fs, path = line.chomp.split(/\t/, 2)
    [path, fs]
  end
  Hash[mounts]
end

.set_mountpoint(fs_name, mountpoint) ⇒ Object



48
49
50
51
# File 'lib/vagrant_zfs/zfs.rb', line 48

def self.set_mountpoint fs_name, mountpoint
  cmd = "set mountpoint=#{mountpoint} #{fs_name}"
  self.exec cmd
end

.snapshot(fsname, snapname, opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/vagrant_zfs/zfs.rb', line 53

def self.snapshot(fsname, snapname)
  #raise NotFound, "no such filesystem" if !exist?
  #raise AlreadyExists, "Snapshot #{snapname} already exists" if ZFS("#{fsname}@#{snapname}").exist?

  cmd = "snapshot #{fsname}@#{snapname}"
  self.exec cmd
  return "#{fsname}@#{snapname}"
end

.zpool_listObject



15
16
17
# File 'lib/vagrant_zfs/zfs.rb', line 15

def self.zpool_list
  `zpool list -H -o name`.split(/\n/)
end