~ubuntu-branches/ubuntu/quantal/lxc/quantal-201206191543

« back to all changes in this revision

Viewing changes to templates/lxc-lenny.in

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2010-06-28 10:15:48 UTC
  • mfrom: (1.2.4 upstream)
  • mto: (3.1.15 sid)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20100628101548-lmqvw55ypaxtbhhh
Tags: upstream-0.7.1
ImportĀ upstreamĀ versionĀ 0.7.1

Show diffs side-by-side

added added

removed removed

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