Extending root filesystem using LVM (Linux)

Step 1: Confirm Disk Partitions in Distribution.

You can do this by typing

  df –h

Step 2: Format data disk as LVM partition

First become root by typing

                    sudo su –

Format disk partition by typing

     fdisk /dev/xvda          

Note: Device name differs for different distributions check by listing them with fdisk –l it is xvda in our case 

To list options available

 

Command (m for help): m

Command action

   a   toggle a bootable flag

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition

   l   list known partition types

   m   print this menu

   n   add a new partition

   o   create a new empty DOS partition table

   p   print the partition table

   q   quit without saving changes

   s   create a new empty Sun disklabel

   t   change a partition's system id

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit

   x   extra functionality (experts only)

 

List LVM partitions present

 

Command (m for help): p

 

Disk /dev/xvda: 1099.5 GB, 1099511627776 bytes

255 heads, 63 sectors/track, 133674 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x000403ec

 

Device Boot      Start         End      Blocks   Id  System

/dev/xvda1   *           1          64      512000   83  Linux

Partition 1 does not end on cylinder boundary.

/dev/xvda2              64        2611    20458496   8e  Linux LVM

 

To add the data disk to partition table

 

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

p

Partition number (1-4): 3

First cylinder (2611-133674, default 2611):

Using default value 2611

Last cylinder, +cylinders or +size{K,M,G} (2611-133674, default 133674):

Using default value 133674

 

Confirm that this has been done by typing p again

 

Command (m for help): p

 

To convert its system type to LVM

 

Command (m for help): t

Partition number (1-4): 3

Hex code (type L to list codes): 8e

Changed system type of partition 3 to 8e (Linux LVM)

 

Confirm the changes

 

Command (m for help): p

 

Write changes to disk

 

Command (m for help): w

 

Reboot the VM at this point to save changes before going to the next step.

Step 3: Create Physical Volume

Check physical volumes present in distribution by typing

          lsblk

Resize Volume Group

pvcreate     /dev/xvda3      

Confirm that the volume has been converted successfully

pvdisplay

Step 4: Add the New Physical Volume to Volume Group

Check volume group already present

vgdisplay       

Add volume to volume group

vgextend /dev/name-of-volume-group     /dev/xvda3

Confirm this using first command

Step 5: Resize root logical volume to occupy all space

Confirm name of root logical volume using lsblk command, some distributions name it as lv_root, we used root in this guide.

Resize root                        

lvextend -l 100%FREE /dev/name-of-volume-group/root                                                       

Confirm using

lvdisplay

You can replace the 100%FREE command with preferred space in MB, for example, to add 256 MB replace it with +256M

Step 6: Copy changes to filesystem

df -h still shows 19G for the root volume; to make the filesystem report the actual size use

For ext4 filesystem

                resize2fs /dev/name-of-volume-group/root

For xfs filesystem

xfs_growfs /dev/name-of-volume-group/root

Was this answer helpful?

 Print this Article

Also Read

Create a new user (Linux)

Please note that user refers to the username of the user you wish to create. Create a user -...

Generating SSH Keys - Mac OS and Linux

Public key authentication provides additional security as compared to password authentication....

How to free up space on /boot partition (Linux)

On Centos:1. Check the current kernel that is in use - uname -a2. Run ls /boot to see the kernels...

Allow ssh password login (Linux)

Open the file /etc/ssh/sshd_config and change the line PasswordAuthentication no to...