Class: Haconiwa::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/haconiwa/namespace.rb

Constant Summary collapse

UNSHARE =
272
SETNS =
308
CLONE_FS =

from linux/sched.h

0x00000200
CLONE_FILES =
0x00000400
CLONE_NEWNS =
0x00020000
CLONE_SYSVSEM =
0x00040000
CLONE_NEWCGROUP =
0x02000000
CLONE_NEWUTS =
0x04000000
CLONE_NEWIPC =
0x08000000
CLONE_NEWUSER =
0x10000000
CLONE_NEWPID =
0x20000000
CLONE_NEWNET =
0x40000000
NS_MAPPINGS =
{
  "cgroup" => CLONE_NEWCGROUP,
  "ipc"    => CLONE_NEWIPC,
  "net"    => CLONE_NEWNET,
  "mount"  => CLONE_NEWNS,
  "pid"    => CLONE_NEWPID,
  "user"   => CLONE_NEWUSER,
  "uts"    => CLONE_NEWUTS,
}
FLAG_TO_PARAM =
{
  CLONE_NEWCGROUP => "cgroup",
  CLONE_NEWIPC    => "ipc",
  CLONE_NEWNET    => "net",
  CLONE_NEWNS     => "mount",
  CLONE_NEWPID    => "pid",
  CLONE_NEWUSER   => "user",
  CLONE_NEWUTS    => "uts",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNamespace



39
40
41
42
# File 'lib/haconiwa/namespace.rb', line 39

def initialize
  @use_ns = []
  @netns_name = nil
end

Instance Attribute Details

#use_pid_nsObject (readonly)

Returns the value of attribute use_pid_ns.



57
58
59
# File 'lib/haconiwa/namespace.rb', line 57

def use_pid_ns
  @use_pid_ns
end

Instance Method Details

#apply!Object



63
64
65
66
67
# File 'lib/haconiwa/namespace.rb', line 63

def apply!
  flag = to_ns_flag
  STDERR.puts "unshare(2) flag: 0x%s" % flag.to_s(16)
  Kernel.syscall(UNSHARE, flag)
end

#enter(pid: nil, wrapper_path: nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/haconiwa/namespace.rb', line 69

def enter(pid: nil, wrapper_path: nil)
  ns_params = use_ns_all.map{|f| "--#{FLAG_TO_PARAM[f]}" }
  exec "nsenter",
       "--target", "#{pid}",
       *ns_params,
       "--", wrapper_path.to_s
end

#unshare(ns) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/haconiwa/namespace.rb', line 44

def unshare(ns)
  flag = case ns
         when String, Symbol
           NS_MAPPINGS[ns.to_s]
         when Integer
           ns
         end
  if flag == CLONE_NEWPID
    @use_pid_ns = true
  else
    @use_ns << flag
  end
end

#use_netns(name) ⇒ Object



59
60
61
# File 'lib/haconiwa/namespace.rb', line 59

def use_netns(name)
  @netns_name = name
end