Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 53a5929f

History | View | Annotate | Download (7.1 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
set -o errtrace
23

    
24
. @osdir@/common.sh
25

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

    
29
    cat <<EOF
30

    
31
Usage: $0 [options]
32

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

    
37
OPTIONS:
38
    -d DIRECTORY
39
        Use this directory to host the created files, instead of the default
40
        [default: $HELPER_DIR]
41

    
42
    -h  Print this message
43

    
44
    -p PACKAGE
45
        Install this deb package in the helper image, instead of the default
46
        [default: $HELPER_PKG]
47

    
48
    -r  Don't use any existing cache
49

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

    
52
EOF
53

    
54
    exit "$rc"
55
}
56

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

    
74
echo
75
echo "This is the update helper image script for snf-image."
76
echo "If you don't know what to do, use \`-h'."
77
echo
78

    
79
if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
80
    log_error "ERROR:"
81
    log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
82
    "has no write permission on it."
83
    exit 1
84
fi
85

    
86
if [ ! -r "$HELPER_PKG" ]; then
87
    log_error "ERROR:"
88
    log_error "Helper package \`$HELPER_PKG' does not exist or is not" \
89
    "readable by the script."
90
    exit 1
91
fi
92

    
93
if [ ! -e "$MULTISTRAP_CONFIG" ]; then
94
    log_error "ERROR:"
95
    log_error "Multistrap configuration file: \`$MULTISTRAP_CONFIG' does not" \
96
    "exist or is not readable by the script."
97
    exit 1
98
fi
99

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

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

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

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

    
124
echo -n "Allocating space for helper disk image..."
125
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
126
trap "$(printf "rm -r %q" "${helper_img}")" ERR
127

    
128
dd if=/dev/zero of="$helper_img" bs=1k count=500000 &> /dev/null
129
echo "done"
130

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

    
135
sleep 1 # sometimes losetup returns and the device is still busy..
136

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

    
139
root_dev=$(map_disk0 "$blockdev")-1
140
add_cleanup unmap_disk0 "$blockdev"
141

    
142
echo Creating and configuring filesystem...
143
mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
144
# The helper vm should never do filesystem checks...
145
tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
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 in \`$HELPER_CACHE_DIR'..."
154
if [ "$NO_CACHE" == "yes" ]; then
155
    echo "don't use cache (option -r applied)"
156
    do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
157
elif [ -f "$HELPER_CACHE_DIR/root.tar" ]; then
158
    echo "found"
159

    
160
    missing_pkgs="no"
161
    echo -n "Checking if needed packages are present..."
162
    if [ ! -f "$HELPER_CACHE_DIR/packages" ]; then
163
        missing_pkgs="yes"
164
        echo "packages file: \`$HELPER_CACHE_DIR/packages' does not exist"
165
    else
166
        extra_pkgs=$(grep ^packages= "$MULTISTRAP_CONFIG" | cut -d= -f2 | tr  '\n' ' ')
167
        for pkg in $extra_pkgs; do
168
            if ! grep "^$pkg\$" "$HELPER_CACHE_DIR/packages" > /dev/null; then
169
		missing_pkgs="yes"
170
                echo "$pkg is missing."
171
                break
172
            fi
173
        done
174
        if [ "$missing_pkgs" == "no" ]; then
175
            echo "done"
176
        fi
177
    fi
178
    if [ "$missing_pkgs" == "yes" ]; then
179
            do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
180
    fi
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_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
195
            break;
196
        fi
197
    done
198

    
199
else
200
    echo "not found"
201
    do_multistrap "$target" "$HELPER_CACHE_DIR/root.tar" "$HELPER_CACHE_DIR/packages"
202
fi
203

    
204
tar xf "$HELPER_CACHE_DIR/root.tar" -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="$target/$(chroot "$target" readlink -en /vmlinuz)"
228
initrd="$target/$(chroot "$target" readlink -en /initrd.img)"
229

    
230
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
231
cp "$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
cleanup
247

    
248
mv "$helper_img" "$HELPER_DIR/image"
249

    
250
trap - EXIT
251

    
252
echo "Files in \`$HELPER_DIR' were updated successfully"
253
exit 0
254

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