Class: OsCtld::Commands::Container::MapMode

Inherits:
Logged
  • Object
show all
Includes:
OsCtl::Lib::Utils::Log, OsCtl::Lib::Utils::System
Defined in:
lib/osctld/commands/container/map_mode.rb

Instance Attribute Summary

Attributes inherited from Base

#client, #client_handler, #id, #opts

Instance Method Summary collapse

Methods inherited from Logged

#base_execute

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

#execute(ct) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/osctld/commands/container/map_mode.rb', line 15

def execute(ct)
  if !Container::MAP_MODES.include?(opts[:map_mode])
    error!('invalid map mode')
  elsif ct.map_mode == opts[:map_mode]
    return ok
  end

  manipulate(ct) do
    error!('container is running') if ct.running?

    if ct.map_mode == 'native' && opts[:map_mode] == 'zfs'
      native_to_zfs(ct)
    elsif ct.map_mode == 'zfs' && opts[:map_mode] == 'native'
      zfs_to_native(ct)
    end

    ct.map_mode = opts[:map_mode]

    ok
  end
end

#findObject



10
11
12
13
# File 'lib/osctld/commands/container/map_mode.rb', line 10

def find
  ct = DB::Containers.find(opts[:id], opts[:pool])
  ct || error!('container not found')
end

#native_to_zfs(ct) ⇒ Object (protected)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/osctld/commands/container/map_mode.rb', line 39

def native_to_zfs(ct)
  datasets = ct.datasets

  datasets.reverse_each do |ds|
    progress("Unmounting dataset #{ds.relative_name}")
    zfs(:unmount, nil, ds, valid_rcs: [1])
  end

  datasets.each do |ds|
    progress("Setting UID/GID mapping of #{ds.relative_name}")
    zfs(
      :set,
      "uidmap=\"#{ct.uid_map.map(&:to_s).join(',')}\" " \
      "gidmap=\"#{ct.gid_map.map(&:to_s).join(',')}\"",
      ds
    )

    progress("Remounting dataset #{ds.relative_name}")
    zfs(:mount, nil, ds)
  end
end

#zfs_to_native(ct) ⇒ Object (protected)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/osctld/commands/container/map_mode.rb', line 61

def zfs_to_native(ct)
  datasets = ct.datasets

  datasets.reverse_each do |ds|
    progress("Unmounting dataset #{ds.relative_name}")
    zfs(:unmount, nil, ds, valid_rcs: [1])
  end

  datasets.each do |ds|
    progress("Unsetting UID/GID mapping of #{ds.relative_name}")
    zfs(
      :set,
      'uidmap=none gidmap=none',
      ds
    )

    progress("Remounting dataset #{ds.relative_name}")
    zfs(:mount, nil, ds)
  end
end