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
21 PROGNAME=$(basename $0)
23 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
36 for arg; do cmd+=$(printf "%q " "$arg"); done
41 echo "ERROR: $@" | tee $RESULT >&2
46 echo "Warning: $@" >&2
52 if [ -e "$root_dir/etc/debian_version" ]; then
54 elif [ -e "$root_dir/etc/redhat-release" ]; then
56 elif [ -e "$root_dir/etc/slackware-version" ]; then
58 elif [ -e "$root_dir/SuSE-release" ]; then
60 elif [ -e "$root_dir/gentoo-release" ]; then
68 if [ -e "$root_dir/etc/debian_version" ]; then
70 if [ -e ${root_dir}/etc/lsb-release ]; then
71 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
72 if [ "x$ID" = "xUbuntu" ]; then
77 elif [ -e "$root_dir/etc/fedora-release" ]; then
79 elif [ -e "$root_dir/etc/centos-release" ]; then
81 elif [ -e "$root_dir/etc/redhat-release" ]; then
83 elif [ -e "$root_dir/etc/slackware-version" ]; then
85 elif [ -e "$root_dir/SuSE-release" ]; then
87 elif [ -e "$root_dir/gentoo-release" ]; then
92 get_last_partition() {
95 "$PARTED" -s -m "$dev" unit s print | tail -1
102 "$PARTED" -s -m "$dev" unit s print | grep "^$id"
105 get_partition_count() {
108 expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
111 get_last_free_sector() {
113 local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
114 local type=$(echo "$last_line" | cut -d: -f 5)
116 if [ "$type" = "free;" ]; then
117 echo "$last_line" | cut -d: -f 3
122 # if something fails here, it shouldn't call cleanup again...
125 if [ ${#CLEANUP[*]} -gt 0 ]; then
126 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
127 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
128 for i in $REVERSE_INDEXES; do
129 # If something fails here, it's better to retry it for a few times
130 # before we give up with an error. This is needed for kpartx when
131 # dealing with ntfs partitions mounted through fuse. umount is not
132 # synchronous and may return while the partition is still busy. A
133 # premature attempt to delete partition mappings through kpartx on a
134 # device that hosts previously mounted ntfs partition may fail with
135 # a `device-mapper: remove ioctl failed: Device or resource busy'
136 # error. A sensible workaround for this is to wait for a while and
138 local cmd=${CLEANUP[$i]}
139 $cmd || for interval in 0.25 0.5 1 2 4; do
140 echo "Command $cmd failed!"
141 echo "I'll wait for $interval secs and will retry..."
145 if [ "$?" != "0" ]; then
154 check_if_excluded() {
156 test "$PROGNAME" = "snf-image-helper" && return 0
158 eval local do_exclude=\$SNF_IMAGE_PROPERTY_EXCLUDE_${PROGNAME:2}_TASK
159 if [ -n "$do_exclude" ]; then
160 warn "Task $PROGNAME was excluded and will not run."
169 # Check if the execution of a task should be ommited
172 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :