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