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
37 DATE="date -u" # Time in UTC
45 MSG_TYPE_TASK_START="TASK_START"
46 MSG_TYPE_TASK_END="TASK_END"
52 for arg; do cmd+=$(printf "%q " "$arg"); done
63 echo "$@" > /dev/ttyS1
66 send_monitor_message_kvm() {
67 echo "$@" > /dev/ttyS2
71 xenstore-write /local/domain/0/snf-image-helper/$DOMID "$*"
74 send_monitor_message_xen() {
75 #Broadcast the message
76 echo "$@" | socat "STDIO" "UDP-DATAGRAM:${BROADCAST}:${MONITOR_PORT},broadcast"
80 local cmdline item key val hypervisor domid
82 read -a cmdline < /proc/cmdline
83 for item in "${cmdline[@]}"; do
84 key=$(cut -d= -f1 <<< "$item")
85 val=$(cut -d= -f2 <<< "$item")
86 if [ "$key" = "hypervisor" ]; then
89 if [ "$key" = "rules_dev" ]; then
90 export RULES_DEV="$val"
92 if [ "$key" = "helper_ip" ]; then
94 export NETWORK="$IP/24"
95 export BROADCAST="${IP%.*}.255"
97 if [ "$key" = "monitor_port" ]; then
98 export MONITOR_PORT="$val"
102 case "$hypervisor" in
107 if [ -z "$IP" ]; then
108 echo "ERROR: \`helper_ip' not defined or empty" >&2
111 if [ -z "$MONITOR_PORT" ]; then
112 echo "ERROR: \`monitor_port' not defined or empty" >&2
115 $MOUNT -t xenfs xenfs /proc/xen
116 ip addr add "$NETWORK" dev eth0
118 ip route add default dev eth0
119 export DOMID=$(xenstore-read domid)
123 echo "ERROR: Unknown hypervisor: \`$hypervisor'" >&2
133 if [ ${#ERRORS[*]} -eq 0 ]; then
134 # No error message. Print stderr
136 stderr="$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE")"
137 lines=$(wc -l <<< "$stderr")
138 msg="STDERR:${lines}:$stderr"
140 for line in "${ERRORS[@]}"; do
141 msg+="ERROR:$line"$'\n'
145 send_monitor_message_${HYPERVISOR} "$msg"
151 send_result_${HYPERVISOR} "ERROR: $@"
153 # Use return instead of exit. The set -x options will terminate the script
154 # but will also trigger ERR traps if defined.
159 echo "Warning: $@" >&2
160 send_monitor_message_${HYPERVISOR} "WARNING: $@"
163 report_task_start() {
164 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_START:${PROGNAME:2}"
168 send_monitor_message_${HYPERVISOR} "$MSG_TYPE_TASK_END:${PROGNAME:2}"
173 # Credits to psomas@grnet.gr for this ...
174 echo o > /proc/sysrq-trigger
182 if [ -e "$root_dir/etc/debian_version" ]; then
184 elif [ -e "$root_dir/etc/redhat-release" ]; then
186 elif [ -e "$root_dir/etc/slackware-version" ]; then
188 elif [ -e "$root_dir/etc/SuSE-release" ]; then
190 elif [ -e "$root_dir/etc/gentoo-release" ]; then
192 elif [ -e "$root_dir/etc/arch-release" ]; then
194 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
197 warn "Unknown base distro."
202 local root_dir distro
205 if [ -e "$root_dir/etc/debian_version" ]; then
207 if [ -e ${root_dir}/etc/lsb-release ]; then
208 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
209 if [ "x$ID" = "xUbuntu" ]; then
214 elif [ -e "$root_dir/etc/fedora-release" ]; then
216 elif [ -e "$root_dir/etc/centos-release" ]; then
218 elif [ -e "$root_dir/etc/redhat-release" ]; then
220 elif [ -e "$root_dir/etc/slackware-version" ]; then
222 elif [ -e "$root_dir/etc/SuSE-release" ]; then
224 elif [ -e "$root_dir/etc/gentoo-release" ]; then
226 elif [ -e "$root_dir/etc/arch-release" ]; then
228 elif [ -e "$root_dir/etc/freebsd-update.conf" ]; then
231 warn "Unknown distro."
236 get_partition_table() {
239 # If the partition table is gpt then parted will raise an error if the
240 # secondary gpt is not it the end of the disk, and a warning that has to
241 # do with the "Last Usable LBA" entry in gpt.
242 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
243 log_error "Unable to read partition table for device \`${dev}'. The image seems corrupted."
249 get_partition_table_type() {
250 local ptable dev field
253 dev="$(sed -n 2p <<< "$ptable")"
254 IFS=':' read -ra field <<< "$dev"
259 get_partition_count() {
262 expr $(echo "$ptable" | wc -l) - 2
265 get_partition_by_num() {
269 grep "^$id:" <<< "$ptable"
272 get_last_partition() {
275 echo "$ptable" | tail -1
278 is_extended_partition() {
282 id=$($SFDISK --force --print-id "$dev" "$part_num")
283 if [ "$id" = "5" -o "$id" = "f" ]; then
290 get_extended_partition() {
293 dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
295 tail -n +3 <<< "$ptable" | while read line; do
296 part_num=$(cut -d':' -f1 <<< "$line")
297 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
305 get_logical_partitions() {
306 local ptable part_num
309 tail -n +3 <<< "$ptable" | while read line; do
310 part_num=$(cut -d':' -f1 <<< "$line")
311 if [ $part_num -ge 5 ]; then
319 get_last_primary_partition() {
320 local ptable dev output
322 dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
325 if output=$(grep "^$i:" <<< "$ptable"); then
333 get_partition_to_resize() {
334 local dev table table_type last_part last_part_num extended last_primary \
338 table=$(get_partition_table "$dev")
340 if [ $(get_partition_count "$table") -eq 0 ]; then
344 table_type=$(get_partition_table_type "$table")
345 last_part=$(get_last_partition "$table")
346 last_part_num=$(cut -d: -f1 <<< "$last_part")
348 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
349 extended=$(get_extended_partition "$table")
350 last_primary=$(get_last_primary_partition "$table")
351 ext_num=$(cut -d: -f1 <<< "$extended")
352 last_prim_num=$(cut -d: -f1 <<< "$last_primary")
354 if [ "$ext_num" != "$last_prim_num" ]; then
355 echo "$last_prim_num"
357 echo "$last_part_num"
360 echo "$last_part_num"
370 IFS=":;" read -ra fields <<< "$part"
371 local id="${fields[0]}"
372 local start="${fields[1]}"
373 local end="${fields[2]}"
374 local size="${fields[3]}"
375 local fs="${fields[4]}"
376 local name="${fields[5]}"
377 local flags="${fields[6]//,/ }"
379 if [ "$ptype" = "primary" -o "$ptype" = "logical" -o "$ptype" = "extended" ]; then
380 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
381 for flag in $flags; do
382 $PARTED -s -m $device set "$id" "$flag" on
386 start=${start:0:${#start}-1} # remove the s at the end
387 end=${end:0:${#end}-1} # remove the s at the end
388 $SGDISK -n "$id":"$start":"$end" -t "$id":"$ptype" "$device"
392 enlarge_partition() {
393 local device part ptype new_end fields new_part table logical id
399 if [ -z "$new_end" ]; then
400 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
404 IFS=":;" read -ra fields <<< "$part"
408 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
409 new_part="$new_part":"${fields[$i]}"
411 new_part=${new_part:1}
413 # If this is an extended partition, removing it will also remove the
414 # logical partitions it contains. We need to save them for later.
415 if [ "$ptype" = "extended" ]; then
416 table="$(get_partition_table "$device")"
417 logical="$(get_logical_partitions "$table")"
421 $PARTED -s -m "$device" rm "$id"
422 create_partition "$device" "$new_part" "$ptype"
424 if [ "$ptype" = "extended" ]; then
425 # Recreate logical partitions
426 echo "$logical" | while read logical_part; do
427 create_partition "$device" "$logical_part" "logical"
432 get_last_free_sector() {
433 local dev unit last_line ptype
437 if [ -n "$unit" ]; then
441 last_line="$($PARTED -s -m "$dev" "$unit" print free | tail -1)"
442 ptype="$(cut -d: -f 5 <<< "$last_line")"
444 if [ "$ptype" = "free;" ]; then
453 # Workaround to search for $target/Unattend.xml in an case insensitive way.
454 exists=$(find "$target"/ -maxdepth 1 -iname unattend.xml)
455 if [ $(wc -l <<< "$exists") -gt 1 ]; then
456 log_error "Found multiple Unattend.xml files in the image:" $exists
466 # Unmount file systems mounted under directory `target'
467 mpoints="$({ awk "{ if (match(\$2, \"^$target\")) { print \$2 } }" < /proc/mounts; } | sort -rbd | uniq)"
469 for mpoint in $mpoints; do
475 # if something fails here, it shouldn't call cleanup again...
478 if [ ${#CLEANUP[*]} -gt 0 ]; then
479 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
480 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
481 for i in $REVERSE_INDEXES; do
482 # If something fails here, it's better to retry it for a few times
483 # before we give up with an error. This is needed for kpartx when
484 # dealing with ntfs partitions mounted through fuse. umount is not
485 # synchronous and may return while the partition is still busy. A
486 # premature attempt to delete partition mappings through kpartx on
487 # a device that hosts previously mounted ntfs partition may fail
488 # with a `device-mapper: remove ioctl failed: Device or resource
489 # busy' error. A sensible workaround for this is to wait for a
490 # while and then try again.
491 local cmd=${CLEANUP[$i]}
492 $cmd || for interval in 0.25 0.5 1 2 4; do
493 echo "Command $cmd failed!"
494 echo "I'll wait for $interval secs and will retry..."
498 if [ "$?" != "0" ]; then
509 if [ $rc -eq 0 ]; then
518 check_if_excluded() {
520 name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
521 exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
522 if [ -n "${!exclude}" ]; then
523 warn "Task ${PROGNAME:2} was excluded and will not run."
532 send_result_${HYPERVISOR} "SUCCESS"
538 STDERR_FILE=$(mktemp)
539 add_cleanup rm -f "$STDERR_FILE"
540 exec 2> >(tee -a "$STDERR_FILE" >&2)
542 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :