]> git.sven.stormbind.net Git - sven/scripts.git/blob - stormbind/lxc-debian
Add hacked lxc-debian script for Debian/Squeeze.
[sven/scripts.git] / stormbind / lxc-debian
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <daniel.lezcano@free.fr>
8
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 configure_debian()
24 {
25     rootfs=$1
26     hostname=$2
27
28     # configure the inittab
29     cat <<EOF > $rootfs/etc/inittab
30 id:3:initdefault:
31 si::sysinit:/etc/init.d/rcS
32 l0:0:wait:/etc/init.d/rc 0
33 l1:1:wait:/etc/init.d/rc 1
34 l2:2:wait:/etc/init.d/rc 2
35 l3:3:wait:/etc/init.d/rc 3
36 l4:4:wait:/etc/init.d/rc 4
37 l5:5:wait:/etc/init.d/rc 5
38 l6:6:wait:/etc/init.d/rc 6
39 # Normally not reached, but fallthrough in case of emergency.
40 z6:6:respawn:/sbin/sulogin
41 1:2345:respawn:/sbin/getty 38400 console
42 c1:12345:respawn:/sbin/getty 38400 tty1 linux
43 c2:12345:respawn:/sbin/getty 38400 tty2 linux
44 c3:12345:respawn:/sbin/getty 38400 tty3 linux
45 c4:12345:respawn:/sbin/getty 38400 tty4 linux
46 EOF
47
48     # disable selinux in debian
49     mkdir -p $rootfs/selinux
50     echo 0 > $rootfs/selinux/enforce
51
52     # configure the network using the dhcp
53     cat <<EOF > $rootfs/etc/network/interfaces
54 auto lo
55 iface lo inet loopback
56
57 auto eth0
58 iface eth0 inet dhcp
59 EOF
60
61     # set the hostname
62     cat <<EOF > $rootfs/etc/hostname
63 $hostname
64 EOF
65
66     # reconfigure some services
67     if [ -z "$LANG" ]; then
68         chroot $rootfs locale-gen en_US.UTF-8
69         chroot $rootfs update-locale LANG=en_US.UTF-8
70     else
71         chroot $rootfs locale-gen $LANG
72         chroot $rootfs update-locale LANG=$LANG
73     fi
74
75     # remove pointless services in a container
76     chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
77     chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
78     chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
79
80     echo "root:root" | chroot $rootfs chpasswd
81     echo "Root password is 'root', please change !"
82
83     return 0
84 }
85
86 download_debian()
87 {
88     packages=\
89 ifupdown,\
90 locales,\
91 libui-dialog-perl,\
92 dialog,\
93 isc-dhcp-client,\
94 netbase,\
95 net-tools,\
96 iproute,\
97 openssh-server
98
99     cache=$1
100     arch=$2
101
102     # check the mini debian was not already downloaded
103     mkdir -p "$cache/partial-$arch"
104     if [ $? -ne 0 ]; then
105         echo "Failed to create '$cache/partial-$arch' directory"
106         return 1
107     fi
108
109     # download a mini debian into a cache
110     echo "Downloading debian minimal ..."
111     debootstrap --verbose --variant=minbase --arch=$arch \
112         --include $packages \
113         squeeze $cache/partial-$arch http://ftp.hosteurope.de/mirror/ftp.debian.org/debian
114     if [ $? -ne 0 ]; then
115         echo "Failed to download the rootfs, aborting."
116         return 1
117     fi
118
119     mv "$1/partial-$arch" "$1/rootfs-$arch"
120     echo "Download complete."
121
122     return 0
123 }
124
125 copy_debian()
126 {
127     cache=$1
128     arch=$2
129     rootfs=$3
130
131     # make a local copy of the minidebian
132     echo -n "Copying rootfs to $rootfs..."
133     cp -a $cache/rootfs-$arch $rootfs || return 1
134     return 0
135 }
136
137 install_debian()
138 {
139     cache="/var/cache/lxc/debian"
140     rootfs=$1
141     mkdir -p /var/lock/subsys/
142     (
143         flock -n -x 200
144         if [ $? -ne 0 ]; then
145             echo "Cache repository is busy."
146             return 1
147         fi
148
149         arch=$(arch)
150         if [ "$arch" == "x86_64" ]; then
151             arch=amd64
152         fi
153
154         if [ "$arch" == "i686" ]; then
155             arch=i386
156         fi
157
158         echo "Checking cache download in $cache/rootfs-$arch ... "
159         if [ ! -e "$cache/rootfs-$arch" ]; then
160             download_debian $cache $arch
161             if [ $? -ne 0 ]; then
162                 echo "Failed to download 'debian base'"
163                 return 1
164             fi
165         fi
166
167         copy_debian $cache $arch $rootfs
168         if [ $? -ne 0 ]; then
169             echo "Failed to copy rootfs"
170             return 1
171         fi
172
173         return 0
174
175         ) 200>/var/lock/subsys/lxc
176
177     return $?
178 }
179
180 copy_configuration()
181 {
182     path=$1
183     rootfs=$2
184     name=$3
185
186     cat <<EOF >> $path/config
187 lxc.tty = 4
188 lxc.pts = 1024
189 lxc.rootfs = $rootfs
190 lxc.cgroup.devices.deny = a
191 # /dev/null and zero
192 lxc.cgroup.devices.allow = c 1:3 rwm
193 lxc.cgroup.devices.allow = c 1:5 rwm
194 # consoles
195 lxc.cgroup.devices.allow = c 5:1 rwm
196 lxc.cgroup.devices.allow = c 5:0 rwm
197 lxc.cgroup.devices.allow = c 4:0 rwm
198 lxc.cgroup.devices.allow = c 4:1 rwm
199 # /dev/{,u}random
200 lxc.cgroup.devices.allow = c 1:9 rwm
201 lxc.cgroup.devices.allow = c 1:8 rwm
202 lxc.cgroup.devices.allow = c 136:* rwm
203 lxc.cgroup.devices.allow = c 5:2 rwm
204 # rtc
205 lxc.cgroup.devices.allow = c 254:0 rwm
206
207 # mounts point
208 lxc.mount.entry=proc $rootfs/proc proc nodev,noexec,nosuid 0 0
209 lxc.mount.entry=devpts $rootfs/dev/pts devpts defaults 0 0
210 lxc.mount.entry=sysfs $rootfs/sys sysfs defaults  0 0
211 EOF
212
213     if [ $? -ne 0 ]; then
214         echo "Failed to add configuration"
215         return 1
216     fi
217
218     return 0
219 }
220
221 clean()
222 {
223     cache="/var/cache/lxc/debian"
224
225     if [ ! -e $cache ]; then
226         exit 0
227     fi
228
229     # lock, so we won't purge while someone is creating a repository
230     (
231         flock -n -x 200
232         if [ $? != 0 ]; then
233             echo "Cache repository is busy."
234             exit 1
235         fi
236
237         echo -n "Purging the download cache..."
238         rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
239         exit 0
240
241     ) 200>/var/lock/subsys/lxc
242 }
243
244 usage()
245 {
246     cat <<EOF
247 $1 -h|--help -p|--path=<path> --clean
248 EOF
249     return 0
250 }
251
252 options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
253 if [ $? -ne 0 ]; then
254         usage $(basename $0)
255         exit 1
256 fi
257 eval set -- "$options"
258
259 while true
260 do
261     case "$1" in
262         -h|--help)      usage $0 && exit 0;;
263         -p|--path)      path=$2; shift 2;;
264         -n|--name)      name=$2; shift 2;;
265         -c|--clean)     clean=$2; shift 2;;
266         --)             shift 1; break ;;
267         *)              break ;;
268     esac
269 done
270
271 if [ ! -z "$clean" -a -z "$path" ]; then
272     clean || exit 1
273     exit 0
274 fi
275
276 type debootstrap
277 if [ $? -ne 0 ]; then
278     echo "'debootstrap' command is missing"
279     exit 1
280 fi
281
282 if [ -z "$path" ]; then
283     echo "'path' parameter is required"
284     exit 1
285 fi
286
287 if [ "$(id -u)" != "0" ]; then
288     echo "This script should be run as 'root'"
289     exit 1
290 fi
291
292 rootfs=$path/
293
294 install_debian $rootfs
295 if [ $? -ne 0 ]; then
296     echo "failed to install debian"
297     exit 1
298 fi
299
300 configure_debian $rootfs $name
301 if [ $? -ne 0 ]; then
302     echo "failed to configure debian for a container"
303     exit 1
304 fi
305
306 copy_configuration $path $rootfs
307 if [ $? -ne 0 ]; then
308     echo "failed write configuration file"
309     exit 1
310 fi
311
312 if [ ! -z $clean ]; then
313     clean || exit 1
314     exit 0
315 fi