Class: OsCtl::Lib::Zfs::Dataset

Inherits:
Object
  • Object
show all
Includes:
Utils::Log, Utils::System
Defined in:
lib/libosctl/zfs/dataset.rb

Overview

Class representing a single ZFS dataset

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::System

#repeat_on_failure, #syscmd, #zfs

Methods included from Utils::Log

included

Constructor Details

#initialize(name, base: '', properties: {}, cache: nil) ⇒ Dataset

Returns a new instance of Dataset.

Parameters:

  • name (String)
  • base (String) (defaults to: '')

    container subdatasets are relative to the container dataset, so the base should be set to the container's root dataset, e.g. `<pool>/ct/<id>`

  • properties (Hash) (defaults to: {})
  • cache (Zfs::DatasetCache, nil) (defaults to: nil)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/libosctl/zfs/dataset.rb', line 25

def initialize(name, base: '', properties: {}, cache: nil)
  @name = name
  @base = base
  @properties = properties

  if properties[:mountpoint]
    @mountpoint = properties[:mountpoint]
  elsif cache
    ds = cache[name]

    if ds && ds.properties[:mountpoint]
      @mountpoint = ds.properties[:mountpoint]
    end
  end
end

Instance Attribute Details

#baseString (readonly)

Base name, i.e. path that this dataset is relative to

Returns:

  • (String)


17
18
19
# File 'lib/libosctl/zfs/dataset.rb', line 17

def base
  @base
end

#nameString (readonly)

Full dataset name

Returns:

  • (String)


9
10
11
# File 'lib/libosctl/zfs/dataset.rb', line 9

def name
  @name
end

#propertiesHash<String, String> (readonly)

Dataset properties, only loaded properties are present

Returns:

  • (Hash<String, String>)


13
14
15
# File 'lib/libosctl/zfs/dataset.rb', line 13

def properties
  @properties
end

Instance Method Details

#base_nameString

Return the last component of the dataset name

Returns:

  • (String)


242
243
244
# File 'lib/libosctl/zfs/dataset.rb', line 242

def base_name
  name.split('/').last
end

#childrenArray<Zfs::Dataset>

Return all direct children

Returns:



226
227
228
229
230
# File 'lib/libosctl/zfs/dataset.rb', line 226

def children
  ret = list(depth: 1)
  ret.shift # remove the current dataset
  ret
end

#create!(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • :parents (Boolean)
  • :properties (Hash)


58
59
60
61
62
63
64
65
66
67
# File 'lib/libosctl/zfs/dataset.rb', line 58

def create!(opts)
  zfs_opts = []
  zfs_opts << '-p' if opts[:parents]

  (opts[:properties] || {}).each do |k, v|
    zfs_opts << '-o' << "#{k}=#{v}"
  end

  zfs(:create, zfs_opts.join(' '), name)
end

#create_private!(opts = {}) ⇒ Object

Parameters:

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :parents (Boolean)

    create the private dir for all parents as well



72
73
74
75
76
77
78
79
80
81
# File 'lib/libosctl/zfs/dataset.rb', line 72

def create_private!(opts = {})
  if opts[:parents]
    parents.each do |ds|
      break if Dir.exist?(ds.private_path)
      ds.create_private!
    end
  end

  Dir.mkdir(private_path, 0750)
end

#descendantsArray<Zfs::Dataset>

Return all direct and indirect children

Returns:



234
235
236
237
238
# File 'lib/libosctl/zfs/dataset.rb', line 234

def descendants
  ret = list
  ret.shift # remove the current dataset
  ret
end

#destroy!(opts) ⇒ Object

Parameters:

  • opts (Hash)

    options

Options Hash (opts):

  • :recursive (Boolean)


85
86
87
# File 'lib/libosctl/zfs/dataset.rb', line 85

def destroy!(opts)
  zfs(:destroy, opts[:recursive] ? '-r' : nil, name)
end

#exist?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/libosctl/zfs/dataset.rb', line 90

def exist?
  zfs(:get, '-o value name', name, valid_rcs: [1]).success?
end

#exportHash

Export to client

Returns:

  • (Hash)


296
297
298
299
300
# File 'lib/libosctl/zfs/dataset.rb', line 296

def export
  ret = {name: relative_name, dataset: name}
  ret.update(properties)
  ret
end

#list(opts = {}) ⇒ Array<Zfs::Dataset>

List descendant datasets and read selected properties

Parameters:

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :depth (Integer)
  • :type (:filesystem)
  • :properties (Array<String>, Array<Symbol>)
  • :create (Proc)

Returns:



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/libosctl/zfs/dataset.rb', line 106

def list(opts = {})
  zfs_opts = [
    '-r', '-H', '-p',
    '-o', 'name,property,value',
    '-t', opts[:type] || 'filesystem'
  ]
  zfs_opts << '-d' << opts[:depth] if opts[:depth]

  properties = []
  properties.concat(opts[:properties]) if opts[:properties]
  properties << 'name' if properties.empty?
  properties.uniq!

  zfs_opts << properties.map(&:to_s).join(',')

  ret = []
  last = nil

  zfs(:get, zfs_opts.join(' '), name).output.strip.split("\n").each do |line|
    name, property, value = line.split

    if !last || last.name != name
      ret << last unless last.nil?

      if opts[:create]
        last = opts[:create].call(name)
      else
        last = self.class.new(name, base: base)
      end
    end

    next if property == 'name'
    last.properties[property.to_sym] = value
  end

  ret << last unless last.nil?
  ret
