Rephrase the create-fixed-helper usage msg
[snf-image] / snf-image-host / snf-image-create-fixed-helper.in
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 multistrap and creates a small Debian image populated with
34 the snf-image-helper package. This image is needed for ganeti's \`snf-image'
35 guest OS type to work.
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
47     -r  Don't use any existing cache
48
49     -y  Assume Yes to all queries and do not prompt
50
51 EOF
52
53     exit "$rc"
54 }
55
56 while getopts "d:hp:ry" opt; do
57     case $opt in
58         d) HELPER_DIR="$OPTARG"
59             ;;
60         h) usage 0
61             ;;
62         p) HELPER_PKG="$OPTARG"
63             ;;
64         r) NO_CACHE="yes"
65             ;;
66         y) NO_PROMPT="yes"
67             ;;
68         ?) log_error "Use \`-h' for help"; exit 1
69             ;;
70     esac
71 done
72
73 echo
74 echo "This is the update helper image script for snf-image."
75 echo "If you don't know what to do, use \`-h'."
76 echo
77
78 if [ ! -d "$HELPER_DIR" -o ! -w "$HELPER_DIR" ]; then
79     log_error "Helper directory \`$HELPER_DIR' does not exist or the script" \
80     "has no write permission on it."
81     exit 1
82 fi
83
84 if [ ! -e "$MULTISTRAP_CONFIG" ]; then
85     log_error "Multistrap configuration file: \`$MULTISTRAP_CONFIG' does not" \
86     "exist or is not readable by the script."
87     exit 1
88 fi
89
90 cat >&1 <<EOF
91 This program will overwrite the following files if present:
92   \`$HELPER_DIR/initrd'
93   \`$HELPER_DIR/kernel'
94   \`$HELPER_DIR/initrd-xen'
95   \`$HELPER_DIR/kernel-xen'
96   \`$HELPER_DIR/image'
97   \`$HELPER_DIR/packages'
98 EOF
99
100 while [[ 1 ]]; do
101     echo -n "Do you want to continue [y/N]? "
102     if [ "x$NO_PROMPT" = "xyes" ]; then
103         echo "y";
104         break;
105     fi
106
107     read answer
108     [ "$(echo -n "$answer" | tr [A-Z] [a-z])" = "y" ] && break
109     if [ -z "$answer" -o "$(echo -n "$answer" | tr [A-Z] [a-z])" = "n" ]; then
110         log_error "Abort."
111         exit 1
112     fi
113 done
114
115 rm -f "$HELPER_DIR/initrd" "$HELPER_DIR/kernel" "$HELPER_DIR/image"
116
117 echo -n "Allocating space for helper disk image..."
118 helper_img=$(mktemp "$HELPER_DIR/image.XXXXXX")
119 add_cleanup rm -f "$helper_img"
120 dd if=/dev/zero of="$helper_img" bs=1M count="$HELPER_SIZE" &> /dev/null
121 echo "done"
122
123 echo "Creating partitions..."
124 blockdev=$("$LOSETUP" -sf $helper_img)
125 add_cleanup "$LOSETUP" -d "$blockdev"
126
127 sleep 1 # sometimes losetup returns and the device is still busy..
128
129 format_disk0 "$blockdev" "extdump"  2>&1 | sed -e 's/^/CFDISK: /g'
130
131 root_dev=$(map_disk0 "$blockdev")-1
132 add_cleanup unmap_disk0 "$blockdev"
133
134 echo Creating and configuring filesystem...
135 mkfs.ext3 "$root_dev" 2>&1 | sed -e 's/^/MKFS.EXT3: /g'
136 # The helper vm should never do filesystem checks...
137 tune2fs -i 0 -c 0 "$root_dev" 2>&1 | sed -e 's/^/TUNE2FS: /g'
138
139 root_uuid=$(blkid -s UUID -o value "$root_dev")
140
141 target=$(mktemp -d)
142 add_cleanup rmdir "$target"
143
144 mount "$root_dev" "$target"
145 add_cleanup umount "$root_dev"
146
147 do_multistrap "$target"
148
149 # Save the package list
150 chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
151
152 echo -n "Configuring the helper image..."
153 echo snf-image-helper > "$target/etc/hostname"
154
155 cat > "$target/etc/fstab" <<EOF
156 # /etc/fstab: static file system information.
157 #
158 # <file system>   <mount point>   <type>  <options>       <dump>  <pass>
159 UUID=$root_uuid         /               ext3    defaults        0       1
160 proc              /proc           proc    defaults        0       0
161 EOF
162
163 # We need this since we mount the helper VM ro
164 ln -sf /proc/mounts "$target/etc/mtab"
165
166 echo "done"
167
168 echo -n "Extracting kernel..."
169 if [ ! -L "$target/vmlinuz" -o ! -L "$target/vmlinuz" ]; then
170     echo -e "\033[1;31mfailed\033[0m"
171     log_error "vmlinuz or initrd.img link in root is missing."
172     log_error "I don't know how to find a usable kernel/initrd pair."
173     exit 1
174 fi
175 echo "done"
176
177 # Make sure extended globbing is enabled
178 shopt -s extglob
179
180 kernel=$(ls "$target"/boot/vmlinuz-+([0-9.])-+([a-z0-9])-amd64)
181 initrd=$(ls "$target"/boot/initrd.img-+([0-9.])-+([a-z0-9])-amd64)
182
183 kernel_xen=$(ls "$target"/boot/vmlinuz-+([0-9.])-+([a-z0-9])-xen-amd64)
184 initrd_xen=$(ls "$target"/boot/initrd.img-+([0-9.])-+([a-z0-9])-xen-amd64)
185
186 echo "Moving $(basename "$kernel") and $(basename "$initrd") to \`$HELPER_DIR'"
187 cp "$kernel" "$initrd" "$HELPER_DIR"
188
189 echo "Moving $(basename "$kernel_xen") and $(basename "$initrd_xen") to \`$HELPER_DIR'"
190 cp "$kernel_xen" "$initrd_xen" "$HELPER_DIR"
191
192 kernel=$(basename "$kernel")
193 initrd=$(basename "$initrd")
194
195 kernel_xen=$(basename "$kernel_xen")
196 initrd_xen=$(basename "$initrd_xen")
197 (cd "$HELPER_DIR"; ln -fs "$kernel" kernel; ln -fs "$initrd" initrd)
198 (cd "$HELPER_DIR"; ln -fs "$kernel_xen" kernel-xen; ln -fs "$initrd_xen" initrd-xen)
199
200
201 pkg_installed=$(grep snf-image-helper "$HELPER_DIR/packages" > /dev/null && echo yes)
202
203 if [ -z "$HELPER_PKG" -a -z "$pkg_installed" ]; then
204     log_error "No helper package was specified and non was found by the apt."
205     exit 1
206 fi
207
208 if [ -r "$HELPER_PKG" ]; then
209     echo "Installing snf-image-helper pkg in the new image..."
210     cp "$HELPER_PKG" "$target/tmp/"
211     pkg_name=$(basename "$HELPER_PKG")
212     add_cleanup rm "$target/tmp/$pkg_name"
213     chroot "$target" dpkg -i "/tmp/$pkg_name" 2>&1 | sed -e 's/^/DPKG: /g'
214
215     # Recreate package list
216     chroot "$target" dpkg-query -W  > "$HELPER_DIR/packages"
217 else
218     echo "snf-image-helper pkg was installed from the apt repository."
219 fi
220
221 helper_version="$(grep ^snf-image-helper[[:space:]] "$HELPER_DIR/packages" | cut -f2)"
222 host_version="$(dpkg-query -W -f "\${Version}\n" snf-image)"
223
224 if [ "$VERSION_CHECK" == yes -a -z "$HELPER_PKG" ]; then
225     if [ "$host_version" != "$helper_version" ]; then
226         log_error "snf-image version (=$host_version) and " \
227             "snf-image-helper version (=$helper_version) don't match."
228         exit 1
229     fi
230 fi
231
232 mv "$helper_img" "$HELPER_DIR/image"
233
234 echo "Files in \`$HELPER_DIR' were updated successfully" >&2
235
236 TMPDIR=$(mktemp -d)
237
238 echo "Creating fixed appliance in \`$TMPDIR':" >&2
239 echo >&2
240 (cd "$HELPER_DIR"; $TAR -cvf "$TMPDIR/snf_image_helper_image-${helper_version}.tar" "$kernel_xen" "$initrd_xen" "$kernel" "$initrd" kernel initrd kernel-xen initrd-xen image packages)
241 echo >&2
242 echo -n "Compressing fixed appliance ..." >&2
243 (cd "$HELPER_DIR"; $XZ -9 "$TMPDIR/snf_image_helper_image-${helper_version}.tar")
244 echo "done" >&2
245
246 exit 0
247
248 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :