root / snf-image-host / snf-image-update-helper.in @ a1aef659
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 |
list_defaults() { |
9 |
cat <<EOF |
10 |
|
11 |
*** DEFAULT HELPER VALUES *** |
12 |
|
13 |
Helper directory: $HELPER_DIR |
14 |
Helper cache file: $HELPER_CACHE_FILE |
15 |
Helper snf-image-helper pkg: $HELPER_PKG |
16 |
|
17 |
EOF |
18 |
} |
19 |
|
20 |
usage() { |
21 |
local rc="$1" |
22 |
|
23 |
cat <<EOF |
24 |
|
25 |
Usage: $0 [options] |
26 |
|
27 |
This script runs a debootstrap and creates a small Debian image populated with |
28 |
the snf-image-helper package. This needs to be done before the first use of |
29 |
ganeti's \`snf-image' guest OS type. |
30 |
|
31 |
OPTIONS: |
32 |
-c Overwrite the default cache file |
33 |
-d Overwrite the default directory where the helper image will be hosted |
34 |
-h Show this message |
35 |
-l List default helper values |
36 |
-p Overwrite the default snf-image-helper debian package |
37 |
-y Automatic yes to prompts and run non-interactively. |
38 |
|
39 |
EOF |
40 |
|
41 |
exit "$rc" |
42 |
} |
43 |
|
44 |
while getopts "c:d:hlp:y" opt; do |
45 |
case $opt in |
46 |
c) HELPER_CACHE_FILE="$OPTARG" |
47 |
;; |
48 |
d) HELPER_DIR="$OPTARG" |
49 |
;; |
50 |
h) usage 0 |
51 |
;; |
52 |
l) list_defaults; exit 0 |
53 |
;; |
54 |
p) HELPER_PKG="$OPTARG" |
55 |
;; |
56 |
y) NO_PROMPT="yes" |
57 |
;; |
58 |
?) log_error "Use \`-h' for help"; exit 1 |
59 |
;; |
60 |
esac |
61 |
done |
62 |
|
63 |
echo |
64 |
echo "This is the update helper image script for snf-image." |
65 |
echo "If you don't know what to do, use \`-h'." |
66 |
echo |
67 |
|
68 |
if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then |
69 |
log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \ |
70 |
"has no write permission on it." |
71 |
exit 1 |
72 |
fi |
73 |
|
74 |
if [ ! -r "$HELPER_PKG" ]; then |
75 |
log_error "Helper package \`$HELPER_PKG' does not exist or is not " \ |
76 |
"readable by the script." |
77 |
exit 1 |
78 |
fi |
79 |
|
80 |
cat >&1 <<EOF |
81 |
This program will overwrite the following files if present: |
82 |
\`$HELPER_DIR/initrd' |
83 |
\`$HELPER_DIR/kernel' |
84 |
\`$HELPER_DIR/image' |
85 |
EOF |
86 |
|
87 |
while [[ 1 ]]; do |
88 |
echo -n "Do you want to continue [y/N]? " |
89 |
if [ "x$NO_PROMPT" = "xyes" ]; then |
90 |
echo "y"; |
91 |
break; |
92 |
fi |
93 |
|
94 |
read answer |
95 |
[ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break |
96 |
if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then |
97 |
log_error "Abort." |
98 |
exit 1 |
99 |
fi |
100 |
done |
101 |
|
102 |
rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image" |
103 |
|
104 |
echo -n "Allocating space for helper disk image..." |
105 |
helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX") |
106 |
|
107 |
dd if=/dev/zero of="$helper_img" bs=1k count=400000 &> /dev/null |
108 |
echo "done" |
109 |
|
110 |
echo "Creating partitions..." |
111 |
blockdev=$("$LOSETUP" -sf $helper_img) |
112 |
add_cleanup "$LOSETUP" -d "$blockdev" |
113 |
|
114 |
sleep 1 # sometimes losetup returns and the device is still busy.. |
115 |
|
116 |
format_disk0 "$blockdev" "extdump" 2>&1 | sed -e 's/^/CFDISK: /g' |
117 |
|
118 |
root_dev=$(map_disk0 "$blockdev")-1 |
119 |
add_cleanup unmap_disk0 "$blockdev" |
120 |
|
121 |
echo Creating and configuring filesystem... |
122 |
mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g' |
123 |
# The helper vm should never do filesystem checks... |
124 |
tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g' |
125 |
|
126 |
target=$(mktemp -d) |
127 |
add_cleanup rmdir "$target" |
128 |
|
129 |
mount "$root_dev" "$target" |
130 |
add_cleanup umount "$root_dev" |
131 |
|
132 |
echo -n "Checking for cached root filesystem file \`$HELPER_CACHE_FILE'..." |
133 |
if [ -f "$HELPER_CACHE_FILE" ]; then |
134 |
echo "found" |
135 |
tar xf "$HELPER_CACHE_FILE" -C "$target" |
136 |
else |
137 |
echo "not found" |
138 |
echo "Debootstraping to create a new root filesystem:" |
139 |
|
140 |
# Create a policy-rc.d file to deny init script execution |
141 |
mkdir -p "$target/usr/sbin" |
142 |
cat > "$target/usr/sbin/policy-rc.d" <<EOF |
143 |
#!/bin/sh |
144 |
exit 101 |
145 |
EOF |
146 |
chmod +x "$target/usr/sbin/policy-rc.d" |
147 |
|
148 |
debootstrap --arch amd64 --include "$HELPER_EXTRA_PKGS" \ |
149 |
--variant=minbase stable "$target" "$HELPER_MIRROR" 2>&1 | \ |
150 |
sed -e 's/^/DEBOOTSTRAP: /g' |
151 |
|
152 |
rm "$target/usr/sbin/policy-rc.d" |
153 |
|
154 |
# remove the downloaded debs, as they are no longer needed |
155 |
find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \ |
156 |
xargs -r0 rm -f |
157 |
|
158 |
tmp_cache=$(mktemp "$HELPER_CACHE_FILE.XXXXXX") |
159 |
tar cf "$tmp_cache" --one-file-system -C "$target" . || \ |
160 |
{ rm "$tmp_cache"; false; } |
161 |
mv -f "$tmp_cache" "$HELPER_CACHE_FILE" |
162 |
fi |
163 |
|
164 |
echo -n "Configuring the helper image..." |
165 |
echo snf-image-helper > "$target/etc/hostname" |
166 |
|
167 |
cat > "$target/etc/fstab" <<EOF |
168 |
# /etc/fstab: static file system information. |
169 |
# |
170 |
# <file system> <mount point> <type> <options> <dump> <pass> |
171 |
/dev/sda1 / ext3 defaults 0 1 |
172 |
proc /proc proc defaults 0 0 |
173 |
EOF |
174 |
echo "done" |
175 |
|
176 |
echo -n "Extracting kernel..." |
177 |
if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then |
178 |
echo -e "\033[1;31mfailed\033[0m" |
179 |
log_error "vmlinuz or initrd.img link in root is missing." |
180 |
log_error "I don't know how to find a usable kernel/initrd pair." |
181 |
exit 1 |
182 |
fi |
183 |
echo "done" |
184 |
|
185 |
kernel=$(readlink -en "$target/vmlinuz") |
186 |
initrd=$(readlink -en "$target/initrd.img") |
187 |
|
188 |
echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'" |
189 |
mv "$kernel" "$initrd" "$HELPER_DIR" |
190 |
|
191 |
kernel=$(basename "$kernel") |
192 |
initrd=$(basename "$initrd") |
193 |
|
194 |
(cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd) |
195 |
|
196 |
rm "$target/vmlinuz" "$target/initrd.img" |
197 |
|
198 |
echo "Installing snf-image-helper pkg in the new image..." |
199 |
cp "$HELPER_PKG" "$target/tmp/" |
200 |
pkg_name=$(basename "$HELPER_PKG") |
201 |
add_cleanup rm "$target/tmp/$pkg_name" |
202 |
chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g' |
203 |
|
204 |
cat > "$target/etc/rc.local" <<EOF |
205 |
#!/bin/sh -e |
206 |
# |
207 |
# rc.local |
208 |
# |
209 |
# This script is executed at the end of each multiuser runlevel. |
210 |
# Make sure that the script will "exit 0" on success or any other |
211 |
# value on error. |
212 |
# |
213 |
# In order to enable or disable this script just change the execution |
214 |
# bits. |
215 |
# |
216 |
# By default this script does nothing. |
217 |
|
218 |
if ! grep -q snf_image_activate_helper /proc/cmdline; then |
219 |
echo "WARNING: NOT calling snf-image-helper, add snf_image_activate_helper" |
220 |
echo "to the kernel command line if you want to do so." |
221 |
else |
222 |
/usr/bin/snf-image-helper --force |
223 |
fi |
224 |
|
225 |
exit 0 |
226 |
EOF |
227 |
|
228 |
chmod +x "$target/etc/rc.local" |
229 |
|
230 |
echo "done" |
231 |
|
232 |
cleanup |
233 |
|
234 |
mv "$helper_img" "$HELPER_DIR/image" |
235 |
|
236 |
trap - EXIT |
237 |
|
238 |
echo "Files in \`$HELPER_DIR' were updated successfully" |
239 |
exit 0 |
240 |
|
241 |
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai : |