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
22 # minimum device size is 256 MB, but we use 255 to account for
24 declare -ri MIN_DEV_SIZE=$((255*1048576))
28 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
32 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
33 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
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 -sf $blockdev)
40 CLEANUP+=("losetup -d $blockdev")
43 DEVICE_SIZE=$(blockdev --getsize64 $blockdev)
44 if [ "$DEVICE_SIZE" -lt $MIN_DEV_SIZE ]; then
45 echo "Device size is too small ($((DEVICE_SIZE/1048576)) MB)" 1>&2
46 echo "Required size is at least 256MB" 1>&2
50 if [ "$PARTITION_STYLE" = "none" ]; then
51 filesystem_dev=$blockdev
52 elif [ "$PARTITION_STYLE" = "msdos" ]; then
53 # Create one big partition, and make it bootable
54 format_disk0 $blockdev
55 filesystem_dev=$(map_disk0 $blockdev)
56 CLEANUP+=("unmap_disk0 $blockdev")
58 echo "Unknown partition style $PARTITION_STYLE" 1>&2
62 mke2fs -Fjqt $OSP_FILESYSTEM $filesystem_dev
63 root_uuid=$($VOL_ID $filesystem_dev )
65 if [ -n "$swapdev" ]; then
67 swap_uuid=$($VOL_ID $swapdev || true )
70 TMPDIR=`mktemp -d` || exit 1
71 CLEANUP+=("rmdir $TMPDIR")
73 mount $filesystem_dev $TMPDIR
74 CLEANUP+=("umount $TMPDIR")
76 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
77 # default due to the standard umask)
78 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
79 find "$CACHE_DIR" -name 'cache-*.tar' -type f \
80 -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
84 if [ -f "$CACHE_FILE" ]; then
85 tar xf "$CACHE_FILE" -C $TMPDIR
88 export http_proxy="$PROXY"
89 export https_proxy="$PROXY"
91 # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
92 # build the full parameter format from it
93 INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
94 COMP=${COMPONENTS:+"--components=$COMPONENTS"}
99 "$SUITE" $TMPDIR $MIRROR
101 # remove the downloaded debs, as they are no longer needed
102 find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
105 # remove the persistent-net rules, otherwise it will remember the node's
106 # interfaces as eth0, eth1, ...
107 rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
109 if [ "$GENERATE_CACHE" = "yes" ]; then
110 TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
111 tar cf "$TMP_CACHE" -C $TMPDIR .
112 mv -f "$TMP_CACHE" "$CACHE_FILE"
116 # reset the root password
117 chroot $TMPDIR passwd -d root
119 cp -p /etc/hosts $TMPDIR/etc/hosts
120 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
121 echo $instance > $TMPDIR/etc/hostname
122 echo $instance > $TMPDIR/etc/mailname
124 cat > $TMPDIR/etc/fstab <<EOF
125 # /etc/fstab: static file system information.
127 # <file system> <mount point> <type> <options> <dump> <pass>
128 UUID=$root_uuid / ext3 defaults 0 1
129 proc /proc proc defaults 0 0
132 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
133 UUID=$swap_uuid swap swap defaults 0 0
136 cat > $TMPDIR/etc/network/interfaces <<EOF
138 iface lo inet loopback
141 # for kvm, we should only activate a serial console if the
142 # 'serial_console' parameter is set; for xen-pvm though, we should
143 # always define a serial console
145 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
147 elif [ "$HYPERVISOR" = "xen-pvm" ]; then
151 if [ -n "$SERIAL_PORT" ]; then
152 if [ -e $TMPDIR/etc/inittab ]; then
154 echo "T0:23:respawn:/sbin/getty $SERIAL_PORT 38400" >> $TMPDIR/etc/inittab
155 elif [ -e $TMPDIR/etc/init ]; then
156 # ubuntu (eg. karmic)
157 cat > $TMPDIR/etc/init/${SERIAL_PORT}.conf <<EOF
158 start on stopped rc RUNLEVEL=[2345]
159 stop on runlevel [!2345]
162 exec /sbin/getty -8 38400 $SERIAL_PORT
164 elif [ -e $TMPDIR/etc/event.d ]; then
165 # ubuntu (eg. intrepid)
166 cat > $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <<EOF
177 exec /sbin/getty 38400 ${SERIAL_PORT}
182 RUN_PARTS=`which run-parts`
184 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
187 FSYSDEV=$filesystem_dev
188 export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
189 $RUN_PARTS $CUSTOMIZE_DIR