1 # Copyright (C) 2011 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
39 MSG_TYPE_TASK_START="TASK_START"
40 MSG_TYPE_TASK_END="TASK_END"
45 # hypervisor param is passed in /proc/cmdline
63 echo "HELPER_MONITOR_$@" > "$MONITOR"
66 echo "HELPER_MONITOR_$@" | socat STDIO INTERFACE:eth0
75 echo "HELPER_RESULT_$@" > "$RESULT"
78 domid=$(xenstore-read domid)
79 xenstore-write /local/domain/0/helper/$domid "HELPER_RESULT_$@"
88 for arg; do cmd+=$(printf "%q " "$arg"); done
100 echo "Warning: $@" >&2
101 to_monitor "WARNING: $@"
104 report_task_start() {
105 to_monitor "$MSG_TYPE_TASK_START:${PROGNAME:2}"
109 to_monitor "$MSG_TYPE_TASK_END:${PROGNAME:2}"
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"
122 for line in "${ERRORS[@]}"; do
123 to_monitor "ERROR: $line"
131 if [ -e "$root_dir/etc/debian_version" ]; then
133 elif [ -e "$root_dir/etc/redhat-release" ]; then
135 elif [ -e "$root_dir/etc/slackware-version" ]; then
137 elif [ -e "$root_dir/etc/SuSE-release" ]; then
139 elif [ -e "$root_dir/etc/gentoo-release" ]; then
141 elif [ -e "$root_dir/etc/arch-release" ]; then
144 warn "Unknown base distro."
151 if [ -e "$root_dir/etc/debian_version" ]; then
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
160 elif [ -e "$root_dir/etc/fedora-release" ]; then
162 elif [ -e "$root_dir/etc/centos-release" ]; then
164 elif [ -e "$root_dir/etc/redhat-release" ]; then
166 elif [ -e "$root_dir/etc/slackware-version" ]; then
168 elif [ -e "$root_dir/etc/SuSE-release" ]; then
170 elif [ -e "$root_dir/etc/gentoo-release" ]; then
172 elif [ -e "$root_dir/etc/arch-release" ]; then
175 warn "Unknown distro."
180 get_partition_table() {
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}'"
192 get_partition_table_type() {
195 local dev="$(sed -n 2p <<< "$ptable")"
197 IFS=':' read -ra field <<< "$dev"
202 get_partition_count() {
205 expr $(echo "$ptable" | wc -l) - 2
208 get_partition_by_num() {
212 grep "^$id:" <<< "$ptable"
215 get_last_partition() {
218 echo "$ptable" | tail -1
221 is_extended_partition() {
225 id=$($SFDISK --print-id "$dev" "$part_num")
226 if [ "$id" = "5" ]; then
233 get_extended_partition() {
235 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
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
247 get_logical_partitions() {
250 tail -n +3 <<< "$ptable" | while read line; do
251 part_num=$(cut -d':' -f1 <<< "$line")
252 if [ $part_num -ge 5 ]; then
260 get_last_primary_partition() {
262 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
265 if output=$(grep "^$i:" <<< "$ptable"); then
273 get_partition_to_resize() {
276 table=$(get_partition_table "$dev")
278 if [ $(get_partition_count "$table") -eq 0 ]; then
282 table_type=$(get_partition_table_type "$table")
283 last_part=$(get_last_partition "$table")
284 last_part_num=$(cut -d: -f1 <<< "$last_part")
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")
292 if [ "$ext_num" != "$last_prim_num" ]; then
293 echo "$last_prim_num"
295 echo "$last_part_num"
298 echo "$last_part_num"
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]//,/ }"
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
323 enlarge_partition() {
329 if [ -z "$new_end" ]; then
330 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
334 IFS=":;" read -ra fields <<< "$part"
338 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
339 new_part="$new_part":"${fields[$i]}"
341 new_part=${new_part:1}
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")"
351 $PARTED -s -m "$device" rm "$id"
352 create_partition "$device" "$new_part" "$ptype"
354 if [ "$ptype" = "extended" ]; then
355 # Recreate logical partitions
356 echo "$logical" | while read logical_part; do
357 create_partition "$device" "$logical_part" "logical"
362 get_last_free_sector() {
366 if [ -n "$unit" ]; then
370 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
371 local ptype="$(cut -d: -f 5 <<< "$last_line")"
373 if [ "$ptype" = "free;" ]; then
379 # if something fails here, it shouldn't call cleanup again...
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..."
402 if [ "$?" != "0" ]; then
413 if [ $rc -eq 0 ]; then
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."
443 STDERR_FILE=$(mktemp)
444 add_cleanup rm -f "$STDERR_FILE"
445 exec 2> >(tee -a "$STDERR_FILE" >&2)
447 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :