How to Resize a Linux File System on a Linux VM under Hyper-V

Updated on June 3, 2016

Introduction

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 .

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Kadir 

      3 months ago

      Thank you!

      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..."

    • profile image

      Danilo 

      6 months ago

      You saved my day. Thanks.

    • profile image

      Andrea 

      9 months ago

      Grazie!

    • sengstar2005 profile imageAUTHOR

      sengstar2005 

      10 months ago from Sydney

      Thanks Bruno.

    • profile image

      Bruno 

      10 months ago

      Great help, thanks!

    • sengstar2005 profile imageAUTHOR

      sengstar2005 

      11 months ago from Sydney

      Thanks Vladimir.

    • profile image

      Vladimir 

      11 months ago

      Thanks a lot! This is really great instruction!

    • sengstar2005 profile imageAUTHOR

      sengstar2005 

      18 months ago from Sydney

      Thank you for your comment.

    • profile image

      Minh Hoang 

      18 months ago

      Thank for the great work. Very detail and logic.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)