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

« back to all changes in this revision

Viewing changes to templates/lxc-opensuse.in

  • Committer: Stéphane Graber
  • Date: 2011-08-11 18:43:51 UTC
  • mto: (3.1.15 sid)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: stgraber@ubuntu.com-20110811184351-xa4c5v0ct5ud45d0
Tags: upstream-0.7.5
Import upstream version 0.7.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
#
 
4
# template script for generating suse container for LXC
 
5
#
 
6
 
 
7
#
 
8
# lxc: linux Container library
 
9
 
 
10
# Authors:
 
11
# Daniel Lezcano <daniel.lezcano@free.fr>
 
12
# Frederic Crozat <fcrozat@suse.com>
 
13
 
 
14
# This library is free software; you can redistribute it and/or
 
15
# modify it under the terms of the GNU Lesser General Public
 
16
# License as published by the Free Software Foundation; either
 
17
# version 2.1 of the License, or (at your option) any later version.
 
18
 
 
19
# This library is distributed in the hope that it will be useful,
 
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
22
# Lesser General Public License for more details.
 
23
 
 
24
# You should have received a copy of the GNU Lesser General Public
 
25
# License along with this library; if not, write to the Free Software
 
26
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
27
 
 
28
DISTRO=11.4
 
29
 
 
30
configure_opensuse()
 
31
{
 
32
    rootfs=$1
 
33
    hostname=$2
 
34
 
 
35
   # set network as static, but everything is done by LXC outside the container
 
36
   cat <<EOF > $rootfs/etc/sysconfig/network/ifcfg-eth0
 
37
STARTMODE='auto'
 
38
BOOTPROTO='static'
 
39
EOF
 
40
 
 
41
   # set default route
 
42
   IP=$(/sbin/ip route | awk '/default/ { print $3 }')
 
43
   echo "default $IP - -" > $rootfs/etc/sysconfig/network/routes
 
44
 
 
45
   # create empty fstab
 
46
   touch $rootfs/etc/fstab
 
47
 
 
48
    # create minimal /dev
 
49
    mknod -m 666 $rootfs/dev/random c 1 8
 
50
    mknod -m 666 $rootfs/dev/urandom c 1 9
 
51
    mkdir -m 755 $rootfs/dev/pts
 
52
    mkdir -m 1777 $rootfs/dev/shm
 
53
    mknod -m 666 $rootfs/dev/tty c 5 0
 
54
    mknod -m 600 $rootfs/dev/console c 5 1
 
55
    mknod -m 666 $rootfs/dev/tty0 c 4 0
 
56
    mknod -m 666 $rootfs/dev/tty1 c 4 1
 
57
    mknod -m 666 $rootfs/dev/tty2 c 4 2
 
58
    mknod -m 666 $rootfs/dev/tty3 c 4 3
 
59
    mknod -m 666 $rootfs/dev/tty4 c 4 4
 
60
    ln -s null $rootfs/dev/tty10
 
61
    mknod -m 666 $rootfs/dev/full c 1 7
 
62
    mknod -m 666 $rootfs/dev/ptmx c 5 2
 
63
    ln -s /proc/self/fd $rootfs/dev/fd
 
64
    ln -s /proc/kcore $rootfs/dev/core
 
65
    mkdir -m 755 $rootfs/dev/mapper
 
66
    mknod -m 600 $rootfs/dev/mapper/control c 10 60
 
67
    mkdir -m 755 $rootfs/dev/net
 
68
    mknod -m 666 $rootfs/dev/net/tun c 10 200
 
69
 
 
70
    # set the hostname
 
71
    cat <<EOF > $rootfs/etc/HOSTNAME
 
72
$hostname
 
73
EOF
 
74
 
 
75
    # do not use hostname from HOSTNAME variable
 
76
    cat <<EOF >> $rootfs/etc/sysconfig/cron
 
77
unset HOSTNAME
 
78
EOF
 
79
 
 
80
    # set minimal hosts
 
81
    cat <<EOF > $rootfs/etc/hosts
 
82
127.0.0.1 localhost $hostname
 
83
EOF
 
84
 
 
85
    # disable various services
 
86
    # disable yast->bootloader in container
 
87
    cat <<EOF > $rootfs/etc/sysconfig/bootloader
 
88
LOADER_TYPE=none
 
89
LOADER_LOCATION=none
 
90
EOF
 
91
 
 
92
    # cut down inittab
 
93
    cat <<EOF > $rootfs/etc/inittab
 
94
id:3:initdefault:
 
95
si::bootwait:/etc/init.d/boot
 
96
l0:0:wait:/etc/init.d/rc 0
 
97
l1:1:wait:/etc/init.d/rc 1
 
98
l2:2:wait:/etc/init.d/rc 2
 
99
l3:3:wait:/etc/init.d/rc 3
 
100
l6:6:wait:/etc/init.d/rc 6
 
101
ls:S:wait:/etc/init.d/rc S
 
102
~~:S:respawn:/sbin/sulogin
 
103
p6::ctrlaltdel:/sbin/init 6
 
104
p0::powerfail:/sbin/init 0
 
105
cons:2345:respawn:/sbin/mingetty --noclear console screen
 
106
c1:2345:respawn:/sbin/mingetty --noclear tty1 screen
 
107
EOF
 
108
 
 
109
    # patch boot script, no longer needed in openSUSE 12.1 / SLE11-SP2
 
110
    patch --quiet -d $rootfs/etc/init.d/ << EOF
 
111
--- boot.orig   2011-05-26 16:03:07.000000000 +0200
 
112
+++ boot        2011-05-26 16:03:19.000000000 +0200
 
113
@@ -98,12 +98,12 @@
 
114
     echo "***************************************************************"
 
115
     /sbin/halt -f
 
116
     fi
 
117
-    echo -n "Mounting devtmpfs at /dev"
 
118
-    mount -n -t devtmpfs -o mode=0755 devtmpfs /dev
 
119
-    rc_status -v -r
 
120
+#    echo -n "Mounting devtmpfs at /dev"
 
121
+#    mount -n -t devtmpfs -o mode=0755 devtmpfs /dev
 
122
+#    rc_status -v -r
 
123
 fi
 
124
 
 
125
-cp -axT --remove-destination /lib/udev/devices /dev
 
126
+#cp -axT --remove-destination /lib/udev/devices /dev
 
127
 
 
128
 if test -d /sys/kernel/debug -a "$HAVE_DEBUGFS" = "1" ; then
 
129
     mount -n -t debugfs debugfs /sys/kernel/debug > /dev/null 2>&1
 
130
EOF
 
131
    cat <<EOF >> $rootfs/etc/sysconfig/boot
 
132
# disable root fsck
 
133
ROOTFS_FSCK="0"
 
134
ROOTFS_BLKDEV="/dev/null"
 
135
EOF
 
136
 
 
137
 
 
138
    # remove pointless services in a container
 
139
    insserv -r -f -p $rootfs/etc/init.d boot.udev boot.udev_retry boot.md boot.lvm boot.loadmodules boot.device-mapper boot.clock boot.swap boot.klog
 
140
 
 
141
    echo "Please change root-password !"
 
142
    echo "root:root" | chroot $rootfs chpasswd
 
143
 
 
144
    return 0
 
145
}
 
146
 
 
147
download_opensuse()
 
148
{
 
149
    cache=$1
 
150
    arch=$2
 
151
 
 
152
    # check the mini opensuse was not already downloaded
 
153
    mkdir -p "$cache/partial-$arch"
 
154
 
 
155
    if [ $? -ne 0 ]; then
 
156
        echo "Failed to create '$cache/partial-$arch' directory"
 
157
        return 1
 
158
    fi
 
159
 
 
160
    # download a mini opensuse into a cache
 
161
    echo "Downloading opensuse minimal ..."
 
162
    mkdir -p "$cache/partial-$arch/dev"
 
163
    mknod -m 666 $cache/partial-$arch/dev/null c 1 3
 
164
    mknod -m 666 $cache/partial-$arch/dev/zero c 1 5
 
165
    zypper --quiet --root $cache/partial-$arch --non-interactive ar http://download.opensuse.org/distribution/$DISTRO/repo/oss/ repo-oss
 
166
    zypper --quiet --root $cache/partial-$arch --non-interactive ar http://download.opensuse.org/update/$DISTRO/ update
 
167
    zypper --quiet --root $cache/partial-$arch --non-interactive --gpg-auto-import-keys in --auto-agree-with-licenses -t pattern base
 
168
    zypper --quiet --root $cache/partial-$arch --non-interactive --gpg-auto-import-keys in +lxc -kbd -patterns-openSUSE-base
 
169
    if [ $? -ne 0 ]; then
 
170
        echo "Failed to download the rootfs, aborting."
 
171
        return 1
 
172
    fi
 
173
 
 
174
    mv "$1/partial-$arch" "$1/rootfs-$arch"
 
175
    echo "Download complete."
 
176
 
 
177
    return 0
 
178
}
 
179
 
 
180
copy_opensuse()
 
181
{
 
182
    cache=$1
 
183
    arch=$2
 
184
    rootfs=$3
 
185
 
 
186
    # make a local copy of the mini opensuse
 
187
    echo -n "Copying rootfs to $rootfs ..."
 
188
    cp -a $cache/rootfs-$arch $rootfs || return 1
 
189
    return 0
 
190
}
 
191
 
 
192
install_opensuse()
 
193
{
 
194
    cache="/var/cache/lxc/opensuse"
 
195
    rootfs=$1
 
196
    mkdir -p /var/lock/subsys/
 
197
    (
 
198
        flock -n -x 200
 
199
        if [ $? -ne 0 ]; then
 
200
            echo "Cache repository is busy."
 
201
            return 1
 
202
        fi
 
203
 
 
204
        arch=$(arch)
 
205
 
 
206
        echo "Checking cache download in $cache/rootfs-$arch ... "
 
207
        if [ ! -e "$cache/rootfs-$arch" ]; then
 
208
            download_opensuse $cache $arch
 
209
            if [ $? -ne 0 ]; then
 
210
                echo "Failed to download 'opensuse base'"
 
211
                return 1
 
212
            fi
 
213
        fi
 
214
 
 
215
        echo "Copy $cache/rootfs-$arch to $rootfs ... "
 
216
        copy_opensuse $cache $arch $rootfs
 
217
        if [ $? -ne 0 ]; then
 
218
            echo "Failed to copy rootfs"
 
219
            return 1
 
220
        fi
 
221
 
 
222
        return 0
 
223
 
 
224
        ) 200>/var/lock/subsys/lxc
 
225
 
 
226
    return $?
 
227
}
 
228
 
 
229
copy_configuration()
 
230
{
 
231
    path=$1
 
232
    rootfs=$2
 
233
    name=$3
 
234
 
 
235
    cat <<EOF >> $path/config
 
236
lxc.utsname = $name
 
237
 
 
238
lxc.tty = 4
 
239
lxc.pts = 1024
 
240
lxc.rootfs = $rootfs
 
241
lxc.mount  = $path/fstab
 
242
 
 
243
lxc.cgroup.devices.deny = a
 
244
# /dev/null and zero
 
245
lxc.cgroup.devices.allow = c 1:3 rwm
 
246
lxc.cgroup.devices.allow = c 1:5 rwm
 
247
# consoles
 
248
lxc.cgroup.devices.allow = c 5:1 rwm
 
249
lxc.cgroup.devices.allow = c 5:0 rwm
 
250
lxc.cgroup.devices.allow = c 4:0 rwm
 
251
lxc.cgroup.devices.allow = c 4:1 rwm
 
252
# /dev/{,u}random
 
253
lxc.cgroup.devices.allow = c 1:9 rwm
 
254
lxc.cgroup.devices.allow = c 1:8 rwm
 
255
lxc.cgroup.devices.allow = c 136:* rwm
 
256
lxc.cgroup.devices.allow = c 5:2 rwm
 
257
# rtc
 
258
lxc.cgroup.devices.allow = c 254:0 rwm
 
259
EOF
 
260
 
 
261
    cat <<EOF > $path/fstab
 
262
proc            $rootfs/proc         proc       nodev,noexec,nosuid 0 0
 
263
sysfs           $rootfs/sys          sysfs      defaults  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="/var/cache/lxc/opensuse"
 
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>/var/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 zypper > /dev/null
 
330
if [ $? -ne 0 ]; then
 
331
    echo "'zypper' 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_opensuse $rootfs
 
348
if [ $? -ne 0 ]; then
 
349
    echo "failed to install opensuse"
 
350
    exit 1
 
351
fi
 
352
 
 
353
configure_opensuse $rootfs $name
 
354
if [ $? -ne 0 ]; then
 
355
    echo "failed to configure opensuse for a container"
 
356
    exit 1
 
357
fi
 
358
 
 
359
copy_configuration $path $rootfs $name
 
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