2018年3月31日星期六

How to resize a qcow2 virtual disk image

Not sure why I haven't written this down, since I did it countless times back in my earliest days with IBM in 2010 or 2011. Today I need to do it again, growing a 20GB qcow2 disk image to, say, 80GB. Here is how, with self explanatory comments.

root@fabv1:/var/lib/libvirt/images# virsh list --all | grep tony1                                # make sure the virtual machine has been shut down
 -     tony1                          shut off
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# mv tony1.qcow2 tony1.small.qcow2                             # we don't want to accidentally ruin any data
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# qemu-img convert -p -O raw tony1.small.qcow2 tony1.large.raw # parted and resize2fs can only operate on a raw disk
    (100.00/100%)
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# qemu-img info tony1.large.raw
image: tony1.large.raw
file format: raw
virtual size: 20G (21474836480 bytes)
disk size: 20G
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# qemu-img resize tony1.large.raw 80G                          # first enlarge the virtual disk size
Image resized.
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# qemu-img info tony1.large.raw
image: tony1.large.raw
file format: raw
virtual size: 80G (85899345920 bytes)
disk size: 20G
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# parted tony1.large.raw -- print
Model:  (file)
Disk /var/lib/libvirt/images/tony1.large.raw: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  21.5GB  21.5GB  primary  ext4         boot

root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# parted tony1.large.raw -- resizepart 1 -1                    # and then modify the partition table
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# parted tony1.large.raw -- print
Model:  (file)
Disk /var/lib/libvirt/images/tony1.large.raw: 85.9GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  85.9GB  85.9GB  primary  ext4         boot

root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# kpartx -av tony1.large.raw
add map loop0p1 (252:0): 0 167768160 linear /dev/loop0 2048
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# e2fsck -f /dev/mapper/loop0p1                                # a must before resize2fs
e2fsck 1.42.9 (4-Feb-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/loop0p1: 636755/1310720 files (0.4% non-contiguous), 3724312/5242368 blocks
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# resize2fs -p /dev/mapper/loop0p1                             # finally resize the filesystem
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/mapper/loop0p1 to 20971020 (4k) blocks.
The filesystem on /dev/mapper/loop0p1 is now 20971020 blocks long.

root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# kpartx -d tony1.large.raw
loop deleted : /dev/loop0
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# losetup -a                                                   # run "losetup -d /dev/loop0" if it shows in the output
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# qemu-img convert -p -O qcow2 tony1.large.raw tony1.qcow2
    (100.00/100%)
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# ll | grep tony1
-rw-r--r-- 1 root         root  85899345920 Apr  1 02:00 tony1.large.raw
-rw-r--r-- 1 root         root  21268922368 Apr  1 02:03 tony1.qcow2
-rw-r--r-- 1 libvirt-qemu kvm   21319450624 Mar 31 00:18 tony1.small.qcow2
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# chown libvirt-qemu:kvm tony1.qcow2                           # we've been manipulating the files as root so far
root@fabv1:/var/lib/libvirt/images#
root@fabv1:/var/lib/libvirt/images# ll | grep tony1
-rw-r--r-- 1 root         root  85899345920 Apr  1 02:00 tony1.large.raw
-rw-r--r-- 1 libvirt-qemu kvm   21268922368 Apr  1 02:03 tony1.qcow2
-rw-r--r-- 1 libvirt-qemu kvm   21319450624 Mar 31 00:18 tony1.small.qcow2
root@fabv1:/var/lib/libvirt/images#

A side note: nowadays people tend to use the high level command virt-resize combined with qemu-img resize, which makes you less concerned about all these underlying details. I haven't tried it yet though.

没有评论:

发表评论