forked from phbaer/lxc-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxc-debian
executable file
·395 lines (327 loc) · 9.29 KB
/
lxc-debian
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/bin/bash
#
# Creates a debian based container
#
# This script is based on the lxc-debian example script that ships with lxc.
#
# Copyright (C) 2010 Nigel McNie
#
CONFFILE="lxc.conf"
MNTFILE=
TMPMNTFILE=
UTSNAME=
MTU="1500"
DEBMIRROR=http://ftp.debian.org/debian
FQDN=$(hostname --fqdn)
################################################################################
# debian custom configuration files
################################################################################
# Disable selinux in the container
write_debian_selinux() {
mkdir -p $ROOTFS/selinux
echo 0 > $ROOTFS/selinux/enforce
}
# Write out a custom fstab
write_debian_fstab() {
cat <<EOF > $ROOTFS/etc/fstab
tmpfs /dev/shm tmpfs defaults 0 0
EOF
}
# Write out a custom inittab
write_debian_inittab() {
cat <<EOF > $ROOTFS/etc/inittab
id:3:initdefault:
si::sysinit:/etc/init.d/rcS
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
z6:6:respawn:/sbin/sulogin
1:2345:respawn:/sbin/getty 38400 console
c1:12345:respawn:/sbin/getty 38400 tty1 linux
c2:12345:respawn:/sbin/getty 38400 tty2 linux
c3:12345:respawn:/sbin/getty 38400 tty3 linux
c4:12345:respawn:/sbin/getty 38400 tty4 linux
EOF
}
# Write out network config (dhcp)
write_debian_network() {
cat <<EOF > $ROOTFS/etc/network/interfaces
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet dhcp
EOF
cat <<EOF >> $ROOTFS/etc/dhcp3/dhclient.conf
send host-name "$UTSNAME";
EOF
}
# Set the hostname for the container
write_debian_hostname() {
cat <<EOF > $ROOTFS/etc/hostname
$UTSNAME
EOF
cat <<EOF > $ROOTFS/etc/hosts
127.0.0.1 $UTSNAME localhost
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF
}
# Writes a custom ssh config that allows root logins
write_debian_sshd_config() {
cat <<EOF > $ROOTFS/etc/ssh/sshd_config
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
UsePrivilegeSeparation yes
KeyRegenerationInterval 3600
ServerKeyBits 768
SyslogFacility AUTH
LogLevel INFO
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes
RSAAuthentication yes
PubkeyAuthentication yes
IgnoreRhosts yes
RhostsRSAAuthentication no
HostbasedAuthentication no
PermitEmptyPasswords yes
ChallengeResponseAuthentication no
EOF
}
# Autoconfigures packages in the container so no debconf questions are asked
# when it's made
reconfigure_debian_packages() {
default_environment_locale=$(debconf-show locales | grep default_environment_locale | cut -f 2 -d :)
locales_to_be_generated=$(debconf-show locales | grep locales_to_be_generated | cut -f 2 -d :)
tzdata_area=$(debconf-show tzdata | grep Areas | cut -f 2 -d :)
tzdata_zone=$(debconf-show tzdata | grep Zones/${tzdata_area:1} | cut -f 2 -d :)
cat <<EOF | chroot $ROOTFS sh
#!/bin/sh
export LANG=C
echo "locales locales/default_environment_locale select$default_environment_locale" > /debconf-selections
echo "locales locales/locales_to_be_generated multiselect$locales_to_be_generated" >> /debconf-selections
echo "tzdata tzdata/Areas select$tzdata_area" >> /debconf-selections
echo "tzdata tzdata/Zones/${tzdata_area:1} select$tzdata_zone" >> /debconf-selections
debconf-set-selections /debconf-selections
rm -f /etc/locale.gen
dpkg-reconfigure -u locales
echo ${tzdata_area:1}/${tzdata_zone:1} > /etc/timezone
cp /usr/share/zoneinfo/${tzdata_area:1}/${tzdata_zone:1} /etc/localtime
dpkg-reconfigure -u tzdata
rm -f /debconf-selections
EOF
}
# Disables services a container doesn't need
disable_debian_services() {
chroot $ROOTFS /usr/sbin/update-rc.d -f umountfs remove
chroot $ROOTFS /usr/sbin/update-rc.d -f hwclock.sh remove
chroot $ROOTFS /usr/sbin/update-rc.d -f hwclockfirst.sh remove
}
################################################################################
# lxc configuration files
################################################################################
write_lxc_configuration() {
cat <<EOF > $CONFFILE
lxc.utsname = $UTSNAME
lxc.tty = 4
lxc.pts = 1024
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.name = eth0
lxc.network.mtu = $MTU
lxc.mount = $TMPMNTFILE
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
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() {
TMPMNTFILE=$(mktemp lxc.XXXXXXXXXX)
if [ ! -z "$MNTFILE" ]; then
cp $MNTFILE $TMPMNTFILE
fi
}
collect_information() {
# 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
echo -n "Specify the location for an extra fstab file [(none)] "
read _MNTFILE_
if [ ! -z "$_MNTFILE_" ]; then
MNTFILE=$_MNTFILE_
fi
}
install_debian()
{
ARCH=$(dpkg --print-architecture)
case $ARCH in
amd64) ARCH=amd64 ;;
i386 ) ARCH=i386 ;;
* ) echo "Unsupported architecture $ARCH!"; exit 1; ;;
esac
echo "Choose the architecture for the container (choices as for deboostrap, e.g.: amd64, i386"
echo -n "Choice ? [$ARCH] "
read _ARCH_
if [ ! -z "$_ARCH_" ]; then
ARCH=$_ARCH_
fi
# choose a mirror
echo -n "Specify the debian mirror to use to download the rootfs [$DEBMIRROR] "
read _DEBMIRROR_
if [ ! -z "$_DEBMIRROR_" ]; then
DEBMIRROR=$_DEBMIRROR_
fi
# download a mini debian into a cache
echo "Downloading debian minimal ..."
debootstrap --verbose --variant=minbase --arch=$ARCH \
--include ifupdown,locales,libui-dialog-perl,dialog,netbase,net-tools,iproute,openssh-server,dhcp3-client \
lenny $ROOTFS $DEBMIRROR
RESULT=$?
if [ "$RESULT" != "0" ]; then
echo "Failed to download the rootfs, aborting."
exit 1
fi
}
run_pre_create_hook() {
# source a hook that's run before the container is created
# sourced so it has access to our variables, like $NAME
test -x /etc/lxc-debian/host-pre-create && . /etc/lxc-debian/host-pre-create
}
run_post_create_hooks() {
# source host and guest hooks for after the container is created
test -x /etc/lxc-debian/host-post-create && . /etc/lxc-debian/host-post-create
if [ -x /etc/lxc-debian/guest-post-create ]; then
cp /etc/lxc-debian/guest-post-create $ROOTFS/guest-post-create
chroot $ROOTFS /bin/bash /guest-post-create
chroot $ROOTFS /bin/rm /guest-post-create
fi
}
create() {
collect_information
write_lxc_mounts
write_lxc_configuration
/usr/bin/lxc-create -n $NAME -f $CONFFILE
RES=$?
# remove the configuration files
rm -f $CONFFILE
rm -f $TMPMNTFILE
if [ "$RES" != "0" ]; then
echo "Failed to create '$NAME'"
exit 1
fi
install_debian
write_debian_inittab
write_debian_hostname
write_debian_fstab
write_debian_network
write_debian_sshd_config
write_debian_selinux
reconfigure_debian_packages
disable_debian_services
run_pre_create_hook
run_post_create_hooks
echo "Done."
echo -e "\nYou can run your container with the 'lxc-start -n $NAME'\n"
}
destroy() {
echo -n "This will PERMANENTLY DESTROY the container $NAME. Are you sure? [y/N] ? "
read
if [ "$REPLY" != "y" ]; then
echo "Abort."
exit
fi
halt=$(which lxc-halt)
if lxc-info -n $NAME | grep RUNNING > /dev/null; then
if [ -n "$halt" ]; then
lxc-halt -n $NAME
else
lxc-stop -n $NAME
fi
fi
sleep .5
rm -rf $ROOTFS
/usr/bin/lxc-destroy -n $NAME
RETVAL=$?
if [ ! $RETVAL -eq 0 ]; then
echo "Failed to destroyed '$NAME'"
return $RETVAL
fi
return 0
}
help() {
cat <<EOF
This script is a helper to create debian system containers.
The script will create the container configuration file following
the informations submitted interactively with 'lxc-debian create'
Have fun :)
EOF
}
usage() {
echo "Usage: $(readlink -f $0) {create|destroy} -n NAME"
exit 0
}
# Main program
if [ $# = 0 ]; then
usage
fi
case "$1" in
-h|-help|--help) help;;
esac
if [ "$(id -u)" != "0" ]; then
echo "This script should be run as 'root'"
exit 1
fi
NAME=""
COMMAND=$1
shift
while getopts "n:" flag
do
case $flag in
n) NAME="$OPTARG" ;;
*)
echo "Unknown argument: $flag"
exit 1
;;
esac
done
if [ -z "$NAME" ]; then
echo "lxc-debian: missing container name, use -n option"
exit 1
fi
ROOTFS="/var/lib/lxc/$NAME/rootfs"
case "$COMMAND" in
create) create;;
destroy) destroy;;
*) usage;;
esac