summaryrefslogtreecommitdiffstats
path: root/zfs_monkeypatch.rb
blob: 20fb7d9fd4ddff555fe208ace87d2384cea11b9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Monkeypatch the create() method, to add sparse volumes support
class ZFS

	# Create filesystem
	def create(opts={})
		return nil if exist?

		cmd = [ZFS.zfs_path].flatten + ['create']
		cmd << '-p' if opts[:parents]
		cmd << '-s' if opts[:volume] and opts[:sparse]
		cmd += opts[:zfsopts].map{|el| ['-o', el]}.flatten if opts[:zfsopts]
		cmd += ['-V', opts[:volume]] if opts[:volume]
		cmd << name

		out, status = Open3.capture2e(*cmd)
		if status.success? and out.empty?
			return self
		elsif out.match(/dataset already exists\n$/)
			nil
		else
			raise Exception, "something went wrong: #{out}, #{status}"
		end
	end

end