Update the README file
[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 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
23 if [ -f "$DEFAULT_FILE" ]; then
24     . "$DEFAULT_FILE"
25 fi
26
27 # note: we don't set a default mirror since debian and ubuntu have
28 # different defaults, and it's better to use the default
29
30 # only if the user want to specify a mirror in the defaults file we
31 # will use it, this declaration is to make sure the variable is set
32 : ${MIRROR:=""}
33 : ${SUITE:="lenny"}
34 : ${ARCH:=""}
35 : ${EXTRA_PKGS:=""}
36 : ${GENERATE_CACHE:="yes"}
37 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
38
39 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
40
41 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
42   mkdir -p "$CACHE_DIR"
43 fi
44
45 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
46 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
47
48 TEMP=`getopt -o i:b:s: -n '$0' -- "$@"`
49
50 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
51
52 # Note the quotes around `$TEMP': they are essential!
53 eval set -- "$TEMP"
54
55 while true; do
56   case "$1" in
57     -i) instance=$2; shift 2;;
58
59     -b) blockdev=$2; shift 2;;
60
61     -s) swapdev=$2; shift 2;;
62
63     --) shift; break;;
64
65     *)  echo "Internal error!"; exit 1;;
66   esac
67 done
68 if [ -z "$instance" -o -z "$blockdev" -o -z "$swapdev" ]; then
69   echo "Missing -i or -b or -s argument!"
70   exit 1
71 fi
72
73 mkswap $swapdev
74 mke2fs -Fjq $blockdev
75 TMPDIR=`mktemp -d` || exit 1
76 trap "umount $TMPDIR; rmdir $TMPDIR" EXIT
77
78 # If it's not a block device try to mount it via loopback device.
79 # This is needed for file disks.
80 MOUNT_OPTIONS=""
81 if [ ! -b $blockdev ]; then
82   MOUNT_OPTIONS="$MOUNT_OPTIONS -o loop"
83 fi
84 mount $MOUNT_OPTIONS $blockdev $TMPDIR
85
86 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
87 # default due to the standard umask)
88 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
89   find "$CACHE_DIR" -name 'cache-*.tar' -type f \
90     -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
91     xargs -r0 rm -f
92 fi
93
94 if [ -f "$CACHE_FILE" ]; then
95   tar xf "$CACHE_FILE" -C $TMPDIR
96 else
97   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
98   # build the full parameter format from it
99   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
100   debootstrap \
101     --arch "$DPKG_ARCH" \
102     $INCLUDE \
103     "$SUITE" $TMPDIR $MIRROR
104
105   # remove the downloaded debs, as they are no longer needed
106   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
107     xargs -r0 rm -f
108
109   # remove the persistent-net rules, otherwise it will remember the node's
110   # interfaces as eth0, eth1, ...
111   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
112
113   if [ "$GENERATE_CACHE" = "yes" ]; then
114     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
115     tar cf "$TMP_CACHE" -C $TMPDIR .
116     mv -f "$TMP_CACHE" "$CACHE_FILE"
117   fi
118 fi
119
120 cp -p /etc/hosts $TMPDIR/etc/hosts
121 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
122 echo $instance > $TMPDIR/etc/hostname
123 echo $instance > $TMPDIR/etc/mailname
124
125 cat > $TMPDIR/etc/fstab <<EOF
126 # /etc/fstab: static file system information.
127 #
128 # <file system> <mount point>   <type>  <options>       <dump>  <pass>
129 /dev/sda        /               ext3    defaults        0       1
130 /dev/sdb        swap            swap    defaults        0       0
131 proc            /proc           proc    defaults        0       0
132 EOF
133
134 cat > $TMPDIR/etc/network/interfaces <<EOF
135 auto lo
136 iface lo inet loopback
137 EOF
138
139 umount $TMPDIR
140 rmdir $TMPDIR
141 trap - EXIT
142
143 exit 0