-
Notifications
You must be signed in to change notification settings - Fork 1
/
xlxc.rb
162 lines (135 loc) · 3.74 KB
/
xlxc.rb
1
2
3
4
5
6
7
8
9
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#
# xlxc: a class for XIA LXC container defintions
#
# Author: Cody Doucette <doucette@bu.edu>
#
class XLXC
# Directory where containers are kept on host.
LXC = "/var/lib/lxc"
# Directories that are bind mounted (read-only) from the host.
BIND_MOUNTED_DIRECTORIES = [
"/bin",
"/lib64",
"/lib",
"/sbin",
"/usr"
]
# Bind mount a source file to a destination file.
#
def self.bind_mount(src, dst, isDir, readOnly)
if isDir
FileUtils.mkdir_p(dst)
else
FileUtils.touch(dst)
end
`mount --rbind #{src} #{dst}`
if readOnly
`mount -o remount,ro #{dst}`
end
end
# Set up the network for this bridge. This involves re-creating the
# Ethernet bridge (if necessary) and allocating an IP address.
#
def self.setup_net(name)
bridge = XLXC_BRIDGE.get_bridge(name)
cidr = XLXC_BRIDGE.get_bridge_cidr(bridge)
iface = XLXC_BRIDGE.get_bridge_iface(bridge)
# If this interface is not up, create it.
interfaces = Dir.entries(XLXC_BRIDGE::INTERFACES)
if !interfaces.include?(bridge)
XLXC_BRIDGE.add_interface(bridge, cidr, iface)
end
if XLXC_BRIDGE.get_ip_addr(name, bridge) == nil
XLXC_BRIDGE.alloc_ip_address_from_bridge(name, bridge)
end
end
# Perform bind mounts necessary to run container.
#
def self.setup_fs(name)
rootfs = File.join(LXC, name, "rootfs")
# Bind mount (read-only) directories from host.
for dir in BIND_MOUNTED_DIRECTORIES
if !Dir.exists?(File.join(rootfs, dir)) ||
Dir.entries(File.join(rootfs, dir)).size() <= 2
bind_mount(dir, File.join(rootfs, dir), true, true)
end
end
end
# Default configuration data for each LXC container. More
# configuration data is appended in xlxc-create.
LXC_CONFIG_TEMPLATE =
"lxc.network.type=veth
lxc.network.flags=up
lxc.devttydir=lxc
lxc.tty=4
lxc.pts=1024
lxc.cap.drop=sys_module mac_admin mac_override
lxc.pivotdir=lxc_putold
lxc.cgroup.devices.deny = a
# Allow any mknod (but not using the node)
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
# /dev/null and zero
lxc.cgroup.devices.allow = c 1:3 rwm
lxc.cgroup.devices.allow = c 1:5 rwm
# consoles
lxc.cgroup.devices.allow = c 5:1 rwm
lxc.cgroup.devices.allow = c 5:0 rwm
#lxc.cgroup.devices.allow = c 4:0 rwm
#lxc.cgroup.devices.allow = c 4:1 rwm
# /dev/{,u}random
lxc.cgroup.devices.allow = c 1:9 rwm
lxc.cgroup.devices.allow = c 1:8 rwm
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
#fuse
lxc.cgroup.devices.allow = c 10:229 rwm
#tun
lxc.cgroup.devices.allow = c 10:200 rwm
#full
lxc.cgroup.devices.allow = c 1:7 rwm
#hpet
lxc.cgroup.devices.allow = c 10:228 rwm
#kvm
lxc.cgroup.devices.allow = c 10:232 rwm
lxc.arch=amd64
lxc.aa_allow_incomplete = 1
"
# Data to be entered into each container's fstab file.
FSTAB_TEMPLATE =
"proc proc proc nodev,noexec,nosuid 0 0
sysfs sys sysfs defaults 0 0
"
# File that holds interface information.
INTERFACES_FILE = "/etc/network/interfaces"
# File that holds host information.
HOSTS_FILE = "/etc/hosts"
# File that holds hostname information.
HOSTNAME_FILE = "/etc/hostname"
# Interface file with a format tag to make each
# container's IP address unique.
INTERFACES_TEMPLATE =
"auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address %s
netmask %s
network %s
broadcast %s
gateway %s
"
# Hosts file with a format tag for a unique hostname.
HOSTS_TEMPLATE =
"127.0.0.1 localhost
127.0.1.1 %s
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
"
end