Clear the root password
[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 # minimum device size is 256 MB, but we use 255 to account for
23 # potential rounding
24 declare -ri MIN_DEV_SIZE=$((255*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 -sf $blockdev)
40   CLEANUP+=("losetup -d $blockdev")
41 fi
42
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
47   exit 1
48 fi
49
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")
57 else
58   echo "Unknown partition style $PARTITION_STYLE" 1>&2
59   exit 1
60 fi
61
62 mke2fs -Fjqt $OSP_FILESYSTEM $filesystem_dev
63 root_uuid=$($VOL_ID $filesystem_dev )
64
65 if [ -n "$swapdev" ]; then
66   mkswap $swapdev
67   swap_uuid=$($VOL_ID $swapdev || true )
68 fi
69
70 TMPDIR=`mktemp -d` || exit 1
71 CLEANUP+=("rmdir $TMPDIR")
72
73 mount $filesystem_dev $TMPDIR
74 CLEANUP+=("umount $TMPDIR")
75
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 | \
81     xargs -r0 rm -f
82 fi
83
84 if [ -f "$CACHE_FILE" ]; then
85   tar xf "$CACHE_FILE" -C $TMPDIR
86 else
87   if [ "$PROXY" ]; then
88     export http_proxy="$PROXY"
89     export https_proxy="$PROXY"
90   fi
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"}
95   debootstrap \
96     --arch "$DPKG_ARCH" \
97     $INCLUDE \
98     $COMP \
99     "$SUITE" $TMPDIR $MIRROR
100
101   # remove the downloaded debs, as they are no longer needed
102   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
103     xargs -r0 rm -f
104
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"
108
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"
113   fi
114 fi
115
116 # reset the root password
117 chroot $TMPDIR passwd -d root
118
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
123
124 cat > $TMPDIR/etc/fstab <<EOF
125 # /etc/fstab: static file system information.
126 #
127 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
128 UUID=$root_uuid   /               ext3    defaults        0       1
129 proc              /proc           proc    defaults        0       0
130 EOF
131
132 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
133 UUID=$swap_uuid   swap            swap    defaults        0       0
134 EOF
135
136 cat > $TMPDIR/etc/network/interfaces <<EOF
137 auto lo
138 iface lo inet loopback
139 EOF
140
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
144 SERIAL_PORT=""
145 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
146   SERIAL_PORT="ttyS0"
147 elif [ "$HYPERVISOR" = "xen-pvm" ]; then
148   SERIAL_PORT="hvc0"
149 fi
150
151 if [ -n "$SERIAL_PORT" ]; then
152   if [ -e $TMPDIR/etc/inittab ]; then
153     # debian
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]
160
161 respawn
162 exec /sbin/getty -8 38400 $SERIAL_PORT
163 EOF
164   elif [ -e $TMPDIR/etc/event.d ]; then
165     # ubuntu (eg. intrepid)
166     cat > $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <<EOF
167 start on stopped rc2
168 start on stopped rc3
169 start on stopped rc4
170 start on stopped rc5
171
172 stop on runlevel 0
173 stop on runlevel 1
174 stop on runlevel 6
175
176 respawn
177 exec /sbin/getty 38400 ${SERIAL_PORT}
178 EOF
179   fi
180 fi
181
182 RUN_PARTS=`which run-parts`
183
184 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
185   TARGET=$TMPDIR
186   BLOCKDEV=$blockdev
187   FSYSDEV=$filesystem_dev
188   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
189   $RUN_PARTS $CUSTOMIZE_DIR
190 fi
191
192 # execute cleanups
193 cleanup
194 trap - EXIT
195
196 exit 0