Switch indendation to two spaces
[ext/instance-debootstrap.git] / create.in
1 #!/bin/sh
2
3 # Copyright (C) 2007 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 # Customize this if you need another mirror, or set to empty to get the
23 # node's sources.list
24 MIRROR="http://ftp.debian.org/debian"
25 DPKG_ARCH="`dpkg --print-architecture`"
26 CACHE_FILE="cache-${DPKG_ARCH}.tar"
27
28 TEMP=`getopt -o i:b:s: -n '$0' -- "$@"`
29
30 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
31
32 # Note the quotes around `$TEMP': they are essential!
33 eval set -- "$TEMP"
34
35 while true; do
36   case "$1" in
37     -i) instance=$2; shift 2;;
38
39     -b) blockdev=$2; shift 2;;
40
41     -s) swapdev=$2; shift 2;;
42
43     --) shift; break;;
44
45     *)  echo "Internal error!"; exit 1;;
46   esac
47 done
48 if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
49   echo "Missing -i or -b or -s argument!"
50   exit 1
51 fi
52
53 mkswap $swapdev
54 mke2fs -Fjq $blockdev
55 TMPDIR=`mktemp -d` || exit 1
56 trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
57
58 # If it's not a block device try to mount it via loopback device.
59 # This is needed for file disks.
60 MOUNT_OPTIONS=""
61 if [ ! -b $blockdev ]; then
62   MOUNT_OPTIONS="$MOUNT_OPTIONS -o loop"
63 fi
64 mount $MOUNT_OPTIONS $blockdev $TMPDIR
65
66 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
67 # default due to the standard umask)
68 if [ -f "$CACHE_FILE" ]; then
69   find "$CACHE_FILE" -perm -0200 -daystart -mtime +14 -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   # On i386, we need the xen-specific library
77   if [ "$DPKG_ARCH" = "i386" ]; then
78     EXTRAPKG="linux-image-xen-686 libc6-xen"
79   elif [ "$DPKG_ARCH" = "amd64" ]; then
80     EXTRAPKG="linux-image-xen-amd64"
81   fi
82
83   debootstrap --include="$EXTRAPKG" etch $TMPDIR $MIRROR
84
85   # remove the downloaded debs, as they are no longer needed
86   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
87     xargs -r0 rm -f
88
89   # remove the persistent-net rules, otherwise it will remember the node's
90   # interfaces as eth0, eth1, ...
91
92   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
93
94   if [ ! -e no_cache ]; then
95     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
96     tar cf "$TMP_CACHE" -C $TMPDIR .
97     mv -f "$TMP_CACHE" "$CACHE_FILE"
98   fi
99 fi
100
101 cp -p /etc/hosts $TMPDIR/etc/hosts
102 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
103 echo $instance > $TMPDIR/etc/hostname
104 echo $instance > $TMPDIR/etc/mailname
105
106 cat > $TMPDIR/etc/fstab <<EOF
107 # /etc/fstab: static file system information.
108 #
109 # <file system> <mount point>   <type>  <options>       <dump>  <pass>
110 /dev/sda        /               ext3    defaults        0       1
111 /dev/sdb        swap            swap    defaults        0       0
112 proc            /proc           proc    defaults        0       0
113 EOF
114
115 cat > $TMPDIR/etc/network/interfaces <<EOF
116 auto lo
117 iface lo inet loopback
118 EOF
119
120 umount $TMPDIR
121 rmdir $TMPDIR
122 trap - EXIT
123
124 exit 0