~serge-hallyn/ubuntu/natty/lxc/fix-restart

« back to all changes in this revision

Viewing changes to .pc/0005-Make-debian-mirror-configurable-and-default-to-cdn.patch/templates/lxc-debian.in

  • Committer: Bazaar Package Importer
  • Author(s): Serge Hallyn
  • Date: 2011-01-23 17:28:55 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20110123172855-1vpaxel9kwocx7qz
Tags: 0.7.3.1-0ubuntu1
* Base on new upstream git tree with new maverick and natty templates,
  and able to run without ns cgroup.
* Send a 'container=lxc' variable to upstart.  The upstream git has
  the same patch, though this tree has it as a quilt patch.
* Add lxcguest package which converts a system into one which can
  boot upstart both as a container and a (kvm or bare-metal) host.
* Add a MIRROR default in /etc/default/lxc, and use that in the
  debootstrap command in the lucid, maverick and natty templates.
* Remove 0004-restore-lxc.mount-lxc.mount.entry-functionality.patch
  which prevents containers from starting.

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