Try   HackMD
tags: Linux

How to increase the hard disk size of the VM? (Ubuntu default setup as example)

The VM template have 50GB by default which is usually not enough. Once the VM have setup with additional hard disk space, the actual root disk may still have only 50GB, you needs to add the additional space with LVM to make root disk able to use it

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

To do this, please follow the below steps. The below steps are dangerous, you should follow the steps carefully under run though the whole steps.

  1. Type “lsblk” to see what is the disk name, for example, the above picture already shows that we need to work with sda, because root disk “/” is under sda3. Follow Steps will assume you are working with “sda”.

  2. Type “sudo fdisk /dev/sda”, this will get you into the fdisk tool, the tool used for format and partitioning the hard disk

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  3. Type “n” to add a new partition, we will need to create a new partition with all new available space. It will ask you the setting, all you need to do is just pressing enter and use the default setting instead, usually this should create a new partition called sda4. After that, press “w” and enter, this will write the table and exit the tool to confirm the setting. Press Ctrl+C to quit the tool if it didn’t quit.

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  4. Type “sudo pvcreate /dev/sda4” to make the new partition as new physical volume.

  5. Now, type “sudo vgs” to check the VG name and remember it, usually it should named “ubuntu-vg”

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  6. Now, type “sudo vgextend ubuntu-vg /dev/sda4” to add /dev/sda4 into this VG. You should see the VG is now extended with newly added partition with “sudo vgdisplay”

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  7. Now, type “sudo lvs”, here should will see the system LV name, usually called “
    ubuntu-lv”

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  8. The actual LV path usually format as /dev/mapper/[VG path]-[LV path], do for the above case, LV actual path is /dev/mapper/ubuntu—vg-ubuntu—lv. Type “sudo lvextend -l +100%FREE /dev/mapper/ubuntu—vg-ubuntu—lv” to extend all free space to the LV level volume.

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  9. Finally, you get free LV volume able to add to the root disk, and resize it at file system level. Type “sudo resize2fs /dev/mapper/ubuntu—vg-ubuntu—lv”, the root disk will now extended successfully, and I will recommend you restart the server once at this stage.

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →