Class: Dcmgr::Drivers::Zfs

Inherits:
BackingStore show all
Includes:
Helpers::CliHelper, Logger
Defined in:
lib/dcmgr/drivers/zfs.rb

Instance Method Summary collapse

Methods included from Helpers::CliHelper

#sh, #tryagain

Methods included from Logger

create, default_logdev, included

Methods inherited from BackingStore

#delete_snapshot, select_backing_store

Instance Method Details

#create_snapshot(ctx, zsnap_file) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dcmgr/drivers/zfs.rb', line 64

def create_snapshot(ctx, zsnap_file)
  @snapshot_id = ctx.snapshot_id
  @destination = ctx.destination
  @snapshot    = ctx.snapshot
  @volume      = ctx.volume

  vol_path = "#{@volume[:storage_node][:export_path]}/#{@volume[:uuid]}"
  sh("/usr/sbin/zfs snapshot %s@%s", [vol_path, @snapshot[:uuid]])
  sh("/usr/sbin/zfs send %s@%s > %s", [vol_path, @snapshot[:uuid], zsnap_file])
  sh("/usr/sbin/zfs destroy %s@%s", [vol_path, @snapshot[:uuid]])
end

#create_volume(ctx, zsnap_file) ⇒ Object



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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dcmgr/drivers/zfs.rb', line 10

def create_volume(ctx, zsnap_file)
  @volume_id   = ctx.volume_id
  @volume      = ctx.volume
  @snapshot    = ctx.snapshot
  @destination = ctx.destination

  ### sta_handler :create_volume
  vol_path = "#{@volume[:storage_node][:export_path]}/#{@volume[:uuid]}"
  sh("/usr/sbin/zfs list %s", [File.dirname(vol_path)])
  if $?.exitstatus != 0
    # create parent filesystem
    sh("/usr/sbin/zfs create -p %s", [File.dirname(vol_path)])
    logger.info("create parent filesystem: #{File.dirname(vol_path)}")
  end

  if @snapshot
    # create volume from snapshot
    if File.exists?(zsnap_file)
      sh("/usr/sbin/zfs receive %s < %s", [vol_path, zsnap_file])
      if $?.exitstatus != 0
        raise "volume already exists: #{@volume_id}"
      end
    else
      raise "snapshot file isn't exists: #{zsnap_file}"
    end

    sh("/usr/sbin/zfs destroy %s@%s", [vol_path, @snapshot[:uuid]])
    if $?.exitstatus != 0
      raise "volume snapshot has not deleted: #{@volume_id}@#{@snapshot[:uuid]}"
    end
  else
    # create volume
    #sh("/usr/sbin/zfs create -p -V %s %s", ["#{@volume[:size]}m", vol_path])
    # thin provisioning
    sh("/usr/sbin/zfs create -p -s -V %s %s", ["#{@volume[:size]}m", vol_path])
    if $?.exitstatus != 0
      raise "volume already exists: #{@volume_id}"
    end
  end
  
  sh("/usr/sbin/zfs list %s", [vol_path])
  if $?.exitstatus != 0
    raise "volume has not be created: #{@volume_id}"
  end

  logger.info("created new volume: #{@volume_id}")
  ### sta_handler :create_volume
end

#delete_volume(ctx) ⇒ Object



59
60
61
62
# File 'lib/dcmgr/drivers/zfs.rb', line 59

def delete_volume(ctx)
  @volume = ctx.volume
  sh("/usr/sbin/zfs destroy %s", ["#{@volume[:storage_node][:export_path]}/#{@volume[:uuid]}"])
end