Add mechanism for monitoring snf-image events
[snf-image] / snf-image-host / common.sh.in
1 # Copyright (C) 2011 GRNET S.A. 
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 # 02110-1301, USA.
18
19 AWK="awk"
20 KPARTX="kpartx"
21 LOSETUP="losetup"
22 SFDISK="sfdisk"
23 QEMU_IMG="qemu-img"
24 INSTALL_MBR="install-mbr"
25 TIMELIMIT="timelimit"
26 CURL="curl"
27
28 network_backend_support="@network_backend_support@"
29
30 # Use file descriptors in the range 3-9. File descriptors below 3 are used for
31 # standard input, output, and error, the ones above 9 may be used by the shell
32 # internally.
33 MONITOR_FD=9
34
35 CLEANUP=( )
36
37 add_cleanup() {
38     local cmd=""
39     for arg; do cmd+=$(printf "%q " "$arg"); done
40     CLEANUP+=("$cmd")
41 }
42
43 log_error() {
44     echo "$@" >&2
45 }
46
47 close_fd() {
48     local fd="$1"
49     eval "exec $fd>&-"
50 }
51
52 get_api5_arguments() {
53     GETOPT_RESULT=$*
54     # Note the quotes around `$TEMP': they are essential!
55     eval set -- "$GETOPT_RESULT"
56     while true; do
57         case "$1" in
58             -i|-n) instance=$2; shift 2;;
59
60             -o) old_name=$2; shift 2;;
61
62             -b) blockdev=$2; shift 2;;
63
64             -s) swapdev=$2; shift 2;;
65
66             --) shift; break;;
67
68             *)  log_error "Internal error!" >&2; exit 1;;
69         esac
70     done
71     if [ -z "$instance" -o -z "$blockdev" ]; then
72         log_error "Missing OS API Argument (-i, -n, or -b)"
73         exit 1
74     fi
75     if [ "$SCRIPT_NAME" != "export" -a -z "$swapdev"  ]; then
76         log_error "Missing OS API Argument -s (swapdev)"
77         exit 1
78     fi
79     if [ "$SCRIPT_NAME" = "rename" -a -z "$old_name"  ]; then
80         log_error "Missing OS API Argument -o (old_name)"
81         exit 1
82     fi
83 }
84
85 get_api10_arguments() {
86     if [ -z "$INSTANCE_NAME" -o -z "$HYPERVISOR" -o -z "$DISK_COUNT" ]; then
87         log_error "Missing OS API Variable:"
88         log_error "(INSTANCE_NAME HYPERVISOR or DISK_COUNT)"
89         exit 1
90     fi
91     instance=$INSTANCE_NAME
92     if [ $DISK_COUNT -lt 1 -o -z "$DISK_0_PATH" ]; then
93         log_error "At least one disk is needed"
94         exit 1
95     fi
96     if [ "$SCRIPT_NAME" = "export" ]; then
97         if [ -z "$EXPORT_DEVICE" ]; then
98         log_error "Missing OS API Variable EXPORT_DEVICE"
99     fi
100     blockdev=$EXPORT_DEVICE
101     elif [ "$SCRIPT_NAME" = "import" ]; then
102         if [ -z "$IMPORT_DEVICE" ]; then
103         log_error "Missing OS API Variable IMPORT_DEVICE"
104         fi
105         blockdev=$IMPORT_DEVICE
106     else
107         blockdev=$DISK_0_PATH
108     fi
109     if [ "$SCRIPT_NAME" = "rename" -a -z "$OLD_INSTANCE_NAME" ]; then
110         log_error "Missing OS API Variable OLD_INSTANCE_NAME"
111     fi
112     old_name=$OLD_INSTANCE_NAME
113 }
114
115 get_api20_arguments() {
116     get_api10_arguments
117     if [ -z "$OSP_IMG_ID" ]; then
118         log_error "Missing OS API Parameter: OSP_IMG_ID"
119         exit 1
120     fi
121     if [ -z "$OSP_IMG_FORMAT" ]; then
122         log_error "Missing OS API Parameter: OSP_IMG_FORMAT"
123         exit 1
124     fi
125     if [ -z "$OSP_IMG_PASSWD" ]; then
126         log_error "Missing OS API Parameter: OSP_IMG_PASSWD"
127         exit 1
128     fi
129
130     IMG_ID=$OSP_IMG_ID
131     IMG_FORMAT=$OSP_IMG_FORMAT
132     IMG_PASSWD=$OSP_IMG_PASSWD
133     if [ -n "$OSP_IMG_PROPERTIES" ]; then
134         IMG_PROPERTIES="$OSP_IMG_PROPERTIES"
135     fi
136     if [ -n "$OSP_IMG_PERSONALITY" ]; then
137         IMG_PERSONALITY="$OSP_IMG_PERSONALITY"
138     fi
139 }
140
141 map_disk0() {
142     blockdev="$1"
143     filesystem_dev_base=$($KPARTX -l -p- $blockdev | \
144                             grep -m 1 -- "-1.*$blockdev" | \
145                             $AWK '{print $1}')
146     if [ -z "$filesystem_dev_base" ]; then
147         log_error "Cannot interpret kpartx output and get partition mapping"
148         exit 1
149     fi
150     $KPARTX -a -p- "$blockdev" > /dev/null
151     filesystem_dev="/dev/mapper/${filesystem_dev_base/%-1/}"
152     if [ ! -b "/dev/mapper/$filesystem_dev_base" ]; then
153         log_error "Can't find kpartx mapped partition:" \
154                                             "/dev/mapper/$filesystem_dev_base"
155         exit 1
156     fi
157     echo "$filesystem_dev"
158 }
159
160 unmap_disk0() {
161     $KPARTX -d -p- "$1"
162 }
163
164 format_disk0() {
165     local device="$1"
166     local image_type="$2"
167
168     declare -A part_id=( ['extdump']="83" ["ntfsdump"]="7" )
169
170     # The -f is needed, because we use an optimal alignment and sfdisk complains
171     # about partitions not ending on clylinder boundary.
172     local sfdisk_cmd="$SFDISK -uS -H 255 -S 63 -f --quiet --Linux --DOS $device"
173
174     $sfdisk_cmd > /dev/null <<EOF
175 2048,,${part_id["$image_type"]},*
176 EOF
177 }
178
179 create_floppy() {
180     local img=$1
181
182     local target=$(mktemp -d)
183     add_cleanup rmdir "$target"
184
185     dd bs=512 count=2880 if=/dev/zero of="$img"
186     mkfs.ext2 -F "$img" > /dev/null
187     mount "$img" "$target" -o loop
188     set | egrep ^snf_export_\\w+= | sed -e 's/^snf_export_/export SNF_IMAGE_/' \
189         > "$target/rules"
190     if [ -n "$UNATTEND" ]; then
191         if [ -f "$UNATTEND" ]; then
192             cat "$UNATTEND" > "$target/unattend.xml"
193         else
194             log_error "Unattend file: \`"$UNATTEND"' does not exist"
195         fi
196     fi
197     umount "$target"
198 }
199
200 get_backend_type() {
201     local id=$1
202
203     if [[ "$id" =~ ^pithos: ]]; then
204         echo "pithos"
205     elif [[ "$id" =~ ^(http|ftp)s?: ]]; then
206         if [ "$network_backend_support" = "yes" ]; then
207             echo "network";
208         else
209             log_error "Retrieving images from the network is not supported."
210             exit 1
211         fi
212     else
213         echo "local";
214     fi
215 }
216
217 # this one is only to be called by create
218 ganeti_os_main() {
219     if [ -z "$OS_API_VERSION" -o "$OS_API_VERSION" = "5" ]; then
220         OS_API_VERSION=5
221         GETOPT_RESULT=`getopt -o o:n:i:b:s: -n '$0' -- "$@"`
222         if [ $? != 0 ] ; then log_error "Terminating..."; exit 1 ; fi
223         get_api5_arguments $GETOPT_RESULT
224     elif [ "$OS_API_VERSION" = "10" -o "$OS_API_VERSION" = "15" ]; then
225         get_api10_arguments
226     elif [ "$OS_API_VERSION" = "20" ]; then
227         get_api20_arguments
228         IMAGE_NAME="$IMG_ID"
229         IMAGE_TYPE="$IMG_FORMAT"
230         BACKEND_TYPE=$(get_backend_type $IMG_ID)
231     else
232         log_error "Unknown OS API VERSION $OS_API_VERSION"
233         exit 1
234     fi
235     
236     if [ -n "$OS_VARIANT" ]; then
237         if [ ! -d "$VARIANTS_DIR" ]; then
238             log_error "OS Variants directory $VARIANTS_DIR doesn't exist"
239             exit 1
240         fi
241         VARIANT_CONFIG="$VARIANTS_DIR/$OS_VARIANT.conf"
242         if [ -f "$VARIANT_CONFIG" ]; then
243             . "$VARIANT_CONFIG"
244         else
245             if grep -qxF "$OS_VARIANT" variants.list; then
246                 log_error "ERROR: instance-image configuration error"
247                 log_error "  Published variant $OS_VARIANT is missing its" \
248                     "config file"
249                 log_error "  Please create $VARIANT_CONFIG or unpublish the" \
250                     "variant"
251                 log_error "  (by removing $OS_VARIANT from variants.list)"
252             else
253                 log_error "Unofficial variant $OS_VARIANT is unsupported"
254                 log_error "Most probably this is a user error, forcing a" \
255                     "wrong name"
256                 log_error "To support this variant please create file" \
257                     "$VARIANT_CONFIG"
258             fi
259             exit 1
260         fi
261     fi
262
263 }
264
265 do_debootstrap() {
266     local target="$1"
267
268     echo "Debootstraping to create a new root filesystem:"
269
270     # Create a policy-rc.d file to deny init script execution
271     mkdir -p "$target/usr/sbin"
272     cat > "$target/usr/sbin/policy-rc.d" <<EOF
273 #!/bin/sh
274 exit 101
275 EOF
276     chmod +x "$target/usr/sbin/policy-rc.d"
277
278     debootstrap --arch $(dpkg --print-architecture) \
279         --include "$HELPER_EXTRA_PKGS" --variant=minbase stable "$target" \
280         "$HELPER_MIRROR" 2>&1 | sed -e 's/^/DEBOOTSTRAP: /g'
281
282     # Save the package list
283     chroot "$target" dpkg-query -W -f "\${Package}\n" > "$HELPER_CACHE_PKGS"
284
285     rm "$target/usr/sbin/policy-rc.d"
286
287     # remove the downloaded debs, as they are no longer needed
288     find "$target/var/cache/apt/archives" -type f -name '*.deb' -print0 | \
289         xargs -r0 rm -f
290
291     local tmp_cache=$(mktemp "$CACHE_FILE.XXXXXX")
292     tar cf "$tmp_cache" --one-file-system -C "$target" . || \
293         { rm "$tmp_cache"; false; }
294     # Overwrite the default cache file. Not the user specified if present.
295     mv -f "$tmp_cache" "$HELPER_CACHE_FILE"
296 }
297
298 cleanup() {
299 # if something fails here, it souldn't call cleanup again...
300     trap - EXIT
301     if [ ${#CLEANUP[*]} -gt 0 ]; then
302         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
303         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
304         for i in $REVERSE_INDEXES; do
305             # If something fails here, it's better to retry it for a few times
306             # before we give up with an error. This is needed for kpartx when
307             # dealing with ntfs partitions mounted through fuse. umount is not
308             # synchronous and may return while the partition is still busy. A
309             # premature attempt to delete partition mappings through kpartx on
310             # a device that hosts previously mounted ntfs partition may fail
311             # with errors like this one:
312             # `device-mapper: remove ioctl failed: Device or resource busy'
313             # A sensible workaround for this is to wait for a while and then
314             # retry it.
315             local cmd=${CLEANUP[$i]}
316             $cmd || for interval in 0.25 0.5 1 2 4; do
317             echo "Command $cmd failed!"
318             echo "I'll wait for $interval secs and will retry..."
319             sleep $interval
320             $cmd && break
321         done
322         if [ "$?" != "0" ]; then
323             echo "Giving Up..."
324             exit 1;
325         fi
326     done
327   fi
328 }
329
330 trap cleanup EXIT
331
332 DEFAULT_FILE="@sysconfdir@/default/snf-image"
333 if [ -f "$DEFAULT_FILE" ]; then
334     . "$DEFAULT_FILE"
335 fi
336
337 : ${VARIANTS_DIR:="@sysconfdir@/ganeti/snf-image/variants"}
338 : ${IMAGE_DIR:="@localstatedir@/lib/snf-image"}
339 : ${HELPER_DIR:="@HELPER_DIR@"}
340 : ${HELPER_IMG:="@HELPER_IMG@"}
341 : ${HELPER_KERNEL:="@HELPER_KERNEL@"}
342 : ${HELPER_INITRD:="@HELPER_INITRD@"}
343 : ${HELPER_PKG:="@HELPER_DIR@/snf-image-helper.deb"}
344 : ${HELPER_SOFT_TIMEOUT:=15}
345 : ${HELPER_HARD_TIMEOUT:=5}
346 : ${HELPER_USER:="nobody"}
347 : ${HELPER_CACHE_FILE:="@HELPER_DIR@/cache.tar"}
348 : ${HELPER_CACHE_PKGS:="@HELPER_DIR@/packages"}
349 : ${HELPER_EXTRA_PKGS:="linux-image-amd64,e2fsprogs,ntfs-3g,ntfsprogs,xmlstarlet,python,parted,reglookup,chntpw,util-linux"}
350 : ${HELPER_MIRROR:=""}
351 : ${PITHOS_DB:="sqlite:////@localstatedir@/lib/pithos/backend.db"}
352 : ${PITHOS_DATA:="@localstatedir@/lib/pithos/data/"}
353 : ${PROGRESS_MONITOR:="@PROGRESS_MONITOR@"}
354 : ${UNATTEND:="@UNATTEND@"}
355
356 SCRIPT_NAME=$(basename $0)
357
358 if [ -f /sbin/blkid -a -x /sbin/blkid ]; then
359     VOL_ID="/sbin/blkid -c /dev/null -o value -s UUID"
360     VOL_TYPE="/sbin/blkid -c /dev/null -o value -s TYPE"
361 else
362     for dir in /lib/udev /sbin; do
363         if [ -f $dir/vol_id -a -x $dir/vol_id ]; then
364             VOL_ID="$dir/vol_id -u"
365             VOL_TYPE="$dir/vol_id -t"
366         fi
367     done
368 fi
369
370 if [ -z "$VOL_ID" ]; then
371     log_error "vol_id or blkid not found, please install udev or util-linux"
372     exit 1
373 fi
374
375 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :