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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#!/bin/bash
# set -ex
DISTRO="fedora"
DISTROVER="12"
CACHE="/var/cache/lxc/${DISTRO}"
# Default container name
NAME="fedora"
CONFFILE="lxc.conf"
MNTFILE="mount.conf"
UTSNAME=
MTU="1500"
# These paths are within the container so do not need to obey configure prefixes
FSTAB="/etc/fstab"
################################################################################
# DISTRO custom configuration files
################################################################################
# custom selinux
write_distro_selinux() {
mkdir -p ${ROOTFS}/selinux
echo 0 > ${ROOTFS}/selinux/enforce
}
# custom fstab
write_distro_fstab() {
cat <<EOF > ${ROOTFS}/${FSTAB}
tmpfs /dev/shm tmpfs defaults 0 0
none /dev/pts devpts defaults 0 0
EOF
}
# custom network configuration
write_distro_network() {
cat <<EOF > ${ROOTFS}/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
HOSTNAME=${UTSNAME}
NM_CONTROLLED=no
TYPE=Ethernet
MTU=${MTU}
EOF
}
# custom hostname
write_distro_hostname() {
cat <<EOF > ${ROOTFS}/etc/sysconfig/network
NETWORKING=yes
HOSTNAME=${UTSNAME}
EOF
}
################################################################################
# lxc configuration files
################################################################################
write_lxc_configuration() {
cat <<EOF > ${CONFFILE}
lxc.utsname = ${UTSNAME}
lxc.tty = 4
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = virbr0
lxc.network.name = eth0
lxc.network.mtu = ${MTU}
lxc.mount = ${MNTFILE}
lxc.rootfs = ${ROOTFS}
lxc.cgroup.devices.deny = a
# /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
# /dev/pts/* - pts namespaces are "coming soon"
lxc.cgroup.devices.allow = c 136:* rwm
lxc.cgroup.devices.allow = c 5:2 rwm
# rtc
lxc.cgroup.devices.allow = c 254:0 rwm
EOF
}
write_lxc_mounts() {
cat <<EOF > ${MNTFILE}
EOF
}
post_config_distro() {
sed -i 's|.sbin.start_udev||' ${ROOTFS}/etc/rc.sysinit
sed -i 's|.sbin.start_udev||' ${ROOTFS}/etc/rc.d/rc.sysinit
chroot ${ROOTFS} chkconfig udev-post off
chroot ${ROOTFS} chkconfig auditd off
chroot ${ROOTFS} chkconfig network on
chroot ${ROOTFS} passwd
}
write_distro_devd() {
# taken from http://blog.bodhizazen.net/linux/lxc-configure-fedora-containers/
ROOTFSDEV="${ROOTFS}/dev"
rm -rf ${ROOTFSDEV}/
mkdir ${ROOTFSDEV}
mknod -m 666 ${ROOTFSDEV}/null c 1 3
mknod -m 666 ${ROOTFSDEV}/zero c 1 5
mknod -m 666 ${ROOTFSDEV}/random c 1 8
mknod -m 666 ${ROOTFSDEV}/urandom c 1 9
mkdir -m 755 ${ROOTFSDEV}/pts
mkdir -m 1777 ${ROOTFSDEV}/shm
mknod -m 666 ${ROOTFSDEV}/tty c 5 0
mknod -m 666 ${ROOTFSDEV}/tty0 c 4 0
mknod -m 666 ${ROOTFSDEV}/tty1 c 4 1
mknod -m 666 ${ROOTFSDEV}/tty2 c 4 2
mknod -m 666 ${ROOTFSDEV}/tty3 c 4 3
mknod -m 666 ${ROOTFSDEV}/tty4 c 4 4
mknod -m 600 ${ROOTFSDEV}/console c 5 1
mknod -m 666 ${ROOTFSDEV}/full c 1 7
mknod -m 600 ${ROOTFSDEV}/initctl p
mknod -m 666 ${ROOTFSDEV}/ptmx c 5 2
}
create() {
# choose a container name, default is already in shell NAME variable
echo -n "What is the name for the container ? [${NAME}] "
read _NAME_
if [ ! -z "${_NAME_}" ]; then
NAME=${_NAME_}
fi
# choose a hostname, default is the container name
echo -n "What hostname do you wish for this container ? [${NAME}] "
read _UTSNAME_
if [ ! -z "${_UTSNAME_}" ]; then
UTSNAME=${_UTSNAME_}
else
UTSNAME=${NAME}
fi
# choose the MTU size
echo -n "What is the MTU size ? [$MTU] "
read _MTU_
if [ ! -z "$_MTU_" ]; then
MTU=$_MTU_
fi
# the rootfs name will be build with the container name
ROOTFS="/var/lib/lxc/rootfs/${NAME}"
# check if the rootfs does already exist
if [ ! -e "${ROOTFS}" ]; then
mkdir -p /var/lock/subsys/
(
flock -n -x 200
RES=$?
if [ "${RES}" != "0" ]; then
echo "Cache repository is busy."
break
fi
# check the mini distro was not already downloaded
echo -n "Checking cache download ..."
if [ ! -e "${CACHE}/rootfs" ]; then
echo "not cached"
echo "Bootstrapping ${DISTRO}"
febootstrap ${DISTRO}-${DISTROVER} ${CACHE}/partial
RESULT=$?
if [ "${RESULT}" != "0" ]; then
echo "Failed to download the rootfs, aborting."
exit 1
fi
mv "${CACHE}/partial" "${CACHE}/rootfs"
echo "Download complete."
else
echo "Found."
fi
# make a local copy of the mini
echo -n "Copying rootfs ..."
cp -a ${CACHE}/rootfs ${ROOTFS} && echo "Done." || exit
) 200> "/var/lock/subsys/lxc"
fi
write_lxc_mounts
write_lxc_configuration
write_distro_hostname
write_distro_fstab
write_distro_network
write_distro_selinux
write_distro_devd
post_config_distro
/usr/bin/lxc-create -n ${NAME} -f ${CONFFILE}
RES=$?
# remove the configuration files
rm -f ${CONFFILE}
rm -f ${MNTFILE}
if [ "${RES}" != "0" ]; then
echo "Failed to create '${NAME}'"
exit 1
fi
echo "Done."
echo -e "\nYou can run your container with the 'lxc-start -n ${NAME}'\n"
}
destroy() {
echo -n "What is the name for the container ? [${NAME}] "
read _NAME_
if [ ! -z "${_NAME_}" ]; then
NAME=${_NAME_}
fi
/usr/bin/lxc-destroy -n ${NAME}
RETVAL=$?
if [ ! ${RETVAL} -eq 0 ]; then
echo "Failed to destroyed '${NAME}'"
return ${RETVAL}
fi
ROOTFS="/var/lib/lxc/rootfs/${NAME}"
echo -n "Shall I remove the rootfs [y/n] ? "
read
if [ "${REPLY}" = "y" ]; then
rm -rf ${ROOTFS}
fi
return 0
}
help() {
cat <<EOF
This script is a helper to create ${DISTRO} system containers.
The script will create the container configuration file following
the informations submitted interactively with 'lxc-${DISTRO} create'
The first creation will download, with yum, a ${DISTRO} minimal
install and store it into a cache.
The script will copy from the cache the root filesystem to the
current directory.
If there is a problem with the container, (bad configuration for
example), you can destroy the container with 'lxc-${DISTRO} destroy'
but without removing the rootfs and recreate it again with
'lxc-${DISTRO} create'.
If you want to create another ${DISTRO} container, call the 'lxc-${DISTRO}
create' again, specifying another name and new parameters.
At any time you can purge the ${DISTRO} cache download by calling
'lxc-${DISTRO} purge'
Have fun :)
EOF
}
purge() {
if [ ! -e ${CACHE} ]; then
exit 0
fi
# lock, so we won't purge while someone is creating a repository
(
flock -n -x 200
RES=$?
if [ "${RES}" != "0" ]; then
echo "Cache repository is busy."
exit 1
fi
echo -n "Purging the download cache..."
rm --preserve-root --one-file-system -rf ${CACHE} && echo "Done." || exit 1
exit 0
) 200> "/var/lock/subsys/lxc"
}
# Note: assuming uid==0 is root -- might break with userns??
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
# Detect which executable we were run as, lxc-fedora or lxc-redhat
DISTRO="fedora"
CACHE="/var/cache/lxc/${DISTRO}"
mkdir -p ${CACHE}
case "$1" in
create)
create;;
destroy)
destroy;;
help)
help;;
purge)
purge;;
*)
echo "Usage: $0 {create|destroy|purge|help}"
esac
|