~ubuntu-branches/ubuntu/oneiric/lxc/oneiric

« back to all changes in this revision

Viewing changes to scripts/lxc-fedora.in

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2009-04-29 17:49:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090429174913-jvahs1ykizqtodje
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# set -ex
 
3
 
 
4
DISTRO="fedora"
 
5
CACHE="@LOCALSTATEDIR@/cache/lxc/${DISTRO}"
 
6
 
 
7
# Default container name
 
8
NAME="fedora"
 
9
CONFFILE="lxc.conf"
 
10
MNTFILE="mount.conf"
 
11
UTSNAME=
 
12
IPV4="172.20.0.21"
 
13
GATEWAY="172.20.0.1"
 
14
MTU="1500"
 
15
 
 
16
# These paths are within the container so do not need to obey configure prefixes
 
17
INITTAB="/etc/inittab"
 
18
FSTAB="/etc/fstab"
 
19
SSHD_CONFIG="/etc/ssh/sshd_config"
 
20
 
 
21
################################################################################
 
22
#                    DISTRO custom configuration files
 
23
################################################################################
 
24
 
 
25
# custom selinux
 
26
 
 
27
write_distro_selinux() {
 
28
    mkdir -p ${ROOTFS}/selinux
 
29
    echo 0 > ${ROOTFS}/selinux/enforce
 
30
}
 
31
 
 
32
# custom fstab
 
33
 
 
34
write_distro_fstab() {
 
35
cat <<EOF > ${ROOTFS}/${FSTAB}
 
36
tmpfs  /dev/shm   tmpfs  defaults  0 0
 
37
EOF
 
38
}
 
39
 
 
40
# custom inittab
 
41
 
 
42
write_distro_inittab() {
 
43
cat <<EOF > ${ROOTFS}/${INITTAB}
 
44
id:3:initdefault:
 
45
si::sysinit:/etc/init.d/rcS
 
46
l0:0:wait:/etc/init.d/rc 0
 
47
l1:1:wait:/etc/init.d/rc 1
 
48
l2:2:wait:/etc/init.d/rc 2
 
49
l3:3:wait:/etc/init.d/rc 3
 
50
l4:4:wait:/etc/init.d/rc 4
 
51
l5:5:wait:/etc/init.d/rc 5
 
52
l6:6:wait:/etc/init.d/rc 6
 
53
# Normally not reached, but fallthrough in case of emergency.
 
54
z6:6:respawn:/sbin/sulogin
 
55
1:2345:respawn:/sbin/getty 38400 console
 
56
c1:12345:respawn:/sbin/getty 38400 tty1 linux
 
57
c2:12345:respawn:/sbin/getty 38400 tty2 linux
 
58
c3:12345:respawn:/sbin/getty 38400 tty3 linux
 
59
c4:12345:respawn:/sbin/getty 38400 tty4 linux
 
60
EOF
 
61
}
 
62
 
 
63
# custom network configuration
 
64
write_distro_network() {
 
65
cat <<EOF > ${ROOTFS}/etc/sysconfig/network-scripts/ifcfg-lo
 
66
DEVICE=lo
 
67
IPADDR=127.0.0.1
 
68
NETMASK=255.0.0.0
 
69
NETWORK=127.0.0.0
 
70
# If you're having problems with gated making 127.0.0.0/8 a martian,
 
71
# you can change this to something else (255.255.255.255, for example)
 
72
BROADCAST=127.255.255.255
 
73
ONBOOT=yes
 
74
NAME=loopback
 
75
EOF
 
76
cat <<EOF > ${ROOTFS}/etc/sysconfig/network-scripts/ifcfg-eth0
 
77
DEVICE=eth0
 
78
BOOTPROTO=static
 
79
HWADDR=52:54:00:12:34:56
 
80
ONBOOT=yes
 
81
HOSTNAME=${UTSNAME}
 
82
NM_CONTROLLED=no
 
83
TYPE=Ethernet
 
84
IPADDR=${IPV4}
 
85
NETWORK=$(ipcalc -sn ${IPV4} 255.255.255.0)
 
86
GATEWAY=${GATEWAY}
 
87
BROADCAST=$(ipcalc -sb ${IPV4} 255.255.255.0)
 
88
NETMASK=255.255.255.0
 
89
MTU=${MTU}
 
90
EOF
 
91
}
 
92
 
 
93
# custom hostname
 
94
 
 
95
write_distro_hostname() {
 
96
cat <<EOF > ${ROOTFS}/sysconfig/network
 
97
NETWORKING=yes
 
98
HOSTNAME=${UTSNAME}
 
99
EOF
 
100
}
 
101
 
 
102
# custom sshd configuration file
 
103
 
 
104
write_distro_sshd_config() {
 
105
cat <<EOF > ${ROOTFS}/${SSHD_CONFIG}
 
106
Port 22
 
107
Protocol 2
 
108
HostKey /etc/ssh/ssh_host_rsa_key
 
109
HostKey /etc/ssh/ssh_host_dsa_key
 
110
UsePrivilegeSeparation yes
 
111
KeyRegenerationInterval 3600
 
112
ServerKeyBits 768
 
113
SyslogFacility AUTH
 
114
LogLevel INFO
 
115
LoginGraceTime 120
 
116
PermitRootLogin yes
 
117
StrictModes yes
 
118
RSAAuthentication yes
 
119
PubkeyAuthentication yes
 
120
IgnoreRhosts yes
 
121
RhostsRSAAuthentication no
 
122
HostbasedAuthentication no
 
123
PermitEmptyPasswords yes
 
124
ChallengeResponseAuthentication no
 
125
EOF
 
126
}
 
127
 
 
128
################################################################################
 
129
#                        lxc configuration files
 
130
################################################################################
 
131
 
 
132
write_lxc_configuration() {
 
133
cat <<EOF > ${CONFFILE}
 
134
lxc.utsname = ${UTSNAME}
 
135
lxc.tty = 4
 
136
lxc.network.type = veth
 
137
lxc.network.flags = up
 
138
lxc.network.link = br0
 
139
lxc.network.name = eth0
 
140
lxc.network.mtu = ${MTU}
 
141
lxc.mount = ${MNTFILE}
 
142
lxc.rootfs = ${ROOTFS}
 
143
lxc.cgroup.devices.deny = a
 
144
# /dev/null and zero
 
145
lxc.cgroup.devices.allow = c 1:3 rwm
 
146
lxc.cgroup.devices.allow = c 1:5 rwm
 
147
# consoles
 
148
lxc.cgroup.devices.allow = c 5:1 rwm
 
149
lxc.cgroup.devices.allow = c 5:0 rwm
 
150
lxc.cgroup.devices.allow = c 4:0 rwm
 
151
lxc.cgroup.devices.allow = c 4:1 rwm
 
152
# /dev/{,u}random
 
153
lxc.cgroup.devices.allow = c 1:9 rwm
 
154
lxc.cgroup.devices.allow = c 1:8 rwm
 
155
# /dev/pts/* - pts namespaces are "coming soon"
 
156
lxc.cgroup.devices.allow = c 136:* rwm
 
157
lxc.cgroup.devices.allow = c 5:2 rwm
 
158
# rtc
 
159
lxc.cgroup.devices.allow = c 254:0 rwm
 
160
EOF
 
161
}
 
162
 
 
163
write_lxc_mounts() {
 
164
cat <<EOF > ${MNTFILE}
 
165
 
 
166
EOF
 
167
}
 
168
 
 
169
create() {
 
170
 
 
171
    # choose a container name, default is already in shell NAME variable
 
172
    echo -n "What is the name for the container ? [${NAME}] "
 
173
    read _NAME_
 
174
 
 
175
    if [ ! -z "${_NAME_}" ]; then
 
176
        NAME=${_NAME_}
 
177
    fi
 
178
 
 
179
    # choose a hostname, default is the container name
 
180
    echo -n "What hostname do you wish for this container ? [${NAME}] "
 
181
    read _UTSNAME_
 
182
 
 
183
    if [ ! -z "${_UTSNAME_}" ]; then
 
184
        UTSNAME=${_UTSNAME_}
 
185
    else
 
186
        UTSNAME=${NAME}
 
187
    fi
 
188
 
 
189
    # choose an ipv4 address, better to choose the same network than
 
190
    # your host
 
191
    echo -n "What IP address do you wish for this container ? [${IPV4}] "
 
192
    read _IPV4_
 
193
 
 
194
    if [ ! -z "${_IPV4_}" ]; then
 
195
        IPV4=${_IPV4_}
 
196
    fi
 
197
 
 
198
    # choose the gateway ip address
 
199
    echo -n "What is the gateway IP address ? [${GATEWAY}] "
 
200
    read _GATEWAY_
 
201
 
 
202
    if [ ! -z "${_GATEWAY_}" ]; then
 
203
        GATEWAY=${_GATEWAY_}
 
204
    fi
 
205
 
 
206
    # choose the MTU size
 
207
    echo -n "What is the MTU size ? [$MTU] "
 
208
    read _MTU_
 
209
 
 
210
    if [ ! -z "$_MTU_" ]; then
 
211
        MTU=$_MTU_
 
212
    fi
 
213
 
 
214
    # the rootfs name will be build with the container name
 
215
    ROOTFS="./rootfs.${NAME}"
 
216
 
 
217
    # check if the rootfs does already exist
 
218
    if [ ! -e "${ROOTFS}" ]; then
 
219
            mkdir -p @LOCALSTATEDIR@/lock/subsys/
 
220
        (
 
221
            flock -n -x 200
 
222
 
 
223
 
 
224
            RES=$?
 
225
            if [ "${RES}" != "0" ]; then
 
226
                echo "Cache repository is busy."
 
227
                break
 
228
            fi
 
229
 
 
230
            # check the mini distro was not already downloaded
 
231
            echo -n "Checking cache download ..."
 
232
            if [ ! -e "${CACHE}/rootfs" ]; then
 
233
 
 
234
                echo "not cached"
 
235
 
 
236
                # Rather than write a special yum config we just make the
 
237
                # default RPM and yum layout in ${CACHE}. The alternative is
 
238
                # to copy /etc/yum/yum.conf or /etc/yum.conf and fiddle with
 
239
                # some settings.
 
240
                mkdir -p "${CACHE}/partial/var/lib/rpm"
 
241
                mkdir -p "${CACHE}/partial/var/log"
 
242
                touch "${CACHE}/partial/var/log/yum.log"
 
243
 
 
244
                RELEASE="$(yum info ${DISTRO}-release | \
 
245
                        awk -F '[[:space:]]*:[[:space:]]*' \
 
246
                                '/^Release/ { release = $2 }
 
247
                                /^Version/ { version = $2 }
 
248
                                END { print version "-" release }')"
 
249
                PKG="${DISTRO}-release.noarch.rpm"
 
250
                RPM="rpm --root \"${CACHE}/partial\""
 
251
 
 
252
                echo "Initializing RPM cache ..."
 
253
                ${RPM} --initdb
 
254
                echo "Downloading ${DISTRO} Release ${RELEASE} description ..."
 
255
                yumdownloader --destdir="${CACHE}/partial" "${DISTRO}-release.noarch.rpm" && \
 
256
                ${RPM} --nodeps -ihv "${CACHE}/partial/${DISTRO}-release*.noarch.rpm"
 
257
                echo "Downloading ${DISTRO} minimal ..."
 
258
                yum --installroot="${CACHE}/partial" -y groupinstall Base
 
259
                RESULT=$?
 
260
                if [ "${RESULT}" != "0" ]; then
 
261
                    echo "Failed to download the rootfs, aborting."
 
262
                    exit 1
 
263
                fi
 
264
                mv "${CACHE}/partial" "${CACHE}/rootfs"
 
265
                echo "Download complete."
 
266
            else
 
267
                echo "Found."
 
268
            fi
 
269
 
 
270
            # make a local copy of the mini
 
271
            echo -n "Copying rootfs ..."
 
272
            cp -a ${CACHE}/rootfs ${ROOTFS} && echo "Done." || exit
 
273
        ) 200> "@LOCALSTATEDIR@/lock/subsys/lxc"
 
274
    fi
 
275
 
 
276
write_lxc_mounts
 
277
 
 
278
write_lxc_configuration
 
279
 
 
280
write_distro_inittab
 
281
 
 
282
write_distro_hostname
 
283
 
 
284
write_distro_fstab
 
285
 
 
286
write_distro_network
 
287
 
 
288
write_distro_sshd_config
 
289
 
 
290
write_distro_selinux
 
291
 
 
292
@BINDIR@/lxc-create -n ${NAME} -f ${CONFFILE}
 
293
RES=$?
 
294
 
 
295
# remove the configuration files
 
296
rm -f ${CONFFILE}
 
297
rm -f ${MNTFILE}
 
298
 
 
299
if [ "${RES}" != "0" ]; then
 
300
    echo "Failed to create '${NAME}'"
 
301
    exit 1
 
302
fi
 
303
 
 
304
echo "Done."
 
305
echo -e "\nYou can run your container with the 'lxc-start -n ${NAME}'\n"
 
306
}
 
307
 
 
308
destroy() {
 
309
 
 
310
    echo -n "What is the name for the container ? [${NAME}] "
 
311
    read _NAME_
 
312
 
 
313
    if [ ! -z "${_NAME_}" ]; then
 
314
        NAME=${_NAME_}
 
315
    fi
 
316
 
 
317
    @BINDIR@/lxc-destroy -n ${NAME}
 
318
    RETVAL=$?
 
319
    if [ ! ${RETVAL} -eq 0 ]; then
 
320
        echo "Failed to destroyed '${NAME}'"
 
321
        return ${RETVAL}
 
322
    fi
 
323
 
 
324
    ROOTFS="./rootfs.${NAME}"
 
325
 
 
326
    echo -n "Shall I remove the rootfs [y/n] ? "
 
327
    read
 
328
    if [ "${REPLY}" = "y" ]; then
 
329
        rm -rf ${ROOTFS}
 
330
    fi
 
331
 
 
332
    return 0
 
333
}
 
334
 
 
335
help() {
 
336
    cat <<EOF
 
337
 
 
338
This script is a helper to create ${DISTRO} system containers.
 
339
 
 
340
The script will create the container configuration file following
 
341
the informations submitted interactively with 'lxc-${DISTRO} create'
 
342
 
 
343
The first creation will download, with yum, a ${DISTRO} minimal
 
344
install and store it into a cache.
 
345
 
 
346
The script will copy from the cache the root filesystem to the
 
347
current directory.
 
348
 
 
349
If there is a problem with the container, (bad configuration for
 
350
example), you can destroy the container with 'lxc-${DISTRO} destroy'
 
351
but without removing the rootfs and recreate it again with
 
352
'lxc-${DISTRO} create'.
 
353
 
 
354
If you want to create another ${DISTRO} container, call the 'lxc-${DISTRO}
 
355
 create' again, specifying another name and new parameters.
 
356
 
 
357
At any time you can purge the ${DISTRO} cache download by calling
 
358
'lxc-${DISTRO} purge'
 
359
 
 
360
Have fun :)
 
361
 
 
362
EOF
 
363
}
 
364
 
 
365
purge() {
 
366
 
 
367
    if [ ! -e ${CACHE} ]; then
 
368
        exit 0
 
369
    fi
 
370
 
 
371
    # lock, so we won't purge while someone is creating a repository
 
372
    (
 
373
        flock -n -x 200
 
374
 
 
375
        RES=$?
 
376
        if [ "${RES}" != "0" ]; then
 
377
            echo "Cache repository is busy."
 
378
            exit 1
 
379
        fi
 
380
 
 
381
        echo -n "Purging the download cache..."
 
382
        rm --preserve-root --one-file-system -rf ${CACHE} && echo "Done." || exit 1
 
383
        exit 0
 
384
 
 
385
    ) 200> "@LOCALSTATEDIR@/lock/subsys/lxc"
 
386
}
 
387
 
 
388
# Note: assuming uid==0 is root -- might break with userns??
 
389
if [ "$(id -u)" != "0" ]; then
 
390
        echo "This script should be run as 'root'"
 
391
        exit 1
 
392
fi
 
393
 
 
394
# Detect which executable we were run as, lxc-fedora or lxc-redhat
 
395
case "$0" in
 
396
    *lxc-redhat)
 
397
        DISTRO="redhat";;
 
398
    *) # default is fedora
 
399
        DISTRO="fedora";;
 
400
esac
 
401
CACHE="@LOCALSTATEDIR@/cache/lxc/${DISTRO}"
 
402
 
 
403
case "$1" in
 
404
    create)
 
405
        create;;
 
406
    destroy)
 
407
        destroy;;
 
408
    help)
 
409
        help;;
 
410
    purge)
 
411
        purge;;
 
412
    *)
 
413
        echo "Usage: $0 {create|destroy|purge|help}"
 
414
        exit 1;;
 
415
esac