最新消息:

修改 qcow2 镜像大小

qcow2 admin 7858浏览 0评论

思路

修改 qcow2 镜像可以分为两类:扩大和缩小。

扩大的操作思路是:先扩大镜像(磁盘),后修改分区表扩大分区信息。整个过程如下图所示,第一步先把镜像整个容量扩大,第二步把 sda2 扩大,把余下的空间填满。
缩小的操作思路和扩大相反:先修改分区表缩小分区信息,后缩小镜像(硬盘)。

操作过程

扩大

现在把一个 120G 的镜像扩大到 200G,其中多出来的 80G 全部划到 sda3 中。

首先查看镜像的大小和分区表:

 $ qemu-img info image1.qcow2
image: image1.qcow2
file format: qcow2
virtual size: 120G (128849018880 bytes)
disk size: 2.0G
cluster_size: 65536

$ virt-df image1.qcow2 
Filesystem                           1K-blocks       Used  Available  Use%
image1.qcow2:/dev/sda1                  194241      25578     158423   14%
image1.qcow2:/dev/sda3               119391468    1824776  111495244    2%

第一步:创建空镜像

  1. 先创建出一个 200G 的空镜像:
$ qemu-img create -f qcow2 image2.qcow2 200G
Formatting 'image2.qcow2', fmt=qcow2 size=214748364800 encryption=off cluster_size=65536 

第二步:virt-resize

  1. 将老镜像扩大到 200G,并把数据复制到新镜像中:
$ virt-resize --expand /dev/sda3 image1.qcow2 image2.qcow2
Examining image1.qcow2 ...
**********

Summary of changes:

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

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

/dev/sda3: This partition will be resized from 115.8G to 195.8G.  The
    filesystem ext4 on /dev/sda3 will be expanded using the 'resize2fs'
    method.

**********
Setting up initial partition table on image2.qcow2 ...
Copying /dev/sda1 ...
Copying /dev/sda2 ...
100%
Copying /dev/sda3 ...
100%
Expanding /dev/sda3 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.

如果镜像很大用时会很长,要有心里准备。。

完成后效果:

$ qemu-img info image2.qcow2
image: image2.qcow2
file format: qcow2
virtual size: 200G (214748364800 bytes)
disk size: 121G
cluster_size: 65536

$ virt-df image2.qcow2
Filesystem                           1K-blocks       Used  Available  Use%
image2.qcow2:/dev/sda1                  194241      25578     158423   14%
image2.qcow2:/dev/sda3               201960128    1824476  189869968    1%

缩小

现在把一个 120G 的镜像缩小到 100G。

基本信息:

$ virt-df Centos65v10.qcow2 
Filesystem                           1K-blocks       Used  Available  Use%
Centos65v10.qcow2:/dev/sda1             194241      29758     154243   16%
Centos65v10.qcow2:/dev/sda3          119391468    2103976  111216044    2%

$ qemu-img info Centos65v10.qcow2
image: Centos65v10.qcow2
file format: qcow2
virtual size: 120G (128849018880 bytes)
disk size: 2.2G
cluster_size: 65536

第一步:缩小分区表

注意:这步会修改镜像内容,请事先备份镜像。

guestfish -a Centos65v10.qcow2
><fs> launch
><fs> list-partitions 
/dev/sda1
/dev/sda2
/dev/sda3
><fs> resize2fs-size /dev/sda3 100G
><fs> sync
><fs> exit

此时分区表已经被修改:

$ virt-df Centos65v10.qcow2
Filesystem                           1K-blocks       Used  Available  Use%
Centos65v10.qcow2:/dev/sda1             194241      29758     154243   16%
Centos65v10.qcow2:/dev/sda3          103081248    2104036   95734336    3%

第二步:缩小镜像

为了安全起见多留了一个空间,所以镜像总大小是 105G。

$ qemu-img create -f qcow2 Centos65_small.qcow2 105G

$ virt-resize --shrink /dev/sda3 Centos65v10.qcow2 Centos65_small.qcow2 
Examining Centos65v10.qcow2 ...
**********

Summary of changes:

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

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

/dev/sda3: This partition will be resized from 115.8G to 98.8G.  The 
    filesystem ext4 on /dev/sda3 will be expanded using the 'resize2fs' 
    method.

**********
Setting up initial partition table on Centos65_small.qcow2 ...
Copying /dev/sda1 ...
Copying /dev/sda2 ...
……

继续是等待……

参考文档

  • https://rwmj.wordpress.com/2010/09/27/virt-resize-shrink-now-works/
  • http://libguestfs.org/virt-resize.1.html

转载请注明:爱开源 » 修改 qcow2 镜像大小

您必须 登录 才能发表评论!