end

#mount(recursive: false) ⇒ Object

Ensure the dataset is mounted

Parameters:

  • recursive (Boolean) (defaults to: false)

    mount subdatasets as well



160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/libosctl/zfs/dataset.rb', line 160

def mount(recursive: false)
  zfs(
    :list,
    "-H -o name,mounted -t filesystem #{recursive ? '-r' : ''}",
    name,
  ).output.split("\n").each do |line|
    ds, mounted = line.split
    next if mounted == 'yes'

    zfs(:mount, nil, ds)
  end
end

#mounted?(recursive: false) ⇒ Boolean

Check if the dataset is mounted

Parameters:

  • recursive (Boolean) (defaults to: false)

    check all subdatasets as well

Returns:

  • (Boolean)


190
191
192
193
194
195
196
# File 'lib/libosctl/zfs/dataset.rb', line 190

def mounted?(recursive: false)
  zfs(
    :get,
    "-H #{recursive ? '-r' : ''} -t filesystem -o value mounted",
    name
  ).output.split("\n").all? { |v| v == 'yes' }
end

#mountpointString

Returns:

  • (String)


146
147
148
149
150
151
152
153
154
155
156
# File 'lib/libosctl/zfs/dataset.rb', line 146

def mountpoint
  if @mountpoint
    return @mountpoint

  elsif properties[:mountpoint]
    @mountpoint = properties[:mountpoint]

  else
    @mountpoint = zfs(:get, '-H -o value mountpoint', name).output.strip
  end
end

#on_pool?(pool) ⇒ Boolean

Parameters:

  • pool (String)

Returns:

  • (Boolean)


46
47
48
# File 'lib/libosctl/zfs/dataset.rb', line 46

def on_pool?(pool)
  name.start_with?("#{pool}/")
end

#parentZfs::Dataset

Return the direct parent

Returns:



200
201
202
203
204
205
206
207
208
209
# File 'lib/libosctl/zfs/dataset.rb', line 200

def parent
  parts = name.split('/')

  if parts.count == 1
    nil

  else
    self.class.new(parts[0..-2].join('/'), base: base)
  end
end

#parentsArray<Zfs::Dataset>

Return all parent datasets, up to the root

Returns:



213
214
215
216
217
218
219
220
221
222
# File 'lib/libosctl/zfs/dataset.rb', line 213

def parents
  ret = []
  parts = name.split('/')[0..-2]

  parts.each_with_index do |v, i|
    ret << self.class.new(parts[0..i].join('/'), base: base)
  end

  ret.reverse!
end

#private_pathString

Returns:

  • (String)


95
96
97
# File 'lib/libosctl/zfs/dataset.rb', line 95

def private_path
  File.join(mountpoint, 'private')
end

#relative_nameString

Return the dataset name relative to the base

Returns:

  • (String)


248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/libosctl/zfs/dataset.rb', line 248

def relative_name
  return @relative_name if @relative_name

  @relative_name = if base == name
    '/'

  elsif !name.start_with?("#{base}/")
    fail "invalid base '#{base}' for '#{name}'"

  else
    name[(base.length+1)..-1]
  end
end

#relative_parentZfs::Dataset?

Return the dataset's parent relative to the base

Returns:



264
265
266
267
268
269
270
271
272
# File 'lib/libosctl/zfs/dataset.rb', line 264

def relative_parent
  ret = parent

  @relative_parent = if ret.name.length < @base.length
    nil
  else
    ret
  end
end

#relative_parentsArray<Zfs::Dataset>

Return the parents up to the base

Returns:



276
277
278
# File 'lib/libosctl/zfs/dataset.rb', line 276

def relative_parents
  parents.take_while { |ds| ds.name.length >= @base.length }
end

#root?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/libosctl/zfs/dataset.rb', line 281

def root?
  name == base || !name.index('/')
end

#snapshotsArray<Zfs::Snapshot>

Returns:



286
287
288
289
290
291
292
# File 'lib/libosctl/zfs/dataset.rb', line 286

def snapshots
  list(
    type: 'snapshot',
    depth: 1,
    create: ->(name) { Zfs::Snapshot.new(self, name.split('@')[1]) }
  )
end

#subdataset_of?(dataset) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


51
52
53
# File 'lib/libosctl/zfs/dataset.rb', line 51

def subdataset_of?(dataset)
  name.start_with?("#{dataset.name}/")
end

#to_sObject



41
42
43
# File 'lib/libosctl/zfs/dataset.rb', line 41

def to_s
  name
end

#unmount(recursive: false) ⇒ Object

Ensure the dataset is unmounted

Parameters:

  • recursive (Boolean) (defaults to: false)

    unmount subdatasets as well



175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/libosctl/zfs/dataset.rb', line 175

def unmount(recursive: false)
  zfs(
    :list,
    "-H -o name,mounted -t filesystem #{recursive ? '-r' : ''}",
    name,
  ).output.split("\n").reverse_each do |line|
    ds, mounted = line.split
    next if mounted != 'yes'

    zfs(:unmount, nil, ds)
  end
end