Some extra example hooks
[ext/instance-debootstrap.git] / create
1 #!/bin/bash
2
3 # Copyright (C) 2007, 2008, 2009 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 . common.sh
23
24 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
25   mkdir -p "$CACHE_DIR"
26 fi
27
28 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
29 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
30
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")
37 fi
38
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")
46 else
47   echo "Unknown partition style $PARTITION_STYLE"
48   exit 1
49 fi
50
51 mke2fs -Fjq $filesystem_dev
52 root_uuid=$($VOL_ID $filesystem_dev )
53
54 if [ -n "$swapdev" ]; then
55   mkswap $swapdev
56   swap_uuid=$($VOL_ID $swapdev || true )
57 fi
58
59 TMPDIR=`mktemp -d` || exit 1
60 CLEANUP+=("rmdir $TMPDIR")
61
62 mount $filesystem_dev $TMPDIR
63 CLEANUP+=("umount $TMPDIR")
64
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 | \
70     xargs -r0 rm -f
71 fi
72
73 if [ -f "$CACHE_FILE" ]; then
74   tar xf "$CACHE_FILE" -C $TMPDIR
75 else
76   if [ "$PROXY" ]; then
77     export http_proxy="$PROXY"
78   fi
79   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
80   # build the full parameter format from it
81   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
82   debootstrap \
83     --arch "$DPKG_ARCH" \
84     $INCLUDE \
85     "$SUITE" $TMPDIR $MIRROR
86
87   # remove the downloaded debs, as they are no longer needed
88   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
89     xargs -r0 rm -f
90
91   # remove the persistent-net rules, otherwise it will remember the node's
92   # interfaces as eth0, eth1, ...
93   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
94
95   if [ "$GENERATE_CACHE" = "yes" ]; then
96     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
97     tar cf "$TMP_CACHE" -C $TMPDIR .
98     mv -f "$TMP_CACHE" "$CACHE_FILE"
99   fi
100 fi
101
102 cp -p /etc/hosts $TMPDIR/etc/hosts
103 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
104 echo $instance > $TMPDIR/etc/hostname
105 echo $instance > $TMPDIR/etc/mailname
106
107 cat > $TMPDIR/etc/fstab <<EOF
108 # /etc/fstab: static file system information.
109 #
110 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
111 UUID=$root_uuid   /               ext3    defaults        0       1
112 proc              /proc           proc    defaults        0       0
113 EOF
114
115 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
116 UUID=$swap_uuid   swap            swap    defaults        0       0
117 EOF
118
119 cat > $TMPDIR/etc/network/interfaces <<EOF
120 auto lo
121 iface lo inet loopback
122 EOF
123
124 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
125   if [ -e $TMPDIR/etc/inittab ]; then
126     # debian
127     echo "T0:23:respawn:/sbin/getty ttyS0 38400" >> $TMPDIR/etc/inittab
128   elif [ -e $TMPDIR/etc/init ]; then
129     # ubuntu (eg. karmic)
130     cat > $TMPDIR/etc/init/ttyS0.conf <<EOF
131 start on stopped rc RUNLEVEL=[2345]
132 stop on runlevel [!2345]
133
134 respawn
135 exec /sbin/getty -8 38400 ttyS0
136 EOF
137   elif [ -e $TMPDIR/etc/event.d ]; then
138     # ubuntu (eg. intrepid)
139     cat > $TMPDIR/etc/event.d/ttyS0.conf <<EOF
140 start on stopped rc2
141 start on stopped rc3
142 start on stopped rc4
143 start on stopped rc5
144
145 stop on runlevel 0
146 stop on runlevel 1
147 stop on runlevel 6
148
149 respawn
150 exec /sbin/getty 38400 ttyS0
151 EOF
152   fi
153 fi
154
155 RUN_PARTS=`which run-parts`
156
157 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
158   TARGET=$TMPDIR
159   BLOCKDEV=$blockdev
160   FSYSDEV=$filesystem_dev
161   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
162   $RUN_PARTS $CUSTOMIZE_DIR
163 fi
164
165 # execute cleanups
166 cleanup
167 trap - EXIT
168
169 exit 0