Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 21be5a41

History | View | Annotate | Download (7.3 kB)

1 d51a95a8 Nikos Skalkotos
#!/bin/bash
2 d51a95a8 Nikos Skalkotos
3 3709b5f4 Nikos Skalkotos
# Copyright (C) 2011 GRNET S.A. 
4 3709b5f4 Nikos Skalkotos
#
5 3709b5f4 Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
6 3709b5f4 Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
7 3709b5f4 Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
8 3709b5f4 Nikos Skalkotos
# (at your option) any later version.
9 3709b5f4 Nikos Skalkotos
#
10 3709b5f4 Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
11 3709b5f4 Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 3709b5f4 Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 3709b5f4 Nikos Skalkotos
# General Public License for more details.
14 3709b5f4 Nikos Skalkotos
#
15 3709b5f4 Nikos Skalkotos
# You should have received a copy of the GNU General Public License
16 3709b5f4 Nikos Skalkotos
# along with this program; if not, write to the Free Software
17 3709b5f4 Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 3709b5f4 Nikos Skalkotos
# 02110-1301, USA.
19 3709b5f4 Nikos Skalkotos
20 d51a95a8 Nikos Skalkotos
set -e
21 bad5ca1f Nikos Skalkotos
set -o pipefail
22 d51a95a8 Nikos Skalkotos
23 d51a95a8 Nikos Skalkotos
. @osdir@/common.sh
24 d51a95a8 Nikos Skalkotos
25 a1aef659 Nikos Skalkotos
usage() {
26 a1aef659 Nikos Skalkotos
    local rc="$1"
27 a1aef659 Nikos Skalkotos
28 a1aef659 Nikos Skalkotos
    cat <<EOF
29 a1aef659 Nikos Skalkotos
30 a1aef659 Nikos Skalkotos
Usage: $0 [options]
31 a1aef659 Nikos Skalkotos
32 a1aef659 Nikos Skalkotos
This script runs a debootstrap and creates a small Debian image populated with
33 a1aef659 Nikos Skalkotos
the snf-image-helper package. This needs to be done before the first use of
34 a1aef659 Nikos Skalkotos
ganeti's \`snf-image' guest OS type.
35 a1aef659 Nikos Skalkotos
36 a1aef659 Nikos Skalkotos
OPTIONS:
37 cfd38847 Nikos Skalkotos
    -c CACHE_FILE
38 cfd38847 Nikos Skalkotos
        Use this cache file, instead of the default
39 cfd38847 Nikos Skalkotos
        [default: $HELPER_CACHE_FILE]
40 cfd38847 Nikos Skalkotos
41 cfd38847 Nikos Skalkotos
    -d DIRECTORY
42 cfd38847 Nikos Skalkotos
        Use this directory to host the created files, instead of the default
43 cfd38847 Nikos Skalkotos
        [default: $HELPER_DIR]
44 cfd38847 Nikos Skalkotos
45 cfd38847 Nikos Skalkotos
    -h  Print this message
46 cfd38847 Nikos Skalkotos
47 cfd38847 Nikos Skalkotos
    -p PACKAGE
48 cfd38847 Nikos Skalkotos
        Install this deb package in the helper image, instead of the default
49 cfd38847 Nikos Skalkotos
        [default: $HELPER_PKG]
50 cfd38847 Nikos Skalkotos
51 cfd38847 Nikos Skalkotos
    -y  Assume Yes to all queries and do not prompt
52 a1aef659 Nikos Skalkotos
53 a1aef659 Nikos Skalkotos
EOF
54 a1aef659 Nikos Skalkotos
55 a1aef659 Nikos Skalkotos
    exit "$rc"
56 a1aef659 Nikos Skalkotos
}
57 a1aef659 Nikos Skalkotos
58 cfd38847 Nikos Skalkotos
while getopts "c:d:hp:y" opt; do
59 a1aef659 Nikos Skalkotos
    case $opt in
60 448a829f Nikos Skalkotos
        c) CACHE_FILE="$OPTARG"
61 448a829f Nikos Skalkotos
        OVERWRITTEN_CACHE_FILE="yes"
62 a1aef659 Nikos Skalkotos
            ;;
63 a1aef659 Nikos Skalkotos
        d) HELPER_DIR="$OPTARG"
64 a1aef659 Nikos Skalkotos
            ;;
65 a1aef659 Nikos Skalkotos
        h) usage 0
66 a1aef659 Nikos Skalkotos
            ;;
67 a1aef659 Nikos Skalkotos
        p) HELPER_PKG="$OPTARG"
68 a1aef659 Nikos Skalkotos
            ;;
69 a1aef659 Nikos Skalkotos
        y) NO_PROMPT="yes"
70 a1aef659 Nikos Skalkotos
            ;;
71 a1aef659 Nikos Skalkotos
        ?) log_error "Use \`-h' for help"; exit 1
72 a1aef659 Nikos Skalkotos
            ;;
73 a1aef659 Nikos Skalkotos
    esac
74 a1aef659 Nikos Skalkotos
done
75 a1aef659 Nikos Skalkotos
76 448a829f Nikos Skalkotos
if [ x"$OVERWRITTEN_CACHE_FILE" != "xyes" ] ; then
77 448a829f Nikos Skalkotos
    CACHE_FILE="$HELPER_CACHE_FILE"
78 448a829f Nikos Skalkotos
fi
79 448a829f Nikos Skalkotos
80 a1aef659 Nikos Skalkotos
echo
81 a1aef659 Nikos Skalkotos
echo "This is the update helper image script for snf-image."
82 a1aef659 Nikos Skalkotos
echo "If you don't know what to do, use \`-h'."
83 a1aef659 Nikos Skalkotos
echo
84 a1aef659 Nikos Skalkotos
85 a1aef659 Nikos Skalkotos
if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
86 cfd38847 Nikos Skalkotos
    log_error "ERROR:"
87 a1aef659 Nikos Skalkotos
    log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
88 a1aef659 Nikos Skalkotos
    "has no write permission on it."
89 a1aef659 Nikos Skalkotos
    exit 1
90 a1aef659 Nikos Skalkotos
fi
91 a1aef659 Nikos Skalkotos
92 a1aef659 Nikos Skalkotos
if [ ! -r "$HELPER_PKG" ]; then
93 cfd38847 Nikos Skalkotos
    log_error "ERROR:"
94 a1aef659 Nikos Skalkotos
    log_error "Helper package \`$HELPER_PKG' does not exist or is not " \
95 a1aef659 Nikos Skalkotos
    "readable by the script."
96 52ac594c Nikos Skalkotos
    exit 1
97 52ac594c Nikos Skalkotos
fi
98 bad5ca1f Nikos Skalkotos
99 52ac594c Nikos Skalkotos
cat >&1 <<EOF
100 a1aef659 Nikos Skalkotos
This program will overwrite the following files if present:
101 52ac594c Nikos Skalkotos
  \`$HELPER_DIR/initrd'
102 52ac594c Nikos Skalkotos
  \`$HELPER_DIR/kernel'
103 52ac594c Nikos Skalkotos
  \`$HELPER_DIR/image'
104 52ac594c Nikos Skalkotos
EOF
105 52ac594c Nikos Skalkotos
106 52ac594c Nikos Skalkotos
while [[ 1 ]]; do
107 c36351f5 Nikos Skalkotos
    echo -n "Do you want to continue [y/N]? "
108 a1aef659 Nikos Skalkotos
    if [ "x$NO_PROMPT" = "xyes" ]; then
109 a1aef659 Nikos Skalkotos
        echo "y";
110 a1aef659 Nikos Skalkotos
        break;
111 a1aef659 Nikos Skalkotos
    fi
112 a1aef659 Nikos Skalkotos
113 52ac594c Nikos Skalkotos
    read answer
114 52ac594c Nikos Skalkotos
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
115 52ac594c Nikos Skalkotos
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
116 c36351f5 Nikos Skalkotos
        log_error "Abort."
117 52ac594c Nikos Skalkotos
        exit 1
118 52ac594c Nikos Skalkotos
    fi
119 52ac594c Nikos Skalkotos
done
120 52ac594c Nikos Skalkotos
121 52ac594c Nikos Skalkotos
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
122 52ac594c Nikos Skalkotos
123 52ac594c Nikos Skalkotos
echo -n "Allocating space for helper disk image..."
124 bad5ca1f Nikos Skalkotos
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
125 d51a95a8 Nikos Skalkotos
126 bad5ca1f Nikos Skalkotos
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null
127 52ac594c Nikos Skalkotos
echo "done"
128 52ac594c Nikos Skalkotos
129 bad5ca1f Nikos Skalkotos
echo "Creating partitions..."
130 55d2fc47 Nikos Skalkotos
blockdev=$("$LOSETUP" -sf $helper_img)
131 55d2fc47 Nikos Skalkotos
add_cleanup "$LOSETUP" -d "$blockdev"
132 d51a95a8 Nikos Skalkotos
133 52ac594c Nikos Skalkotos
sleep 1 # sometimes losetup returns and the device is still busy..
134 52ac594c Nikos Skalkotos
135 bad5ca1f Nikos Skalkotos
format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
136 d51a95a8 Nikos Skalkotos
137 bad5ca1f Nikos Skalkotos
root_dev=$(map_disk0 "$blockdev")-1
138 bad5ca1f Nikos Skalkotos
add_cleanup unmap_disk0 "$blockdev"
139 d51a95a8 Nikos Skalkotos
140 63d6058d Nikos Skalkotos
echo Creating and configuring filesystem...
141 bad5ca1f Nikos Skalkotos
mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
142 aac16418 Nikos Skalkotos
# The helper vm should never do filesystem checks...
143 bad5ca1f Nikos Skalkotos
tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
144 d51a95a8 Nikos Skalkotos
145 bad5ca1f Nikos Skalkotos
target=$(mktemp -d)
146 bad5ca1f Nikos Skalkotos
add_cleanup rmdir "$target"
147 d51a95a8 Nikos Skalkotos
148 bad5ca1f Nikos Skalkotos
mount "$root_dev" "$target"
149 bad5ca1f Nikos Skalkotos
add_cleanup umount "$root_dev"
150 d51a95a8 Nikos Skalkotos
151 448a829f Nikos Skalkotos
echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..."
152 d259b27c Nikos Skalkotos
if [ -f "$CACHE_FILE" ]; then
153 52ac594c Nikos Skalkotos
    echo "found"
154 5a31f7ef Nikos Skalkotos
155 d259b27c Nikos Skalkotos
156 d259b27c Nikos Skalkotos
    missing_pkgs="no"
157 d259b27c Nikos Skalkotos
    if [ "$CACHE_FILE" == "$HELPER_CACHE_FILE" ]; then
158 d259b27c Nikos Skalkotos
        echo -n "Checking if needed packages are present..."
159 d259b27c Nikos Skalkotos
        if [ ! -f "$HELPER_CACHE_PKGS" ]; then
160 d259b27c Nikos Skalkotos
            missing_pkgs="yes"
161 d259b27c Nikos Skalkotos
            echo "packages file: \`$HELPER_CACHE_PKGS' does not exist"
162 d259b27c Nikos Skalkotos
        else
163 d259b27c Nikos Skalkotos
            OLD_IFS="$IFS"
164 d259b27c Nikos Skalkotos
            IFS=,; for pkg in $HELPER_EXTRA_PKGS; do
165 d259b27c Nikos Skalkotos
                if ! grep "^$pkg\$" "$HELPER_CACHE_PKGS" > /dev/null; then
166 d259b27c Nikos Skalkotos
		    missing_pkgs="yes"
167 d259b27c Nikos Skalkotos
                    echo "$pkg is missing."
168 d259b27c Nikos Skalkotos
                    break
169 d259b27c Nikos Skalkotos
                fi
170 d259b27c Nikos Skalkotos
            done
171 d259b27c Nikos Skalkotos
            IFS="$OLD_IFS"
172 d259b27c Nikos Skalkotos
            if [ "$missing_pkgs" == "no" ]; then
173 d259b27c Nikos Skalkotos
                echo "done"
174 d259b27c Nikos Skalkotos
            fi
175 d259b27c Nikos Skalkotos
	fi
176 d259b27c Nikos Skalkotos
        if [ "$missing_pkgs" == "yes" ]; then
177 d259b27c Nikos Skalkotos
                do_debootstrap "$target"
178 d259b27c Nikos Skalkotos
        fi
179 d259b27c Nikos Skalkotos
    fi
180 d259b27c Nikos Skalkotos
181 d259b27c Nikos Skalkotos
182 d259b27c Nikos Skalkotos
    test "$missing_pkgs" == "no" && while [[ 1 ]]; do
183 448a829f Nikos Skalkotos
        echo -n "Use the cached file [Y/n]? "
184 448a829f Nikos Skalkotos
        if [ "x$NO_PROMPT" = "xyes" ]; then
185 448a829f Nikos Skalkotos
            echo "y";
186 448a829f Nikos Skalkotos
            break;
187 448a829f Nikos Skalkotos
        fi
188 5a31f7ef Nikos Skalkotos
189 448a829f Nikos Skalkotos
        read answer
190 5a31f7ef Nikos Skalkotos
    
191 448a829f Nikos Skalkotos
        if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
192 448a829f Nikos Skalkotos
            break;
193 448a829f Nikos Skalkotos
        elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
194 448a829f Nikos Skalkotos
            do_debootstrap "$target"
195 448a829f Nikos Skalkotos
            break;
196 448a829f Nikos Skalkotos
        fi
197 448a829f Nikos Skalkotos
    done
198 d259b27c Nikos Skalkotos
199 448a829f Nikos Skalkotos
else
200 448a829f Nikos Skalkotos
    echo "not found"
201 448a829f Nikos Skalkotos
    do_debootstrap "$target"
202 d51a95a8 Nikos Skalkotos
fi
203 d51a95a8 Nikos Skalkotos
204 448a829f Nikos Skalkotos
tar xf "$HELPER_CACHE_FILE" -C "$target"
205 448a829f Nikos Skalkotos
206 63d6058d Nikos Skalkotos
echo -n "Configuring the helper image..."
207 bad5ca1f Nikos Skalkotos
echo snf-image-helper > "$target/etc/hostname"
208 d51a95a8 Nikos Skalkotos
209 bad5ca1f Nikos Skalkotos
cat > "$target/etc/fstab" <<EOF
210 d51a95a8 Nikos Skalkotos
# /etc/fstab: static file system information.
211 d51a95a8 Nikos Skalkotos
#
212 d51a95a8 Nikos Skalkotos
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
213 d51a95a8 Nikos Skalkotos
/dev/sda1         /               ext3    defaults        0       1
214 d51a95a8 Nikos Skalkotos
proc              /proc           proc    defaults        0       0
215 d51a95a8 Nikos Skalkotos
EOF
216 52ac594c Nikos Skalkotos
echo "done"
217 d51a95a8 Nikos Skalkotos
218 52ac594c Nikos Skalkotos
echo -n "Extracting kernel..."
219 bad5ca1f Nikos Skalkotos
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
220 bad5ca1f Nikos Skalkotos
    echo -e "\033[1;31mfailed\033[0m"
221 bad5ca1f Nikos Skalkotos
    log_error "vmlinuz or initrd.img link in root is missing."
222 bad5ca1f Nikos Skalkotos
    log_error "I don't know how to find a usable kernel/initrd pair."
223 bad5ca1f Nikos Skalkotos
    exit 1
224 d51a95a8 Nikos Skalkotos
fi
225 52ac594c Nikos Skalkotos
echo "done"
226 d51a95a8 Nikos Skalkotos
227 bad5ca1f Nikos Skalkotos
kernel=$(readlink -en "$target/vmlinuz")
228 bad5ca1f Nikos Skalkotos
initrd=$(readlink -en "$target/initrd.img")
229 d51a95a8 Nikos Skalkotos
230 bad5ca1f Nikos Skalkotos
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
231 bad5ca1f Nikos Skalkotos
mv "$kernel" "$initrd" "$HELPER_DIR"
232 d51a95a8 Nikos Skalkotos
233 bad5ca1f Nikos Skalkotos
kernel=$(basename "$kernel")
234 bad5ca1f Nikos Skalkotos
initrd=$(basename "$initrd")
235 d51a95a8 Nikos Skalkotos
236 bad5ca1f Nikos Skalkotos
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
237 d51a95a8 Nikos Skalkotos
238 bad5ca1f Nikos Skalkotos
rm "$target/vmlinuz" "$target/initrd.img"
239 d51a95a8 Nikos Skalkotos
240 52ac594c Nikos Skalkotos
echo "Installing snf-image-helper pkg in the new image..."
241 bad5ca1f Nikos Skalkotos
cp "$HELPER_PKG" "$target/tmp/"
242 bad5ca1f Nikos Skalkotos
pkg_name=$(basename "$HELPER_PKG")
243 bad5ca1f Nikos Skalkotos
add_cleanup rm "$target/tmp/$pkg_name"
244 bad5ca1f Nikos Skalkotos
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
245 3b0420ce Nikos Skalkotos
246 bad5ca1f Nikos Skalkotos
cat > "$target/etc/rc.local" <<EOF
247 3b0420ce Nikos Skalkotos
#!/bin/sh -e
248 3b0420ce Nikos Skalkotos
#
249 3b0420ce Nikos Skalkotos
# rc.local
250 3b0420ce Nikos Skalkotos
#
251 3b0420ce Nikos Skalkotos
# This script is executed at the end of each multiuser runlevel.
252 3b0420ce Nikos Skalkotos
# Make sure that the script will "exit 0" on success or any other
253 3b0420ce Nikos Skalkotos
# value on error.
254 3b0420ce Nikos Skalkotos
#
255 3b0420ce Nikos Skalkotos
# In order to enable or disable this script just change the execution
256 3b0420ce Nikos Skalkotos
# bits.
257 3b0420ce Nikos Skalkotos
#
258 3b0420ce Nikos Skalkotos
# By default this script does nothing.
259 3b0420ce Nikos Skalkotos
260 ff7783b8 Vangelis Koukis
if ! grep -q snf_image_activate_helper /proc/cmdline; then
261 ff7783b8 Vangelis Koukis
    echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
262 ff7783b8 Vangelis Koukis
    echo "to the kernel command line if you want to do so."
263 ff7783b8 Vangelis Koukis
else
264 ff7783b8 Vangelis Koukis
    /usr/bin/snf-image-helper --force
265 ff7783b8 Vangelis Koukis
fi
266 3b0420ce Nikos Skalkotos
267 3b0420ce Nikos Skalkotos
exit 0
268 3b0420ce Nikos Skalkotos
EOF
269 3b0420ce Nikos Skalkotos
270 bad5ca1f Nikos Skalkotos
chmod +x "$target/etc/rc.local"
271 3b0420ce Nikos Skalkotos
272 52ac594c Nikos Skalkotos
echo "done"
273 52ac594c Nikos Skalkotos
274 d51a95a8 Nikos Skalkotos
cleanup
275 d51a95a8 Nikos Skalkotos
276 bad5ca1f Nikos Skalkotos
mv "$helper_img" "$HELPER_DIR/image"
277 d51a95a8 Nikos Skalkotos
278 d51a95a8 Nikos Skalkotos
trap - EXIT
279 d51a95a8 Nikos Skalkotos
280 63d6058d Nikos Skalkotos
echo "Files in \`$HELPER_DIR' were updated successfully"
281 d51a95a8 Nikos Skalkotos
exit 0
282 d51a95a8 Nikos Skalkotos
283 d51a95a8 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :