Statistics
| Branch: | Tag: | Revision:

root / snf-image-host / snf-image-update-helper.in @ 23996b5d

History | View | Annotate | Download (6.1 kB)

1
#!/bin/bash
2

    
3
set -e
4
set -o pipefail
5

    
6
. @osdir@/common.sh
7

    
8
usage() {
9
    local rc="$1"
10

    
11
    cat <<EOF
12

    
13
Usage: $0 [options]
14

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

    
19
OPTIONS:
20
    -c CACHE_FILE
21
        Use this cache file, instead of the default
22
        [default: $HELPER_CACHE_FILE]
23

    
24
    -d DIRECTORY
25
        Use this directory to host the created files, instead of the default
26
        [default: $HELPER_DIR]
27

    
28
    -h  Print this message
29

    
30
    -p PACKAGE
31
        Install this deb package in the helper image, instead of the default
32
        [default: $HELPER_PKG]
33

    
34
    -y  Assume Yes to all queries and do not prompt
35

    
36
EOF
37

    
38
    exit "$rc"
39
}
40

    
41
while getopts "c:d:hp:y" opt; do
42
    case $opt in
43
        c) HELPER_CACHE_FILE="$OPTARG"
44
            ;;
45
        d) HELPER_DIR="$OPTARG"
46
            ;;
47
        h) usage 0
48
            ;;
49
        l) list_defaults; exit 0
50
            ;;
51
        p) HELPER_PKG="$OPTARG"
52
            ;;
53
        y) NO_PROMPT="yes"
54
            ;;
55
        ?) log_error "Use \`-h' for help"; exit 1
56
            ;;
57
    esac
58
done
59

    
60
echo
61
echo "This is the update helper image script for snf-image."
62
echo "If you don't know what to do, use \`-h'."
63
echo
64

    
65
if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
66
    log_error "ERROR:"
67
    log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
68
    "has no write permission on it."
69
    exit 1
70
fi
71

    
72
if [ ! -r "$HELPER_PKG" ]; then
73
    log_error "ERROR:"
74
    log_error "Helper package \`$HELPER_PKG' does not exist or is not " \
75
    "readable by the script."
76
    exit 1
77
fi
78

    
79
cat >&1 <<EOF
80
This program will overwrite the following files if present:
81
  \`$HELPER_DIR/initrd'
82
  \`$HELPER_DIR/kernel'
83
  \`$HELPER_DIR/image'
84
EOF
85

    
86
while [[ 1 ]]; do
87
    echo -n "Do you want to continue [y/N]? "
88
    if [ "x$NO_PROMPT" = "xyes" ]; then
89
        echo "y";
90
        break;
91
    fi
92

    
93
    read answer
94
    [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
95
    if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
96
        log_error "Abort."
97
        exit 1
98
    fi
99
done
100

    
101
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
102

    
103
echo -n "Allocating space for helper disk image..."
104
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
105

    
106
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null
107
echo "done"
108

    
109
echo "Creating partitions..."
110
blockdev=$("$LOSETUP" -sf $helper_img)
111
add_cleanup "$LOSETUP" -d "$blockdev"
112

    
113
sleep 1 # sometimes losetup returns and the device is still busy..
114

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

    
117
root_dev=$(map_disk0 "$blockdev")-1
118
add_cleanup unmap_disk0 "$blockdev"
119

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

    
125
target=$(mktemp -d)
126
add_cleanup rmdir "$target"
127

    
128
mount "$root_dev" "$target"
129
add_cleanup umount "$root_dev"
130

    
131
echo -n "Checking for cached root filesystem file \`$HELPER_CACHE_FILE'..." 
132
if [  -f "$HELPER_CACHE_FILE" ]; then
133
    echo "found"
134
    tar xf "$HELPER_CACHE_FILE" -C "$target"
135
else
136
    echo "not found"
137
    echo "Debootstraping to create a new root filesystem:"
138

    
139
    # Create a policy-rc.d file to deny init script execution
140
    mkdir -p "$target/usr/sbin"
141
    cat > "$target/usr/sbin/policy-rc.d" <<EOF
142
#!/bin/sh
143
exit 101
144
EOF
145
    chmod +x "$target/usr/sbin/policy-rc.d"
146

    
147
    debootstrap --arch amd64 --include "$HELPER_EXTRA_PKGS" \
148
        --variant=minbase stable "$target" "$HELPER_MIRROR" 2>&1 | \
149
        sed -e 's/^/DEBOOTSTRAP: /g'
150

    
151
    rm "$target/usr/sbin/policy-rc.d"
152
    
153
    # remove the downloaded debs, as they are no longer needed
154
    find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
155
        xargs -r0 rm -f
156

    
157
    tmp_cache=$(mktemp "$HELPER_CACHE_FILE.XXXXXX")
158
    tar cf "$tmp_cache" --one-file-system -C "$target" . || \
159
        { rm "$tmp_cache"; false; }
160
    mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
161
fi
162

    
163
echo -n "Configuring the helper image..."
164
echo snf-image-helper > "$target/etc/hostname"
165

    
166
cat > "$target/etc/fstab" <<EOF
167
# /etc/fstab: static file system information.
168
#
169
# <file system>   <mount point>   <type>  <options>       <dump>  <pass>
170
/dev/sda1         /               ext3    defaults        0       1
171
proc              /proc           proc    defaults        0       0
172
EOF
173
echo "done"
174

    
175
echo -n "Extracting kernel..."
176
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
177
    echo -e "\033[1;31mfailed\033[0m"
178
    log_error "vmlinuz or initrd.img link in root is missing."
179
    log_error "I don't know how to find a usable kernel/initrd pair."
180
    exit 1
181
fi
182
echo "done"
183

    
184
kernel=$(readlink -en "$target/vmlinuz")
185
initrd=$(readlink -en "$target/initrd.img")
186

    
187
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
188
mv "$kernel" "$initrd" "$HELPER_DIR"
189

    
190
kernel=$(basename "$kernel")
191
initrd=$(basename "$initrd")
192

    
193
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
194

    
195
rm "$target/vmlinuz" "$target/initrd.img"
196

    
197
echo "Installing snf-image-helper pkg in the new image..."
198
cp "$HELPER_PKG" "$target/tmp/"
199
pkg_name=$(basename "$HELPER_PKG")
200
add_cleanup rm "$target/tmp/$pkg_name"
201
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
202

    
203
cat > "$target/etc/rc.local" <<EOF
204
#!/bin/sh -e
205
#
206
# rc.local
207
#
208
# This script is executed at the end of each multiuser runlevel.
209
# Make sure that the script will "exit 0" on success or any other
210
# value on error.
211
#
212
# In order to enable or disable this script just change the execution
213
# bits.
214
#
215
# By default this script does nothing.
216

    
217
if ! grep -q snf_image_activate_helper /proc/cmdline; then
218
    echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper"
219
    echo "to the kernel command line if you want to do so."
220
else
221
    /usr/bin/snf-image-helper --force
222
fi
223

    
224
exit 0
225
EOF
226

    
227
chmod +x "$target/etc/rc.local"
228

    
229
echo "done"
230

    
231
cleanup
232

    
233
mv "$helper_img" "$HELPER_DIR/image"
234

    
235
trap - EXIT
236

    
237
echo "Files in \`$HELPER_DIR' were updated successfully"
238
exit 0
239

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