Use socat to dump helper messages in case of xen
[snf-image] / snf-image-helper / common.sh
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 PROGNAME=$(basename $0)
20
21 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
22
23 # Programs
24 XMLSTARLET=xmlstarlet
25 TUNE2FS=tune2fs
26 RESIZE2FS=resize2fs
27 PARTED=parted
28 SFDISK=sfdisk
29 MKSWAP=mkswap
30 BLKID=blkid
31 BLOCKDEV=blockdev
32 REGLOOKUP=reglookup
33 CHNTPW=chntpw
34
35 CLEANUP=( )
36 ERRORS=( )
37 WARNINGS=( )
38
39 MSG_TYPE_TASK_START="TASK_START"
40 MSG_TYPE_TASK_END="TASK_END"
41
42 STDERR_LINE_SIZE=10
43
44
45 # hypervisor param is passed in /proc/cmdline
46 case "$hypervisor" in
47   kvm)
48     FLOPPY_DEV=/dev/fd0
49     IMG_DEV=/dev/vda
50     RESULT=/dev/ttyS1
51     MONITOR=/dev/ttyS2
52     ;;
53   xen-hvm|xen-pvm)
54     FLOPPY_DEV=/dev/xvdc1
55     IMG_DEV=/dev/xvdb
56     ;;
57 esac
58
59 to_monitor() {
60
61     case $hypervisor in
62       kvm)
63           echo "HELPER_MONITOR_$@" > "$MONITOR"
64           ;;
65       xen-pvm|xen-hvm)
66           echo  "HELPER_MONITOR_$@" | socat STDIO INTERFACE:eth0
67           ;;
68     esac
69 }
70
71 to_result() {
72
73     case $hypervisor in
74       kvm)
75         echo "HELPER_RESULT_$@" > "$RESULT"
76         ;;
77       xen-pvm|xen-hvm)
78         domid=$(xenstore-read domid)
79         xenstore-write /local/domain/0/helper/$domid "HELPER_RESULT_$@"
80         ;;
81     esac
82
83 }
84
85
86 add_cleanup() {
87     local cmd=""
88     for arg; do cmd+=$(printf "%q " "$arg"); done
89     CLEANUP+=("$cmd")
90 }
91
92 log_error() {
93     ERRORS+=("$@")
94     echo "ERROR: $@" >&2
95     to_result "ERROR: $@"
96     exit 1
97 }
98
99 warn() {
100     echo "Warning: $@" >&2
101     to_monitor "WARNING: $@"
102 }
103
104 report_task_start() {
105     to_monitor "$MSG_TYPE_TASK_START:${PROGNAME:2}"
106 }
107
108 report_task_end() {
109     to_monitor "$MSG_TYPE_TASK_END:${PROGNAME:2}"
110 }
111
112 report_error() {
113     if [ ${#ERRORS[*]} -eq 0 ]; then
114         # No error message. Print stderr
115               lines="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
116         cnt=$(echo $lines | wc -l)
117         for line in lines; do
118           to_monitor "$STDERR:$cnt: $line"
119           let cnt--
120         done
121     else
122         for line in "${ERRORS[@]}"; do
123             to_monitor "ERROR: $line"
124         done
125     fi
126 }
127
128 get_base_distro() {
129     local root_dir=$1
130
131     if [ -e "$root_dir/etc/debian_version" ]; then
132         echo "debian"
133     elif [ -e "$root_dir/etc/redhat-release" ]; then
134         echo "redhat"
135     elif [ -e "$root_dir/etc/slackware-version" ]; then
136         echo "slackware"
137     elif [ -e "$root_dir/etc/SuSE-release" ]; then
138         echo "suse"
139     elif [ -e "$root_dir/etc/gentoo-release" ]; then
140         echo "gentoo"
141     elif [ -e "$root_dir/etc/arch-release" ]; then
142         echo "arch"
143     else
144         warn "Unknown base distro."
145     fi
146 }
147
148 get_distro() {
149     local root_dir=$1
150
151     if [ -e "$root_dir/etc/debian_version" ]; then
152         distro="debian"
153         if [ -e ${root_dir}/etc/lsb-release ]; then
154             ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
155             if [ "x$ID" = "xUbuntu" ]; then
156                 distro="ubuntu"
157             fi
158         fi
159         echo "$distro"
160     elif [ -e "$root_dir/etc/fedora-release" ]; then
161         echo "fedora"
162     elif [ -e "$root_dir/etc/centos-release" ]; then
163         echo "centos"
164     elif [ -e "$root_dir/etc/redhat-release" ]; then
165         echo "redhat"
166     elif [ -e "$root_dir/etc/slackware-version" ]; then
167         echo "slackware"
168     elif [ -e "$root_dir/etc/SuSE-release" ]; then
169         echo "suse"
170     elif [ -e "$root_dir/etc/gentoo-release" ]; then
171         echo "gentoo"
172     elif [ -e "$root_dir/etc/arch-release" ]; then
173         echo "arch"
174     else
175         warn "Unknown distro."
176     fi
177 }
178
179
180 get_partition_table() {
181     local dev="$1"
182     # If the partition table is gpt then parted will raise an error if the
183     # secondary gpt is not it the end of the disk, and a warning that has to
184     # do with the "Last Usable LBA" entry in gpt.
185     if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
186         log_error "Unable to read partition table for device \`${dev}'"
187     fi
188
189     echo "$output"
190 }
191
192 get_partition_table_type() {
193     local ptable="$1"
194
195     local dev="$(sed -n 2p <<< "$ptable")"
196     declare -a field
197     IFS=':' read -ra field <<< "$dev"
198
199     echo "${field[5]}"
200 }
201
202 get_partition_count() {
203     local ptable="$1"
204
205     expr $(echo "$ptable" | wc -l) - 2
206 }
207
208 get_partition_by_num() {
209     local ptable="$1"
210     local id="$2"
211
212     grep "^$id:" <<< "$ptable"
213 }
214
215 get_last_partition() {
216     local ptable="$1"
217
218     echo "$ptable" | tail -1
219 }
220
221 is_extended_partition() {
222     local dev="$1"
223     local part_num="$2"
224
225     id=$($SFDISK --print-id "$dev" "$part_num")
226     if [ "$id" = "5" ]; then
227         echo "yes"
228     else
229         echo "no"
230     fi
231 }
232
233 get_extended_partition() {
234     local ptable="$1"
235     local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
236
237     tail -n +3 <<< "$ptable" | while read line; do
238         part_num=$(cut -d':' -f1 <<< "$line")
239         if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
240             echo "$line"
241             return 0
242         fi
243     done
244     echo ""
245 }
246
247 get_logical_partitions() {
248     local ptable="$1"
249
250     tail -n +3 <<< "$ptable" | while read line; do
251         part_num=$(cut -d':' -f1 <<< "$line")
252         if [ $part_num -ge 5 ]; then
253             echo "$line"
254         fi
255     done
256
257     return 0
258 }
259
260 get_last_primary_partition() {
261     local ptable="$1"
262     local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
263
264     for i in 4 3 2 1; do
265         if output=$(grep "^$i:" <<< "$ptable"); then
266             echo "$output"
267             return 0
268         fi
269     done
270     echo ""
271 }
272
273 get_partition_to_resize() {
274     local dev="$1"
275
276     table=$(get_partition_table "$dev")
277
278     if [ $(get_partition_count "$table") -eq 0 ]; then
279         return 0
280     fi
281
282     table_type=$(get_partition_table_type "$table")
283     last_part=$(get_last_partition "$table")
284     last_part_num=$(cut -d: -f1 <<< "$last_part")
285
286     if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
287         extended=$(get_extended_partition "$table")
288         last_primary=$(get_last_primary_partition "$table")
289         ext_num=$(cut -d: -f1 <<< "$extended")
290         prim_num=$(cut -d: -f1 <<< "$last_primary")
291
292         if [ "$ext_num" != "$last_prim_num" ]; then
293             echo "$last_prim_num"
294         else
295             echo "$last_part_num"
296         fi
297     else
298         echo "$last_part_num"
299     fi
300 }
301
302 create_partition() {
303     local device="$1"
304     local part="$2"
305     local ptype="$3"
306
307     declare -a fields
308     IFS=":;" read -ra fields <<< "$part"
309     local id="${fields[0]}"
310     local start="${fields[1]}"
311     local end="${fields[2]}"
312     local size="${fields[3]}"
313     local fs="${fields[4]}"
314     local name="${fields[5]}"
315     local flags="${fields[6]//,/ }"
316
317     $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
318     for flag in $flags; do
319         $PARTED -s -m $device set "$id" "$flag" on
320     done
321 }
322
323 enlarge_partition() {
324     local device="$1"
325     local part="$2"
326     local ptype="$3"
327     local new_end="$4"
328
329     if [ -z "$new_end" ]; then
330         new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
331     fi
332
333     declare -a fields
334     IFS=":;" read -ra fields <<< "$part"
335     fields[2]="$new_end"
336
337     local new_part=""
338     for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
339         new_part="$new_part":"${fields[$i]}"
340     done
341     new_part=${new_part:1}
342
343     # If this is an extended partition, removing it will also remove the
344     # logical partitions it contains. We need to save them for later.
345     if [ "$ptype" = "extended" ]; then
346         local table="$(get_partition_table "$device")"
347         local logical="$(get_logical_partitions "$table")"
348     fi
349
350     id=${fields[0]}
351     $PARTED -s -m "$device" rm "$id"
352     create_partition "$device" "$new_part" "$ptype"
353
354     if [ "$ptype" = "extended" ]; then
355         # Recreate logical partitions
356         echo "$logical" | while read logical_part; do
357             create_partition "$device" "$logical_part" "logical"
358         done
359     fi
360 }
361
362 get_last_free_sector() {
363     local dev="$1"
364     local unit="$2"
365
366     if [ -n "$unit" ]; then
367         unit="unit $unit"
368     fi
369
370     local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
371     local ptype="$(cut -d: -f 5 <<< "$last_line")"
372
373     if [ "$ptype" = "free;" ]; then
374         echo "$last_line"
375     fi
376 }
377
378 cleanup() {
379     # if something fails here, it shouldn't call cleanup again...
380     trap - EXIT
381
382     if [ ${#CLEANUP[*]} -gt 0 ]; then
383         LAST_ELEMENT=$((${#CLEANUP[*]}-1))
384         REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
385         for i in $REVERSE_INDEXES; do
386             # If something fails here, it's better to retry it for a few times
387             # before we give up with an error. This is needed for kpartx when
388             # dealing with ntfs partitions mounted through fuse. umount is not
389             # synchronous and may return while the partition is still busy. A
390             # premature attempt to delete partition mappings through kpartx on
391             # a device that hosts previously mounted ntfs partition may fail
392             # with a `device-mapper: remove ioctl failed: Device or resource
393             # busy' error. A sensible workaround for this is to wait for a
394             # while and then try again.
395             local cmd=${CLEANUP[$i]}
396             $cmd || for interval in 0.25 0.5 1 2 4; do
397             echo "Command $cmd failed!"
398             echo "I'll wait for $interval secs and will retry..."
399             sleep $interval
400             $cmd && break
401         done
402         if [ "$?" != "0" ]; then
403             echo "Giving Up..."
404             exit 1;
405         fi
406     done
407   fi
408 }
409
410 task_cleanup() {
411     rc=$?
412
413     if [ $rc -eq 0 ]; then
414        report_task_end
415     else
416        report_error
417     fi
418
419     cleanup
420 }
421
422 check_if_excluded() {
423     local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
424     local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
425     if [ -n "${!exclude}" ]; then
426         warn "Task ${PROGNAME:2} was excluded and will not run."
427         exit 0
428     fi
429
430     return 0
431 }
432
433
434 return_success() {
435
436     to_result SUCCESS
437
438 }
439
440 trap cleanup EXIT
441 set -o pipefail
442
443 STDERR_FILE=$(mktemp)
444 add_cleanup rm -f "$STDERR_FILE"
445 exec 2> >(tee -a "$STDERR_FILE" >&2)
446
447 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :