Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 2187080b

History | View | Annotate | Download (5.7 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 "Helper directory \`$HELPER_DIR' does not exist or the script" \
81
    "has no write permission on it."
82
    exit 1
83
fi
84

    
85
if [ ! -e "$MULTISTRAP_CONFIG" ]; then
86
    log_error "Multistrap configuration file: \`$MULTISTRAP_CONFIG' does not" \
87
    "exist or is not readable by the script."
88
    exit 1
89
fi
90

    
91
cat >&1 <<EOF
92
This program will overwrite the following files if present:
93
  \`$HELPER_DIR/initrd'
94
  \`$HELPER_DIR/kernel'
95
  \`$HELPER_DIR/image'
96
EOF
97

    
98
while [[ 1 ]]; do
99
    echo -n "Do you want to continue [y/N]? "
100
    if [ "x$NO_PROMPT" = "xyes" ]; then
101
        echo "y";
102
        break;
103
    fi
104

    
105
    read answer
106
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
107
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
108
        log_error "Abort."
109
        exit 1
110
    fi
111
done
112

    
113
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
114

    
115
echo -n "Allocating space for helper disk image..."
116
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
117
add_cleanup rm -f "$helper_img"
118
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null
119
echo "done"
120

    
121
echo "Creating partitions..."
122
blockdev=$("$LOSETUP" -sf $helper_img)
123
add_cleanup "$LOSETUP" -d "$blockdev"
124

    
125
sleep 1 # sometimes losetup returns and the device is still busy..
126

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

    
129
root_dev=$(map_disk0 "$blockdev")-1
130
add_cleanup unmap_disk0 "$blockdev"
131

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

    
137
target=$(mktemp -d)
138
add_cleanup rmdir "$target"
139

    
140
mount "$root_dev" "$target"
141
add_cleanup umount "$root_dev"
142

    
143
do_multistrap "$target"
144

    
145
# Save the package list
146
chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
147

    
148
echo -n "Configuring the helper image..."
149
echo snf-image-helper > "$target/etc/hostname"
150

    
151
cat > "$target/etc/fstab" <<EOF
152
# /etc/fstab: static file system information.
153
#
154
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
155
/dev/sda1         /               ext3    defaults        0       1
156
proc              /proc           proc    defaults        0       0
157
EOF
158
echo "done"
159

    
160
echo -n "Extracting kernel..."
161
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
162
    echo -e "\033[1;31mfailed\033[0m"
163
    log_error "vmlinuz or initrd.img link in root is missing."
164
    log_error "I don't know how to find a usable kernel/initrd pair."
165
    exit 1
166
fi
167
echo "done"
168

    
169
kernel="$target/$(chroot "$target" readlink -en /vmlinuz)"
170
initrd="$target/$(chroot "$target" readlink -en /initrd.img)"
171

    
172
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
173
cp "$kernel" "$initrd" "$HELPER_DIR"
174

    
175
kernel=$(basename "$kernel")
176
initrd=$(basename "$initrd")
177

    
178
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
179

    
180
rm "$target/vmlinuz" "$target/initrd.img"
181

    
182

    
183
pkg_installed=$(grep snf-image-helper "$HELPER_DIR/packages" > /dev/null && echo yes)
184

    
185
if [ ! -r "$HELPER_PKG" -a -z "$pkg_installed" ]; then
186
    log_error "ERROR:"
187
    log_error "Helper package \`$HELPER_PKG' does not exist or is not" \
188
    "readable by the script."
189
    exit 1
190
fi
191

    
192
if [ -r "$HELPER_PKG" ]; then
193
    echo "Installing snf-image-helper pkg in the new image..."
194
    cp "$HELPER_PKG" "$target/tmp/"
195
    pkg_name=$(basename "$HELPER_PKG")
196
    add_cleanup rm "$target/tmp/$pkg_name"
197
    chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
198

    
199
    # Recreate package list
200
    chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
201
else
202
    echo "snf-image-helper pkg was installed from the apt repository."
203
fi
204

    
205
mv "$helper_img" "$HELPER_DIR/image"
206

    
207
echo "Files in \`$HELPER_DIR' were updated successfully"
208
exit 0
209

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