source: lab.git/lxc/0.7.5-3ubuntu63/templates/lxc-debian @ 995c968

vendor
Last change on this file since 995c968 was 995c968, checked in by mitty <mitty@…>, 12 years ago
  • /usr/lib/lxc/templates of lxc 0.7.5-3ubuntu63 on Ubuntu 12.04.1

git-svn-id: https://lab.mitty.jp/svn/lab/vendor@174 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

  • Property mode set to 100755
File size: 9.4 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:2: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
57EOF
58
59    # disable selinux in debian
60    mkdir -p $rootfs/selinux
61    echo 0 > $rootfs/selinux/enforce
62
63    # configure the network using the dhcp
64    cat <<EOF > $rootfs/etc/network/interfaces
65auto lo
66iface lo inet loopback
67
68auto eth0
69iface eth0 inet dhcp
70EOF
71
72    # set the hostname
73    cat <<EOF > $rootfs/etc/hostname
74$hostname
75EOF
76
77    # reconfigure some services
78    LANG="${LANG:-en_US.UTF-8}"
79
80    locale="$LANG $(echo $LANG | cut -d. -f2)"
81    chroot $rootfs echo "locales locales/default_environment_locale select $LANG" | chroot $rootfs sh -c "LANG=C debconf-set-selections"
82    chroot $rootfs echo "locales locales/default_environment_locale seen true" | chroot $rootfs sh -c "LANG=C debconf-set-selections"
83    chroot $rootfs echo "locales locales/locales_to_be_generated seen true" | chroot $rootfs sh -c "LANG=C debconf-set-selections"
84    chroot $rootfs sed -i -e "0,/^[# ]*$locale *$/ s/^[# ]*$locale *$/$locale/" /etc/locale.gen
85    chroot $rootfs sh -c "LANG=C dpkg-reconfigure locales -f noninteractive"
86
87    # remove pointless services in a container
88    chroot $rootfs /usr/sbin/update-rc.d -f checkroot.sh remove # S
89    chroot $rootfs /usr/sbin/update-rc.d checkroot.sh stop 09 S .
90
91    chroot $rootfs /usr/sbin/update-rc.d -f umountfs remove # 0 6
92    chroot $rootfs /usr/sbin/update-rc.d umountfs start 09 0 6 .
93
94    chroot $rootfs /usr/sbin/update-rc.d -f umountroot remove # 0 6
95    chroot $rootfs /usr/sbin/update-rc.d umountroot start 10 0 6 .
96
97    # The following initscripts don't provide an empty start or stop block.
98    # To prevent them being enabled on upgrades, we leave a start link on
99    # runlevel 3.
100    chroot $rootfs /usr/sbin/update-rc.d -f hwclock.sh remove # S 0 6
101    chroot $rootfs /usr/sbin/update-rc.d hwclock.sh start 10 3 .
102
103    chroot $rootfs /usr/sbin/update-rc.d -f hwclockfirst.sh remove # S
104    chroot $rootfs /usr/sbin/update-rc.d hwclockfirst start 08 3 .
105
106    chroot $rootfs /usr/sbin/update-rc.d -f module-init-tools remove # S
107    chroot $rootfs /usr/sbin/update-rc.d module-init-tools start 10 3 .
108
109    echo "root:root" | chroot $rootfs chpasswd
110    echo "Root password is 'root', please change !"
111
112    return 0
113}
114
115download_debian()
116{
117    packages=\
118ifupdown,\
119locales,\
120libui-dialog-perl,\
121dialog,\
122dhcp3-client,\
123netbase,\
124net-tools,\
125iproute,\
126openssh-server
127
128    cache=$1
129    arch=$2
130
131    # check the mini debian was not already downloaded
132    mkdir -p "$cache/partial-$SUITE-$arch"
133    if [ $? -ne 0 ]; then
134    echo "Failed to create '$cache/partial-$SUITE-$arch' directory"
135    return 1
136    fi
137
138    # download a mini debian into a cache
139    echo "Downloading debian minimal ..."
140    debootstrap --verbose --variant=minbase --arch=$arch \
141    --include=$packages \
142    "$SUITE" "$cache/partial-$SUITE-$arch" $MIRROR
143    if [ $? -ne 0 ]; then
144    echo "Failed to download the rootfs, aborting."
145    return 1
146    fi
147
148    mv "$1/partial-$SUITE-$arch" "$1/rootfs-$SUITE-$arch"
149    echo "Download complete."
150
151    return 0
152}
153
154copy_debian()
155{
156    cache=$1
157    arch=$2
158    rootfs=$3
159
160    # make a local copy of the minidebian
161    echo -n "Copying rootfs to $rootfs..."
162    mkdir -p $rootfs
163    rsync -a "$cache/rootfs-$SUITE-$arch"/ $rootfs/ || return 1
164    return 0
165}
166
167install_debian()
168{
169    cache="/var/cache/lxc/debian"
170    rootfs=$1
171    mkdir -p /var/lock/subsys/
172    (
173    flock -x 200
174    if [ $? -ne 0 ]; then
175        echo "Cache repository is busy."
176        return 1
177    fi
178
179    # Code taken from debootstrap
180    if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
181        arch=`/usr/bin/dpkg --print-architecture`
182    elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
183        arch=`/usr/bin/udpkg --print-architecture`
184    else
185        arch=$(arch)
186        case $arch in
187        686)           arch="i386";;
188        x86_64)        arch="amd64";;
189        ppc)           arch="powerpc";;
190        esac
191    fi
192
193    echo "Checking cache download in $cache/rootfs-$SUITE-$arch ... "
194    if [ ! -e "$cache/rootfs-$SUITE-$arch" ]; then
195        download_debian $cache $arch
196        if [ $? -ne 0 ]; then
197        echo "Failed to download 'debian base'"
198        return 1
199        fi
200    fi
201
202    copy_debian $cache $arch $rootfs
203    if [ $? -ne 0 ]; then
204        echo "Failed to copy rootfs"
205        return 1
206    fi
207
208    return 0
209
210    ) 200>/var/lock/subsys/lxc
211
212    return $?
213}
214
215copy_configuration()
216{
217    path=$1
218    rootfs=$2
219    name=$3
220
221    cat >> $path/config << EOF
222# $path/config
223
224## Container
225lxc.utsname                             = $name
226lxc.rootfs                              = $rootfs
227lxc.tty                                 = 4
228lxc.pts                                 = 1024
229#lxc.console                            = /var/log/lxc/$name.console
230
231## Capabilities
232lxc.cap.drop                            = sys_admin
233
234# uncomment the next line to run the container unconfined:
235#lxc.aa_profile = unconfined
236
237## Devices
238#lxc.cgroup.devices.allow               = a
239lxc.cgroup.devices.deny                 = a
240# /dev/null
241lxc.cgroup.devices.allow                = c 1:3 rwm
242# /dev/zero
243lxc.cgroup.devices.allow                = c 1:5 rwm
244# /dev/tty[1-4] consoles
245lxc.cgroup.devices.allow                = c 5:1 rwm
246lxc.cgroup.devices.allow                = c 5:0 rwm
247lxc.cgroup.devices.allow                = c 4:0 rwm
248lxc.cgroup.devices.allow                = c 4:1 rwm
249# /dev/{,u}random
250lxc.cgroup.devices.allow                = c 1:9 rwm
251lxc.cgroup.devices.allow                = c 1:8 rwm
252lxc.cgroup.devices.allow                = c 136:* rwm
253lxc.cgroup.devices.allow                = c 5:2 rwm
254# /dev/rtc
255lxc.cgroup.devices.allow                = c 254:0 rwm
256
257## Limits
258#lxc.cgroup.cpu.shares                  = 1024
259#lxc.cgroup.cpuset.cpus                 = 0
260#lxc.cgroup.memory.limit_in_bytes       = 256M
261#lxc.cgroup.memory.memsw.limit_in_bytes = 1G
262
263## Filesystem
264lxc.mount.entry                         = proc proc proc nodev,noexec,nosuid 0 0
265lxc.mount.entry                         = sysfs sys sysfs defaults,ro 0 0
266#lxc.mount.entry                        = /srv/$name srv/$name none defaults,bind 0 0
267EOF
268
269    if [ $? -ne 0 ]; then
270    echo "Failed to add configuration"
271    return 1
272    fi
273
274    return 0
275}
276
277clean()
278{
279    cache="/var/cache/lxc/debian"
280
281    if [ ! -e $cache ]; then
282    exit 0
283    fi
284
285    # lock, so we won't purge while someone is creating a repository
286    (
287    flock -x 200
288    if [ $? != 0 ]; then
289        echo "Cache repository is busy."
290        exit 1
291    fi
292
293    echo -n "Purging the download cache..."
294    rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
295    exit 0
296
297    ) 200>/var/lock/subsys/lxc
298}
299
300usage()
301{
302    cat <<EOF
303$1 -h|--help -p|--path=<path> --clean
304EOF
305    return 0
306}
307
308options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
309if [ $? -ne 0 ]; then
310        usage $(basename $0)
311    exit 1
312fi
313eval set -- "$options"
314
315while true
316do
317    case "$1" in
318        -h|--help)      usage $0 && exit 0;;
319        -p|--path)      path=$2; shift 2;;
320    -n|--name)      name=$2; shift 2;;
321    -c|--clean)     clean=$2; shift 2;;
322        --)             shift 1; break ;;
323        *)              break ;;
324    esac
325done
326
327if [ ! -z "$clean" -a -z "$path" ]; then
328    clean || exit 1
329    exit 0
330fi
331
332type debootstrap
333if [ $? -ne 0 ]; then
334    echo "'debootstrap' command is missing"
335    exit 1
336fi
337
338if [ -z "$path" ]; then
339    echo "'path' parameter is required"
340    exit 1
341fi
342
343if [ "$(id -u)" != "0" ]; then
344    echo "This script should be run as 'root'"
345    exit 1
346fi
347
348rootfs=$path/rootfs
349
350install_debian $rootfs
351if [ $? -ne 0 ]; then
352    echo "failed to install debian"
353    exit 1
354fi
355
356configure_debian $rootfs $name
357if [ $? -ne 0 ]; then
358    echo "failed to configure debian for a container"
359    exit 1
360fi
361
362copy_configuration $path $rootfs
363if [ $? -ne 0 ]; then
364    echo "failed write configuration file"
365    exit 1
366fi
367
368if [ ! -z $clean ]; then
369    clean || exit 1
370    exit 0
371fi
Note: See TracBrowser for help on using the repository browser.