~rcj/maas-images/ppc64el

« back to all changes in this revision

Viewing changes to bin/maas-cloudimg2eph2

  • Committer: Robert Jennings
  • Date: 2014-12-04 13:56:57 UTC
  • Revision ID: robert.jennings@canonical.com-20141204135657-we3tt4glmu9c8m04
Resize image to 2G prior to customization (lp#1396803)

Package installation into the image was failing due to insufficient space.
Increasing this to 2G provides enough room to customize the base image.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
VERBOSITY=1
4
4
TEMP_D=""
5
5
 
 
6
info() { echo "$@" 1>&2; }
6
7
error() { echo "$@" 1>&2; }
7
8
fail() { [ $# -eq 0 ] || error "$@"; exit 1; }
8
9
 
105
106
    return 0
106
107
}
107
108
 
 
109
# Grow the image container and the filesystem within to the specified # of MB
 
110
# Requirements:
 
111
#  The image container must be raw format.
 
112
#  The image container has no partitioning.
 
113
#  The filesystem in the container must be ext2/3/4.
 
114
grow_img() {
 
115
    local img="$1" size_MB="$2"
 
116
 
 
117
    # Check that this is a raw image
 
118
    local img_fmt=$(qemu-img info --output=json ${img} | \
 
119
        python -c 'import json,sys;obj=json.load(sys.stdin);print obj["format"];')
 
120
    if [ "${img_fmt}" != "raw" ] ; then
 
121
        error "Expected 'raw' image format, found '${img_fmt}'.  Can not resize"
 
122
        return 1
 
123
    fi
 
124
 
 
125
    # Get current size of image
 
126
    local img_bytes=$(qemu-img info --output=json ${img} | \
 
127
        python -c 'import json,sys;obj=json.load(sys.stdin);print obj["virtual-size"];')
 
128
 
 
129
    size_bytes=$(($size_MB * 1024 * 1024))
 
130
    if [ ${size_bytes} -lt ${img_bytes} ] ; then
 
131
        info "Will not shrink image ${img} from ${img_bytes} to ${size_bytes}"
 
132
        return 0
 
133
    fi
 
134
 
 
135
    info "Resizing image container to ${size_MB}MB"
 
136
    qemu-img resize ${img} ${size_MB}M
 
137
 
 
138
    info "Attaching loop device to image"
 
139
    LODEV=$(losetup --find --show ${img}) ||
 
140
        { error "failed to expand image to ${size_MB}M"; return 1; }
 
141
    [ -z "${LODEV}" ] && { error "could not setup loop device"; return 1; }
 
142
 
 
143
    info "Checking image filesystem"
 
144
    e2fsck -f -y ${LODEV} || { error "fsck failed for image"; return 1; }
 
145
    info "Resizing image filesystem to fill container"
 
146
    resize2fs ${LODEV} || { error "resize2fs failed on image"; return 1; }
 
147
 
 
148
    info "Detaching loop device for image"
 
149
    losetup --detach ${LODEV} || { error "loop detach failure"; return 1; }
 
150
}
 
151
 
108
152
main() {
109
153
    local short_opts="a:hk:p:v"
110
154
    local long_opts="arch:,help,kernel:,krd-pack:,no-gzip,verbose"
180
224
    ensure_file_d "$output" ||
181
225
        { error "failed to create dir for $output"; return 1; }
182
226
 
 
227
    # Resize to 2G
 
228
    grow_img ${imgfile} 2048
 
229
    
183
230
    # now imgfile has the root filesystem image to adjust
184
231
    local pkout="${TEMP_D}/$kpkg-kernel" piout="${TEMP_D}/$kpkg-initrd"
185
232
    local manif="${TEMP_D}/manifest"