Home arrow Blog arrow Walk-Throughs arrow Resizing Logical Volume - Updated
Border States Consulting, LLC |
Home
Services
Service Plans
Sales
Blog
News Feeds
Links
Advanced Search
Contact
Jobs
Other Menu
Antivirus
Remote Support
Trouble Ticket
Administrator

Authorize.Net Certified Developer

Avast antivirus

 
 
 
Resizing Logical Volume - Updated PDF Print E-mail

A few years back I posted an article on resizing the logical volume of a linux virtual machine. I've since discovered a simpler way. Here are the steps used on a CentOS 7 host with a Fedora 25 guest.

For this example the guest was named BlueHat1 with a root partition of 8GB, and we're going to create a clone with a resized root partition of 13GB. The following steps were performed from a terminal on the host as the root user. The final two steps were performed from a terminal on the new guest. The virtual machine should be shut down before starting.

In my case, I needed to install a couple dependencies, you may or may not already have these packages installed:

[root@VM_Host ~]# yum install perl-XML-XPath libguestfs-tools-c

Next change directory to where your vm images are located:

[root@VM_Host ~]# cd /var/lib/libvirt/images

The image file for this virtual machine is BlueHat1.qcow2. To view the disk partitions for that image use the virt-filesystems command:

[root@VM_Host images]# virt-filesystems --long -h --all -a BlueHat1.qcow2
Name Type VFS Label MBR Size Parent
/dev/sda1 filesystem ext4 - - 1.0G -
/dev/fedora/root filesystem ext4 - - 8.0G -
/dev/fedora/swap filesystem swap - - 1.0G -
/dev/fedora/root lv - - - 8.0G /dev/fedora
/dev/fedora/swap lv - - - 1.0G /dev/fedora
/dev/fedora vg - - - 9.0G /dev/sda2
/dev/sda2 pv - - - 9.0G -
/dev/sda1 partition - - 83 1.0G /dev/sda
/dev/sda2 partition - - 8e 9.0G /dev/sda
/dev/sda device - - - 10G -

The partition we need to resize is /dev/sda2, which contains two logical volumes (root and swap), the root lv is the one we need to resize. Note the name of this lv as we will use that in the final two steps. In this example we'll increase it by 5GB.

[root@VM_Host images]# truncate -r BlueHat1.qcow2 BlueHat2.qcow2
[root@VM_Host images]# truncate -s +5G BlueHat2.qcow2
[root@VM_Host images]# virt-resize --expand /dev/sda2 BlueHat1.qcow2 BlueHat2.qcow2
[ 0.0] Examining BlueHat1.qcow2
**********

Summary of changes:

/dev/sda1: This partition will be left alone.

/dev/sda2: This partition will be resized from 9.0G to 14.0G. The LVM PV
on /dev/sda2 will be expanded using the 'pvresize' method.

**********
[ 3.7] Setting up initial partition table on BlueHat2.qcow2
[ 4.0] Copying /dev/sda1
100% ???????????????????????????????????????????????????????? 00:00
[ 12.0] Copying /dev/sda2
100% ????????????????????????????????????????????????????????? 00:00
100% ????????????????????????????????????????????????????????? 00:00
[ 201.9] Expanding /dev/sda2 using the 'pvresize' method

Resize operation completed with no errors. Before deleting the old disk,
carefully check that the resized disk boots and works correctly.
[root@eyelash images]#

Now in the Virtual Machine Manager on the host, add a new VM choosing to import an existing image (the new image we just created, which in this example was BlueHat2.qcow2), and boot it. In a terminal as root on the guest perform the final two steps:

[root@BlueHat2 /]# lvresize /dev/fedora/root -L +5G
Size of logical volume fedora/root changed from 8.00 GiB (2047 extents) to 13.00 GiB (3327 extents).
Logical volume fedora/root successfully resized.
[root@BlueHat2 /]# resize2fs /dev/fedora/root
resize2fs 1.43.3 (04-Sep-2016)
Filesystem at /dev/fedora/root is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/fedora/root is now 3406848 (4k) blocks long.

Now we've successfully resized our root partition from 8GB to 13GB. Once you are sure the new VM boots and works as expected you can delete the old one.

 

Last Updated ( May 31, 2017 at 12:20 AM )
 
   
     

 
LEGO® Juniors: Introduce your child to the world of building.

Mambo is Free Software released under the GNU/GPL License. Hosted by <Border States Consulting, LLC>