Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 448a829f

History | View | Annotate | Download (6.5 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
    while [[ 1 ]]; do
156
        echo -n "Use the cached file [Y/n]? "
157
        if [ "x$NO_PROMPT" = "xyes" ]; then
158
            echo "y";
159
            break;
160
        fi
161

    
162
        read answer
163
    
164
        if [ -z "$answer" -o "$(tr [A-Z] [a-z] <<< "$answer")" = "y" ]; then
165
            break;
166
        elif [ "$(tr [A-Z] [a-z] <<< "$answer" )" = "n" ]; then
167
            do_debootstrap "$target"
168
            break;
169
        fi
170
    done
171
else
172
    echo "not found"
173
    do_debootstrap "$target"
174
fi
175

    
176
tar xf "$HELPER_CACHE_FILE" -C "$target"
177

    
178
echo -n "Configuring the helper image..."
179
echo snf-image-helper > "$target/etc/hostname"
180

    
181
cat > "$target/etc/fstab" <<EOF
182
# /etc/fstab: static file system information.
183
#
184
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
185
/dev/sda1         /               ext3    defaults        0       1
186
proc              /proc           proc    defaults        0       0
187
EOF
188
echo "done"
189

    
190
echo -n "Extracting kernel..."
191
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
192
    echo -e "\033[1;31mfailed\033[0m"
193
    log_error "vmlinuz or initrd.img link in root is missing."
194
    log_error "I don't know how to find a usable kernel/initrd pair."
195
    exit 1
196
fi
197
echo "done"
198

    
199
kernel=$(readlink -en "$target/vmlinuz")
200
initrd=$(readlink -en "$target/initrd.img")
201

    
202
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
203
mv "$kernel" "$initrd" "$HELPER_DIR"
204

    
205
kernel=$(basename "$kernel")
206
initrd=$(basename "$initrd")
207

    
208
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
209

    
210
rm "$target/vmlinuz" "$target/initrd.img"
211

    
212
echo "Installing snf-image-helper pkg in the new image..."
213
cp "$HELPER_PKG" "$target/tmp/"
214
pkg_name=$(basename "$HELPER_PKG")
215
add_cleanup rm "$target/tmp/$pkg_name"
216
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
217

    
218
cat > "$target/etc/rc.local" <<EOF
219
#!/bin/sh -e
220
#
221
# rc.local
222
#
223
# This script is executed at the end of each multiuser runlevel.
224
# Make sure that the script will "exit 0" on success or any other
225
# value on error.
226
#
227
# In order to enable or disable this script just change the execution
228
# bits.
229
#
230
# By default this script does nothing.
231

    
232
if ! grep -q snf_image_activate_helper /proc/cmdline; then
233
    echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
234
    echo "to the kernel command line if you want to do so."
235
else
236
    /usr/bin/snf-image-helper --force
237
fi
238

    
239
exit 0
240
EOF
241

    
242
chmod +x "$target/etc/rc.local"
243

    
244
echo "done"
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 :