Update NEWS file and bump version for 0.11 release
[ext/instance-debootstrap.git] / create
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 . 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     export https_proxy="$PROXY"
79   fi
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"}
83   debootstrap \
84     --arch "$DPKG_ARCH" \
85     $INCLUDE \
86     "$SUITE" $TMPDIR $MIRROR
87
88   # remove the downloaded debs, as they are no longer needed
89   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
90     xargs -r0 rm -f
91
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"
95
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"
100   fi
101 fi
102
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
107
108 cat > $TMPDIR/etc/fstab <<EOF
109 # /etc/fstab: static file system information.
110 #
111 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
112 UUID=$root_uuid   /               ext3    defaults        0       1
113 proc              /proc           proc    defaults        0       0
114 EOF
115
116 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
117 UUID=$swap_uuid   swap            swap    defaults        0       0
118 EOF
119
120 cat > $TMPDIR/etc/network/interfaces <<EOF
121 auto lo
122 iface lo inet loopback
123 EOF
124
125 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
126   if [ -e $TMPDIR/etc/inittab ]; then
127     # debian
128     echo "T0:23:respawn:/sbin/getty ttyS0 38400" >> $TMPDIR/etc/inittab
129   elif [ -e $TMPDIR/etc/init ]; then
130     # ubuntu (eg. karmic)
131     cat > $TMPDIR/etc/init/ttyS0.conf <<EOF
132 start on stopped rc RUNLEVEL=[2345]
133 stop on runlevel [!2345]
134
135 respawn
136 exec /sbin/getty -8 38400 ttyS0
137 EOF
138   elif [ -e $TMPDIR/etc/event.d ]; then
139     # ubuntu (eg. intrepid)
140     cat > $TMPDIR/etc/event.d/ttyS0.conf <<EOF
141 start on stopped rc2
142 start on stopped rc3
143 start on stopped rc4
144 start on stopped rc5
145
146 stop on runlevel 0
147 stop on runlevel 1
148 stop on runlevel 6
149
150 respawn
151 exec /sbin/getty 38400 ttyS0
152 EOF
153   fi
154 fi
155
156 RUN_PARTS=`which run-parts`
157
158 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
159   TARGET=$TMPDIR
160   BLOCKDEV=$blockdev
161   FSYSDEV=$filesystem_dev
162   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
163   $RUN_PARTS $CUSTOMIZE_DIR
164 fi
165
166 # execute cleanups
167 cleanup
168 trap - EXIT
169
170 exit 0