Disk resizing for virtual machines in kvm

Source: Internet
Author: User

How to add a hard disk to a linux Virtual Machine in kvm by resizing the hard disk of a virtual machine in KVM has been running in many ways recently. The number of disks allocated by virtual machines is small at the time of startup. As logs accumulate and files are uploaded at ordinary times, an alarm is triggered on disk space. I checked the information online and did my own experiment. To sum up the methods for adding disk space to virtual machines. For hard drive environments in sda/vda formats such as www.2cto.com: Attributes of centos 6.1 Virtual Machine: domainname test Disk path/var/lib/libvirt/images/test. img hard disk partition: [root @ localhost ~] # Df-hTFilesystem Type Size Used Avail Use % Mounted on/dev/vda1 ext4 6.8G 3.2G 3.3G 50%/tmpfs 499 M 0 499 M 0%/dev/shm need to add Hard Disk space. Method 1: Since there is one less hard disk, simply add a hard disk to the virtual machine and mount it directly to a directory in the root partition. In this way, the hard disk space in the root partition is extended. Solution 1 A of www.2cto.com. It is easy to generate A new hard disk and use virt-manager. Directly click "add Hardware" in the virtual Attribute-"storage" to select the size of the space, driver type, cache mode. Click Finish. Note that some hard disks support hot swapping and some do not. Except for hard disks in IDE format, hot swapping is supported. This means that if an IDE hard disk is added, the virtual machine needs to be restarted to identify the newly added hard disk. If you use the virsh command to add it. Run the command # qemu-img create-f raw test_add.img 10G, generate a new raw empty disk # virsh attach-disk test/var/lib/libmongod/images/test_add.img vdb-cache none or # Add virsh edit test to the disk in xml as follows several rows. <Disk type = 'file' device = 'disk'> <driver name = 'qemu' type = 'raw 'cache = 'none'/> <source file ='/var/ lib/libvirt/images/test_add.img '/> <target dev = 'vdb' bus = 'virtio'/> </disk> B, format the hard disk in the VM and log on to the VM. First, check whether the hard disk can be recognized. # fdisk-l check whether the newly added Hard Disk/dev/vdb is displayed. Then, format the vdb, # mkfs. next, create a directory to mount the New Hard Disk # mkdir/test # mount/dev/vdb/test, add the mount to the startup instance # blkid/dev/vdb // obtain the hard disk UUID/dev/vdb: UUID = "19fc1d1d-7891-4e22 -99ef-ea3e08a61840 "TYPE =" ext4 "# vim/etc/fstab // Add boot loading, add UUID = Nobody/test ext4 ults 1 2 to method 1 in the last line, method 2: Pull up the partitioning path directly. The mounting method is to add a disk. Is there any way to directly stretch the hard disk. Qemu-img provides a resize command, but this command simply pulls up or down the size of a raw img image, but cannot modify the partition. I need to expand the partitions. Red Hat often provides such plug-ins for time. This method is extended using the built-in virt-resize plug-in of Red Hat. The command first obtains the original partition information and other file information. Then, repartition and format the new image. Finally, copy the files in the original image to the new file system, and replace the original image with the new extended image. Because the copy method is used, it takes a long time. If it is a large image, we do not recommend using this solution. Install the libguestfs-tools toolkit. Disable the VM # yum-y install libguestfs-tools A and create A large image # qemu-img create-f raw test_extend.img 15G, here the img size is the total size you need to expand B, use virt-resize to pull up the partition # virt-resize-expand/dev/vda1/var/lib/libvirt/images/test. img/var/lib/libvirt/images/test_new.img Note: This is a long time. Please wait for C. use the new extended image to replace the original image # mv/var/lib/libvirt/images/test_new.img/var/lib/libvirt/images/test. imgD: start the virtual machine # virsh start test. virt-resize has the following advantages: it can expand specific partitions in the virtual machine. And can expand windows images. You do not need to log on to the Virtual Machine for any operations. Disadvantage: shutdown is required during expansion. For large images, it takes a long time to expand. For a virtual machine in LVM format, if your hard disk format supports LVM. It will be much easier for you to expand your hard disk. LVM supports online resizing of the hard disk. It is quite convenient. Procedure: # lvcreate-L 40G-n lv_vm_test1 VolGroup # virsh attach-disk test/dev/mapper/VolGroup/lv_vm_test vdb. You may encounter permission issues. First, modify the permissions of/dev/mapper/VolGroup/lv_vm_test, which can be mounted by virtual machines. To access a virtual machine: # pvcreate/dev/vdb # vgextend VolGroup/dev/vdb # vgs # lvextend-l + 100% FREE/dev/VolGroup/lv_root # resize2fs-p/dev/VolGroup/lv_root advantages: rapid expansion, supporting dynamic expansion. Disadvantage: it is not suitable for window expansion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.