Remove .debs from the instance filesystem
[ext/instance-debootstrap.git] / create
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 mount $blockdev $TMPDIR
58
59 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
60 # default due to the standard umask)
61 if [ -f "$CACHE_FILE" ]; then
62         find "$CACHE_FILE" -perm -0200 -daystart -mtime +14 -print0 | \
63                 xargs -r0 rm -f
64 fi
65
66 if [ -f "$CACHE_FILE" ]; then
67         tar xf "$CACHE_FILE" -C $TMPDIR
68 else
69         # On i386, we need the xen-specific library
70         if [ "$DPKG_ARCH" = "i386" ]; then
71                 EXTRAPKG="linux-image-xen-686 libc6-xen"
72         elif [ "$DPKG_ARCH" = "amd64" ]; then
73                 EXTRAPKG="linux-image-xen-amd64"
74         fi
75
76         debootstrap --include="$EXTRAPKG" etch $TMPDIR $MIRROR
77
78         # remove the downloaded debs, as they are no longer needed
79         find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
80                 xargs -r0 rm -f
81
82         # remove the persistent-net rules, otherwise it will remember the node's
83         # interfaces as eth0, eth1, ...
84
85         rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
86
87         if [ ! -e no_cache ]; then
88                 TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
89                 tar cf "$TMP_CACHE" -C $TMPDIR .
90                 mv -f "$TMP_CACHE" "$CACHE_FILE"
91         fi
92 fi
93
94 cp -p /etc/hosts $TMPDIR/etc/hosts
95 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
96 echo $instance > $TMPDIR/etc/hostname
97 echo $instance > $TMPDIR/etc/mailname
98
99 cat > $TMPDIR/etc/fstab <<EOF
100 # /etc/fstab: static file system information.
101 #
102 # <file system> <mount point>   <type>  <options>       <dump>  <pass>
103 /dev/sda        /               ext3    defaults        0       1
104 /dev/sdb        swap            swap    defaults        0       0
105 proc            /proc           proc    defaults        0       0
106 EOF
107
108 cat > $TMPDIR/etc/network/interfaces <<EOF
109 auto lo
110 iface lo inet loopback
111 EOF
112
113 umount $TMPDIR
114 rmdir $TMPDIR
115 trap - EXIT
116
117 exit 0