| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright (C) 2007, 2008, 2009, 2012 Google Inc. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 |
# 02110-1301, USA. |
| 19 |
|
| 20 |
set -e |
| 21 |
|
| 22 |
# minimum device size is 512 MB, but we use 511 to account for |
| 23 |
# potential rounding |
| 24 |
declare -ri MIN_DEV_SIZE=$((511*1048576)) |
| 25 |
|
| 26 |
. common.sh |
| 27 |
|
| 28 |
if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then |
| 29 |
mkdir -p "$CACHE_DIR" |
| 30 |
fi |
| 31 |
|
| 32 |
DPKG_ARCH=${ARCH:-`dpkg --print-architecture`} |
| 33 |
CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar" |
| 34 |
|
| 35 |
# If the target device is not a real block device we'll first losetup it. |
| 36 |
# This is needed for file disks. |
| 37 |
if [ ! -b $blockdev ]; then |
| 38 |
ORIGINAL_BLOCKDEV=$blockdev |
| 39 |
blockdev=$(losetup --show -f $blockdev) |
| 40 |
CLEANUP+=("losetup -d $blockdev") |
| 41 |
CLEANUP+=("udevadm settle") |
| 42 |
fi |
| 43 |
|
| 44 |
DEVICE_SIZE=$(blockdev --getsize64 $blockdev) |
| 45 |
if [ "$DEVICE_SIZE" -lt $MIN_DEV_SIZE ]; then |
| 46 |
echo "Device size is too small ($((DEVICE_SIZE/1048576)) MB)" 1>&2 |
| 47 |
echo "Required size is at least 512MB" 1>&2 |
| 48 |
exit 1 |
| 49 |
fi |
| 50 |
|
| 51 |
if [ "$PARTITION_STYLE" = "none" ]; then |
| 52 |
filesystem_dev=$blockdev |
| 53 |
elif [ "$PARTITION_STYLE" = "msdos" ]; then |
| 54 |
# Create one big partition, and make it bootable |
| 55 |
format_disk0 $blockdev |
| 56 |
filesystem_dev=$(map_disk0 $blockdev) |
| 57 |
CLEANUP+=("unmap_disk0 $blockdev") |
| 58 |
else |
| 59 |
echo "Unknown partition style $PARTITION_STYLE" 1>&2 |
| 60 |
exit 1 |
| 61 |
fi |
| 62 |
|
| 63 |
mke2fs -Fjqt $OSP_FILESYSTEM $filesystem_dev |
| 64 |
root_uuid=$($VOL_ID $filesystem_dev ) |
| 65 |
|
| 66 |
if [ -n "$swapdev" ]; then |
| 67 |
mkswap $swapdev |
| 68 |
swap_uuid=$($VOL_ID $swapdev || true ) |
| 69 |
fi |
| 70 |
|
| 71 |
TMPDIR=`mktemp -d` || exit 1 |
| 72 |
CLEANUP+=("rmdir $TMPDIR") |
| 73 |
|
| 74 |
mount $filesystem_dev $TMPDIR |
| 75 |
CLEANUP+=("umount $TMPDIR") |
| 76 |
|
| 77 |
# remove the cache file if it's old (> 2 weeks) and writable by the owner (the |
| 78 |
# default due to the standard umask) |
| 79 |
if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then |
| 80 |
find "$CACHE_DIR" -name 'cache-*.tar' -type f \ |
| 81 |
-daystart -mtime "+${CLEAN_CACHE}" -print0 | \ |
| 82 |
xargs -r0 rm -f |
| 83 |
fi |
| 84 |
|
| 85 |
if [ "$PROXY" ]; then |
| 86 |
export http_proxy="$PROXY" |
| 87 |
export https_proxy="$PROXY" |
| 88 |
fi |
| 89 |
|
| 90 |
if [ -f "$CACHE_FILE" ]; then |
| 91 |
tar --acls --selinux --xattrs --xattrs-include='*' -x -f "$CACHE_FILE" -C $TMPDIR |
| 92 |
else |
| 93 |
# INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we |
| 94 |
# build the full parameter format from it |
| 95 |
INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"} |
| 96 |
COMP=${COMPONENTS:+"--components=$COMPONENTS"} |
| 97 |
debootstrap \ |
| 98 |
--arch "$DPKG_ARCH" \ |
| 99 |
$INCLUDE \ |
| 100 |
$COMP \ |
| 101 |
"$SUITE" $TMPDIR $MIRROR |
| 102 |
|
| 103 |
# remove the downloaded debs, as they are no longer needed |
| 104 |
find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \ |
| 105 |
xargs -r0 rm -f |
| 106 |
|
| 107 |
# remove the persistent-net rules, otherwise it will remember the node's |
| 108 |
# interfaces as eth0, eth1, ... |
| 109 |
rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules" |
| 110 |
|
| 111 |
if [ "$GENERATE_CACHE" = "yes" ]; then |
| 112 |
TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"` |
| 113 |
tar --acls --selinux --xattrs --xattrs-include='*' -c -f "$TMP_CACHE" -C $TMPDIR . |
| 114 |
mv -f "$TMP_CACHE" "$CACHE_FILE" |
| 115 |
fi |
| 116 |
fi |
| 117 |
|
| 118 |
# reset the root password |
| 119 |
chroot $TMPDIR passwd -d root |
| 120 |
|
| 121 |
cp -p /etc/hosts $TMPDIR/etc/hosts |
| 122 |
cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf |
| 123 |
echo $instance > $TMPDIR/etc/hostname |
| 124 |
echo $instance > $TMPDIR/etc/mailname |
| 125 |
|
| 126 |
cat > $TMPDIR/etc/fstab <<EOF |
| 127 |
# /etc/fstab: static file system information. |
| 128 |
# |
| 129 |
# <file system> <mount point> <type> <options> <dump> <pass> |
| 130 |
UUID=$root_uuid / $OSP_FILESYSTEM defaults 0 1 |
| 131 |
proc /proc proc defaults 0 0 |
| 132 |
EOF |
| 133 |
|
| 134 |
[ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF |
| 135 |
UUID=$swap_uuid swap swap defaults 0 0 |
| 136 |
EOF |
| 137 |
|
| 138 |
cat > $TMPDIR/etc/network/interfaces <<EOF |
| 139 |
auto lo |
| 140 |
iface lo inet loopback |
| 141 |
EOF |
| 142 |
|
| 143 |
# for kvm, we should only activate a serial console if the |
| 144 |
# 'serial_console' parameter is set; for xen-pvm though, we should |
| 145 |
# always define a serial console |
| 146 |
SERIAL_PORT="" |
| 147 |
if [ "$INSTANCE_HV_serial_console" = "True" ]; then |
| 148 |
SERIAL_PORT="ttyS0" |
| 149 |
elif [ "$HYPERVISOR" = "xen-pvm" ]; then |
| 150 |
SERIAL_PORT="hvc0" |
| 151 |
fi |
| 152 |
|
| 153 |
if [ -n "$SERIAL_PORT" ]; then |
| 154 |
if [ -e $TMPDIR/etc/inittab ]; then |
| 155 |
# debian |
| 156 |
echo "T0:23:respawn:/sbin/getty $SERIAL_PORT 38400" >> $TMPDIR/etc/inittab |
| 157 |
elif [ -e $TMPDIR/etc/init ]; then |
| 158 |
# ubuntu (eg. karmic) |
| 159 |
cat > $TMPDIR/etc/init/${SERIAL_PORT}.conf <<EOF |
| 160 |
start on stopped rc RUNLEVEL=[2345] |
| 161 |
stop on runlevel [!2345] |
| 162 |
|
| 163 |
respawn |
| 164 |
exec /sbin/getty -8 38400 $SERIAL_PORT |
| 165 |
EOF |
| 166 |
elif [ -e $TMPDIR/etc/event.d ]; then |
| 167 |
# ubuntu (eg. intrepid) |
| 168 |
cat > $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <<EOF |
| 169 |
start on stopped rc2 |
| 170 |
start on stopped rc3 |
| 171 |
start on stopped rc4 |
| 172 |
start on stopped rc5 |
| 173 |
|
| 174 |
stop on runlevel 0 |
| 175 |
stop on runlevel 1 |
| 176 |
stop on runlevel 6 |
| 177 |
|
| 178 |
respawn |
| 179 |
exec /sbin/getty 38400 ${SERIAL_PORT} |
| 180 |
EOF |
| 181 |
fi |
| 182 |
fi |
| 183 |
|
| 184 |
RUN_PARTS=`which run-parts` |
| 185 |
|
| 186 |
if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then |
| 187 |
TARGET=$TMPDIR |
| 188 |
BLOCKDEV=$blockdev |
| 189 |
FSYSDEV=$filesystem_dev |
| 190 |
export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV |
| 191 |
$RUN_PARTS $CUSTOMIZE_DIR |
| 192 |
fi |
| 193 |
|
| 194 |
# execute cleanups |
| 195 |
cleanup |
| 196 |
trap - EXIT |
| 197 |
|
| 198 |
exit 0 |