3 # Copyright (C) 2007, 2008, 2009, 2012 Google Inc.
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.
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.
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
24 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
28 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
29 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
31 # If the target device is not a real block device we'll first losetup it.
32 # This is needed for file disks.
33 if [ ! -b $blockdev ]; then
34 ORIGINAL_BLOCKDEV=$blockdev
35 blockdev=$(losetup -sf $blockdev)
36 CLEANUP+=("losetup -d $blockdev")
39 if [ "$PARTITION_STYLE" = "none" ]; then
40 filesystem_dev=$blockdev
41 elif [ "$PARTITION_STYLE" = "msdos" ]; then
42 # Create one big partition, and make it bootable
43 format_disk0 $blockdev
44 filesystem_dev=$(map_disk0 $blockdev)
45 CLEANUP+=("unmap_disk0 $blockdev")
47 echo "Unknown partition style $PARTITION_STYLE"
51 mke2fs -Fjq $filesystem_dev
52 root_uuid=$($VOL_ID $filesystem_dev )
54 if [ -n "$swapdev" ]; then
56 swap_uuid=$($VOL_ID $swapdev || true )
59 TMPDIR=`mktemp -d` || exit 1
60 CLEANUP+=("rmdir $TMPDIR")
62 mount $filesystem_dev $TMPDIR
63 CLEANUP+=("umount $TMPDIR")
65 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
66 # default due to the standard umask)
67 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
68 find "$CACHE_DIR" -name 'cache-*.tar' -type f \
69 -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
73 if [ -f "$CACHE_FILE" ]; then
74 tar xf "$CACHE_FILE" -C $TMPDIR
77 export http_proxy="$PROXY"
78 export https_proxy="$PROXY"
80 # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
81 # build the full parameter format from it
82 INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
86 "$SUITE" $TMPDIR $MIRROR
88 # remove the downloaded debs, as they are no longer needed
89 find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
92 # remove the persistent-net rules, otherwise it will remember the node's
93 # interfaces as eth0, eth1, ...
94 rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
96 if [ "$GENERATE_CACHE" = "yes" ]; then
97 TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
98 tar cf "$TMP_CACHE" -C $TMPDIR .
99 mv -f "$TMP_CACHE" "$CACHE_FILE"
103 cp -p /etc/hosts $TMPDIR/etc/hosts
104 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
105 echo $instance > $TMPDIR/etc/hostname
106 echo $instance > $TMPDIR/etc/mailname
108 cat > $TMPDIR/etc/fstab <<EOF
109 # /etc/fstab: static file system information.
111 # <file system> <mount point> <type> <options> <dump> <pass>
112 UUID=$root_uuid / ext3 defaults 0 1
113 proc /proc proc defaults 0 0
116 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
117 UUID=$swap_uuid swap swap defaults 0 0
120 cat > $TMPDIR/etc/network/interfaces <<EOF
122 iface lo inet loopback
125 # for kvm, we should only activate a serial console if the
126 # 'serial_console' parameter is set; for xen-pvm though, we should
127 # always define a serial console
129 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
131 elif [ "$HYPERVISOR" = "xen-pvm" ]; then
135 if [ -n "$SERIAL_PORT" ]; then
136 if [ -e $TMPDIR/etc/inittab ]; then
138 echo "T0:23:respawn:/sbin/getty $SERIAL_PORT 38400" >> $TMPDIR/etc/inittab
139 elif [ -e $TMPDIR/etc/init ]; then
140 # ubuntu (eg. karmic)
141 cat > $TMPDIR/etc/init/${SERIAL_PORT}.conf <<EOF
142 start on stopped rc RUNLEVEL=[2345]
143 stop on runlevel [!2345]
146 exec /sbin/getty -8 38400 $SERIAL_PORT
148 elif [ -e $TMPDIR/etc/event.d ]; then
149 # ubuntu (eg. intrepid)
150 cat > $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <<EOF
161 exec /sbin/getty 38400 ${SERIAL_PORT}
166 RUN_PARTS=`which run-parts`
168 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
171 FSYSDEV=$filesystem_dev
172 export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
173 $RUN_PARTS $CUSTOMIZE_DIR