Report error when exporting non-ext partition
[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 cp -p /etc/hosts $TMPDIR/etc/hosts
117 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
118 echo $instance > $TMPDIR/etc/hostname
119 echo $instance > $TMPDIR/etc/mailname
120
121 cat > $TMPDIR/etc/fstab <<EOF
122 # /etc/fstab: static file system information.
123 #
124 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
125 UUID=$root_uuid   /               ext3    defaults        0       1
126 proc              /proc           proc    defaults        0       0
127 EOF
128
129 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
130 UUID=$swap_uuid   swap            swap    defaults        0       0
131 EOF
132
133 cat > $TMPDIR/etc/network/interfaces <<EOF
134 auto lo
135 iface lo inet loopback
136 EOF
137
138 # for kvm, we should only activate a serial console if the
139 # 'serial_console' parameter is set; for xen-pvm though, we should
140 # always define a serial console
141 SERIAL_PORT=""
142 if [ "$INSTANCE_HV_serial_console" = "True" ]; then
143   SERIAL_PORT="ttyS0"
144 elif [ "$HYPERVISOR" = "xen-pvm" ]; then
145   SERIAL_PORT="hvc0"
146 fi
147
148 if [ -n "$SERIAL_PORT" ]; then
149   if [ -e $TMPDIR/etc/inittab ]; then
150     # debian
151     echo "T0:23:respawn:/sbin/getty $SERIAL_PORT 38400" >> $TMPDIR/etc/inittab
152   elif [ -e $TMPDIR/etc/init ]; then
153     # ubuntu (eg. karmic)
154     cat > $TMPDIR/etc/init/${SERIAL_PORT}.conf <<EOF
155 start on stopped rc RUNLEVEL=[2345]
156 stop on runlevel [!2345]
157
158 respawn
159 exec /sbin/getty -8 38400 $SERIAL_PORT
160 EOF
161   elif [ -e $TMPDIR/etc/event.d ]; then
162     # ubuntu (eg. intrepid)
163     cat > $TMPDIR/etc/event.d/${SERIAL_PORT}.conf <<EOF
164 start on stopped rc2
165 start on stopped rc3
166 start on stopped rc4
167 start on stopped rc5
168
169 stop on runlevel 0
170 stop on runlevel 1
171 stop on runlevel 6
172
173 respawn
174 exec /sbin/getty 38400 ${SERIAL_PORT}
175 EOF
176   fi
177 fi
178
179 RUN_PARTS=`which run-parts`
180
181 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
182   TARGET=$TMPDIR
183   BLOCKDEV=$blockdev
184   FSYSDEV=$filesystem_dev
185   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
186   $RUN_PARTS $CUSTOMIZE_DIR
187 fi
188
189 # execute cleanups
190 cleanup
191 trap - EXIT
192
193 exit 0