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

« back to all changes in this revision

Viewing changes to .pc/0062-templates-relative-paths/templates/lxc-ubuntu.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
# template script for generating ubuntu container for LXC
 
5
#
 
6
# This script consolidates and extends the existing lxc ubuntu scripts
 
7
#
 
8
 
 
9
# Copyright � 2011 Serge Hallyn <serge.hallyn@canonical.com>
 
10
# Copyright � 2010 Wilhelm Meier
 
11
# Author: Wilhelm Meier <wilhelm.meier@fh-kl.de>
 
12
#
 
13
# This program is free software; you can redistribute it and/or modify
 
14
# it under the terms of the GNU General Public License version 2, as
 
15
# published by the Free Software Foundation.
 
16
 
 
17
# This program is distributed in the hope that it will be useful,
 
18
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
# GNU General Public License for more details.
 
21
 
 
22
# You should have received a copy of the GNU General Public License along
 
23
# with this program; if not, write to the Free Software Foundation, Inc.,
 
24
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
25
#
 
26
 
 
27
set -e
 
28
 
 
29
if [ -r /etc/default/lxc ]; then
 
30
    . /etc/default/lxc
 
31
fi
 
32
 
 
33
configure_ubuntu()
 
34
{
 
35
    rootfs=$1
 
36
    hostname=$2
 
37
    release=$3
 
38
 
 
39
   # configure the network using the dhcp
 
40
    cat <<EOF > $rootfs/etc/network/interfaces
 
41
auto lo
 
42
iface lo inet loopback
 
43
 
 
44
auto eth0
 
45
iface eth0 inet dhcp
 
46
EOF
 
47
 
 
48
    # set the hostname
 
49
    cat <<EOF > $rootfs/etc/hostname
 
50
$hostname
 
51
EOF
 
52
    # set minimal hosts
 
53
    cat <<EOF > $rootfs/etc/hosts
 
54
127.0.0.1 localhost $hostname
 
55
EOF
 
56
 
 
57
    if [ "$release" != "precise" ]; then
 
58
        # suppress log level output for udev
 
59
        sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
 
60
 
 
61
        # remove jobs for consoles 5 and 6 since we only create 4 consoles in
 
62
        # this template
 
63
        rm -f $rootfs/etc/init/tty{5,6}.conf
 
64
    fi
 
65
 
 
66
    if [ -z "$bindhome" ]; then
 
67
        chroot $rootfs useradd --create-home -s /bin/bash ubuntu
 
68
        echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
 
69
    fi
 
70
 
 
71
    return 0
 
72
}
 
73
 
 
74
# finish setting up the user in the container by injecting ssh key and
 
75
# adding sudo group membership.
 
76
# passed-in user is either 'ubuntu' or the user to bind in from host.
 
77
finalize_user()
 
78
{
 
79
    user=$1
 
80
 
 
81
    if [ "$release" = "precise" ]; then
 
82
        groups="sudo"
 
83
    else
 
84
        groups="sudo admin"
 
85
    fi
 
86
 
 
87
    for group in $groups; do
 
88
        chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
 
89
        chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
 
90
    done
 
91
 
 
92
    if [ -n "$auth_key" -a -f "$auth_key" ]; then
 
93
        u_path="/home/${user}/.ssh"
 
94
        root_u_path="$rootfs/$u_path"
 
95
        mkdir -p $root_u_path
 
96
        cp $auth_key "$root_u_path/authorized_keys"
 
97
        chroot $rootfs chown -R ${user}: "$u_path"
 
98
 
 
99
        echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
 
100
    fi
 
101
    return 0
 
102
}
 
103
 
 
104
write_sourceslist()
 
105
{
 
106
    # $1 => path to the rootfs
 
107
    # $2 => architecture we want to add
 
108
    # $3 => whether to use the multi-arch syntax or not
 
109
 
 
110
    case $2 in
 
111
      amd64|i386)
 
112
            MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
 
113
            SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
 
114
            ;;
 
115
      sparc)
 
116
            case $SUITE in
 
117
              gutsy)
 
118
            MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
 
119
            SECURITY_MIRROR=${SECURITY_MIRRORMIRROR:-http://security.ubuntu.com/ubuntu}
 
120
            ;;
 
121
              *)
 
122
            MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
 
123
            SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
 
124
            ;;
 
125
            esac
 
126
            ;;
 
127
      *)
 
128
            MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
 
129
            SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
 
130
            ;;
 
131
    esac
 
132
    if [ -n "$3" ]; then
 
133
        cat >> "$1/etc/apt/sources.list" << EOF
 
134
deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
 
135
deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
 
136
deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
 
137
EOF
 
138
    else
 
139
        cat >> "$1/etc/apt/sources.list" << EOF
 
140
deb $MIRROR ${release} main restricted universe multiverse
 
141
deb $MIRROR ${release}-updates main restricted universe multiverse
 
142
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
 
143
EOF
 
144
    fi
 
145
}
 
146
 
 
147
download_ubuntu()
 
148
{
 
149
    cache=$1
 
150
    arch=$2
 
151
    release=$3
 
152
 
 
153
    if [ $release = "lucid" ]; then
 
154
        packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg
 
155
    elif [ $release = "maverick" ]; then
 
156
        packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,dhcp3-client,ssh,lsb-release,gnupg,netbase
 
157
    elif [ $release = "natty" ]; then
 
158
        packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase
 
159
    else
 
160
        packages=dialog,apt,apt-utils,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
 
161
    fi
 
162
    echo "installing packages: $packages"
 
163
 
 
164
    # check the mini ubuntu was not already downloaded
 
165
    mkdir -p "$cache/partial-$arch"
 
166
    if [ $? -ne 0 ]; then
 
167
        echo "Failed to create '$cache/partial-$arch' directory"
 
168
        return 1
 
169
    fi
 
170
 
 
171
    # download a mini ubuntu into a cache
 
172
    echo "Downloading ubuntu $release minimal ..."
 
173
    if [ -n "$(which qemu-debootstrap)" ]; then
 
174
        qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
 
175
    else
 
176
        debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
 
177
    fi
 
178
 
 
179
    if [ $? -ne 0 ]; then
 
180
        echo "Failed to download the rootfs, aborting."
 
181
            return 1
 
182
    fi
 
183
 
 
184
    # Serge isn't sure whether we should avoid doing this when
 
185
    # $release == `distro-info -d`
 
186
    echo "Installing updates"
 
187
    > $cache/partial-$arch/etc/apt/sources.list
 
188
    write_sourceslist $cache/partial-$arch/ $arch
 
189
 
 
190
    chroot "$1/partial-${arch}" apt-get update
 
191
    if [ $? -ne 0 ]; then
 
192
        echo "Failed to update the apt cache"
 
193
        return 1
 
194
    fi
 
195
    cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
 
196
#!/bin/sh
 
197
exit 101
 
198
EOF
 
199
    chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
 
200
 
 
201
    lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y
 
202
    ret=$?
 
203
    rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
 
204
 
 
205
    if [ $ret -ne 0 ]; then
 
206
        echo "Failed to upgrade the cache"
 
207
        return 1
 
208
    fi
 
209
 
 
210
    mv "$1/partial-$arch" "$1/rootfs-$arch"
 
211
    echo "Download complete"
 
212
    return 0
 
213
}
 
214
 
 
215
copy_ubuntu()
 
216
{
 
217
    cache=$1
 
218
    arch=$2
 
219
    rootfs=$3
 
220
 
 
221
    # make a local copy of the miniubuntu
 
222
    echo "Copying rootfs to $rootfs ..."
 
223
    mkdir -p $rootfs
 
224
    rsync -a $cache/rootfs-$arch/ $rootfs/ || return 1
 
225
    return 0
 
226
}
 
227
 
 
228
install_ubuntu()
 
229
{
 
230
    rootfs=$1
 
231
    release=$2
 
232
    flushcache=$3
 
233
    cache="/var/cache/lxc/$release"
 
234
    mkdir -p /var/lock/subsys/
 
235
    (
 
236
        flock -n -x 200
 
237
        if [ $? -ne 0 ]; then
 
238
            echo "Cache repository is busy."
 
239
            return 1
 
240
        fi
 
241
 
 
242
 
 
243
    if [ $flushcache -eq 1 ]; then
 
244
        echo "Flushing cache..."
 
245
        rm -rf "$cache/partial-$arch"
 
246
        rm -rf "$cache/rootfs-$arch"
 
247
    fi
 
248
 
 
249
        echo "Checking cache download in $cache/rootfs-$arch ... "
 
250
        if [ ! -e "$cache/rootfs-$arch" ]; then
 
251
            download_ubuntu $cache $arch $release
 
252
            if [ $? -ne 0 ]; then
 
253
                echo "Failed to download 'ubuntu $release base'"
 
254
                return 1
 
255
            fi
 
256
        fi
 
257
 
 
258
        echo "Copy $cache/rootfs-$arch to $rootfs ... "
 
259
        copy_ubuntu $cache $arch $rootfs
 
260
        if [ $? -ne 0 ]; then
 
261
            echo "Failed to copy rootfs"
 
262
            return 1
 
263
        fi
 
264
 
 
265
        return 0
 
266
 
 
267
        ) 200>/var/lock/subsys/lxc
 
268
 
 
269
    return $?
 
270
}
 
271
 
 
272
copy_configuration()
 
273
{
 
274
    path=$1
 
275
    rootfs=$2
 
276
    name=$3
 
277
    arch=$4
 
278
    release=$5
 
279
 
 
280
    if [ $arch = "i386" ]; then
 
281
        arch="i686"
 
282
    fi
 
283
 
 
284
    ttydir=""
 
285
    if [ $release = "precise" ]; then
 
286
        ttydir=" lxc"
 
287
    fi
 
288
 
 
289
    # if there is exactly one veth network entry, make sure it has an
 
290
    # associated hwaddr.
 
291
    nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
 
292
    if [ $nics -eq 1 ]; then
 
293
        grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
 
294
lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
 
295
EOF
 
296
    fi
 
297
 
 
298
    cat <<EOF >> $path/config
 
299
lxc.utsname = $name
 
300
 
 
301
lxc.devttydir =$ttydir
 
302
lxc.tty = 4
 
303
lxc.pts = 1024
 
304
lxc.rootfs = $rootfs
 
305
lxc.mount  = $path/fstab
 
306
lxc.arch = $arch
 
307
lxc.cap.drop = sys_module mac_admin
 
308
 
 
309
lxc.cgroup.devices.deny = a
 
310
# Allow any mknod (but not using the node)
 
311
lxc.cgroup.devices.allow = c *:* m
 
312
lxc.cgroup.devices.allow = b *:* m
 
313
# /dev/null and zero
 
314
lxc.cgroup.devices.allow = c 1:3 rwm
 
315
lxc.cgroup.devices.allow = c 1:5 rwm
 
316
# consoles
 
317
lxc.cgroup.devices.allow = c 5:1 rwm
 
318
lxc.cgroup.devices.allow = c 5:0 rwm
 
319
#lxc.cgroup.devices.allow = c 4:0 rwm
 
320
#lxc.cgroup.devices.allow = c 4:1 rwm
 
321
# /dev/{,u}random
 
322
lxc.cgroup.devices.allow = c 1:9 rwm
 
323
lxc.cgroup.devices.allow = c 1:8 rwm
 
324
lxc.cgroup.devices.allow = c 136:* rwm
 
325
lxc.cgroup.devices.allow = c 5:2 rwm
 
326
# rtc
 
327
lxc.cgroup.devices.allow = c 254:0 rwm
 
328
#fuse
 
329
lxc.cgroup.devices.allow = c 10:229 rwm
 
330
#tun
 
331
lxc.cgroup.devices.allow = c 10:200 rwm
 
332
#full
 
333
lxc.cgroup.devices.allow = c 1:7 rwm
 
334
#hpet
 
335
lxc.cgroup.devices.allow = c 10:228 rwm
 
336
#kvm
 
337
lxc.cgroup.devices.allow = c 10:232 rwm
 
338
EOF
 
339
 
 
340
    cat <<EOF > $path/fstab
 
341
proc            $rootfs/proc         proc    nodev,noexec,nosuid 0 0
 
342
sysfs           $rootfs/sys          sysfs defaults  0 0
 
343
EOF
 
344
 
 
345
    if [ $? -ne 0 ]; then
 
346
        echo "Failed to add configuration"
 
347
        return 1
 
348
    fi
 
349
 
 
350
    return 0
 
351
}
 
352
 
 
353
trim()
 
354
{
 
355
    rootfs=$1
 
356
    release=$2
 
357
 
 
358
    # provide the lxc service
 
359
    cat <<EOF > $rootfs/etc/init/lxc.conf
 
360
# fake some events needed for correct startup other services
 
361
 
 
362
description     "Container Upstart"
 
363
 
 
364
start on startup
 
365
 
 
366
script
 
367
        rm -rf /var/run/*.pid
 
368
        rm -rf /var/run/network/*
 
369
        /sbin/initctl emit stopped JOB=udevtrigger --no-wait
 
370
        /sbin/initctl emit started JOB=udev --no-wait
 
371
end script
 
372
EOF
 
373
 
 
374
    # fix buggus runlevel with sshd
 
375
    cat <<EOF > $rootfs/etc/init/ssh.conf
 
376
# ssh - OpenBSD Secure Shell server
 
377
#
 
378
# The OpenSSH server provides secure shell access to the system.
 
379
 
 
380
description     "OpenSSH server"
 
381
 
 
382
start on filesystem
 
383
stop on runlevel [!2345]
 
384
 
 
385
expect fork
 
386
respawn
 
387
respawn limit 10 5
 
388
umask 022
 
389
# replaces SSHD_OOM_ADJUST in /etc/default/ssh
 
390
oom never
 
391
 
 
392
pre-start script
 
393
    test -x /usr/sbin/sshd || { stop; exit 0; }
 
394
    test -e /etc/ssh/sshd_not_to_be_run && { stop; exit 0; }
 
395
    test -c /dev/null || { stop; exit 0; }
 
396
 
 
397
    mkdir -p -m0755 /var/run/sshd
 
398
end script
 
399
 
 
400
# if you used to set SSHD_OPTS in /etc/default/ssh, you can change the
 
401
# 'exec' line here instead
 
402
exec /usr/sbin/sshd
 
403
EOF
 
404
 
 
405
    cat <<EOF > $rootfs/etc/init/console.conf
 
406
# console - getty
 
407
#
 
408
# This service maintains a console on tty1 from the point the system is
 
409
# started until it is shut down again.
 
410
 
 
411
start on stopped rc RUNLEVEL=[2345]
 
412
stop on runlevel [!2345]
 
413
 
 
414
respawn
 
415
exec /sbin/getty -8 38400 /dev/console
 
416
EOF
 
417
 
 
418
    cat <<EOF > $rootfs/lib/init/fstab
 
419
# /lib/init/fstab: cleared out for bare-bones lxc
 
420
EOF
 
421
 
 
422
    # reconfigure some services
 
423
    if [ -z "$LANG" ]; then
 
424
        chroot $rootfs locale-gen en_US.UTF-8
 
425
        chroot $rootfs update-locale LANG=en_US.UTF-8
 
426
    else
 
427
        chroot $rootfs locale-gen $LANG
 
428
        chroot $rootfs update-locale LANG=$LANG
 
429
    fi
 
430
 
 
431
    # remove pointless services in a container
 
432
    chroot $rootfs /usr/sbin/update-rc.d -f ondemand remove
 
433
 
 
434
    chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls u*.conf); do mv $f $f.orig; done'
 
435
    chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls tty[2-9].conf); do mv $f $f.orig; done'
 
436
    chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls plymouth*.conf); do mv $f $f.orig; done'
 
437
    chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls hwclock*.conf); do mv $f $f.orig; done'
 
438
    chroot $rootfs /bin/bash -c 'cd /etc/init; for f in $(ls module*.conf); do mv $f $f.orig; done'
 
439
 
 
440
    # if this isn't lucid, then we need to twiddle the network upstart bits :(
 
441
    if [ $release != "lucid" ]; then
 
442
        sed -i 's/^.*emission handled.*$/echo Emitting lo/' $rootfs/etc/network/if-up.d/upstart
 
443
    fi
 
444
}
 
445
 
 
446
post_process()
 
447
{
 
448
    rootfs=$1
 
449
    release=$2
 
450
    trim_container=$3
 
451
 
 
452
    if [ $trim_container -eq 1 ]; then
 
453
        trim $rootfs $release
 
454
    elif [ $release = "lucid" -o $release = "maverick" -o $release = "natty" \
 
455
                -o $release = "oneiric" ]; then
 
456
        # for lucid and maverick, if not trimming, then add the ubuntu-virt
 
457
        # ppa and install lxcguest
 
458
        if [ $release = "lucid" -o $release = "maverick" ]; then
 
459
            chroot $rootfs apt-get install --force-yes -y python-software-properties
 
460
            chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
 
461
        fi
 
462
        cresolvonf="${rootfs}/etc/resolv.conf"
 
463
        mv $cresolvonf ${cresolvonf}.lxcbak
 
464
        cat /etc/resolv.conf > ${cresolvonf}
 
465
        chroot $rootfs apt-get update
 
466
        chroot $rootfs apt-get install --force-yes -y lxcguest
 
467
        rm -f ${cresolvonf}
 
468
        mv ${cresolvonf}.lxcbak ${cresolvonf}
 
469
    fi
 
470
 
 
471
    # If the container isn't running a native architecture, setup multiarch
 
472
    if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
 
473
        mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
 
474
        echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
 
475
 
 
476
        # Save existing value of MIRROR and SECURITY_MIRROR
 
477
        DEFAULT_MIRROR=$MIRROR
 
478
        DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
 
479
 
 
480
        # Write a new sources.list containing both native and multiarch entries
 
481
        > ${rootfs}/etc/apt/sources.list
 
482
        write_sourceslist $rootfs $arch "native"
 
483
 
 
484
        MIRROR=$DEFAULT_MIRROR
 
485
        SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
 
486
        write_sourceslist $rootfs $hostarch "multiarch"
 
487
 
 
488
        # Finally update the lists and install upstart using the host architecture
 
489
        chroot $rootfs apt-get update
 
490
        chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:amd64 iproute:amd64 isc-dhcp-client:amd64
 
491
    fi
 
492
}
 
493
 
 
494
do_bindhome()
 
495
{
 
496
    rootfs=$1
 
497
    user=$2
 
498
 
 
499
    # copy /etc/passwd, /etc/shadow, and /etc/group entries into container
 
500
    pwd=`getent passwd $user` || { echo "Failed to copy password entry for $user"; false; }
 
501
    echo $pwd >> $rootfs/etc/passwd
 
502
 
 
503
    # make sure user's shell exists in the container
 
504
    shell=`echo $pwd | cut -d: -f 7`
 
505
    if [ ! -x $rootfs/$shell ]; then
 
506
        echo "shell $shell for user $user was not found in the container."
 
507
        pkg=`dpkg -S $(readlink -m $shell) | cut -d ':' -f1`
 
508
        echo "Installing $pkg"
 
509
        chroot $rootfs apt-get --force-yes -y install $pkg
 
510
    fi
 
511
 
 
512
    shad=`getent shadow $user`
 
513
    echo "$shad" >> $rootfs/etc/shadow
 
514
 
 
515
    # bind-mount the user's path into the container's /home
 
516
    h=`getent passwd $user | cut -d: -f 6`
 
517
    mkdir -p $rootfs/$h
 
518
    echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
 
519
 
 
520
    # Make sure the group exists in container
 
521
    grp=`echo $pwd | cut -d: -f 4`  # group number for $user
 
522
    grpe=`getent group $grp` || return 0  # if host doesn't define grp, ignore in container
 
523
    chroot $rootfs getent group "$grpe" || echo "$grpe" >> $rootfs/etc/group
 
524
}
 
525
 
 
526
usage()
 
527
{
 
528
    cat <<EOF
 
529
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
 
530
   [-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
 
531
release: lucid | maverick | natty | oneiric | precise
 
532
trim: make a minimal (faster, but not upgrade-safe) container
 
533
bindhome: bind <user>'s home into the container
 
534
          The ubuntu user will not be created, and <user> will have
 
535
          sudo access.
 
536
arch: amd64 or i386: defaults to host arch
 
537
auth-key: SSH Public key file to inject into container
 
538
EOF
 
539
    return 0
 
540
}
 
541
 
 
542
options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug -- "$@")
 
543
if [ $? -ne 0 ]; then
 
544
    usage $(basename $0)
 
545
    exit 1
 
546
fi
 
547
eval set -- "$options"
 
548
 
 
549
release=lucid
 
550
if [ -f /etc/lsb-release ]; then
 
551
    . /etc/lsb-release
 
552
    case "$DISTRIB_CODENAME" in
 
553
        lucid|maverick|natty|oneiric|precise)
 
554
            release=$DISTRIB_CODENAME
 
555
        ;;
 
556
    esac
 
557
fi
 
558
 
 
559
bindhome=
 
560
arch=$(arch)
 
561
 
 
562
# Code taken from debootstrap
 
563
if [ -x /usr/bin/dpkg ] && /usr/bin/dpkg --print-architecture >/dev/null 2>&1; then
 
564
    arch=`/usr/bin/dpkg --print-architecture`
 
565
elif type udpkg >/dev/null 2>&1 && udpkg --print-architecture >/dev/null 2>&1; then
 
566
    arch=`/usr/bin/udpkg --print-architecture`
 
567
else
 
568
    arch=$(arch)
 
569
    if [ "$arch" = "i686" ]; then
 
570
        arch="i386"
 
571
    elif [ "$arch" = "x86_64" ]; then
 
572
        arch="amd64"
 
573
    elif [ "$arch" = "armv7l" ]; then
 
574
        arch="armel"
 
575
    fi
 
576
fi
 
577
 
 
578
debug=0
 
579
trim_container=0
 
580
hostarch=$arch
 
581
flushcache=0
 
582
while true
 
583
do
 
584
    case "$1" in
 
585
    -h|--help)      usage $0 && exit 0;;
 
586
    -p|--path)      path=$2; shift 2;;
 
587
    -n|--name)      name=$2; shift 2;;
 
588
    -F|--flush-cache) flushcache=1; shift 1;;
 
589
    -r|--release)   release=$2; shift 2;;
 
590
    -b|--bindhome)  bindhome=$2; shift 2;;
 
591
    -a|--arch)      arch=$2; shift 2;;
 
592
    -x|--trim)      trim_container=1; shift 1;;
 
593
    -S|--auth-key)  auth_key=$2; shift 2;;
 
594
    -d|--debug)     debug=1; shift 1;;
 
595
    --)             shift 1; break ;;
 
596
        *)              break ;;
 
597
    esac
 
598
done
 
599
 
 
600
if [ $debug -eq 1 ]; then
 
601
        set -x
 
602
fi
 
603
 
 
604
if [ -n "$bindhome" ]; then
 
605
    pwd=`getent passwd $bindhome`
 
606
    if [ $? -ne 0 ]; then
 
607
        echo "Error: no password entry found for $bindhome"
 
608
        exit 1
 
609
    fi
 
610
fi
 
611
 
 
612
 
 
613
if [ "$arch" == "i686" ]; then
 
614
    arch=i386
 
615
fi
 
616
 
 
617
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
 
618
    echo "can't create amd64 container on i386"
 
619
    exit 1
 
620
fi
 
621
 
 
622
type debootstrap
 
623
if [ $? -ne 0 ]; then
 
624
    echo "'debootstrap' command is missing"
 
625
    exit 1
 
626
fi
 
627
 
 
628
if [ -z "$path" ]; then
 
629
    echo "'path' parameter is required"
 
630
    exit 1
 
631
fi
 
632
 
 
633
if [ "$(id -u)" != "0" ]; then
 
634
    echo "This script should be run as 'root'"
 
635
    exit 1
 
636
fi
 
637
 
 
638
rootfs=$path/rootfs
 
639
 
 
640
install_ubuntu $rootfs $release $flushcache
 
641
if [ $? -ne 0 ]; then
 
642
    echo "failed to install ubuntu $release"
 
643
    exit 1
 
644
fi
 
645
 
 
646
configure_ubuntu $rootfs $name $release
 
647
if [ $? -ne 0 ]; then
 
648
    echo "failed to configure ubuntu $release for a container"
 
649
    exit 1
 
650
fi
 
651
 
 
652
copy_configuration $path $rootfs $name $arch $release
 
653
if [ $? -ne 0 ]; then
 
654
    echo "failed write configuration file"
 
655
    exit 1
 
656
fi
 
657
 
 
658
post_process $rootfs $release $trim_container
 
659
 
 
660
if [ -n "$bindhome" ]; then
 
661
    do_bindhome $rootfs $bindhome
 
662
    finalize_user $bindhome
 
663
else
 
664
    finalize_user ubuntu
 
665
fi
 
666
 
 
667
echo ""
 
668
echo "##"
 
669
echo "# The default user is 'ubuntu' with password 'ubuntu'!"
 
670
echo "# Use the 'sudo' command to run tasks as root in the container."
 
671
echo "##"
 
672
echo ""