手动调整KVM镜像image的disk磁盘大小

声明

此文档只做学习交流使用,请勿用作其他商业用途
author : 朝阳_tony
E-mail : [email protected]
Create Date: 2016-8-15 10:24:05
Last Change: 2016-8-15 11:14:17
转载请注明出处:http://blog.csdn.net/linzhaolover

摘要

在云计算Iaas层中,客户对kvm中image的需求格式各样,我可能就做一个基础的image,然后在上面再安装各类package,以适应不同的业务需求;但是,每个人对image的磁盘分区大小,又有不同的需求,这就需要从外面更改磁盘配置才行,下面就简单介绍一下,我采用的方法,方便大家学习交流

准备image

openstack 支持的image比较多,有qcow2 还有raw,我的qcow2 ,所以image后缀是qcow2

glance image-download --file old_image.qcow2  image_uuid

查看磁盘分区

# qemu-img  info old_image.qcow2 
image: old_image.qcow2
file format: qcow2
virtual size: 20G (21474836480 bytes) ##虚拟磁盘大小是20G
disk size: 1.7G   ##当前实际文件大小1.7G
cluster_size: 65536
Format specific information:
    compat: 0.10

# virt-filesystems --long -h --all -a old_image.qcow2 
Name       Type        VFS      Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4     -      -    16G   -         #系统盘,也就是根目录
/dev/sda2  filesystem  unknown  -      -    1.0K  -
/dev/sda5  filesystem  swap     -      -    4.0G  -
/dev/sda1  partition   -        -      83   16G   /dev/sda  # 系统盘,一会我们扩增这个磁盘
/dev/sda2  partition   -        -      05   1.0K  /dev/sda
/dev/sda5  partition   -        -      82   4.0G  /dev/sda
/dev/sda   device      -        -      -    20G   -

创建新的空image

qemu-img create -f qcow2 newdisk.qcow2 100G
Formatting 'newdisk.qcow2', fmt=qcow2 size=107374182400 encryption=off cluster_size=65536 lazy_refcounts=off

进行磁盘扩展

注意,由于我的image磁盘只是简单分区,没有lvm分区,所以一个–expand 足矣

virt-resize --expand /dev/sda1 old_image.qcow2 newdisk.qcow
Examining old_image.qcow2 ...
**********

Summary of changes:

/dev/sda1: This partition will be resized from 16.0G to 96.0G.  The
    filesystem ext4 on /dev/sda1 will be expanded using the 'resize2fs'
    method.

    /dev/sda2: This partition will be left alone.

    **********
Setting up initial partition table on newdisk.qcow2 ...
Copying /dev/sda1 ...
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Copying /dev/sda2 ...
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
Expanding /dev/sda1 using the 'resize2fs' method ...

Resize operation completed with no errors.  Before deleting the old disk,
carefully check that the resized disk boots and works correctly.

# virt-filesystems --long -h --all -a newdisk.qcow2
Name       Type        VFS      Label  MBR  Size  Parent
/dev/sda1  filesystem  ext4     -      -    96G   -       #新的系统盘大小
/dev/sda2  filesystem  unknown  -      -    1.0K  -
/dev/sda5  filesystem  swap     -      -    4.0G  -
/dev/sda1  partition   -        -      83   96G   /dev/sda # 新系统盘大小
/dev/sda2  partition   -        -      05   1.0K  /dev/sda
/dev/sda5  partition   -        -      82   4.0G  /dev/sda
/dev/sda   device      -        -      -    100G  -

压缩新的image

查看新image大小

 qemu-img info newdisk.qcow2
image: newdisk.qcow2
file format: qcow2
virtual size: 100G (107374182400 bytes)
disk size: 5.7G  # 由1.7G变成5.7G ,这有点大
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

压缩转换image

由于我的image是qcow2的格式,在输出我依然想适用qcow2,所以在-O qcow2 时候输出qcow2.

qemu-img convert -c  -O qcow2 newdisk.qcow2 compress_newdisk.qcow2

qemu-img  info compress_newdisk.qcow2 
image: compress_newdisk.qcow2
file format: qcow2
virtual size: 100G (107374182400 bytes)
disk size: 575M  # 变小了不少,这样导入glance也是很方便使用
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false

* Reference : *
http://serverfault.com/questions/38456/online-resize-of-mounted-ext3-logical-volume
http://libguestfs.org/virt-resize.1.html

你可能感兴趣的:(openstack)