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