haconiwa 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 789ac623034f84c580c9a3c0d86d736b10da1582
4
- data.tar.gz: e3c63d4dfc082b8edb33d34e96863d952360c80f
3
+ metadata.gz: 767036a1ab66ab117bcfd75c18b56a3636fa7358
4
+ data.tar.gz: 1d3cee5db594eb27e3f91eb28bedbf67ef94e146
5
5
  SHA512:
6
- metadata.gz: 570fa08764eeccba256b7c559bda8b31dff0e9039a6679ff2a66477cab0cc808e6eaaa1dfab5bbd5584e3405c4aa054f1e9cb44847ecc992d62b833eec1c7e17
7
- data.tar.gz: 47d510dd9d9485798918214231fba654b2ec5c3cd436e4339bacc9a3469d41a3ac50d01edf70f97c5f75e81f046476aa54e751d961a7a3e759ea58935cd33a5a
6
+ metadata.gz: 41d15e175b6f1b2fd6475ac558a138770167e1ed9e2d075fe48ec91f0493ceab08e53c619d6fee79e60d1972373041453117b70c41d40afe5bbb22292066599a
7
+ data.tar.gz: ad935bf20472c72d34066afc60bcacd682e13d4e489bdc606d4aa6eced14f8e4fff9d07ba24275dbc3b46ea0c86617e696ac347a452fedcc88d5b190b6d9405b
@@ -0,0 +1,19 @@
1
+ # -*- mode: ruby -*-
2
+ Haconiwa.define do |config|
3
+ config.name = "chroot001" # to be hostname
4
+ config.init_command = "/bin/sh" # to be first process
5
+
6
+ root = Pathname.new("/var/haconiwa/root")
7
+ config.add_mount_point "/var/haconiwa/rootfs", to: root, readonly: true
8
+ config.add_mount_point "/lib64", to: root.join("lib64"), readonly: true
9
+ config.add_mount_point "/usr/bin", to: root.join("usr/bin"), readonly: true
10
+ config.add_mount_point "tmpfs", to: root.join("tmp"), fs: "tmpfs"
11
+ config.add_mount_point "/var/haconiwa/user_homes/hakoniwa-test001/home/hakoniwa", to: root.join("home/hakoniwa")
12
+ config.mount_independent_procfs
13
+ config.chroot_to root
14
+
15
+ config.namespace.unshare "mount"
16
+ config.namespace.unshare "ipc"
17
+ config.namespace.unshare "uts"
18
+ config.namespace.unshare "pid"
19
+ end
data/exe/haconiwa ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require 'haconiwa'
3
+ require 'haconiwa/cli'
4
+
5
+ subcommand = ARGV[0]
6
+ case subcommand
7
+ when "version"
8
+ puts Haconiwa::VERSION
9
+ when "run"
10
+ Haconiwa::Cli.run(ARGV[1..-1])
11
+ end
data/lib/haconiwa/base.rb CHANGED
@@ -8,6 +8,7 @@ require "haconiwa/runners"
8
8
  module Haconiwa
9
9
  class Base
10
10
  attr_accessor :name,
11
+ :init_command,
11
12
  :filesystem,
12
13
  :cgroup,
13
14
  :namespace,
@@ -40,12 +41,16 @@ module Haconiwa
40
41
  self.filesystem.mount_independent_procfs = true
41
42
  end
42
43
 
43
- def start(init_command='/sbin/init')
44
+ def start(*init_command)
44
45
  Runners::Linux.run(self, init_command)
45
46
  end
46
47
  alias run start
47
48
  end
48
49
 
50
+ def self.define(&b)
51
+ Base.define(&b)
52
+ end
53
+
49
54
  module Utils
50
55
  # $ ausyscall --dump | grep hostname
51
56
  # 170 sethostname
@@ -0,0 +1,15 @@
1
+ module Haconiwa
2
+ module Cli
3
+ def self.run(args)
4
+ require 'pathname'
5
+ script = File.read(args[0])
6
+ init = args[1..-1]
7
+ if init.first == "--"
8
+ init.shift
9
+ end
10
+
11
+ container = eval(script)
12
+ container.run(*init)
13
+ end
14
+ end
15
+ end
@@ -6,8 +6,12 @@ module Haconiwa::Runners
6
6
  # see http://d.hatena.ne.jp/hiboma/20120518/1337337393
7
7
 
8
8
  class Linux
9
- def self.run(base, init_command)
9
+ def self.run(base, init_command=[])
10
10
  container = fork {
11
+ if init_command.empty?
12
+ init_command = Array(base.init_command)
13
+ end
14
+
11
15
  base.namespace.apply!
12
16
  base.cgroup.register_all!(to: base.name)
13
17
 
@@ -19,12 +23,12 @@ module Haconiwa::Runners
19
23
  wrapper = Tempfile.open("haconiwa-wrapper-#{$$}-#{Time.now.to_i}.sh")
20
24
 
21
25
  wrapper.puts "#!/bin/bash"
22
- wrapper.puts "/bin/bash -c \""
26
+ wrapper.puts "/bin/bash -c '"
23
27
  if base.filesystem.mount_independent_procfs
24
28
  wrapper.puts "mount -t proc proc /proc;"
25
29
  end
26
- wrapper.puts "exec $1;"
27
- wrapper.puts "\""
30
+ wrapper.puts "exec $@;"
31
+ wrapper.puts "' -- \"$@\""
28
32
  wrapper.close
29
33
  FileUtils.chmod 0700, wrapper.path
30
34
 
@@ -34,18 +38,22 @@ module Haconiwa::Runners
34
38
 
35
39
  if base.namespace.use_pid_ns
36
40
  Bundler.with_clean_env {
37
- exec "unshare", "--pid", "--", wrapper.path, init_command
41
+ exec "unshare", "--pid", "--", wrapper.path, *init_command
38
42
  }
39
43
  else
40
- Bundler.with_clean_env { exec wrapper.path, init_command }
44
+ Bundler.with_clean_env { exec wrapper.path, *init_command }
41
45
  end
42
46
  }
43
47
 
44
48
  Haconiwa::SmallCgroup.register_at_exit(pid: container, name: base.name, dirs: base.cgroup.to_dirs)
45
49
  puts "New container: PID = #{container}"
46
50
 
47
- Process.waitpid container
48
- puts "Successfully exit container."
51
+ res = Process.waitpid2 container
52
+ if res[1].success?
53
+ puts "Successfully exit container."
54
+ else
55
+ puts "Container exited with status code <#{res[1].to_i}>."
56
+ end
49
57
  end
50
58
  end
51
59
  end
@@ -1,3 +1,3 @@
1
1
  module Haconiwa
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haconiwa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO
@@ -111,7 +111,8 @@ dependencies:
111
111
  description: Ruby on Container / helper tools with DSL for your handmade linux containers.
112
112
  email:
113
113
  - udzura@udzura.jp
114
- executables: []
114
+ executables:
115
+ - haconiwa
115
116
  extensions: []
116
117
  extra_rdoc_files: []
117
118
  files:
@@ -126,14 +127,17 @@ files:
126
127
  - Vagrantfile
127
128
  - bin/console
128
129
  - bin/setup
130
+ - examples/chroot.haco
129
131
  - examples/chroot.rb
130
132
  - examples/cpu.rb
131
133
  - examples/drop_cap_sys_time.rb
134
+ - exe/haconiwa
132
135
  - haconiwa.gemspec
133
136
  - lib/haconiwa.rb
134
137
  - lib/haconiwa/base.rb
135
138
  - lib/haconiwa/capabilities.rb
136
139
  - lib/haconiwa/cgroup.rb
140
+ - lib/haconiwa/cli.rb
137
141
  - lib/haconiwa/filesystem.rb
138
142
  - lib/haconiwa/mount_point.rb
139
143
  - lib/haconiwa/namespace.rb