instance-debootstrap: release 0.6
[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 CLEANUP=( )
23
24 cleanup() {
25   if [ ${#CLEANUP[*]} -gt 0 ]; then
26     LAST_ELEMENT=$((${#CLEANUP[*]}-1))
27     REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
28     for i in $REVERSE_INDEXES; do
29       ${CLEANUP[$i]}
30     done
31   fi
32 }
33
34 trap cleanup EXIT
35
36 DEFAULT_FILE="@sysconfdir@/default/ganeti-instance-debootstrap"
37 if [ -f "$DEFAULT_FILE" ]; then
38     . "$DEFAULT_FILE"
39 fi
40
41 # note: we don't set a default mirror since debian and ubuntu have
42 # different defaults, and it's better to use the default
43
44 # only if the user want to specify a mirror in the defaults file we
45 # will use it, this declaration is to make sure the variable is set
46 : ${MIRROR:=""}
47 : ${PROXY:=""}
48 : ${SUITE:="lenny"}
49 : ${ARCH:=""}
50 : ${EXTRA_PKGS:=""}
51 : ${GENERATE_CACHE:="yes"}
52 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
53
54 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
55
56 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
57   mkdir -p "$CACHE_DIR"
58 fi
59
60 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
61 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
62
63 . common.sh
64
65 # If the target device is not a real block device we'll first losetup it.
66 # This is needed for file disks.
67 if [ ! -b $blockdev ]; then
68   ORIGINAL_BLOCKDEV=$blockdev
69   blockdev=$(losetup -sf $blockdev)
70   CLEANUP+=("losetup -d $blockdev")
71 fi
72
73 mke2fs -Fjq $blockdev
74 root_uuid=$($VOL_ID -u $blockdev )
75 if [ -n "$swapdev" ]; then
76   mkswap $swapdev
77   swap_uuid=$($VOL_ID -u $swapdev )
78 fi
79
80 TMPDIR=`mktemp -d` || exit 1
81 CLEANUP+=("rmdir $TMPDIR")
82
83 mount $blockdev $TMPDIR
84 CLEANUP+=("umount $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   if [ "$PROXY" ]; then
98     export http_proxy="$PROXY"
99   fi
100   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
101   # build the full parameter format from it
102   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
103   debootstrap \
104     --arch "$DPKG_ARCH" \
105     $INCLUDE \
106     "$SUITE" $TMPDIR $MIRROR
107
108   # remove the downloaded debs, as they are no longer needed
109   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
110     xargs -r0 rm -f
111
112   # remove the persistent-net rules, otherwise it will remember the node's
113   # interfaces as eth0, eth1, ...
114   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
115
116   if [ "$GENERATE_CACHE" = "yes" ]; then
117     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
118     tar cf "$TMP_CACHE" -C $TMPDIR .
119     mv -f "$TMP_CACHE" "$CACHE_FILE"
120   fi
121 fi
122
123 cp -p /etc/hosts $TMPDIR/etc/hosts
124 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
125 echo $instance > $TMPDIR/etc/hostname
126 echo $instance > $TMPDIR/etc/mailname
127
128 cat > $TMPDIR/etc/fstab <<EOF
129 # /etc/fstab: static file system information.
130 #
131 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
132 UUID=$root_uuid   /               ext3    defaults        0       1
133 proc              /proc           proc    defaults        0       0
134 EOF
135
136 [ -n "$swapdev" ] && cat >> $TMPDIR/etc/fstab <<EOF
137 UUID=$swap_uuid   swap            swap    defaults        0       0
138 EOF
139
140 cat > $TMPDIR/etc/network/interfaces <<EOF
141 auto lo
142 iface lo inet loopback
143 EOF
144
145 if [ -e $TMPDIR/etc/inittab ]; then
146   cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
147     > $TMPDIR/etc/inittab.new
148   mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
149 elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
150   cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
151     > $TMPDIR/etc/event.d/console
152   rm $TMPDIR/etc/event.d/tty1
153 fi
154
155 # execute cleanups
156 cleanup
157 trap - EXIT
158
159 exit 0