Class: OsCtld::Commands::Dataset::Delete

Inherits:
Base
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, Utils::SwitchUser
Defined in:
lib/osctld/commands/dataset/delete.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods included from Utils::SwitchUser

#ct_attach, #ct_syscmd

Methods inherited from Base

#base_execute, #call_cmd, #call_cmd!, #error, #error!, handle, #handled, #indirect?, #initialize, #manipulate, #manipulation_holder, #ok, #progress, #request_stop, run, run!

Constructor Details

This class inherits a constructor from OsCtld::Commands::Base

Instance Method Details

#delete_mounts(ct, mounts) ⇒ Object (protected)



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/osctld/commands/dataset/delete.rb', line 68

def delete_mounts(ct, mounts)
  if ct.state == :running
    mounts.each do |mnt|
      next unless Dir.exist?(File.join(ct.run_conf.runtime_rootfs, mnt.mountpoint))

      begin
        ret = ct_syscmd(
          ct,
          ['umount', File.join('/', mnt.mountpoint)],
          valid_rcs: [1]
        )

        if ret.exitstatus == 1 && /not mounted/ !~ ret.output
          error!("unable to unmount #{mnt.mountpoint}: #{ret.output}")
        end
      rescue SystemCommandFailed => e
        error!("unable to unmount #{mnt.mountpoint}: #{e.message}")
      end
    end
  end

  mounts.each do |mnt|
    ct.mounts.delete_at(mnt.mountpoint)
  end
end

#executeObject



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
# File 'lib/osctld/commands/dataset/delete.rb', line 10

def execute
  ct = DB::Containers.find(opts[:id], opts[:pool])
  return error('container not found') unless ct

  name = opts[:name].strip

  if name.empty? || name == '/'
    return error('cannot delete the root dataset')
  end

  manipulate(ct) do
    ds = OsCtl::Lib::Zfs::Dataset.new(
      File.join(ct.dataset.name, name),
      base: ct.dataset.name
    )
    error!("dataset #{ds.name} does not exist") unless ds.exist?

    descendants = ds.descendants

    if descendants.any? && !opts[:recursive]
      error!('dataset has children, recursive delete has to be enabled explicitly')
    end

    mounts = find_mounts(ct, ds, descendants, opts[:recursive])

    if mounts.any?
      unless opts[:unmount]
        error!(
          "the following mountpoints need to be unmounted:\n  #{mounts.map(&:mountpoint).join("\n  ")}"
        )
      end

      delete_mounts(ct, mounts)
    end

    begin
      ds.destroy!(recursive: opts[:recursive])
      ok
    rescue SystemCommandFailed => e
      log(:warn, "Unable to delete dataset: #{e.message}")
      error('delete failed, the dataset is either busy or has children')
    end
  end
end

#find_mounts(ct, dataset, descendants, recursive) ⇒ Object (protected)



57
58
59
60
61
62
63
64
65
66
# File 'lib/osctld/commands/dataset/delete.rb', line 57

def find_mounts(ct, dataset, descendants, recursive)
  datasets = [dataset]
  datasets.concat(descendants) if recursive

  ct.mounts.select do |mnt|
    next(false) unless mnt.dataset

    datasets.detect { |ds| ds.relative_name == mnt.dataset.relative_name }
  end.sort { |a, b| a.mountpoint <=> b.mountpoint }.reverse!
end