~ubuntu-branches/ubuntu/precise/lxc/precise-updates

« back to all changes in this revision

Viewing changes to .pc/0062-templates-relative-paths/templates/lxc-debian.in

  • Committer: Package Import Robot
  • Author(s): Serge Hallyn
  • Date: 2012-03-21 08:20:06 UTC
  • Revision ID: package-import@ubuntu.com-20120321082006-bsepg8w3z7qb79xt
Tags: 0.7.5-3ubuntu41
* add lxc-shutdown command:
  - 0060-lxc-shutdown: add the command to the source
  - debian/lxc.upstart: use lxc-shutdown to shut down containers cleanly
  - debian/lxc.default: add LXC_SHUTDOWN_TIMEOUT (default 120s)
* support per-container apparmor policies:  (LP: #953453)
  - 0061-lxc-start-apparmor: add lxc.aa_profile to config file.  If not
    specified, lxc-default profile is used for container.  Otherwise, the
    specified profile is used.
    Note that per-container profiles must be named 'lxc-*'.
  - split debian/lxc-default.apparmor from debian/lxc.apparmor.
  - have /etc/apparmor.d/lxc-containers #include /etc/apparmor.d/lxc/*
  - debian/lxc.postinst: load the new lxc-containers profiles
  - debian/lxc.postrm: remove lxc-containers profiles
  - debian/rules: make new etc/apparmor.d/lxc dir and copy lxc-default into it
  - debian/control: add libapparmor-dev to build-depends
  - debian/lxc.upstart: load apparmor per-container policies at pre-start.
* debian/lxc.apparmor: insert the stricter mount rules for lxc-start
  (LP: #645625) (LP: #942934)
* debian/local/lxc-start-ephemeral: re-enable aufs option (LP: #960262)
* replace upstream lxc-wait with our own bash script (LP: #951181)
  - debian/local/lxc-wait: the script
  - debian/rules: copy the script into place
* 0062-templates-relative-paths: update templates to use relative paths,
  and make lxc-start always accept /var/lib/lxc/CN/rootfs as target prefix,
  to make lvm containers work.  (LP: #960860)

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:-squeeze}
 
24
MIRROR=${MIRROR:-http://cdn.debian.net/debian}
 
25
 
 
26
configure_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
 
41
id:2:initdefault:
 
42
si::sysinit:/etc/init.d/rcS
 
43
l0:0:wait:/etc/init.d/rc 0
 
44
l1:1:wait:/etc/init.d/rc 1
 
45
l2:2:wait:/etc/init.d/rc 2
 
46
l3:3:wait:/etc/init.d/rc 3
 
47
l4:4:wait:/etc/init.d/rc 4
 
48
l5:5:wait:/etc/init.d/rc 5
 
49
l6:6:wait:/etc/init.d/rc 6
 
50
# Normally not reached, but fallthrough in case of emergency.
 
51
z6:6:respawn:/sbin/sulogin
 
52
1:2345:respawn:/sbin/getty 38400 console
 
53
c1:12345:respawn:/sbin/getty 38400 tty1 linux
 
54
c2:12345:respawn:/sbin/getty 38400 tty2 linux
 
55
c3:12345:respawn:/sbin/getty 38400 tty3 linux
 
56
c4:12345:respawn:/sbin/getty 38400 tty4 linux
 
57
EOF
 
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
 
65
auto lo
 
66
iface lo inet loopback
 
67
 
 
68
auto eth0
 
69
iface eth0 inet dhcp
 
70
EOF
 
71
 
 
72
    # set the hostname
 
73
    cat <<EOF > $rootfs/etc/hostname
 
74
$hostname
 
75
EOF
 
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
 
 
115
download_debian()
 
116
{
 
117
    packages=\
 
118
ifupdown,\
 
119
locales,\
 
120
libui-dialog-perl,\
 
121
dialog,\
 
122
dhcp3-client,\
 
123
netbase,\
 
124
net-tools,\
 
125
iproute,\
 
126
openssh-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
 
 
154
copy_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
 
 
167
install_debian()
 
168
{
 
169
    cache="@LOCALSTATEDIR@/cache/lxc/debian"
 
170
    rootfs=$1
 
171
    mkdir -p @LOCALSTATEDIR@/lock/subsys/
 
172
    (
 
173
        flock -n -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>@LOCALSTATEDIR@/lock/subsys/lxc
 
211
 
 
212
    return $?
 
213
}
 
214
 
 
215
copy_configuration()
 
216
{
 
217
    path=$1
 
218
    rootfs=$2
 
219
    name=$3
 
220
 
 
221
    cat >> $path/config << EOF
 
222
# $path/config
 
223
 
 
224
## Container
 
225
lxc.utsname                             = $name
 
226
lxc.rootfs                              = $rootfs
 
227
lxc.tty                                 = 4
 
228
lxc.pts                                 = 1024
 
229
#lxc.console                            = /var/log/lxc/$name.console
 
230
 
 
231
## Capabilities
 
232
lxc.cap.drop                            = sys_admin
 
233
 
 
234
## Devices
 
235
#lxc.cgroup.devices.allow               = a
 
236
lxc.cgroup.devices.deny                 = a
 
237
# /dev/null
 
238
lxc.cgroup.devices.allow                = c 1:3 rwm
 
239
# /dev/zero
 
240
lxc.cgroup.devices.allow                = c 1:5 rwm
 
241
# /dev/tty[1-4] consoles
 
242
lxc.cgroup.devices.allow                = c 5:1 rwm
 
243
lxc.cgroup.devices.allow                = c 5:0 rwm
 
244
lxc.cgroup.devices.allow                = c 4:0 rwm
 
245
lxc.cgroup.devices.allow                = c 4:1 rwm
 
246
# /dev/{,u}random
 
247
lxc.cgroup.devices.allow                = c 1:9 rwm
 
248
lxc.cgroup.devices.allow                = c 1:8 rwm
 
249
lxc.cgroup.devices.allow                = c 136:* rwm
 
250
lxc.cgroup.devices.allow                = c 5:2 rwm
 
251
# /dev/rtc
 
252
lxc.cgroup.devices.allow                = c 254:0 rwm
 
253
 
 
254
## Limits
 
255
#lxc.cgroup.cpu.shares                  = 1024
 
256
#lxc.cgroup.cpuset.cpus                 = 0
 
257
#lxc.cgroup.memory.limit_in_bytes       = 256M
 
258
#lxc.cgroup.memory.memsw.limit_in_bytes = 1G
 
259
 
 
260
## Filesystem
 
261
lxc.mount.entry                         = proc $rootfs/proc proc nodev,noexec,nosuid 0 0
 
262
lxc.mount.entry                         = sysfs $rootfs/sys sysfs defaults,ro 0 0
 
263
#lxc.mount.entry                        = /srv/$name $rootfs/srv/$name none defaults,bind 0 0
 
264
EOF
 
265
 
 
266
    if [ $? -ne 0 ]; then
 
267
        echo "Failed to add configuration"
 
268
        return 1
 
269
    fi
 
270
 
 
271
    return 0
 
272
}
 
273
 
 
274
clean()
 
275
{
 
276
    cache="@LOCALSTATEDIR@/cache/lxc/debian"
 
277
 
 
278
    if [ ! -e $cache ]; then
 
279
        exit 0
 
280
    fi
 
281
 
 
282
    # lock, so we won't purge while someone is creating a repository
 
283
    (
 
284
        flock -n -x 200
 
285
        if [ $? != 0 ]; then
 
286
            echo "Cache repository is busy."
 
287
            exit 1
 
288
        fi
 
289
 
 
290
        echo -n "Purging the download cache..."
 
291
        rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
 
292
        exit 0
 
293
 
 
294
    ) 200>@LOCALSTATEDIR@/lock/subsys/lxc
 
295
}
 
296
 
 
297
usage()
 
298
{
 
299
    cat <<EOF
 
300
$1 -h|--help -p|--path=<path> --clean
 
301
EOF
 
302
    return 0
 
303
}
 
304
 
 
305
options=$(getopt -o hp:n:c -l help,path:,name:,clean -- "$@")
 
306
if [ $? -ne 0 ]; then
 
307
        usage $(basename $0)
 
308
        exit 1
 
309
fi
 
310
eval set -- "$options"
 
311
 
 
312
while true
 
313
do
 
314
    case "$1" in
 
315
        -h|--help)      usage $0 && exit 0;;
 
316
        -p|--path)      path=$2; shift 2;;
 
317
        -n|--name)      name=$2; shift 2;;
 
318
        -c|--clean)     clean=$2; shift 2;;
 
319
        --)             shift 1; break ;;
 
320
        *)              break ;;
 
321
    esac
 
322
done
 
323
 
 
324
if [ ! -z "$clean" -a -z "$path" ]; then
 
325
    clean || exit 1
 
326
    exit 0
 
327
fi
 
328
 
 
329
type debootstrap
 
330
if [ $? -ne 0 ]; then
 
331
    echo "'debootstrap' command is missing"
 
332
    exit 1
 
333
fi
 
334
 
 
335
if [ -z "$path" ]; then
 
336
    echo "'path' parameter is required"
 
337
    exit 1
 
338
fi
 
339
 
 
340
if [ "$(id -u)" != "0" ]; then
 
341
    echo "This script should be run as 'root'"
 
342
    exit 1
 
343
fi
 
344
 
 
345
rootfs=$path/rootfs
 
346
 
 
347
install_debian $rootfs
 
348
if [ $? -ne 0 ]; then
 
349
    echo "failed to install debian"
 
350
    exit 1
 
351
fi
 
352
 
 
353
configure_debian $rootfs $name
 
354
if [ $? -ne 0 ]; then
 
355
    echo "failed to configure debian for a container"
 
356
    exit 1
 
357
fi
 
358
 
 
359
copy_configuration $path $rootfs
 
360
if [ $? -ne 0 ]; then
 
361
    echo "failed write configuration file"
 
362
    exit 1
 
363
fi
 
364
 
 
365
if [ ! -z $clean ]; then
 
366
    clean || exit 1
 
367
    exit 0
 
368
fi