Resizing or shrinking KVM virtual machine filesystems and disk images

Most everyone wants to increase the size of their disk images, filesystems, or virtual machines (VM). It’s common to create a small, minimal machine (2 GB in size) and increase it depending on the flavor or configuration of the deployed VM.

However, there are some cases where machines will need to be moved from large disk flavors to smaller flavors. Assuming the filesystem isn’t full or is minimal in size, you may be able to successfully resize the disk image so that it will fit on a smaller VM.

Assumptions
I’m using a trivial example: the deployment is using the ext3 filesystem, no swap partition, and will not require calculations for virtual drive geometry or block size.

I have an 80 GB Fedora 20 installation created as a raw img (virtual machine image) on Linux-based KVM which has already been installed and has the qemu utilities available. The operations, below, were performed on a RedHat Enterprise 6.5 Linux installation.

How to shrink a KVM image filesystem

# cd /var/lib/libvirt/images
 
# ls -al
total 189398552
drwxrwxr-x. 2 root root
drwxr-xr-x. 10 root root
-rw-------. 1 root root 85899345920 Sep 16 09:33 Fedora_20_ext3.img
-rw-r--r--. 1 root root 85912715264 Mar 28 14:18 Virtual_Client_for_Linux_Ubuntu_ 12-KVM.qcow2
-rw-rw-r--. 1 root root 9 Mar 28 14:09 Virtual_Client_for_Linux_Ubuntu_12- KVM.qcow2.type
-rw-r--r--. 1 root root 32217432064 Aug 28 16:13 Virtual_Client_for_Linux_Windows_ 7-KVM.qcow2
-rw-rw-r--. 1 root root 10 Apr 24 09:43 Virtual_Client_for_Linux_Windows_7- KVM.qcow2.type
 
# qemu-img info Fedora_20_ext3.img
image: Fedora_20_ext3.img
file format: raw
virtual size: 80G (85899345920 bytes)
disk size: 80G
 
# virt-df Fedora_20_ext3.img
Filesystem 1K-blocks Used Available Use%
Fedora_20_ext3.img.bak:/dev/sda1 81642216 3381656 74113360 5%
 
# yum install -y libguestfs-tools
 
# guestfish
Welcome to guestfish, the libguestfs filesystem interactive shell for editing virtual machine filesystems.
Type: 'help' for help on commands
'man' to read the manual
'quit' to quit the shell
 
><fs> add Fedora_20_ext3.img
><fs> run
100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00
><fs> list-filesystems
/dev/sda1: ext3
><fs> e2fsck-f /dev/sda1
><fs> resize2fs-size /dev/sda1 28G
><fs> list-filesystems
/dev/sda1: ext3
><fs> e2fsck-f /dev/sda1
><fs> quit
 
# qemu-img info Fedora_20_ext3.img
image: Fedora_20_ext3.img
file format: raw
virtual size: 80G (85899345920 bytes)
disk size: 80G
 
# virt-df Fedora_20_ext3.img
Filesystem 1K-blocks Used Available Use%
Fedora_20_ext3.img:/dev/sda1 28899452 3369560 24061888 12%
 
# qemu-img resize Fedora_20_ext3.img 30G
Image resized.
 
# qemu-img info Fedora_20_ext3.img image: Fedora_20_ext3.img
file format: raw
virtual size: 30G (32212254720 bytes) disk size: 30G

原文地址:http://www.mischiefblog.com/2014/09/16/resize-shrink-kvm-img-qcow2-virtual-machine-filesystem-disk-image/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值