hrr_rb_lxns 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +9 -0
- data/lib/hrr_rb_lxns.rb +53 -29
- data/lib/hrr_rb_lxns/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8243b5243363dd383204313c4487478786834e46ff9a3c1be7f90b2c5f77052
|
4
|
+
data.tar.gz: daa322824dc8a8c75a80213d37739b44e223a09647d73502c5b448b014629df1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 948f4793805ee19b20cd5da1fc1f9acff00c065829708bc9f1fb26e947917214e480b6ad8c86d955863dcb0e10a5413f335fa9270dcdf2c816f6ba0d4d2e12a6
|
7
|
+
data.tar.gz: 3a74b885cff6c12dcd179b6ee697d4e0da73f078f6dab6e3de8358e913ef0caa556ad104d05ff188eec848b3e804862fcb818e9da68cd7b0d64c59697347825b
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -68,6 +68,15 @@ HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
|
|
68
68
|
File.readlink "/proc/self/ns/uts" # => uts:[yyy]
|
69
69
|
```
|
70
70
|
|
71
|
+
HrrRbLxns.setns can associate namespaces using files which specify namespaces, instead of specifying pid. The files are specified in an options hash. The keys which are available in the hash for each namespace are `:mount`, `:uts`, `:ipc`, `:network`, `:pid`, `:user`, `:cgroup`, and `:time`.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# Create a network namespace using the ip netns command. The command generates "/run/netns/ns0" file, which is a bind-mounted namespace file.
|
75
|
+
system "ip netns add ns0"
|
76
|
+
# Then associate with the network namespace with specifying the file instead of pid.
|
77
|
+
HrrRbLxns.setns HrrRbLxns::NETNET, nil, {network: "/run/netns/ns0"}
|
78
|
+
```
|
79
|
+
|
71
80
|
## Note
|
72
81
|
|
73
82
|
Some of the namespace operations are not multi-thread friendly. The library expects that only main thread is running before unshare or setns operation.
|
data/lib/hrr_rb_lxns.rb
CHANGED
@@ -58,7 +58,7 @@ module HrrRbLxns
|
|
58
58
|
# HrrRbLxns.setns HrrRbLxns::NEWUTS, pid # => 0
|
59
59
|
# File.readlink "/proc/self/ns/uts" # => uts:[yyy]
|
60
60
|
#
|
61
|
-
# @param flags [Integer] An integer value that represents namespaces to
|
61
|
+
# @param flags [Integer] An integer value that represents namespaces to associate.
|
62
62
|
# @param flags [String] A string that represents namespaces. The mapping of charactors and flags are: <br>
|
63
63
|
# "i" : NEWIPC <br>
|
64
64
|
# "m" : NEWNS <br>
|
@@ -68,21 +68,31 @@ module HrrRbLxns
|
|
68
68
|
# "U" : NEWUSER <br>
|
69
69
|
# "C" : NEWCGROUP <br>
|
70
70
|
# "T" : NEWTIME <br>
|
71
|
-
# @param
|
71
|
+
# @param pid [Integer] Specifies a target process('s namespace) which the caller is to associate with. The paths specifying namespaces specified by pid are: <br>
|
72
|
+
# /proc/pid/ns/mnt : mount namespace <br>
|
73
|
+
# /proc/pid/ns/uts : uts namespace <br>
|
74
|
+
# /proc/pid/ns/ipc : ipc namespace <br>
|
75
|
+
# /proc/pid/ns/net : network namespace <br>
|
76
|
+
# /proc/pid/ns/pid : pid namespace <br>
|
77
|
+
# /proc/pid/ns/user : user namespace <br>
|
78
|
+
# /proc/pid/ns/cgroup : cgroup namespace <br>
|
79
|
+
# /proc/pid/ns/time : time namespace <br>
|
80
|
+
# @param options [Hash] Optional arguments.
|
81
|
+
# @option options [String] :mount A file which specifies the mount namespace to associate with.
|
82
|
+
# @option options [String] :uts A file which specifies the uts namespace to associate with.
|
83
|
+
# @option options [String] :ipc A file which specifies the ipc namespace to associate with.
|
84
|
+
# @option options [String] :network A file which specifies the network namespace to associate with.
|
85
|
+
# @option options [String] :pid A file which specifies the pid namespace to associate with.
|
86
|
+
# @option options [String] :user A file which specifies the user namespace to associate with.
|
87
|
+
# @option options [String] :cgroup A file which specifies the cgroup namespace to associate with.
|
88
|
+
# @option options [String] :time A file which specifies the time namespace to associate with.
|
72
89
|
# @return [Integer] 0.
|
73
|
-
# @raise [ArgumentError] When given flags argument is not appropriate.
|
90
|
+
# @raise [ArgumentError] When given flags argument is not appropriate or when given pid and/or options are not appropriate for the given flags.
|
74
91
|
# @raise [Errno::EXXX] In case setns(2) system call failed.
|
75
92
|
def self.setns flags, pid, options={}
|
76
93
|
_flags = interpret_flags flags
|
77
|
-
|
78
|
-
|
79
|
-
begin
|
80
|
-
fd = File.open(path, File::RDONLY)
|
81
|
-
__setns__ fd.fileno, nstype
|
82
|
-
ensure
|
83
|
-
fd.close rescue nil
|
84
|
-
end
|
85
|
-
end
|
94
|
+
nstype_file_h = get_nstype_file_h _flags, pid, options
|
95
|
+
do_setns nstype_file_h
|
86
96
|
end
|
87
97
|
|
88
98
|
private
|
@@ -110,27 +120,41 @@ module HrrRbLxns
|
|
110
120
|
end
|
111
121
|
end
|
112
122
|
|
113
|
-
def self.
|
123
|
+
def self.do_setns nstype_file_h
|
124
|
+
nstype_file_h.each do |nstype, file|
|
125
|
+
File.open(file, File::RDONLY) do |f|
|
126
|
+
__setns__ f.fileno, nstype
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def self.get_nstype_file_h flags, pid, options
|
114
132
|
list = Array.new
|
115
|
-
list.push ["ipc", NEWIPC
|
116
|
-
list.push ["mnt", NEWNS
|
117
|
-
list.push ["net", NEWNET
|
118
|
-
list.push ["pid", NEWPID
|
119
|
-
list.push ["uts", NEWUTS
|
120
|
-
list.push ["user", NEWUSER
|
121
|
-
list.push ["cgroup", NEWCGROUP] if const_defined?(:NEWCGROUP)
|
122
|
-
list.push ["time", NEWTIME
|
123
|
-
|
124
|
-
list.each do |name, flag|
|
125
|
-
|
126
|
-
|
133
|
+
list.push ["ipc", NEWIPC, :ipc ] if const_defined?(:NEWIPC)
|
134
|
+
list.push ["mnt", NEWNS, :mount ] if const_defined?(:NEWNS)
|
135
|
+
list.push ["net", NEWNET, :network] if const_defined?(:NEWNET)
|
136
|
+
list.push ["pid", NEWPID, :pid ] if const_defined?(:NEWPID)
|
137
|
+
list.push ["uts", NEWUTS, :uts ] if const_defined?(:NEWUTS)
|
138
|
+
list.push ["user", NEWUSER, :user ] if const_defined?(:NEWUSER)
|
139
|
+
list.push ["cgroup", NEWCGROUP, :cgroup ] if const_defined?(:NEWCGROUP)
|
140
|
+
list.push ["time", NEWTIME, :time ] if const_defined?(:NEWTIME)
|
141
|
+
nstype_file_h = Hash.new
|
142
|
+
list.each do |name, flag, key|
|
143
|
+
file = get_file name, (flags & flag), pid, key, options[key]
|
144
|
+
nstype_file_h[flag] = file if file
|
127
145
|
end
|
128
|
-
|
146
|
+
nstype_file_h
|
129
147
|
end
|
130
148
|
|
131
|
-
def self.
|
132
|
-
if flag.zero?.!
|
133
|
-
|
149
|
+
def self.get_file name, flag, pid, key, option
|
150
|
+
if flag.zero?.!
|
151
|
+
if option
|
152
|
+
option
|
153
|
+
elsif pid
|
154
|
+
"/proc/#{pid}/ns/#{name}"
|
155
|
+
else
|
156
|
+
raise ArgumentError, "neither pid nor options[:#{key}] specified for #{key} namespace"
|
157
|
+
end
|
134
158
|
else
|
135
159
|
nil
|
136
160
|
end
|
data/lib/hrr_rb_lxns/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hrr_rb_lxns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hirura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Utilities working with Linux namespaces for CRuby.
|
14
14
|
email:
|