44
43
iface eth0 inet dhcp
47
# so you can 'ssh $hostname.' or 'ssh $hostname.local'
48
if [ -f $rootfs/etc/dhcp/dhclient.conf ]; then
49
sed -i "s/<hostname>/$hostname/" $rootfs/etc/dhcp/dhclient.conf
50
elif [ -f $rootfs/etc/dhcp3/dhclient.conf ]; then
51
sed -i "s/<hostname>/$hostname/" $rootfs/etc/dhcp3/dhclient.conf
55
47
cat <<EOF > $rootfs/etc/hostname
60
52
127.0.0.1 localhost $hostname
63
# suppress log level output for udev
64
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
66
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
68
rm -f $rootfs/etc/init/tty{5,6}.conf
70
echo "Please change root-password !"
71
echo "root:root" | chroot $rootfs chpasswd
55
if [ "$release" = "precise" ]; then
60
# suppress log level output for udev
61
sed -i "s/=\"err\"/=0/" $rootfs/etc/udev/udev.conf
63
# remove jobs for consoles 5 and 6 since we only create 4 consoles in
65
rm -f $rootfs/etc/init/tty{5,6}.conf
68
chroot $rootfs groupadd --system $group >/dev/null 2>&1 || true
69
chroot $rootfs useradd --create-home -s /bin/bash -G $group ubuntu
70
echo "ubuntu:ubuntu" | chroot $rootfs chpasswd
71
if [ -n "$auth_key" -a -f "$auth_key" ]; then
72
u_path="/home/ubuntu/.ssh"
73
root_u_path="$rootfs/$u_path"
75
cp $auth_key "$root_u_path/authorized_keys"
76
chroot $rootfs chown -R ubuntu: "$u_path"
78
echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
85
# $1 => path to the rootfs
86
# $2 => architecture we want to add
87
# $3 => whether to use the multi-arch syntax or not
91
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
92
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
97
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
98
SECURITY_MIRROR=${SECURITY_MIRRORMIRROR:-http://security.ubuntu.com/ubuntu}
101
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
102
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
107
MIRROR=${MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
108
SECURITY_MIRROR=${SECURITY_MIRROR:-http://ports.ubuntu.com/ubuntu-ports}
112
cat >> "$1/etc/apt/sources.list" << EOF
113
deb [arch=$2] $MIRROR ${release} main restricted universe multiverse
114
deb [arch=$2] $MIRROR ${release}-updates main restricted universe multiverse
115
deb [arch=$2] $SECURITY_MIRROR ${release}-security main restricted universe multiverse
118
cat >> "$1/etc/apt/sources.list" << EOF
119
deb $MIRROR ${release} main restricted universe multiverse
120
deb $MIRROR ${release}-updates main restricted universe multiverse
121
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
86
136
elif [ $release = "natty" ]; then
87
137
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase
89
packages=dialog,apt,apt-utils,resolvconf,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
139
packages=dialog,apt,apt-utils,iproute,inetutils-ping,vim,isc-dhcp-client,isc-dhcp-common,ssh,lsb-release,gnupg,netbase,ubuntu-keyring
91
141
echo "installing packages: $packages"
100
150
# download a mini ubuntu into a cache
101
151
echo "Downloading ubuntu $release minimal ..."
102
debootstrap --verbose --variant=minbase --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
152
if [ -n "$(which qemu-debootstrap)" ]; then
153
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
155
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
103
158
if [ $? -ne 0 ]; then
104
159
echo "Failed to download the rootfs, aborting."
163
echo "Installing updates"
164
if [ -z "$MIRROR" ]; then
165
MIRROR="http://archive.ubuntu.com/ubuntu"
167
cat >> "$1/partial-${arch}/etc/apt/sources.list" << EOF
168
deb $MIRROR ${release}-updates main universe
169
deb http://security.ubuntu.com/ubuntu ${release}-security main universe
171
chroot "$1/partial-${arch}" apt-get update
172
if [ $? -ne 0 ]; then
173
echo "Failed to update the apt cache"
176
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
180
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
182
chroot "$1/partial-${arch}" apt-get dist-upgrade -y
185
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
186
if [ $ret -ne 0 ]; then
187
echo "Failed to upgrade the cache"
191
# Serge isn't sure whether we should avoid doing this when
192
# $release == `distro-info -d`
193
echo "Installing updates"
194
> $cache/partial-$arch/etc/apt/sources.list
195
write_sourceslist $cache/partial-$arch/ $arch
197
chroot "$1/partial-${arch}" apt-get update
198
if [ $? -ne 0 ]; then
199
echo "Failed to update the apt cache"
202
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
206
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
208
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y
210
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
212
if [ $ret -ne 0 ]; then
213
echo "Failed to upgrade the cache"
108
217
mv "$1/partial-$arch" "$1/rootfs-$arch"
109
echo "Download complete."
218
echo "Download complete"
250
if [ $flushcache -eq 1 ]; then
251
echo "Flushing cache..."
252
rm -rf "$cache/partial-$arch"
253
rm -rf "$cache/rootfs-$arch"
140
256
echo "Checking cache download in $cache/rootfs-$arch ... "
141
257
if [ ! -e "$cache/rootfs-$arch" ]; then
142
258
download_ubuntu $cache $arch $release
170
287
if [ $arch = "i386" ]; then
292
if [ $release = "precise" ]; then
296
# if there is exactly one veth network entry, make sure it has an
298
nics=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
299
if [ $nics -eq 1 ]; then
300
grep -q "^lxc.network.hwaddr" $path/config || cat <<EOF >> $path/config
301
lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
174
305
cat <<EOF >> $path/config
175
306
lxc.utsname = $name
308
lxc.devttydir = $ttydir
179
311
lxc.rootfs = $rootfs
180
312
lxc.mount = $path/fstab
314
lxc.cap.drop = sys_module mac_admin mac_override
183
316
lxc.cgroup.devices.deny = a
317
# Allow any mknod (but not using the node)
318
lxc.cgroup.devices.allow = c *:* m
319
lxc.cgroup.devices.allow = b *:* m
184
320
# /dev/null and zero
185
321
lxc.cgroup.devices.allow = c 1:3 rwm
186
322
lxc.cgroup.devices.allow = c 1:5 rwm
198
334
lxc.cgroup.devices.allow = c 254:0 rwm
200
336
lxc.cgroup.devices.allow = c 10:229 rwm
338
lxc.cgroup.devices.allow = c 10:200 rwm
340
lxc.cgroup.devices.allow = c 1:7 rwm
342
lxc.cgroup.devices.allow = c 10:228 rwm
344
lxc.cgroup.devices.allow = c 10:232 rwm
203
347
cat <<EOF > $path/fstab
315
459
if [ $trim_container -eq 1 ]; then
316
460
trim $rootfs $release
461
elif [ $release = "lucid" -o $release = "maverick" -o $release = "natty" \
462
-o $release = "oneiric" ]; then
318
463
# for lucid and maverick, if not trimming, then add the ubuntu-virt
319
464
# ppa and install lxcguest
320
465
if [ $release = "lucid" -o $release = "maverick" ]; then
321
466
chroot $rootfs apt-get install --force-yes -y python-software-properties
322
467
chroot $rootfs add-apt-repository ppa:ubuntu-virt/ppa
323
chroot $rootfs apt-get update
469
cp /etc/resolv.conf "${rootfs}/etc"
470
chroot $rootfs apt-get update
325
471
chroot $rootfs apt-get install --force-yes -y lxcguest
474
# If the container isn't running a native architecture, setup multiarch
475
if [ -x "$(ls -1 ${rootfs}/usr/bin/qemu-*-static 2>/dev/null)" ]; then
476
mkdir -p ${rootfs}/etc/dpkg/dpkg.cfg.d
477
echo "foreign-architecture ${hostarch}" > ${rootfs}/etc/dpkg/dpkg.cfg.d/lxc-multiarch
479
# Save existing value of MIRROR and SECURITY_MIRROR
480
DEFAULT_MIRROR=$MIRROR
481
DEFAULT_SECURITY_MIRROR=$SECURITY_MIRROR
483
# Write a new sources.list containing both native and multiarch entries
484
> ${rootfs}/etc/apt/sources.list
485
write_sourceslist $rootfs $arch "native"
487
MIRROR=$DEFAULT_MIRROR
488
SECURITY_MIRROR=$DEFAULT_SECURITY_MIRROR
489
write_sourceslist $rootfs $hostarch "multiarch"
491
# Finally update the lists and install upstart using the host architecture
492
chroot $rootfs apt-get update
493
chroot $rootfs apt-get install --force-yes -y --no-install-recommends upstart:${hostarch} mountall:amd64 iproute:amd64 isc-dhcp-client:amd64
334
# bind-mount the user's path into the container's /home
335
h=`getent passwd $user | cut -d: -f 6`
337
echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
339
502
# copy /etc/passwd, /etc/shadow, and /etc/group entries into container
340
503
pwd=`getent passwd $user`
341
504
if [ $? -ne 0 ]; then
342
505
echo 'Warning: failed to copy password entry for $user'
344
508
echo $pwd >> $rootfs/etc/passwd
346
510
shad=`getent shadow $user`
347
511
echo $shad >> $rootfs/etc/shadow
353
cache="/var/cache/lxc/$release"
355
if [ ! -e $cache ]; then
359
# lock, so we won't purge while someone is creating a repository
363
echo "Cache repository is busy."
367
echo -n "Purging the download cache..."
368
rm --preserve-root --one-file-system -rf $cache && echo "Done." || exit 1
371
) 200>/var/lock/subsys/lxc
513
# bind-mount the user's path into the container's /home
514
h=`getent passwd $user | cut -d: -f 6`
516
echo "$h $rootfs/$h none bind 0 0" >> $path/fstab
377
$1 -h|--help -p|--path=<path> --clean [-a|--arch] [-b|--bindhome <user>] [--trim] [-r|--release]
378
release: lucid | maverick | natty | oneiric
522
$1 -h|--help [-a|--arch] [-b|--bindhome <user>] [--trim]
523
[-F | --flush-cache] [-r|--release <release>] [ -S | --auth_key <keyfile>]
524
release: lucid | maverick | natty | oneiric | precise
379
525
trim: make a minimal (faster, but not upgrade-safe) container
380
526
bindhome: bind <user>'s home into the container
381
527
arch: amd64 or i386: defaults to host arch
528
auth_key: SSH Public key file to inject into container
386
options=$(getopt -o a:b:hp:r:xn:c -l arch:,bindhome:,help,path:,release:,trim,name:,clean -- "$@")
533
options=$(getopt -o a:b:hp:r:xn:FS: -l arch:,bindhome:,help,path:,release:,trim,name:,flush-cache,auth-key: -- "$@")
387
534
if [ $? -ne 0 ]; then
388
535
usage $(basename $0)
427
575
-h|--help) usage $0 && exit 0;;
428
576
-p|--path) path=$2; shift 2;;
429
577
-n|--name) name=$2; shift 2;;
430
-c|--clean) clean=$2; shift 2;;
578
-F|--flush-cache) flushcache=1; shift 1;;
431
579
-r|--release) release=$2; shift 2;;
432
580
-b|--bindhome) bindhome=$2; shift 2;;
433
581
-a|--arch) arch=$2; shift 2;;
434
582
-x|--trim) trim_container=1; shift 1;;
583
-S|--auth_key) auth_key=$2; shift 2;;
435
584
--) shift 1; break ;;
589
pwd=`getent passwd $bindhome`
590
if [ $? -ne 0 ]; then
591
echo "Error: no password entry found for $bindhome"
440
596
if [ "$arch" == "i686" ]; then
444
if [ ! -z "$clean" -a -z "$path" ]; then
449
600
if [ $hostarch = "i386" -a $arch = "amd64" ]; then
450
601
echo "can't create amd64 container on i386"
470
621
rootfs=$path/rootfs
472
install_ubuntu $rootfs $release
623
install_ubuntu $rootfs $release $flushcache
473
624
if [ $? -ne 0 ]; then
474
625
echo "failed to install ubuntu $release"
478
configure_ubuntu $rootfs $name
629
configure_ubuntu $rootfs $name $release
479
630
if [ $? -ne 0 ]; then
480
631
echo "failed to configure ubuntu $release for a container"
484
copy_configuration $path $rootfs $name $arch
635
copy_configuration $path $rootfs $name $arch $release
485
636
if [ $? -ne 0 ]; then
486
637
echo "failed write configuration file"