Resize qcow2 image

Cloud/KVM 2013. 9. 28. 23:07
[Info]
  • e.g.) Default 8GB(orig.img) -> 20GB(test.img) any..
  • Both Single-Image and BackingType-Image 
  • Test format: qcow2
  • Tools: parted, qemu-img, qemu-nbd
  • Test Platform: Ubuntu 13.04 x86_64


[Start]


- Check Image

# qemu-img info orig.img 

image: orig.img

file format: qcow2

virtual size: 8.0G (8589934592 bytes)

disk size: 883M

cluster_size: 65536

# qemu-img create -b /home/orig.img -f qcow2 test.img

# qemu-img info test.img

image: test.img

file format: qcow2

virtual size: 8.0G (8589934592 bytes)

disk size: 196K

cluster_size: 65536

backing file: /home/orig.img


- Connect NBD & Check partitions

# modprobe nbd

# qemu-nbd -c /dev/nbd0 /home/test.img

# ls -al /dev/nbd0*

brw-rw---- 1 root disk 43, 0 Sep 28 23:18 /dev/nbd0

brw-rw---- 1 root disk 43, 1 Sep 28 23:18 /dev/nbd0p1

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 8590MB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  8589MB  8588MB  primary  ext4         boot

        8589MB  8590MB  1049kB           Free Space

- Disconnect NBD & Resize image

# qemu-nbd -d /dev/nbd0

/dev/nbd0 disconnected

# qemu-img resize test.img 20G

Image resized.


- Check image

# qemu-img info test.img

image: test.img

file format: qcow2

virtual size: 20G (21474836480 bytes)

disk size: 200K

cluster_size: 65536

backing file: /home/orig.img


Connect NBD & Check partitions

qemu-nbd -c /dev/nbd0 /home/test.img

parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  8589MB  8588MB  primary  ext4         boot

        8589MB  21.5GB  12.9GB           Free Space


- Re-Partitioning (Remove Partition and Create Partition) ("1" is partition-number)

# parted /dev/nbd0 --script rm 1

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type  File system  Flags

        32.3kB  21.5GB  21.5GB        Free Space


# parted -a optimal /dev/nbd0 --script mkpart primary ext4 0% 100%

# parted /dev/nbd0 --script print free

Model: Unknown (unknown)

Disk /dev/nbd0: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos


Number  Start   End     Size    Type     File system  Flags

        32.3kB  1049kB  1016kB           Free Space

 1      1049kB  21.5GB  21.5GB  primary  ext4


- Mount image's Partition (to edit boot-script :: rc.local)

# kpartx -a /dev/nbd0

# mount /dev/mapper/nbd0p1 /mnt/


- Apply Parition to VM (by resize2fs)

# echo 'resize2fs /dev/vda1; sed -i \"/resize2fs/d\" /etc/rc.local' >> /mnt/etc/rc.local


[End]

Booting VM with new image




[Appendix]

- Disconnect kpartx & NBD

# umount /mnt

# kpartx -d /dev/nbd0

# qemu-nbd -d /dev/nbd0


Posted by 사랑줍는거지
,