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
38 for arg; do cmd+=$(printf "%q " "$arg"); done
43 echo "ERROR: $@" | tee $RESULT >&2
48 echo "Warning: $@" >&2
54 if [ -e "$root_dir/etc/debian_version" ]; then
56 elif [ -e "$root_dir/etc/redhat-release" ]; then
58 elif [ -e "$root_dir/etc/slackware-version" ]; then
60 elif [ -e "$root_dir/etc/SuSE-release" ]; then
62 elif [ -e "$root_dir/etc/gentoo-release" ]; then
65 warn "Unknown base distro."
72 if [ -e "$root_dir/etc/debian_version" ]; then
74 if [ -e ${root_dir}/etc/lsb-release ]; then
75 ID=$(grep ^DISTRIB_ID= ${root_dir}/etc/lsb-release | cut -d= -f2)
76 if [ "x$ID" = "xUbuntu" ]; then
81 elif [ -e "$root_dir/etc/fedora-release" ]; then
83 elif [ -e "$root_dir/etc/centos-release" ]; then
85 elif [ -e "$root_dir/etc/redhat-release" ]; then
87 elif [ -e "$root_dir/etc/slackware-version" ]; then
89 elif [ -e "$root_dir/etc/SuSE-release" ]; then
91 elif [ -e "$root_dir/etc/gentoo-release" ]; then
94 warn "Unknown distro."
99 unit=$(tr [a-z] [A-Z] <<< $1)
107 *) log_error "Unknown unit type: \`$1'";;
111 get_last_partition_id() {
113 if ! output="$("$PARTED" -s -m "$dev" print)"; then
114 log_error "Unable to read partition table for device \`${dev}'"
117 last_line=$(tail -1 <<< "$output")
119 echo $(cut -d: -f1 <<< "$last_line")
122 get_partition_table() {
124 if ! output="$("$PARTED" -s -m "$dev" unit s print)"; then
125 log_error "Unable to read partition table for device \`${dev}'"
131 get_partition_table_type() {
134 local dev="$(sed -n 2p <<< "$ptable")"
136 IFS=':' read -ra field <<< "$dev"
141 get_partition_count() {
144 expr $(echo "$ptable" | wc -l) - 2
147 get_partition_by_id() {
151 grep "^$id:" <<< "$ptable"
154 get_last_partition() {
157 echo "$ptable" | tail -1
160 is_extended_partition() {
164 id=$($SFDISK --print-id "$dev" "$part_num")
165 if [ "$id" = "5" ]; then
172 get_extended_partition() {
174 local dev="$(echo "$ptable" | sed -n 2p | cut -d':' -f1)"
176 tail -n +3 <<< "$ptable" | while read line; do
177 part_num=$(cut -d':' -f1 <<< "$line")
178 if [ $(is_extended_partition "$dev" "$part_num") == "yes" ]; then
186 get_logical_partitions() {
189 tail -n +3 <<< "$ptable" | while read line; do
190 part_num=$(cut -d':' -f1 <<< "$line")
191 if [ $part_num -ge 5 ]; then
199 get_last_primary_partition() {
201 local dev=$(echo "ptable" | sed -n 2p | cut -d':' -f1)
204 if output=$(grep "^$i:" <<< "$ptable"); then
218 IFS=":;" read -ra fields <<< "$part"
219 local id="${fields[0]}"
220 local start="${fields[1]}"
221 local end="${fields[2]}"
222 local size="${fields[3]}"
223 local fs="${fields[4]}"
224 local name="${fields[5]}"
225 local flags="${fields[6]//,/ }"
227 $PARTED -s -m -- $device mkpart "$ptype" $fs "$start" "$end"
228 for flag in $flags; do
229 $PARTED -s -m $device set "$id" "$flag" on
233 enlarge_partition() {
239 if [ -z "$new_end" ]; then
240 new_end=$(cut -d: -f 3 <<< "$(get_last_free_sector "$device")")
244 IFS=":;" read -ra fields <<< "$part"
248 for ((i = 0; i < ${#fields[*]}; i = i + 1)); do
249 new_part="$new_part":"${fields[$i]}"
251 new_part=${new_part:1}
253 # If this is an extended partition, removing it will also remove the
254 # logical partitions it contains. We need to save them for later.
255 if [ "$ptype" = "extended" ]; then
256 local table="$(get_partition_table "$device")"
257 local logical="$(get_logical_partitions "$table")"
261 $PARTED -s -m "$device" rm "$id"
262 create_partition "$device" "$new_part" "$ptype"
264 if [ "$ptype" = "extended" ]; then
265 # Recreate logical partitions
266 echo "$logical" | while read logical_part; do
267 create_partition "$device" "$logical_part" "logical"
272 get_last_free_sector() {
276 if [ -n "$unit" ]; then
280 local last_line="$("$PARTED" -s -m "$dev" "$unit" print free | tail -1)"
281 local ptype="$(cut -d: -f 5 <<< "$last_line")"
283 if [ "$ptype" = "free;" ]; then
289 # if something fails here, it shouldn't call cleanup again...
292 if [ ${#CLEANUP[*]} -gt 0 ]; then
293 LAST_ELEMENT=$((${#CLEANUP[*]}-1))
294 REVERSE_INDEXES=$(seq ${LAST_ELEMENT} -1 0)
295 for i in $REVERSE_INDEXES; do
296 # If something fails here, it's better to retry it for a few times
297 # before we give up with an error. This is needed for kpartx when
298 # dealing with ntfs partitions mounted through fuse. umount is not
299 # synchronous and may return while the partition is still busy. A
300 # premature attempt to delete partition mappings through kpartx on
301 # a device that hosts previously mounted ntfs partition may fail
302 # with a `device-mapper: remove ioctl failed: Device or resource
303 # busy' error. A sensible workaround for this is to wait for a
304 # while and then try again.
305 local cmd=${CLEANUP[$i]}
306 $cmd || for interval in 0.25 0.5 1 2 4; do
307 echo "Command $cmd failed!"
308 echo "I'll wait for $interval secs and will retry..."
312 if [ "$?" != "0" ]; then
320 check_if_excluded() {
322 local exclude=SNF_IMAGE_PROPERTY_EXCLUDE_TASK_${PROGNAME:2}
323 if [ -n "${!exclude}" ]; then
324 warn "Task $PROGNAME was excluded and will not run."
334 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :