Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.3 kB)

1
#!/bin/bash
2

    
3
# Copyright (C) 2011 GRNET S.A. 
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
set -o pipefail
22

    
23
. @osdir@/common.sh
24

    
25
usage() {
26
    local rc="$1"
27

    
28
    cat <<EOF
29

    
30
Usage: $0 [options]
31

    
32
This script runs a debootstrap and creates a small Debian image populated with
33
the snf-image-helper package. This needs to be done before the first use of
34
ganeti's \`snf-image' guest OS type.
35

    
36
OPTIONS:
37
    -c CACHE_FILE
38
        Use this cache file, instead of the default
39
        [default: $HELPER_CACHE_FILE]
40

    
41
    -d DIRECTORY
42
        Use this directory to host the created files, instead of the default
43
        [default: $HELPER_DIR]
44

    
45
    -h  Print this message
46

    
47
    -p PACKAGE
48
        Install this deb package in the helper image, instead of the default
49
        [default: $HELPER_PKG]
50

    
51
    -y  Assume Yes to all queries and do not prompt
52

    
53
EOF
54

    
55
    exit "$rc"
56
}
57

    
58
while getopts "c:d:hp:y" opt; do
59
    case $opt in
60
        c) CACHE_FILE="$OPTARG"
61
        OVERWRITTEN_CACHE_FILE="yes"
62
            ;;
63
        d) HELPER_DIR="$OPTARG"
64
            ;;
65
        h) usage 0
66
            ;;
67
        p) HELPER_PKG="$OPTARG"
68
            ;;
69
        y) NO_PROMPT="yes"
70
            ;;
71
        ?) log_error "Use \`-h' for help"; exit 1
72
            ;;
73
    esac
74
done
75

    
76
if [ x"$OVERWRITTEN_CACHE_FILE" != "xyes" ] ; then
77
    CACHE_FILE="$HELPER_CACHE_FILE"
78
fi
79

    
80
echo
81
echo "This is the update helper image script for snf-image."
82
echo "If you don't know what to do, use \`-h'."
83
echo
84

    
85
if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
86
    log_error "ERROR:"
87
    log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
88
    "has no write permission on it."
89
    exit 1
90
fi
91

    
92
if [ ! -r "$HELPER_PKG" ]; then
93
    log_error "ERROR:"
94
    log_error "Helper package \`$HELPER_PKG' does not exist or is not " \
95
    "readable by the script."
96
    exit 1
