To avoid bashisms, convert to /bin/bash! :)
[ext/instance-debootstrap.git] / create.in
1 #!/bin/bash
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 : ${CUSTOMIZE_DIR:="@sysconfdir@/ganeti/instance-debootstrap.d"}
52 : ${GENERATE_CACHE:="yes"}
53 : ${CLEAN_CACHE:="14"} # number of days to keep a cache file
54 : ${PARTITION_STYLE:="none"} # disk partition style
55
56 CACHE_DIR="@localstatedir@/cache/ganeti-instance-debootstrap"
57
58 if [ "$GENERATE_CACHE" = "yes" -a ! -d "$CACHE_DIR" ]; then
59   mkdir -p "$CACHE_DIR"
60 fi
61
62 DPKG_ARCH=${ARCH:-`dpkg --print-architecture`}
63 CACHE_FILE="$CACHE_DIR/cache-${SUITE}-${DPKG_ARCH}.tar"
64
65 . common.sh
66
67 # If the target device is not a real block device we'll first losetup it.
68 # This is needed for file disks.
69 if [ ! -b $blockdev ]; then
70   ORIGINAL_BLOCKDEV=$blockdev
71   blockdev=$(losetup -sf $blockdev)
72   CLEANUP+=("losetup -d $blockdev")
73 fi
74
75 if [ "$PARTITION_STYLE" = "none" ]; then
76   filesystem_dev=$blockdev
77 elif [ "$PARTITION_STYLE" = "msdos" ]; then
78   # Create one big partition, and make it bootable
79   sfdisk --quiet --Linux $blockdev <<EOF
80 0,,L,*
81 EOF
82   block_base=`basename $blockdev`
83   filesystem_dev_base=`kpartx -l -p- $blockdev | \
84                        grep -m 1 "^$block_base.*$blockdev" | \
85                        awk '{print $1}'`
86   kpartx -a -p- $blockdev
87   CLEANUP+=("kpartx -d -p- $blockdev")
88   filesystem_dev="/dev/mapper/$filesystem_dev_base"
89 else
90   echo "Unknown partition style $PARTITION_STYLE"
91   exit 1
92 fi
93
94 mke2fs -Fjq $filesystem_dev
95 root_uuid=$($VOL_ID -u $filesystem_dev )
96
97 if [ -n "$swapdev" ]; then
98   mkswap $swapdev
99   swap_uuid=$($VOL_ID -u $swapdev || true )
100 fi
101
102 TMPDIR=`mktemp -d` || exit 1
103 CLEANUP+=("rmdir $TMPDIR")
104
105 mount $filesystem_dev $TMPDIR
106 CLEANUP+=("umount $TMPDIR")
107
108 # remove the cache file if it's old (> 2 weeks) and writable by the owner (the
109 # default due to the standard umask)
110 if [ "$CLEAN_CACHE" -a -d "$CACHE_DIR" ]; then
111   find "$CACHE_DIR" -name 'cache-*.tar' -type f \
112     -daystart -mtime "+${CLEAN_CACHE}" -print0 | \
113     xargs -r0 rm -f
114 fi
115
116 if [ -f "$CACHE_FILE" ]; then
117   tar xf "$CACHE_FILE" -C $TMPDIR
118 else
119   if [ "$PROXY" ]; then
120     export http_proxy="$PROXY"
121   fi
122   # INCLUDE will be empty if EXTRA_PKGS is null/empty, otherwise we
123   # build the full parameter format from it
124   INCLUDE=${EXTRA_PKGS:+"--include=$EXTRA_PKGS"}
125   debootstrap \
126     --arch "$DPKG_ARCH" \
127     $INCLUDE \
128     "$SUITE" $TMPDIR $MIRROR
129
130   # remove the downloaded debs, as they are no longer needed
131   find "$TMPDIR/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
132     xargs -r0 rm -f
133
134   # remove the persistent-net rules, otherwise it will remember the node's
135   # interfaces as eth0, eth1, ...
136   rm -f "$TMPDIR/etc/udev/rules.d/z25_persistent-net.rules"
137
138   if [ "$GENERATE_CACHE" = "yes" ]; then
139     TMP_CACHE=`mktemp "${CACHE_FILE}.XXXXXX"`
140     tar cf "$TMP_CACHE" -C $TMPDIR .
141     mv -f "$TMP_CACHE" "$CACHE_FILE"
142   fi
143 fi
144
145 cp -p /etc/hosts $TMPDIR/etc/hosts
146 cp -p /etc/resolv.conf $TMPDIR/etc/resolv.conf
147 echo $instance > $TMPDIR/etc/hostname
148 echo $instance > $TMPDIR/etc/mailname
149
150 cat > $TMPDIR/etc/fstab <<EOF
151 # /etc/fstab: static file system information.
152 #
153 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
154 UUID=$root_uuid   /               ext3    defaults        0       1
155 proc              /proc           proc    defaults        0       0
156 EOF
157
158 [ -n "$swapdev" -a -n "$swap_uuid" ] && cat >> $TMPDIR/etc/fstab <<EOF
159 UUID=$swap_uuid   swap            swap    defaults        0       0
160 EOF
161
162 cat > $TMPDIR/etc/network/interfaces <<EOF
163 auto lo
164 iface lo inet loopback
165 EOF
166
167 if [ -e $TMPDIR/etc/inittab ]; then
168   cat $TMPDIR/etc/inittab | sed -re 's/\stty1$/ console/' \
169     > $TMPDIR/etc/inittab.new
170   mv $TMPDIR/etc/inittab.new $TMPDIR/etc/inittab
171 elif [ -e $TMPDIR/etc/event.d/tty1 ]; then
172   cat $TMPDIR/etc/event.d/tty1 | sed -re 's/tty1/console/' \
173     > $TMPDIR/etc/event.d/console
174   rm $TMPDIR/etc/event.d/tty1
175 fi
176
177 RUN_PARTS=`which run-parts`
178
179 if [ -n "$RUN_PARTS" -a -n "$CUSTOMIZE_DIR" -a -d "$CUSTOMIZE_DIR" ]; then
180   TARGET=$TMPDIR
181   BLOCKDEV=$blockdev
182   FSYSDEV=$filesystem_dev
183   export TARGET SUITE ARCH PARTITION_STYLE EXTRA_PKGS BLOCKDEV FSYSDEV
184   $RUN_PARTS $CUSTOMIZE_DIR
185 fi
186
187 # execute cleanups
188 cleanup
189 trap - EXIT
190
191 exit 0