| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# cloud-resize-image - resize a cloud image |
| 4 |
# |
| 5 |
# Copyright (C) 2010 Canonical Ltd. |
| 6 |
# |
| 7 |
# Authors: Scott Moser <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, version 3 of the License. |
| 12 |
# |
| 13 |
# This program is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU General Public License |
| 19 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
|
| 22 |
Usage() { |
| 23 |
cat <<EOF |
| 24 |
Usage: ${0##*/} [ options ] image size [output] |
| 25 |
Resize a partition image and contained filesystem to a new size. |
| 26 |
if output is given, do not modify 'image', but create new file 'output' |
| 27 |
|
| 28 |
New size is specified per resize2fs(8), e.g. "1G" for 1 gigabyte |
| 29 |
|
| 30 |
options: |
| 31 |
-v | --verbose show command output |
| 32 |
EOF |
| 33 |
return 0 |
| 34 |
} |
| 35 |
|
| 36 |
error() { echo "$@" 1>&2; } |
| 37 |
fail() { [ $# -eq 0 ] || error "$@"; exit 1; } |
| 38 |
|
| 39 |
human2bytes() { |
| 40 |
# converts size suitable for input to resize2fs to bytes |
| 41 |
# s:512 byte sectors, K:kilobytes, M:megabytes, G:gigabytes |
| 42 |
# none: block size of the image |
| 43 |
local input=${1} defunit=${2:-1024} |
| 44 |
local unit count; |
| 45 |
case "$input" in |
| 46 |
*s) count=${input%s}; unit=512;; |
| 47 |
*K) count=${input%K}; unit=1024;; |
| 48 |
*M) count=${input%M}; unit=$((1024*1024));; |
| 49 |
*G) count=${input%G}; unit=$((1024*1024*1024));; |
| 50 |
*) count=${input} ; unit=${2:-1024};; |
| 51 |
esac |
| 52 |
_RET=$((${count}*${unit})) |
| 53 |
} |
| 54 |
|
| 55 |
xtruncate() { |
| 56 |
if which truncate >/dev/null 2>&1; then |
| 57 |
truncate "${@}" |
| 58 |
else |
| 59 |
local size=${1} file=${2} blk="" |
| 60 |
size=${size#-s } |
| 61 |
# this is a poor mans truncate supporting whatever human2bytes supports |
| 62 |
human2bytes "${size}" && blk=$((${_RET}/512)) && |
| 63 |
dd if=/dev/zero of="${file}" obs=512 seek=${blk} count=0 2>/dev/null |
| 64 |
fi |
| 65 |
} |
| 66 |
|
| 67 |
runcmd() { |
| 68 |
local output=$1 |
| 69 |
shift; |
| 70 |
if [ "$output" = "0" ]; then |
| 71 |
local out="" ret=0; |
| 72 |
out=$("${@}" 2>&1) || { ret=$?; error "${out}"; return $ret; } |
| 73 |
else |
| 74 |
"$@" |
| 75 |
fi |
| 76 |
} |
| 77 |
|
| 78 |
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; } |
| 79 |
|
| 80 |
verbose=0 |
| 81 |
[ "$1" = "-v" -o "$1" = "--verbose" ] && |
| 82 |
{ verbose=1; shift; } |
| 83 |
|
| 84 |
[ "${CLOUD_UTILS_WARN_RESIZE:-0}" = "0" ] && _n="${0##*/}" && |
| 85 |
[ "${_n#uec}" != "${_n}" ] && export CLOUD_UTILS_WARN_RESIZE=1 && |
| 86 |
error "WARNING: uec-resize-image is now 'resize-part-image'. Please update your tools or docs." |
| 87 |
|
| 88 |
[ $# -eq 3 -o $# -eq 2 ] || { Usage 1>&2; exit 1; } |
| 89 |
|
| 90 |
old="$1" |
| 91 |
size="$2" |
| 92 |
new="${3:-${old}}" |
| 93 |
|
| 94 |
[ -f "${old}" ] || fail "${old}: does not exist" |
| 95 |
|
| 96 |
human2bytes "${size}" && new_size=${_RET} || |
| 97 |
fail "failed to understand ${size}" |
| 98 |
|
| 99 |
if [ ! "${old}" -ef "${new}" ]; then |
| 100 |
file_out=$(file "${old}") || fail "failed to read ${old} with 'file'" |
| 101 |
case "${file_out}" in |
| 102 |
*gzip\ compressed*) |
| 103 |
file_out_z=$(file -z "${old}") |
| 104 |
case "${file_out_z}" in |
| 105 |
*tar\ archive*) |
| 106 |
: > "${new}" && newd=$(dirname "${new}") || |
| 107 |
fail "failed to get full path for ${new}" |
| 108 |
tmpd=$(mktemp -d "${newd}/.${0##*/}.XXXXXX") && |
| 109 |
( cd "${tmpd}" && tar -S --wildcards -xzf - "*.img" && |
| 110 |
mv *.img "../${new}" ) < "${old}" || { |
| 111 |
rm -Rf "${tmpd}"; |
| 112 |
fail "failed to extract image from ${old}" |
| 113 |
} |
| 114 |
rm -Rf "${tmpd}" |
| 115 |
;; |
| 116 |
*) |
| 117 |
zcat -f "$old" | cp --sparse=always /dev/stdin "$new";; |
| 118 |
esac |
| 119 |
;; |
| 120 |
*) cp --sparse=always "${old}" "${new}";; |
| 121 |
esac |
| 122 |
[ $? -eq 0 ] || fail "failed to cp ${old} -> ${new}" |
| 123 |
else |
| 124 |
# if old=new (in place), it must be a simple image file |
| 125 |
case "${old}" in |
| 126 |
*.gz) fail "refusing work in place compressed or archive file: ${old}";; |
| 127 |
esac |
| 128 |
fi |
| 129 |
|
| 130 |
ls_out=$(ls -l "${new}") && |
| 131 |
old_size=$(echo "${ls_out}" | awk '{print $5}') || |
| 132 |
fail "failed to get size of ${new_img}" |
| 133 |
|
| 134 |
runcmd "${verbose}" e2fsck -fp "${new}" || |
| 135 |
fail "failed to fsck ${new}" |
| 136 |
|
| 137 |
if [ "${old_size}" -lt "${new_size}" ]; then |
| 138 |
xtruncate "-s $size" "$new" || fail "failed to change size of ${new}" |
| 139 |
fi |
| 140 |
|
| 141 |
runcmd "${verbose}" resize2fs "$new" "$size" || |
| 142 |
fail "failed to resize ${new} -> ${size}" |
| 143 |
|
| 144 |
if [ "${old_size}" -gt "${new_size}" ]; then |
| 145 |
xtruncate "-s $size" "$new" || fail "failed to change size of ${new}" |
| 146 |
fi |
| 147 |
|
| 148 |
echo "resized ${new} to ${size}" |
| 149 |
|
| 150 |
exit 0 |
| 151 |
|
| 152 |
# vi: ts=4 noexpandtab |