1 # Copyright (C) 2011, 2012, 2013 GRNET S.A.
2 # Copyright (C) 2007, 2008, 2009 Google Inc.
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.
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.
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
19 PROGNAME=$(basename $0)
21 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
34 DATE="date -u" # Time in UTC
41 MSG_TYPE_TASK_START="TASK_START"
42 MSG_TYPE_TASK_END="TASK_END"
48 for arg; do cmd+=$(printf "%q " "$arg"); done
59 local cmdline item key val hypervisor domid
61 read -a cmdline < /proc/cmdline
62 for item in "${cmdline[@]}"; do
63 key=$(cut -d= -f1 <<< "$item")
64 if [ "$key" = "hypervisor" ]; then
65 val=$(cut -d= -f2 <<< "$item")
73 exec {RESULT_FD}> /dev/ttyS1
74 add_cleanup close_fd ${RESULT_FD}
75 exec {MONITOR_FD}> /dev/ttyS2
76 add_cleanup close_fd ${MONITOR_FD}
79 mount -t xenfs xenfs /proc/xen
80 iptables -P OUTPUT DROP
81 ip6tables -P OUTPUT DROP
84 domid=$(xenstore-read domid)
85 exec {RESULT_FD}> >(xargs -l1 xenstore-write /local/domain/0/snf-image-helper/$domid)
86 add_cleanup close_fd ${RESULT_FD}
87 exec {MONITOR_FD}> >(socat STDIN INTERFACE:eth0)
88 add_cleanup close_fd ${MONITOR_FD}
91 echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
96 export RESULT_FD MONITOR_FD
100 if [ ${#ERRORS[*]} -eq 0 ]; then
101 # No error message. Print stderr
103 lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
104 echo -n "STDERR:${lines}:" >&${MONITOR_FD}
105 tail --lines=$lines "$STDERR_FILE" >&${MONITOR_FD}
107 for line in "${ERRORS[@]}"; do
108 echo "ERROR:$line" >&${MONITOR_FD}
116 echo "ERROR: $@" >&${MONITOR_FD}
118 echo "ERROR" >&${RUSULT_FD}
120 # Use return instead of exit. The set -x options will terminate the script
121 # but will also trigger ERR traps if defined.
126 echo "Warning: $@" >&2
127 echo "WARNING: $@" >&${MONITOR_FD}
130 report_task_start() {
131 echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" >&${MONITOR_FD}
135 echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" >&${MONITOR_FD}
140 # Credits to psomas@grnet.gr for this ...
141 echo o > /proc/sysrq-trigger
149 if [ -e "$root_dir/etc/debian_version" ]; then
151 elif [ -e "$root_dir/etc/redhat-release" ]; then
153 elif [ -e "$root_dir/etc/slackware-version" ]; then
155 elif [ -e "$root_dir/etc/SuSE-release" ]; then
157 elif [ -e "$root_dir/etc/gentoo-release" ]; then
159 elif [ -e "$root_dir/etc/arch-release" ]; then
162 warn "Unknown base distro."
167 local root_dir distro
170 if [ -e "$root_dir/etc/debian_version" ]; then
172 if [ -e ${root_dir}/etc/lsb-release ]; then
173 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
174 if [ "x$ID" = "xUbuntu" ]; then
179 elif [ -e "$root_dir/etc/fedora-release" ]; then
181 elif [ -e "$root_dir/etc/centos-release" ]; then
183 elif [ -e "$root_dir/etc/redhat-release" ]; then
185 elif [ -e "$root_dir/etc/slackware-version" ]; then
187 elif [ -e "$root_dir/etc/SuSE-release" ]; then
189 elif [ -e "$root_dir/etc/gentoo-release" ]; then
191 elif [ -e "$root_dir/etc/arch-release" ]; then
194 warn "Unknown distro."
199 get_partition_table() {
202 # If the partition table is gpt then parted will raise an error if the
203 # secondary gpt is not it the end of the disk, and a warning that has to
204 # do with the "Last Usable LBA" entry in gpt.
205 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
206 log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
212 get_partition_table_type() {
213 local ptable dev field
216 dev="$(sed -n 2p <<< "$ptable")"
217 IFS=':' read -ra field <<< "$dev"
222 get_partition_count() {
225 expr $(echo "$ptable" | wc -l) - 2
228 get_partition_by_num() {
232 grep "^$id:" <<< "$ptable"
235 get_last_partition() {
238 echo "$ptable" | tail -1
241 is_extended_partition() {
245 id=$($SFDISK --print-id "$dev" "$part_num")
246 if [ "$id" = "5" -o "$id" = "f" ]; then
253 get_extended_partition() {
256 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
258 tail -n +3 <<< "$ptable" | while read line; do
259 part_num=$(cut -d':' -f1 <<< "$line")
260 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
268 get_logical_partitions() {
269 local ptable part_num
272 tail -n +3 <<< "$ptable" | while read line; do
273 part_num=$(cut -d':' -f1 <<< "$line")
274 if [ $part_num -ge 5 ]; then
282 get_last_primary_partition() {
283 local ptable dev output
285 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
288 if output=$(grep "^$i:" <<< "$ptable"); then
296 get_partition_to_resize() {
297 local dev table table_type last_part last_part_num extended last_primary \
301 table=$(get_partition_table "$dev")
303 if [ $(get_partition_count "$table") -eq 0 ]; then
307 table_type=$(get_partition_table_type "$table")
308 last_part=$(get_last_partition "$table")
309 last_part_num=$(cut -d: -f1 <<< "$last_part")
311 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
312 extended=$(get_extended_partition "$table")
313 last_primary=$(get_last_primary_partition "$table")
314 ext_num=$(cut -d: -f1 <<< "$extended")
315 last_prim_num=$(cut -d: -f1 <<< "$last_primary")
317 if [ "$ext_num" != "$last_prim_num" ]; then
318 echo "$last_prim_num"
320 echo "$last_part_num"
323 echo "$last_part_num"
333 IFS=":;" read -ra fields <<< "$part"
334 local id="${fields[0]}"
335 local start="${fields[1]}"
336 local end="${fields[2]}"
337 local size="${fields[3]}"
338 local fs="${fields[4]}"
339 local name="${fields[5]}"
340 local flags="${fields[6]//,/ }"
342 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
343 for flag in $flags; do
344 $PARTED -s -m $device set "$id" "$flag" on
348 enlarge_partition() {
349 local device part ptype new_end fields new_part table logical id
355 if [ -z "$new_end" ]; then
356 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
360 IFS=":;" read -ra fields <<< "$part"
364 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
365 new_part="$new_part":"${fields[$i]}"
367 new_part=${new_part:1}
369 # If this is an extended partition, removing it will also remove the
370 # logical partitions it contains. We need to save them for later.
371 if [ "$ptype" = "extended" ]; then
372 table="$(get_partition_table "$device")"
373 logical="$(get_logical_partitions "$table")"
377 $PARTED -s -m "$device" rm "$id"
378 create_partition "$device" "$new_part" "$ptype"
380 if [ "$ptype" = "extended" ]; then
381 # Recreate logical partitions
382 echo "$logical" | while read logical_part; do
383 create_partition "$device" "$logical_part" "logical"
388 get_last_free_sector() {
389 local dev unit last_line ptype
393 if [ -n "$unit" ]; then
397 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
398 ptype="$(cut -d: -f 5 <<< "$last_line")"
400 if [ "$ptype" = "free;" ]; then
409 # Workaround to search for $target/Unattend.xml in an case insensitive way.
410 exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
411 if [ $(wc -l <<< "$exists") -gt 1 ]; then
412 log_error "Found multiple Unattend.xml files in the image:" $exists
422 # Unmount file systems mounted under directory `target'
423 mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
425 for mpoint in $mpoints; do
431 # if something fails here, it shouldn't call cleanup again...
434 if [ ${#CLEANUP[*]} -gt 0 ]; then
435 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
436 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
437 for i in $REVERSE_INDEXES; do
438 # If something fails here, it's better to retry it for a few times
439 # before we give up with an error. This is needed for kpartx when
440 # dealing with ntfs partitions mounted through fuse. umount is not
441 # synchronous and may return while the partition is still busy. A
442 # premature attempt to delete partition mappings through kpartx on
443 # a device that hosts previously mounted ntfs partition may fail
444 # with a `device-mapper: remove ioctl failed: Device or resource
445 # busy' error. A sensible workaround for this is to wait for a
446 # while and then try again.
447 local cmd=${CLEANUP[$i]}
448 $cmd || for interval in 0.25 0.5 1 2 4; do
449 echo "Command $cmd failed!"
450 echo "I'll wait for $interval secs and will retry..."
454 if [ "$?" != "0" ]; then
465 if [ $rc -eq 0 ]; then
474 check_if_excluded() {
476 name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
477 exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
478 if [ -n "${!exclude}" ]; then
479 warn "Task ${PROGNAME:2} was excluded and will not run."
488 echo SUCCESS >&${RESULT_FD}
494 STDERR_FILE=$(mktemp)
495 add_cleanup rm -f "$STDERR_FILE"
496 exec 2> >(tee -a "$STDERR_FILE" >&2)
498 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :