source: lab/vendor/lxc/0.8.0~rc1-4ubuntu37/templates/lxc-debian @ 175

Last change on this file since 175 was 175, checked in by mitty, 12 years ago
  • /usr/lib/lxc/templates of lxc 0.8.0~rc1-4ubuntu37 on Ubuntu 12.10 (beta)
  • Property svn:executable set to *
File size: 7.5 KB
Line 
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
23SUITE=${SUITE:-squeeze}
24MIRROR=${MIRROR:-http://cdn.debian.net/debian}
25
26configure_debian()
27{
28    rootfs=$1
29    hostname=$2
30
31    # squeeze only has /dev/tty and /dev/tty0 by default,
32    # therefore creating missing device nodes for tty1-4.
33    for tty in $(seq 1 4); do
34    if [ ! -e $rootfs/dev/tty$tty ]; then
35        mknod $rootfs/dev/tty$tty c 4 $tty
36    fi
37    done
38
39    # configure the inittab
40    cat <<EOF > $rootfs/etc/inittab
41id:3:initdefault:
42si::sysinit:/etc/init.d/rcS
43l0:0:wait:/etc/init.d/rc 0
44l1:1:wait:/etc/init.d/rc 1
45l2:2:wait:/etc/init.d/rc 2
46l3:3:wait:/etc/init.d/rc 3
47l4:4:wait:/etc/init.d/rc 4
48l5:5:wait:/etc/init.d/rc 5
49l6:6:wait:/etc/init.d/rc 6
50# Normally not reached, but fallthrough in case of emergency.
51z6:6:respawn:/sbin/sulogin
521:2345:respawn:/sbin/getty 38400 console
53c1:12345:respawn:/sbin/getty 38400 tty1 linux
54c2:12345:respawn:/sbin/getty 38400 tty2 linux
55c3:12345:respawn:/sbin/getty 38400 tty3 linux
56c4:12345:respawn:/sbin/getty 38400 tty4 linux
57p6::ctrlaltdel:/sbin/init 6
58p0::powerfail:/sbin/init 0
59EOF
60
61    # disable selinux in debian
62    mkdir -p $rootfs/selinux
63    echo 0 > $rootfs/selinux/enforce
64
65    # configure the network using the dhcp
66    cat <<EOF > $rootfs/etc/network/interfaces
67auto lo
68iface lo inet loopback
69
70auto eth0
71iface eth0 inet dhcp
72EOF
73
74    # set the hostname
75    cat <<EOF > $rootfs/etc/hostname
76$hostname
77EOF
78
79    # reconfigure some services
80    if [ -z "$LANG" ]; then
81    chroot $rootfs locale-gen en_US.UTF-8 UTF-8
82    chroot $rootfs update-locale LANG=en_US.UTF-8
83    else
84    chroot $rootfs locale-gen $LANG $(echo $LANG | cut -d. -f2)
85    chroot $rootfs update-locale LANG=$LANG
86    fi
87
88    # remove pointless services in a container
89    chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh remove
90    chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove
91    chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove
92    chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove
93
94    echo "root:root" | chroot $rootfs chpasswd
95    echo "Root password is 'root', please change !"
96
97    return 0
98}
99
100cleanup()
101{
102    rm -rf $cache/partial-$SUITE-$arch
103    rm -rf $cache/rootfs-$SUITE-$arch
104}
105
106download_debian()
107{
108    packages=\
109ifupdown,\
110locales,\
111libui-dialog-perl,\
112dialog,\
113isc-dhcp-client,\
114netbase,\
115net-tools,\
116iproute,\
117openssh-server
118
119    cache=$1
120    arch=$2
121
122    trap cleanup EXIT SIGHUP SIGINT SIGTERM
123    # check the mini debian was not already downloaded
124    mkdir -p "$cache/partial-$SUITE-$arch"
125    if [ $? -ne 0 ]; then
126    echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
127    return 1
128    fi
129
130    # download a mini debian into a cache
131    echo "Downloading debian minimal ..."
132    debootstrap --verbose --variant=minbase --arch=$arch \
133    --include=$packages \
134    "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
135    if [ $? -ne 0 ]; then
136    echo "Failed to download the rootfs, aborting."
137    return 1
138    fi
139
140    mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
141    echo "Download complete."
142    trap EXIT
143    trap SIGINT
144    trap SIGTERM
145    trap SIGHUP
146
147    return 0
148}
149
150copy_debian()
151{
152    cache=$1
153    arch=$2
154    rootfs=$3
155
156    # make a local copy of the minidebian
157    echo -n "Copying rootfs to $rootfs..."
158    mkdir -p $rootfs
159    rsync -a "$cache/rootfs-$SUITE-$arch"/ $rootfs/ || return 1
160    return 0
161}
162
163install_debian()
164{
165    cache="/var/cache/lxc/debian"
166    rootfs=$1
167    mkdir -p /var/lock/subsys/
168    (
169    flock -x 200
170    if [ $? -ne 0 ]; then
171        echo "Cache repository is busy."
172        return 1
173    fi
174
175    arch=$(dpkg --print-architecture)
176
177    echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
178    if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
179        download_debian $cache $arch
180        if [ $? -ne 0 ]; then
181        echo "Failed to download 'debian base'"
182        return 1
183        fi
184    fi
185
186    copy_debian $cache $arch $rootfs
187    if [ $? -ne 0 ]; then
188        echo "Failed to copy rootfs"
189        return 1
190    fi
191
192    return 0
193
194    ) 200>/var/lock/subsys/lxc
195
196    return $?
197}
198
199copy_configuration()
200{
201    path=$1
202    rootfs=$2
203    hostname=$3
204
205    grep -q "^lxc.rootfs" $path/config 2>/dev/null || echo "lxc.rootfs = $rootfs" >> $path/config
206    cat <<EOF >> $path/config
207lxc.tty = 4
208lxc.pts = 1024
209lxc.utsname = $hostname
210# uncomment the next line to run the container unconfined:
211#lxc.aa_profile = unconfined
212lxc.cgroup.devices.deny = a
213# /dev/null and zero
214lxc.cgroup.devices.allow = c 1:3 rwm
215lxc.cgroup.devices.allow = c 1:5 rwm
216# consoles
217lxc.cgroup.devices.allow = c 5:1 rwm
218lxc.cgroup.devices.allow = c 5:0 rwm
219lxc.cgroup.devices.allow = c 4:0 rwm
220lxc.cgroup.devices.allow = c 4:1 rwm
221# /dev/{,u}random
222lxc.cgroup.devices.allow = c 1:9 rwm
223lxc.cgroup.devices.allow = c 1:8 rwm
224lxc.cgroup.devices.allow = c 136:* rwm
225lxc.cgroup.devices.allow = c 5:2 rwm
226# rtc
227lxc.cgroup.devices.allow = c 254:0 rwm
228
229# mounts point
230lxc.mount.entry=proc proc proc nodev,noexec,nosuid 0 0
231lxc.mount.entry=sysfs sys sysfs defaults  0 0
232EOF
233
234    if [ $? -ne 0 ]; then
235    echo "Failed to add configuration"
236    return 1
237    fi
238
239    return 0
240}
241
242clean()
243{
244    cache="/var/cache/lxc/debian"
245
246    if [ ! -e $cache ]; then
247    exit 0
248    fi
249
250    # lock, so we won't purge while someone is creating a repository
251    (
252    flock -n -x 200
253    if [ $? != 0 ]; then
254        echo "Cache repository is busy."
255        exit 1
256    fi
257
258    echo -n "Purging the download cache..."
259    rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
260    exit 0
261
262    ) 200>/var/lock/subsys/lxc
263}
264
265usage()
266{
267    cat <<EOF
268$1 -h|--help -p|--path=<path> --clean
269EOF
270    return 0
271}
272
273options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
274if [ $? -ne 0 ]; then
275        usage $(basename $0)
276    exit 1
277fi
278eval set -- "$options"
279
280while true
281do
282    case "$1" in
283        -h|--help)      usage $0 && exit 0;;
284        -p|--path)      path=$2; shift 2;;
285    -n|--name)      name=$2; shift 2;;
286    -c|--clean)     clean=$2; shift 2;;
287        --)             shift 1; break ;;
288        *)              break ;;
289    esac
290done
291
292if [ ! -z "$clean" -a -z "$path" ]; then
293    clean || exit 1
294    exit 0
295fi
296
297type debootstrap
298if [ $? -ne 0 ]; then
299    echo "'debootstrap' command is missing"
300    exit 1
301fi
302
303if [ -z "$path" ]; then
304    echo "'path' parameter is required"
305    exit 1
306fi
307
308if [ "$(id -u)" != "0" ]; then
309    echo "This script should be run as 'root'"
310    exit 1
311fi
312
313# detect rootfs
314config="$path/config"
315if grep -q '^lxc.rootfs' $config 2>/dev/null ; then
316    rootfs=`grep 'lxc.rootfs =' $config | awk -F= '{ print $2 }'`
317else
318    rootfs=$path/rootfs
319fi
320
321
322install_debian $rootfs
323if [ $? -ne 0 ]; then
324    echo "failed to install debian"
325    exit 1
326fi
327
328configure_debian $rootfs $name
329if [ $? -ne 0 ]; then
330    echo "failed to configure debian for a container"
331    exit 1
332fi
333
334copy_configuration $path $rootfs $name
335if [ $? -ne 0 ]; then
336    echo "failed write configuration file"
337    exit 1
338fi
339
340if [ ! -z $clean ]; then
341    clean || exit 1
342    exit 0
343fi
Note: See TracBrowser for help on using the repository browser.