Teach you how to increase hard disk space in Linux virtual machine

  
Computer shop news During this time, there are a lot of stuff in the virtual machine. The disk space allocated by the virtual machine is not large enough. The number of logs and installed programs in the machine is increasing, and the disk space is seriously insufficient. I searched the Internet for a lot of relevant information. Summarized several ways to increase disk space for virtual machines, please refer to one. For hard disk formats such as sda/vda Environment: properties of centos 6.1 virtual machine: domainname test Disk path /var/lib/libvirt/images/test. Img hard disk partition: [root@localhost ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/vda1 ext4 6.8G 3.2G 3.3G 50% /tmpfs tmpfs 499M 0 499M 0% /dev/shm Need to add hard disk space. Method 1 idea, since there is one hard disk, then we just add a hard disk to the virtual machine, and then directly mount it to a directory in the root partition. This expands the hard disk space of my root partition. Solution 1 A, generate a new hard disk Using virt-manager is very easy, click directly in the virtual properties < Add Hardware"----<quo;storage” select how much space, drive type, cache mode. Then click Finish. Note that some hard drives are hot swappable and some do not. In addition to the IDE format hard drive, all other support hot swap, which means that if you add the IDE's hard drive, you need to restart the virtual machine to identify the newly added hard drive. If added with the virsh command. Use the command #qemu-img create -f raw test_add.img 10G //to generate a new empty disk in raw format #virsh attach-disk test /var/lib/libvirtd/images/test_add.img vdb —cache none Or #virsh edit test Add the following lines after the disk in xml. <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 virtual machine Log in to the virtual machine, first check if it can Newly identify the hard disk #fdisk -l Check to see if the newly added hard disk /dev/vdb is displayed. Then, format the vdb, #mkfs.ext4 /dev/vdb Next, create a new directory to mount the new hard disk #mkdir /test #mount /dev/vdb /test Finally, add the mount to the boot. #blkid /dev/vdb //Get the UUID of the hard disk /dev/vdb: UUID="19fc1d1d-7891-4e22-99ef- Ea3e08a61840" TYPE="ext4" #vim /etc/fstab //Add boot load, add UUID=19fc1d1d-7891-4e22-99ef-ea3e08a61840 /test ext4 defaults 1 2 to the last line to this method one, add the method 2 , directly pull up the partition

idea, the method of mounting is to add a disk, there is no way Then stretching the hard disk. Qemu-img provides a resize command, but the command is simply simple to pull up or down a raw img image size, but the partition can not be modified. I need to expand the partitions in them. It is quite coincidental that Red Hat offers this plugin for a while. This method is extended by the plug-in virt-resize that comes with Red Hat. The command first gets 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 newly expanded image. Because it actually uses the copy method, it takes a long time. If it is a large image, it is not recommended to use this method
specific solution. Prerequisites Install the libguestfs-tools toolkit. Close the virtual machine #yum -y install libguestfs-tools A, create a new large image #qemu-img create -f raw test_extend.img 15G Note that the img size here is the total size you need to expand B, use virt-resize to pull Lition partition #virt-resize —expand /dev/vda1 /var/lib/libvirt/images/test.img /var/lib/libvirt/images/test_new.img Note that this time is very long, please be patient. Use the new extended image instead of the original image #mv /var/lib/libvirt/images/test_new.img /var/lib/libvirt/images/test.img D, start the virtual machine #virsh start test Expand the completion, virt-resize Advantage: Ability to extend specific partitions in a virtual machine. And can expand the windows image. You do not need to log in to the virtual machine to do anything. Disadvantages: need to shut down when expanding. For large mirrors, the expansion time is longer. Second, for LVM format virtual machines If your hard disk format supports LVM. Then your hard drive expansion will be much easier, LVM supports online expansion of hard drives. Quite convenient. Method steps: #lvcreate -L 40G -n lv_vm_test1 VolGroup #virsh attach-disk test /dev/mapper/VolGroup/lv_vm_test vdb Note that there may be permission issues. Please modify the permissions of /dev/mapper/VolGroup/lv_vm_test first, the virtual machine can be mounted. Enter the virtual machine operation: #pvcreate /dev/vdb #vgextend VolGroup /dev/vdb #vgs #lvextend -l +100%FREE /dev/VolGroup/lv_root #resize2fs -p /dev/VolGroup/lv_root Advantages: Extended time is very Fast, support for dynamic expansion. Disadvantages, not suitable for the expansion of the window.

Copyright © Windows knowledge All Rights Reserved