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
37 for arg; do cmd+=$(printf "%q " "$arg"); done
42 echo "ERROR: $@" | tee $RESULT >&2
47 echo "Warning: $@" >&2
53 if [ -e "$root_dir/etc/debian_version" ]; then
55 elif [ -e "$root_dir/etc/redhat-release" ]; then
57 elif [ -e "$root_dir/etc/slackware-version" ]; then
59 elif [ -e "$root_dir/SuSE-release" ]; then
61 elif [ -e "$root_dir/gentoo-release" ]; then
69 if [ -e "$root_dir/etc/debian_version" ]; then
71 if [ -e ${root_dir}/etc/lsb-release ]; then
72 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
73 if [ "x$ID" = "xUbuntu" ]; then
78 elif [ -e "$root_dir/etc/fedora-release" ]; then
80 elif [ -e "$root_dir/etc/centos-release" ]; then
82 elif [ -e "$root_dir/etc/redhat-release" ]; then
84 elif [ -e "$root_dir/etc/slackware-version" ]; then
86 elif [ -e "$root_dir/SuSE-release" ]; then
88 elif [ -e "$root_dir/gentoo-release" ]; then
93 get_last_partition() {
96 "$PARTED" -s -m "$dev" unit s print | tail -1
103 "$PARTED" -s -m "$dev" unit s print | grep "^$id"
106 get_partition_count() {
109 expr $("$PARTED" -s -m "$dev" unit s print | wc -l) - 2
112 get_last_free_sector() {
114 local last_line=$("$PARTED" -s -m "$dev" unit s print free | tail -1)
115 local type=$(echo "$last_line" | cut -d: -f 5)
117 if [ "$type" = "free;" ]; then
118 echo "$last_line" | cut -d: -f 3
123 # if something fails here, it shouldn't call cleanup again...
126 if [ ${#CLEANUP[*]} -gt 0 ]; then
127 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
128 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
129 for i in $REVERSE_INDEXES; do
130 # If something fails here, it's better to retry it for a few times
131 # before we give up with an error. This is needed for kpartx when
132 # dealing with ntfs partitions mounted through fuse. umount is not
133 # synchronous and may return while the partition is still busy. A
134 # premature attempt to delete partition mappings through kpartx on a
135 # device that hosts previously mounted ntfs partition may fail with
136 # a `device-mapper: remove ioctl failed: Device or resource busy'
137 # error. A sensible workaround for this is to wait for a while and
139 local cmd=${CLEANUP[$i]}
140 $cmd || for interval in 0.25 0.5 1 2 4; do
141 echo "Command $cmd failed!"
142 echo "I'll wait for $interval secs and will retry..."
146 if [ "$?" != "0" ]; then
154 check_if_excluded() {
156 local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
157 if [ -n "${!exclude}" ]; then
158 warn "Task $PROGNAME was excluded and will not run."
167 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :