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
23 PROGNAME=$(basename $0)
25 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
43 MSG_TYPE_ERROR="error"
44 MSG_TYPE_TASK_START="task-start"
45 MSG_TYPE_TASK_END="task-end"
49 for arg; do cmd+=$(printf "%q " "$arg"); done
55 echo "ERROR: $@" | tee $RESULT >&2
61 echo "Warning: $@" >&2
66 local type="$MSG_TYPE_TASK_START"
67 local timestamp=$(date +%s.%N)
68 local name="${PROGNAME}"
70 report+="{\"type\":\"$type\","
71 report+="\"timestamp\":$(date +%s.%N),"
72 report+="\"name\":\"$name\"}"
74 echo "$report" > "$MONITOR"
78 declare -a items=("${!1}")
80 for item in "${items[@]}"; do
81 report+="\"$(sed 's/"/\\"/g' <<< "$item")\","
83 if [ ${#report} -gt 1 ]; then
84 # remove last comma(,)
93 local type="$MSG_TYPE_TASK_END"
94 local timestam=$(date +%s.%N)
95 local name=${PROGNAME}
96 local warnings=$(json_list WARNINGS[@])
98 report="{\"type\":\"$type\","
99 report+="\"timestamp\":$(date +%s.%N),"
100 report+="\"name\":\"$name\","
101 report+="\"warnings\":\"$warnings\"}"
103 echo "$report" > "$MONITOR"
107 local type="$MSG_TYPE_ERROR"
108 local timestamp=$(date +%s.%N)
109 local location="${PROGNAME}"
110 local errors=$(json_list ERRORS[@])
111 local warnings=$(json_list WARNINGS[@])
112 local stderr="$(cat "$STDERR_FILE" | sed 's/"/\\"/g')"
114 report="{\"type\":\"$type\","
115 report+="\"timestamp\":$(date +%s),"
116 report+="\"location\":\"$location\","
117 report+="\"errors\":$errors,"
118 report+="\"warnings\":$warnings,"
119 report+="\"stderr\":\"$stderr\"}"
121 echo "$report" > "$MONITOR"
127 if [ -e "$root_dir/etc/debian_version" ]; then
129 elif [ -e "$root_dir/etc/redhat-release" ]; then
131 elif [ -e "$root_dir/etc/slackware-version" ]; then
133 elif [ -e "$root_dir/etc/SuSE-release" ]; then
135 elif [ -e "$root_dir/etc/gentoo-release" ]; then
138 warn "Unknown base distro."
145 if [ -e "$root_dir/etc/debian_version" ]; then
147 if [ -e ${root_dir}/etc/lsb-release ]; then
148 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
149 if [ "x$ID" = "xUbuntu" ]; then
154 elif [ -e "$root_dir/etc/fedora-release" ]; then
156 elif [ -e "$root_dir/etc/centos-release" ]; then
158 elif [ -e "$root_dir/etc/redhat-release" ]; then
160 elif [ -e "$root_dir/etc/slackware-version" ]; then
162 elif [ -e "$root_dir/etc/SuSE-release" ]; then
164 elif [ -e "$root_dir/etc/gentoo-release" ]; then
167 warn "Unknown distro."
172 get_partition_table() {
174 # If the partition table is gpt then parted will raise an error if the
175 # secondary gpt is not it the end of the disk, and a warning that has to
176 # do with the "Last Usable LBA" entry in gpt.
177 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
178 log_error "Unable to read partition table for device \`${dev}'"
184 get_partition_table_type() {
187 local dev="$(sed -n 2p <<< "$ptable")"
189 IFS=':' read -ra field <<< "$dev"
194 get_partition_count() {
197 expr $(echo "$ptable" | wc -l) - 2
200 get_partition_by_num() {
204 grep "^$id:" <<< "$ptable"
207 get_last_partition() {
210 echo "$ptable" | tail -1
213 is_extended_partition() {
217 id=$($SFDISK --print-id "$dev" "$part_num")
218 if [ "$id" = "5" ]; then
225 get_extended_partition() {
227 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
229 tail -n +3 <<< "$ptable" | while read line; do
230 part_num=$(cut -d':' -f1 <<< "$line")
231 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
239 get_logical_partitions() {
242 tail -n +3 <<< "$ptable" | while read line; do
243 part_num=$(cut -d':' -f1 <<< "$line")
244 if [ $part_num -ge 5 ]; then
252 get_last_primary_partition() {
254 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
257 if output=$(grep "^$i:" <<< "$ptable"); then
265 get_partition_to_resize() {
268 table=$(get_partition_table "$dev")
270 if [ $(get_partition_count "$table") -eq 0 ]; then
274 table_type=$(get_partition_table_type "$table")
275 last_part=$(get_last_partition "$table")
276 last_part_num=$(cut -d: -f1 <<< "$last_part")
278 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
279 extended=$(get_extended_partition "$table")
280 last_primary=$(get_last_primary_partition "$table")
281 ext_num=$(cut -d: -f1 <<< "$extended")
282 prim_num=$(cut -d: -f1 <<< "$last_primary")
284 if [ "$ext_num" != "$last_prim_num" ]; then
285 echo "$last_prim_num"
287 echo "$last_part_num"
290 echo "$last_part_num"
300 IFS=":;" read -ra fields <<< "$part"
301 local id="${fields[0]}"
302 local start="${fields[1]}"
303 local end="${fields[2]}"
304 local size="${fields[3]}"
305 local fs="${fields[4]}"
306 local name="${fields[5]}"
307 local flags="${fields[6]//,/ }"
309 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
310 for flag in $flags; do
311 $PARTED -s -m $device set "$id" "$flag" on
315 enlarge_partition() {
321 if [ -z "$new_end" ]; then
322 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
326 IFS=":;" read -ra fields <<< "$part"
330 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
331 new_part="$new_part":"${fields[$i]}"
333 new_part=${new_part:1}
335 # If this is an extended partition, removing it will also remove the
336 # logical partitions it contains. We need to save them for later.
337 if [ "$ptype" = "extended" ]; then
338 local table="$(get_partition_table "$device")"
339 local logical="$(get_logical_partitions "$table")"
343 $PARTED -s -m "$device" rm "$id"
344 create_partition "$device" "$new_part" "$ptype"
346 if [ "$ptype" = "extended" ]; then
347 # Recreate logical partitions
348 echo "$logical" | while read logical_part; do
349 create_partition "$device" "$logical_part" "logical"
354 get_last_free_sector() {
358 if [ -n "$unit" ]; then
362 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
363 local ptype="$(cut -d: -f 5 <<< "$last_line")"
365 if [ "$ptype" = "free;" ]; then
371 # if something fails here, it shouldn't call cleanup again...
374 if [ ${#CLEANUP[*]} -gt 0 ]; then
375 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
376 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
377 for i in $REVERSE_INDEXES; do
378 # If something fails here, it's better to retry it for a few times
379 # before we give up with an error. This is needed for kpartx when
380 # dealing with ntfs partitions mounted through fuse. umount is not
381 # synchronous and may return while the partition is still busy. A
382 # premature attempt to delete partition mappings through kpartx on
383 # a device that hosts previously mounted ntfs partition may fail
384 # with a `device-mapper: remove ioctl failed: Device or resource
385 # busy' error. A sensible workaround for this is to wait for a
386 # while and then try again.
387 local cmd=${CLEANUP[$i]}
388 $cmd || for interval in 0.25 0.5 1 2 4; do
389 echo "Command $cmd failed!"
390 echo "I'll wait for $interval secs and will retry..."
394 if [ "$?" != "0" ]; then
405 if [ $rc -eq 0 ]; then
414 check_if_excluded() {
415 local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
416 local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
417 if [ -n "${!exclude}" ]; then
418 warn "Task $PROGNAME was excluded and will not run."
428 STDERR_FILE=$(mktemp)
429 add_cleanup rm -f "$STDERR_FILE"
430 exec 2> >(tee -a "$STDERR_FILE" >&2)
432 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :