39
39
# configure the network using the dhcp
40
40
cat <<EOF > $rootfs/etc/network/interfaces
41
# This file describes the network interfaces available on your system
42
# and how to activate them. For more information, see interfaces(5).
44
# The loopback network interface
42
46
iface lo inet loopback
52
56
# set minimal hosts
53
57
cat <<EOF > $rootfs/etc/hosts
54
127.0.0.1 localhost $hostname
61
# The following lines are desirable for IPv6 capable hosts
62
::1 ip6-localhost ip6-loopback
64
ff00::0 ip6-mcastprefix
66
ff02::2 ip6-allrouters
57
if [ "$release" != "precise" ] && [ "$release" != "quantal" ]; then
69
if [ ! -f $rootfs/etc/init/container-detect.conf ]; then
58
70
# suppress log level output for udev
59
71
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
92
106
if [ -n "$auth_key" -a -f "$auth_key" ]; then
93
u_path="/home/${user}/.ssh"
94
root_u_path="$rootfs/$u_path"
96
cp $auth_key "$root_u_path/authorized_keys"
97
chroot $rootfs chown -R ${user}: "$u_path"
99
echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
107
u_path="/home/${user}/.ssh"
108
root_u_path="$rootfs/$u_path"
110
mkdir -p $root_u_path
111
cp $auth_key "$root_u_path/authorized_keys"
112
chroot $rootfs chown -R ${user}: "$u_path"
114
echo "Inserted SSH public key from $auth_key into /home/${user}/.ssh/authorized_keys"
112
127
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
113
128
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
118
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
119
SECURITY_MIRROR=${SECURITY_MIRRORMIRROR:-http://security.ubuntu.com/ubuntu}
122
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
123
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
128
131
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
129
132
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
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 = "natty" ]; then
156
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase
158
packages=dialog,apt,apt-utils,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
160
157
echo "installing packages: $packages"
162
159
# check the mini ubuntu was not already downloaded
259
256
cache="/var/cache/lxc/$release"
260
257
mkdir -p /var/lock/subsys/
263
if [ $? -ne 0 ]; then
264
echo "Cache repository is busy."
269
if [ $flushcache -eq 1 ]; then
270
echo "Flushing cache..."
271
rm -rf "$cache/partial-$arch"
272
rm -rf "$cache/rootfs-$arch"
275
echo "Checking cache download in $cache/rootfs-$arch ... "
276
if [ ! -e "$cache/rootfs-$arch" ]; then
277
download_ubuntu $cache $arch $release
278
if [ $? -ne 0 ]; then
279
echo "Failed to download 'ubuntu $release base'"
284
echo "Copy $cache/rootfs-$arch to $rootfs ... "
285
copy_ubuntu $cache $arch $rootfs
286
if [ $? -ne 0 ]; then
287
echo "Failed to copy rootfs"
293
) 200>/var/lock/subsys/lxc
261
if [ $? -ne 0 ]; then
262
echo "Cache repository is busy."
267
if [ $flushcache -eq 1 ]; then
268
echo "Flushing cache..."
269
rm -rf "$cache/partial-$arch"
270
rm -rf "$cache/rootfs-$arch"
273
echo "Checking cache download in $cache/rootfs-$arch ... "
274
if [ ! -e "$cache/rootfs-$arch" ]; then
275
download_ubuntu $cache $arch $release
276
if [ $? -ne 0 ]; then
277
echo "Failed to download 'ubuntu $release base'"
282
echo "Copy $cache/rootfs-$arch to $rootfs ... "
283
copy_ubuntu $cache $arch $rootfs
284
if [ $? -ne 0 ]; then
285
echo "Failed to copy rootfs"
291
) 200>/var/lock/subsys/lxc
450
448
# reconfigure some services
451
449
if [ -z "$LANG" ]; then
452
chroot $rootfs locale-gen en_US.UTF-8
453
chroot $rootfs update-locale LANG=en_US.UTF-8
450
chroot $rootfs locale-gen en_US.UTF-8
451
chroot $rootfs update-locale LANG=en_US.UTF-8
455
chroot $rootfs locale-gen $LANG
456
chroot $rootfs update-locale LANG=$LANG
453
chroot $rootfs locale-gen $LANG
454
chroot $rootfs update-locale LANG=$LANG
459
457
# remove pointless services in a container
480
478
if [ $trim_container -eq 1 ]; then
481
479
trim $rootfs $release
482
elif [ $release = "lucid" -o $release = "natty" \
483
-o $release = "oneiric" ]; then
480
elif [ ! -f $rootfs/etc/init/container-detect.conf ]; then
481
# Make sure we have a working resolv.conf
482
cresolvonf="${rootfs}/etc/resolv.conf"
483
mv $cresolvonf ${cresolvonf}.lxcbak
484
cat /etc/resolv.conf > ${cresolvonf}
484
486
# for lucid, if not trimming, then add the ubuntu-virt
485
487
# ppa and install lxcguest
486
488
if [ $release = "lucid" ]; then
487
489
chroot $rootfs apt-get install --force-yes -y python-software-properties
488
490
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
490
cresolvonf="${rootfs}/etc/resolv.conf"
491
mv $cresolvonf ${cresolvonf}.lxcbak
492
cat /etc/resolv.conf > ${cresolvonf}
493
493
chroot $rootfs apt-get update
494
494
chroot $rootfs apt-get install --force-yes -y lxcguest
496
mv ${cresolvonf}.lxcbak ${cresolvonf}
496
# Restore old resolv.conf
498
mv ${cresolvonf}.lxcbak ${cresolvonf}
499
501
# If the container isn't running a native architecture, setup multiarch
518
520
chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:${hostarch} iproute:${hostarch} isc-dhcp-client:${hostarch}
521
# rmdir /dev/shm in precise and quantal containers.
523
# rmdir /dev/shm for containers that have /run/shm
522
524
# I'm afraid of doing rm -rf $rootfs/dev/shm, in case it did
523
525
# get bind mounted to the host's /run/shm. So try to rmdir
524
526
# it, and in case that fails move it out of the way.
525
if [ $release = "precise" ] || [ $release = "quantal" ]; then
527
if [ -d $rootfs/run/shm ]; then
526
528
[ -d "$rootfs/dev/shm" ] && rmdir $rootfs/dev/shm
527
529
[ -e "$rootfs/dev/shm" ] && mv $rootfs/dev/shm $rootfs/dev/shm.bak
528
530
ln -s /run/shm $rootfs/dev/shm
553
555
# bind-mount the user's path into the container's /home
554
556
h=`getent passwd $user | cut -d: -f 6`
555
557
mkdir -p $rootfs/$h
556
# use relative path in container
558
while [ ${h2:0:1} = "/" ]; do
559
# use relative path in container
561
while [ ${h2:0:1} = "/" ]; do
561
564
echo "$h $h2 none bind 0 0" >> $path/fstab
563
566
# Make sure the group exists in container
572
575
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim] [-d|--debug]
573
576
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth-key <keyfile>]
574
release: lucid | natty | oneiric | precise | quantal
577
release: the ubuntu release (e.g. precise): defaults to host release on ubuntu, otherwise uses latest LTS
575
578
trim: make a minimal (faster, but not upgrade-safe) container
576
579
bindhome: bind <user>'s home into the container
577
580
The ubuntu user will not be created, and <user> will have
579
arch: amd64 or i386: defaults to host arch
582
arch: the container architecture (e.g. amd64): defaults to host arch
580
583
auth-key: SSH Public key file to inject into container
590
593
eval set -- "$options"
595
release=precise # Default to the last Ubuntu LTS release for non-Ubuntu systems
593
596
if [ -f /etc/lsb-release ]; then
594
597
. /etc/lsb-release
595
case "$DISTRIB_CODENAME" in
596
lucid|natty|oneiric|precise|quantal)
597
release=$DISTRIB_CODENAME
598
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
599
release=$DISTRIB_CODENAME