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_TASK_START="TASK_START"
44 MSG_TYPE_TASK_END="TASK_END"
50 for arg; do cmd+=$(printf "%q " "$arg"); done
56 echo "ERROR: $@" | tee $RESULT >&2
61 echo "Warning: $@" >&2
62 echo "WARNING:$@" > "$MONITOR"
66 echo "$MSG_TYPE_TASK_START:${PROGNAME:2}" > "$MONITOR"
70 echo "$MSG_TYPE_TASK_END:${PROGNAME:2}" > "$MONITOR"
74 if [ ${#ERRORS[*]} -eq 0 ]; then
75 # No error message. Print stderr
76 local lines=$(tail --lines=${STDERR_LINE_SIZE} "$STDERR_FILE" | wc -l)
77 echo -n "STDERR:${lines}:" > "$MONITOR"
78 tail --lines=$lines "$STDERR_FILE" > "$MONITOR"
80 echo -n "ERROR:" > "$MONITOR"
81 for line in "${ERRORS[@]}"; do
82 echo "$line" > "$MONITOR"
90 if [ -e "$root_dir/etc/debian_version" ]; then
92 elif [ -e "$root_dir/etc/redhat-release" ]; then
94 elif [ -e "$root_dir/etc/slackware-version" ]; then
96 elif [ -e "$root_dir/etc/SuSE-release" ]; then
98 elif [ -e "$root_dir/etc/gentoo-release" ]; then
101 warn "Unknown base distro."
108 if [ -e "$root_dir/etc/debian_version" ]; then
110 if [ -e ${root_dir}/etc/lsb-release ]; then
111 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
112 if [ "x$ID" = "xUbuntu" ]; then
117 elif [ -e "$root_dir/etc/fedora-release" ]; then
119 elif [ -e "$root_dir/etc/centos-release" ]; then
121 elif [ -e "$root_dir/etc/redhat-release" ]; then
123 elif [ -e "$root_dir/etc/slackware-version" ]; then
125 elif [ -e "$root_dir/etc/SuSE-release" ]; then
127 elif [ -e "$root_dir/etc/gentoo-release" ]; then
130 warn "Unknown distro."
135 get_partition_table() {
137 # If the partition table is gpt then parted will raise an error if the
138 # secondary gpt is not it the end of the disk, and a warning that has to
139 # do with the "Last Usable LBA" entry in gpt.
140 if ! output="$("$PARTED" -s -m "$dev" unit s print | grep -E -v "^(Warning|Error): ")"; then
141 log_error "Unable to read partition table for device \`${dev}'"
147 get_partition_table_type() {
150 local dev="$(sed -n 2p <<< "$ptable")"
152 IFS=':' read -ra field <<< "$dev"
157 get_partition_count() {
160 expr $(echo "$ptable" | wc -l) - 2
163 get_partition_by_num() {
167 grep "^$id:" <<< "$ptable"
170 get_last_partition() {
173 echo "$ptable" | tail -1
176 is_extended_partition() {
180 id=$($SFDISK --print-id "$dev" "$part_num")
181 if [ "$id" = "5" ]; then
188 get_extended_partition() {
190 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
192 tail -n +3 <<< "$ptable" | while read line; do
193 part_num=$(cut -d':' -f1 <<< "$line")
194 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
202 get_logical_partitions() {
205 tail -n +3 <<< "$ptable" | while read line; do
206 part_num=$(cut -d':' -f1 <<< "$line")
207 if [ $part_num -ge 5 ]; then
215 get_last_primary_partition() {
217 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
220 if output=$(grep "^$i:" <<< "$ptable"); then
228 get_partition_to_resize() {
231 table=$(get_partition_table "$dev")
233 if [ $(get_partition_count "$table") -eq 0 ]; then
237 table_type=$(get_partition_table_type "$table")
238 last_part=$(get_last_partition "$table")
239 last_part_num=$(cut -d: -f1 <<< "$last_part")
241 if [ "$table_type" == "msdos" -a $last_part_num -gt 4 ]; then
242 extended=$(get_extended_partition "$table")
243 last_primary=$(get_last_primary_partition "$table")
244 ext_num=$(cut -d: -f1 <<< "$extended")
245 prim_num=$(cut -d: -f1 <<< "$last_primary")
247 if [ "$ext_num" != "$last_prim_num" ]; then
248 echo "$last_prim_num"
250 echo "$last_part_num"
253 echo "$last_part_num"
263 IFS=":;" read -ra fields <<< "$part"
264 local id="${fields[0]}"
265 local start="${fields[1]}"
266 local end="${fields[2]}"
267 local size="${fields[3]}"
268 local fs="${fields[4]}"
269 local name="${fields[5]}"
270 local flags="${fields[6]//,/ }"
272 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
273 for flag in $flags; do
274 $PARTED -s -m $device set "$id" "$flag" on
278 enlarge_partition() {
284 if [ -z "$new_end" ]; then
285 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
289 IFS=":;" read -ra fields <<< "$part"
293 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
294 new_part="$new_part":"${fields[$i]}"
296 new_part=${new_part:1}
298 # If this is an extended partition, removing it will also remove the
299 # logical partitions it contains. We need to save them for later.
300 if [ "$ptype" = "extended" ]; then
301 local table="$(get_partition_table "$device")"
302 local logical="$(get_logical_partitions "$table")"
306 $PARTED -s -m "$device" rm "$id"
307 create_partition "$device" "$new_part" "$ptype"
309 if [ "$ptype" = "extended" ]; then
310 # Recreate logical partitions
311 echo "$logical" | while read logical_part; do
312 create_partition "$device" "$logical_part" "logical"
317 get_last_free_sector() {
321 if [ -n "$unit" ]; then
325 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
326 local ptype="$(cut -d: -f 5 <<< "$last_line")"
328 if [ "$ptype" = "free;" ]; then
334 # if something fails here, it shouldn't call cleanup again...
337 if [ ${#CLEANUP[*]} -gt 0 ]; then
338 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
339 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
340 for i in $REVERSE_INDEXES; do
341 # If something fails here, it's better to retry it for a few times
342 # before we give up with an error. This is needed for kpartx when
343 # dealing with ntfs partitions mounted through fuse. umount is not
344 # synchronous and may return while the partition is still busy. A
345 # premature attempt to delete partition mappings through kpartx on
346 # a device that hosts previously mounted ntfs partition may fail
347 # with a `device-mapper: remove ioctl failed: Device or resource
348 # busy' error. A sensible workaround for this is to wait for a
349 # while and then try again.
350 local cmd=${CLEANUP[$i]}
351 $cmd || for interval in 0.25 0.5 1 2 4; do
352 echo "Command $cmd failed!"
353 echo "I'll wait for $interval secs and will retry..."
357 if [ "$?" != "0" ]; then
368 if [ $rc -eq 0 ]; then
377 check_if_excluded() {
378 local name="$(tr [a-z] [A-Z] <<< ${PROGNAME:2})"
379 local exclude="SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${name}"
380 if [ -n "${!exclude}" ]; then
381 warn "Task ${PROGNAME:2} was excluded and will not run."
391 STDERR_FILE=$(mktemp)
392 add_cleanup rm -f "$STDERR_FILE"
393 exec 2> >(tee -a "$STDERR_FILE" >&2)
395 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :