~ubuntu-on-ec2/ubuntu-on-ec2/cloud-utils

« back to all changes in this revision

Viewing changes to uec-resize-image

  • Committer: Bazaar Package Importer
  • Author(s): Scott Moser
  • Date: 2010-02-05 18:37:57 UTC
  • Revision ID: james.westby@ubuntu.com-20100205183757-2w2dspl8v0a8irus
Tags: 0.1ubuntu1
Initial release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
# resize-uec-image
 
3
 
 
4
Usage() {
 
5
        cat <<EOF
 
6
Usage: ${0##*/} image size [output]
 
7
   Resize a UEC image to a new size.
 
8
   if output is given, do not modify 'image', but create new file 'output'
 
9
 
 
10
   New size is specified per resize2fs(8), e.g. "1G" for 1 gigabyte
 
11
EOF
 
12
        return 0
 
13
}
 
14
 
 
15
error() { echo "$@" 1>&2; }
 
16
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
 
17
 
 
18
human2bytes() {
 
19
   # converts size suitable for input to resize2fs to bytes
 
20
   # s:512 byte sectors, K:kilobytes, M:megabytes, G:gigabytes
 
21
   # none: block size of the image
 
22
   local input=${1} defunit=${2:-1024}
 
23
   local unit count;
 
24
   case "$input" in
 
25
      *s) count=${input%s}; unit=512;;
 
26
      *K) count=${input%K}; unit=1024;;
 
27
      *M) count=${input%M}; unit=$((1024*1024));;
 
28
      *G) count=${input%G}; unit=$((1024*1024*1024));;
 
29
      *)  count=${input}  ; unit=${2:-1024};;
 
30
   esac
 
31
   _RET=$((${count}*${unit}))
 
32
}
 
33
 
 
34
xtruncate() {
 
35
   if which truncate >/dev/null 2>&1; then
 
36
      truncate "${@}"
 
37
   else
 
38
      local size=${1} file=${2} blk=""
 
39
      size=${size#--size=}
 
40
      # this is a poor mans truncate supporting whatever human2bytes supports
 
41
      human2bytes "${size}" && blk=$((${_RET}/512)) &&
 
42
         dd if=/dev/zero of="${file}" obs=512 seek=${blk} count=0 2>/dev/null
 
43
   fi
 
44
}
 
45
 
 
46
[ "$1" = "-h" -o "$1" = "--help" ] && { Usage; exit 0; }
 
47
 
 
48
[ $# -eq 3 -o $# -eq 2 ] || { Usage 1>&2; exit 1; }
 
49
 
 
50
old="$1"
 
51
size="$2"
 
52
new="${3:-${old}}"
 
53
 
 
54
[ -f "${old}" ] || fail "${old}: does not exist"
 
55
 
 
56
human2bytes "${size}" && new_size=${_RET} ||
 
57
   fail "failed to understand ${size}"
 
58
 
 
59
if [ ! "${old}" -ef "${new}" ]; then
 
60
   file_out=$(file "${old}") || fail "failed to read ${old} with 'file'"
 
61
   case "${file_out}" in
 
62
      *gzip\ compressed*)
 
63
         file_out_z=$(file -z "${old}")
 
64
         case "${file_out_z}" in
 
65
            *tar\ archive*)
 
66
               : > "${new}" && newd=$(dirname "${new}") ||
 
67
                  fail "failed to get full path for ${new}"
 
68
               tmpd=$(mktemp -d "${newd}/.${0##*/}.XXXXXX") &&
 
69
                  ( cd "${tmpd}" && tar -S --wildcards -xzf - "*.img" &&
 
70
                    mv *.img "../${new}" ) < "${old}" || { 
 
71
                    rm -Rf "${tmpd}";
 
72
                    fail "failed to extract image from ${old}"
 
73
                  }
 
74
               rm -Rf "${tmpd}"
 
75
               ;;
 
76
            *)
 
77
               zcat -f "$old" | cp --sparse=always /dev/stdin "$new";;
 
78
         esac
 
79
         ;;
 
80
      *) cp --sparse=always "${old}" "${new}";;
 
81
   esac
 
82
   [ $? -eq 0 ] || fail "failed to cp ${old} -> ${new}"
 
83
else
 
84
   # if old=new (in place), it must be a simple image file
 
85
   case "${old}" in
 
86
      *.gz) fail "refusing work in place compressed or archive file: ${old}";;
 
87
   esac
 
88
fi
 
89
 
 
90
ls_out=$(ls -l "${new}") &&
 
91
   old_size=$(echo "${ls_out}" | awk '{print $5}') ||
 
92
   fail "failed to get size of ${new_img}"
 
93
 
 
94
e2fsck -fp "$new" || fail "failed to fsck ${new}"
 
95
 
 
96
if [ "${old_size}" -lt "${new_size}" ]; then
 
97
   xtruncate "--size=$size" "$new" || fail "failed to change size of ${new}"
 
98
fi
 
99
 
 
100
resize2fs "$new" "$size" || fail "failed to resize ${new} -> ${size}"
 
101
 
 
102
if [ "${old_size}" -gt "${new_size}" ]; then
 
103
   xtruncate "--size=$size" "$new" || fail "failed to change size of ${new}"
 
104
fi
 
105
 
 
106
exit 0