Accomplished systems and network administrator with 10+ years of experience managing server infrastructures and data-center operations.
I was required to increase the size of the root file system on a Linux VM which is running as a guest machine on a Microsoft Hyper-V server. The root file system is running on a logical volume and is managed by the Logical Volume Manager.
Summary of Steps
- We power off the Linux VM and make a copy of the VM’s virtual hard disk by doing a copy and paste of the VHD file.
- Resize the virtual hard disk of the Linux VM in Hyper-V by using the PowerShell resize-vhd cmdlet.
- Start the Linux VM after the resizing of the virtual hard disk has finished.
- List the current Partitions in Linux to find out the next Partition number by using fdisk -l.
- We create a new partition using fdisk. We use the syntax fdisk /dev/<disk device> e.g. fdisk /dev/sda to launch fdisk on the selected disk. Then create a new partition and write the changes to disk.
- Restart the Linux VM for changes to take effect.
- Run fdisk -l to list the partitions including the newly created partition. Take note of the newly created partition name e.g. /dev/sda3.
- List the currently mounted file systems sizes using the df -h command. Also take note of the file system name that you want to resize.
- Create a physical volume out of the new partition by using the pvcreate command e.g. pvcreate /dev/sda3.
- Run vgdisplay to list the current volume groups. Take note of the volume group which you want to extend the size of so that you can increase the file system.
- Use the vgextend command to extend the selected volume group to the new physical volume. e.g. vgextend vg_redhat /dev/sda3.
- Use lvextend to extend the logical volume of the file system to the maximum space that you have added (or a lesser amount if required). We will increase the size by using the number of physical extents by typing in lvextend -l +<extents> <filesystem> e.g. lvextend -l +12789 /dev/mapper/vg_redhat-lv_root .
- We can now increase the size of our file system to the same size as our resized logical volume by typing in resize2fs <filesystem> e.g. resize2fs /dev/mapper/vg_redhat-lv_root .
Backup the VHD file (Virtual Hard Disk)
Firstly, shutdown the Linux VM by issuing shutdown -h now at the Linux command shell.
Go into the Settings of your Linux VM and find out the location of the VHD file that you want to resize.
Go to that location and do a copy and paste to back up the VHD file.
Resize the VHD File
After the copy and paste has finished, run PowerShell As Administrator. You must run as Administrator so that you have the permissions to resize the VHD file. Navigate using the CD command to the location of the VHD file to be resized. Type in the following:
resize-vhd -Path xxxxxxxxx.vhd -SizeBytes nnnGB where xxxxxxxxx is the name of the VHD file and nnn is the size that you want to increase the hard disk to.
Once the resizing has finished, it will just return you back to the PowerShell prompt again.
Start up the Linux VM
Start the Linux VM after the resizing of the virtual hard disk has finished.
List the Current Partitions in Linux
Run fdisk -l to list partitions. We can see two partitions have been created. We will create a new one as partition number 3.
Create a New Partition
Type in fdisk /dev/sda to launch fdisk on the disk. Then type n at the prompt to create a new partition.
Enter p for primary partition. Then enter 3 for the partition number.
Accept the default values offered by just hitting enter until you return to the prompt.
Type w to write changes to disk and exit the fdisk program. We will now reboot the whole server so the new partition gets read and also to make sure nothing has gone wrong with the partition operation.
To reboot, enter the command shutdown -r now .
List the Linux Partitions Including the New Partition
Type fdisk -l to display all the partitions. We can see /dev/sda3 which is our new partition.
List Mounted File Systems Sizes in Linux
Type df -h to list the currently mounted filesystems sizes. Take note of the filesystem we want to resize. We want to increase the size of /dev/mapper/vg_redhat-lv_root.
Create an LVM Physical Volume in Linux
Type pvcreate /dev/sda3 to create a physical volume out of our new partition.
List Volume Groups in Linux
Type vgdisplay . Take note of the volume group name we need to extend, e.g., vg_redhat . Note that the Free PE/Size is 0/0.
Extend a Volume Group to a New LVM Physical Volume in Linux
Type vgextend vg_redhat /dev/sda3 to extend the volume group vg_redhat to include the physical volume /dev/sda3.
Type vgdisplay again. Note the Free PE/Size is now 12798/49.99GiB.
Extend the Logical Volume in Linux
Type in lvextend -l +12789 /dev/mapper/vg_redhat-lv_root to extend the logical volume /dev/mapper/vg_redhat-lv_root to the maximum available space.
Type in df -h . Note that the size of the filesystem /dev/mapper/vg_redhat-lv_root hasn’t increased.
Resize the Linux LVM File System
Type in resize2fs /dev/mapper/vg_redhat-lv_root to increase the filesystem size.
Type in df -h again, and now you should see the filesystem /dev/mapper/vg_redhat-lv_root has increased in size.
Restart the server just to ensure everything is working fine by typing in shutdown -r now .
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
Kadir on August 31, 2018:
Note: for the last step I used xfs_growfs instead of resize2fs. Because it gave me the error: "resize2fs: Bad magic number in super-block while trying to open..."
Danilo on June 04, 2018:
You saved my day. Thanks.
Andrea on March 02, 2018:
sengstar2005 (author) from Sydney on February 15, 2018:
Bruno on February 14, 2018:
Great help, thanks!
sengstar2005 (author) from Sydney on January 05, 2018:
Vladimir on January 05, 2018:
Thanks a lot! This is really great instruction!
sengstar2005 (author) from Sydney on May 25, 2017:
Thank you for your comment.
Minh Hoang on May 25, 2017:
Thank for the great work. Very detail and logic.