How to extend/resize Logical Volume and Volume Group in Linux

I have already written articles on how you can create, remove, merge, extend LVM based snapshots which you may find interesting as it helps performing safer software upgrades in real life scenarios so that you can easily fallback in case of any errors.

In the below article I will show you the steps to add a new disk which can be added into existing volume group to extend a logical volume but you are not using volume group then you must use fdisk or parted to extend or shrink partitions in Linux.

But if you want to reduce (shrink) an existing logical volume and use the extra space to extend another logical volume, please follow below link
How to reduce LVM size in Linux step by step (online without reboot)

As of now you can see my root partition is on LVM with 5.8Gb size. I want to extend my root filesystem with +1GB. Now here there can be two scenarios where

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-root
                      5.8G  4.1G  1.4G  76% /
tmpfs                 504M  260K  504M   1% /dev/shm
/dev/sda1             194M   26M  158M  15% /boot

1. You want to extend size using extra partition
2. You want to extend size with space in existing Volume Group

I will show you steps required to perform the same using both the scenarios

Scenario 1

Extend LVM using additional partition

# fdisk -l

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x000cf049

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1306    10279936   8e  Linux LVM

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

Disk /dev/mapper/VolGroup-root: 6278 MB, 6278873088 bytes
255 heads, 63 sectors/track, 763 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: 0x00000000

Disk /dev/mapper/VolGroup-swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x00000000

So as you see I have added extra partition hard disk /dev/sdb with 2GB space. Let us use it to extend our root file system.

Steps required to perform the same
1. Create partition for /dev/sdb with 1 GB storage
2. Create Physical Volume
3. Extend existing Volume Group
4. Extend LVM
5. Verify the changes

Create partition with /dev/sdb

# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x55693454.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-261, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +1G

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x55693454

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 0x55693454

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  8e  Linux LVM

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

# partprobe /dev/sdb1

Now at this stage our partition is created as /dev/sdb1 with LVM type.

Create Physical Volume

# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created


Extend Volume Group

Before extending the size of our existing Volume group let us verify the size of the same

# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.80 GiB
  PE Size               4.00 MiB
  Total PE              2509
  Alloc PE / Size       2009 / 7.85 GiB
  Free  PE / Size       500 / 1.95 GiB
  VG UUID               uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW

So our Volume Group contains 9.80 GB

Next extend the Volume Group using the below command

# vgextend VolGroup /dev/sdb1
Volume group "VolGroup" successfully extended

Verify the change

# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               10.81 GiB
  PE Size               4.00 MiB
  Total PE              2767
  Alloc PE / Size       2009 / 7.85 GiB
  Free  PE / Size       758 / 2.96 GiB
  VG UUID               uH5AP5-b24E-92h7-nL8b-7Bio-fXe3-pstWIW

As you can verify the change i.e. extra 1 GB has been added to our Volume Group which we can use to extend out root filesystem

Extend LVM

Before extending the root fs let us verify the existing size

# lvdisplay /dev/VolGroup/root
  --- Logical volume ---
  LV Path                /dev/VolGroup/root
  LV Name                root
  VG Name                VolGroup
  LV UUID                Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg
  LV Write Access        read/write
  LV Creation host, time ,
  LV Status              available
  # open                 1
  LV Size                5.85 GiB
  Current LE             1497
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

So our existing LVM size for root is 5.85GB

Let us extend the size with +1GB

# lvextend -L +1G /dev/VolGroup/root
  Extending logical volume root to 6.85 GiB
  Logical volume root successfully resized

Verify the changes

# lvdisplay /dev/VolGroup/root
  --- Logical volume ---
  LV Path                /dev/VolGroup/root
  LV Name                root
  VG Name                VolGroup
  LV UUID                Qn8TnI-TLNm-rl4Y-ORnd-zU3p-2Kj1-ALSLAg
  LV Write Access        read/write
  LV Creation host, time ,
  LV Status              available
  # open                 1
  LV Size                6.85 GiB
  Current LE             1753
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

Let us see if the changes are reflected using df

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-root
                      5.8G  4.1G  1.4G  76% /
tmpfs                 504M  260K  504M   1% /dev/shm
/dev/sda1             194M   26M  158M  15% /boot

But it still shows old size for root. The reason is because we need to update the partition table with the new value of LVM which can be done using the below command

# resize2fs /dev/VolGroup/root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/VolGroup/root to 1795072 (4k) blocks.
The filesystem on /dev/VolGroup/root is now 1795072 blocks long.

In case you notice above initially the command throws out an error as we were attempting to resize on a mounted partition but since our file system is ext4, we get an additional feature of online resizing where we do not need to unmount the file system for extending its size

NOTE: We can resize the file system online if following conditions are met

  • 2.6.x kernel sereis
  • Must be formatted with ext3 fs or above

Re-verify the change now

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-root
                      6.8G  4.2G  2.3G  65% /
tmpfs                 504M  260K  504M   1% /dev/shm
/dev/sda1             194M   26M  158M  15% /boot

So everything worked as expected.

Scenario 2

In case you already have enough space in your volume group for extending the root file system then you don't need to create a new physical volume.

You can just follow the steps of extending LVM as shown above

Related Articles
How to remove logical and physical volume from Volume Group in Linux
How to rename Logical Volume and Volume Group in Linux

14 thoughts on “How to extend/resize Logical Volume and Volume Group in Linux”

  1. do not use this, i had followed everything, after restarting i had this error:
    unable to resume vg group centos rescue mode install exited abnormally. DO NOT USE IT.

    thanks

    Reply
  2. I personally verify all the steps on my machine prior posting it on my website.

    Can you please brief me out about your scenario to help me figure out what exactly went wrong?

    Reply
  3. It works perfectly. I have searched several tutorials over the Internet about how to do this and this is the best one for me. It is clearly explained and you shall not have any problem following the steps. Highlighting the options in blue helped a lot. Just one question: what we would have to do if we had to do it in a multipath disk such as a LUN mapped in FC?

    Reply
  4. In case of multipath also I use the same process, While using SAN either the existing LUN is extended or a new LUN is added. In either case the storage will be mapped on your machine on existing partition or a new partition will be created depending upon your action.

    Further which you can use the same process of extending your VG and LV

    Reply
  5. Awesome Tutorial, I would add on the "#resize2fs /dev/VolGroup/root" another option if error such as "Bad magic number in super-block while trying to open /dev/centos/root" happens:
    xfs_growfs /dev/VolGroup/root.

    BR

    Reply
  6. HI deepak. i am using Multipath when i will add the storage on SAN side in exiting LUN, then LUN size will increase. then what i will do

    Reply

Leave a Comment