How to Resize a Linux File System on a Linux VM under Hyper-V
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
1. 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.
2. Resize the virtual hard disk of the Linux VM in Hyper-V by using the PowerShell resize-vhd cmdlet.
3. Start the Linux VM after the resizing of the virtual hard disk has finished.
4. List the current Partitions in Linux to find out the next Partition number by using fdisk -l.
5. 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.
6. Restart the Linux VM for changes to take effect.
7. Run fdisk -l to list the partitions including the newly created partition. Take note of the newly created partition name e.g. /dev/sda3.
8. 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.
9. Create a physical volume out of the new partition by using the pvcreate command e.g. pvcreate /dev/sda3.
10. 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.
11. Use the vgextend command to extend the selected volume group to the new physical volume. e.g. vgextend vg_redhat /dev/sda3.
12. 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 .
13. 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 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 .
More by this Author
This article will show you how to print Hello World to the default output device in Swift. It is meant as an introduction to learning to program in Swift.
No comments yet.