Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ fcae4e91

History | View | Annotate | Download (7.4 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
root_uuid=$(blkid -s UUID -o value "$root_dev")
146

    
147
target=$(mktemp -d)
148
add_cleanup rmdir "$target"
149

    
150
mount "$root_dev" "$target"
151
add_cleanup umount "$root_dev"
152

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

    
157

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

    
183

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

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

    
201
else
202
    echo "not found"
203
    do_debootstrap "$target"
204
fi
205

    
206
tar xf "$HELPER_CACHE_FILE" -C "$target"
207

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

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

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

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

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

    
235
kernel=$(basename "$kernel")
236
initrd=$(basename "$initrd")
237

    
238
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
239

    
240
rm "$target/vmlinuz" "$target/initrd.img"
241

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

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

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

    
269
exit 0
270
EOF
271

    
272
chmod +x "$target/etc/rc.local"
273

    
274
echo "done"
275

    
276
cleanup
277

    
278
mv "$helper_img" "$HELPER_DIR/image"
279

    
280
trap - EXIT
281

    
282
echo "Files in \`$HELPER_DIR' were updated successfully"
283
exit 0
284

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