summaryrefslogtreecommitdiffstats
path: root/zvolcreator.rb
blob: eb790696de911abac286bb192554fc06b0d4c206 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env ruby

require 'zfs'
require 'socket'
require 'json'

require_relative 'zfs_monkeypatch'
load File.dirname($0) + "/" + 'config.rb'

puts "Filesystem base: #{Config[:basefs]}"
puts "Default user quota: #{Config[:default_user_quota]}"
puts "Socket path: #{Config[:socketpath]}"
sockdir = File.dirname(Config[:socketpath])
puts "socket directory: #{sockdir}"

# Check if socket directory exists, if not - create if with apropriate options
Dir.mkdir(sockdir, 0700) if not File.exists?(sockdir)
File.umask(0077)

server = UNIXServer.open(Config[:socketpath])
loop do
    Thread.start(server.accept) do |client|
        begin
            client.puts({:status => 0, :content => {:app => "zvolcreator", :apiversion => 0}}.to_json)
            response = JSON.parse(client.read)
            raise ArgumentError.new("Bad status") if response["status"] != 0

            client.puts({:status => 0, :content => {:basefs => Config[:basefs], :default_user_quota => Config[:default_user_quota]}}.to_json)
            response = JSON.parse(client.read)
            raise ArgumentError.new("Bad status") if response["status"] != 0
            raise ArgumentError.new("No user specified") if not response["content"]["user"]
            raise ArgumentError.new("Volume size not specified") if not response["content"]["volsize"]
        rescue JSON::ParserError, ArgumentError => e
            client.puts({:status => 1, :content => e.to_s})
            client.close
            Thread.kill
        end
    end
end