97
fi
98

    
99
cat >&1 <<EOF
100
This program will overwrite the following files if present:
101
  \`$HELPER_DIR/initrd'
102
  \`$HELPER_DIR/kernel'
103
  \`$HELPER_DIR/image'
104
EOF
105

    
106
while [[ 1 ]]; do
107
    echo -n "Do you want to continue [y/N]? "
108
    if [ "x$NO_PROMPT" = "xyes" ]; then
109
        echo "y";
110
        break;
111
    fi
112

    
113
    read answer
114
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
115
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
116
        log_error "Abort."
117
        exit 1
118
    fi
119
done
120

    
121
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
122

    
123
echo -n "Allocating space for helper disk image..."
124
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
125

    
126
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null
127
echo "done"
128

    
129
echo "Creating partitions..."
130
blockdev=$("$LOSETUP" -sf $helper_img)
131
add_cleanup "$LOSETUP" -d "$blockdev"
132

    
133
sleep 1 # sometimes losetup returns and the device is still busy..
134

    
135
format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
136

    
137
root_dev=$(map_disk0 "$blockdev")-1
138
add_cleanup unmap_disk0 "$blockdev"
139

    
140
echo Creating and configuring filesystem...
141
mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
142
# The helper vm should never do filesystem checks...
143
tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
144

    
145
target=$(mktemp -d)
146
add_cleanup rmdir "$target"
147

    
148
mount "$root_dev" "$target"
149
add_cleanup umount "$root_dev"
150

    
151
echo -n "Checking for cached root filesystem file \`$CACHE_FILE'..."
152
if [ -f "$CACHE_FILE" ]; then
153
    echo "found"
154

    
155

    
156
    missing_pkgs="no"
157
    if [ "$CACHE_FILE" == "$HELPER_CACHE_FILE" ]; then
158
        echo -n "Checking if needed packages are present..."
159
        if [ ! -f "$HELPER_CACHE_PKGS" ]; then
160
            missing_pkgs="yes"
161
            echo "packages file: \`$HELPER_CACHE_PKGS' does not exist"
162
        else
163
            OLD_IFS="$IFS"
164
            IFS=,; for pkg in $HELPER_EXTRA_PKGS; do
165
                if ! grep "^$pkg\$" "$HELPER_CACHE_PKGS" > /dev/null; then
166
		    missing_pkgs="yes"
167
                    echo "$pkg is missing."
168
                    break
169
                fi
170
            done
171
            IFS="$OLD_IFS"
172
            if [ "$missing_pkgs" == "no" ]; then
173
                echo "done"
174
            fi
175
	fi
176
        if [ "$missing_pkgs" == "yes" ]; then
177
                do_debootstrap "$target"
178
        fi
179
    fi
180

    
181

    
182
    test "$missing_pkgs" == "no" && while [[ 1 ]]; do
183
        echo -n "Use the cached file [Y/n]? "
184
        if [ "x$NO_PROMPT" = "xyes" ]; then
185
            echo "y";
186
            break;
187
        fi
188

    
189
        read answer
190
    
191
        if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
192
            break;
193
        elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
194
            do_debootstrap "$target"
195
            break;
196
        fi
197
    done
198

    
199
else
200
    echo "not found"
201
    do_debootstrap "$target"
202
fi
203

    
204
tar xf "$HELPER_CACHE_FILE" -C "$target"
205

    
206
echo -n "Configuring the helper image..."
207
echo snf-image-helper > "$target/etc/hostname"
208

    
209
cat > "$target/etc/fstab" <<EOF
210
# /etc/fstab: static file system information.
211
#
212
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
213
/dev/sda1         /               ext3    defaults        0       1
214
proc              /proc           proc    defaults        0       0
215
EOF
216
echo "done"
217

    
218
echo -n "Extracting kernel..."
219
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
220
    echo -e "\033[1;31mfailed\033[0m"
221
    log_error "vmlinuz or initrd.img link in root is missing."
222
    log_error "I don't know how to find a usable kernel/initrd pair."
223
    exit 1
224
fi
225
echo "done"
226

    
227
kernel=$(readlink -en "$target/vmlinuz")
228
initrd=$(readlink -en "$target/initrd.img")
229

    
230
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
231
mv "$kernel" "$initrd" "$HELPER_DIR"
232

    
233
kernel=$(basename "$kernel")
234
initrd=$(basename "$initrd")
235

    
236
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
237

    
238
rm "$target/vmlinuz" "$target/initrd.img"
239

    
240
echo "Installing snf-image-helper pkg in the new image..."
241
cp "$HELPER_PKG" "$target/tmp/"
242
pkg_name=$(basename "$HELPER_PKG")
243
add_cleanup rm "$target/tmp/$pkg_name"
244
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
245

    
246
cat > "$target/etc/rc.local" <<EOF
247
#!/bin/sh -e
248
#
249
# rc.local
250
#
251
# This script is executed at the end of each multiuser runlevel.
252
# Make sure that the script will "exit 0" on success or any other
253
# value on error.
254
#
255
# In order to enable or disable this script just change the execution
256
# bits.
257
#
258
# By default this script does nothing.
259

    
260
if ! grep -q snf_image_activate_helper /proc/cmdline; then
261
    echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
262
    echo "to the kernel command line if you want to do so."
263
else
264
    /usr/bin/snf-image-helper --force
265
fi
266

    
267
exit 0
268
EOF
269

    
270
chmod +x "$target/etc/rc.local"
271

    
272
echo "done"
273

    
274
cleanup
275

    
276
mv "$helper_img" "$HELPER_DIR/image"
277

    
278
trap - EXIT
279

    
280
echo "Files in \`$HELPER_DIR' were updated successfully"
281
exit 0
282

    
283
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :