How to extend a filesystem in linux using LVM…

I know this is old hat for most people in the *nix world, but I got asked to do a write up for this, so I figured I might as well post it here. Enjoy

Here’s a break down.

Assumptions.: In this tutorial, we’ve added space to a virtual disk in VMWare VCenter. This tutorial also fully applies if you have extra space on a disk that is sitting unpartitioned. If you are doing this on a physical server, and adding a physical disk, the only adjustments made are the partition letters will change, say, from /dev/sdb to /dev/sde, etc. You’ll need to figure that out on your own.

First, check space available with an fdisk –l

[root@localhost ~]# fdisk -l
Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26         783     6088635   82  Linux swap
/dev/sda3             784        1566     6289447+  8e  Linux LVM
Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3916    31455238+  8e  Linux LVM
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         261     2096451   8e  Linux LVM
Disk /dev/sdd: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        2610    20964793   8e  Linux LVM

Then reboot and check again.

[root@localhost ~]# fdisk -l
Disk /dev/sda: 12.8 GB, 12884901888 bytes
255 heads, 63 sectors/track, 1566 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          25      200781   83  Linux
/dev/sda2              26         783     6088635   82  Linux swap
/dev/sda3             784        1566     6289447+  8e  Linux LVM
Disk /dev/sdb: 75.1 GB, 75161927680 bytes
255 heads, 63 sectors/track, 9137 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        3916    31455238+  8e  Linux LVM
Disk /dev/sdc: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1         261     2096451   8e  Linux LVM
Disk /dev/sdd: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdd1               1        2610    20964793   8e  Linux LVM

Note the change in /dev/sdb – went from 32.2 GB to 75.1GB. That’s where your space is.

Run `pvdisplay` to see the volume is set to 30GB.

[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name               /dev/sdb1
VG Name               vg02
PV Size               30.00 GB / not usable 1.82 MB
Allocatable           yes (but full)
PE Size (KByte)       4096
Total PE              7679
Free PE               0
Allocated PE          7679
PV UUID               x88xxx-xxXX-XxxX-xxXx-xxxX-xxXX-xxxxXX

Add the space to a new partition with fdisk- max space, write and exit. Partprobe to scan the partitions into the OS again, then add it to the PV.

[root@localhost ~]# fdisk /dev/sdb
[root@localhost ~]# partprobe
[root@localhost ~]# pvcreate /dev/sdb2 #------- This will be the new partition you created…run `fdisk –l` again if you're not sure which number.

Extend the VG onto the new PV…

[root@localhost ~]# vgextend vg02 /dev/sdb2
Volume group "vg02" successfully extended

So now we have 75GB showing, and need to extend the volume group to match. Notice the pvdisplay shows Free PE’s jump from 0 to 10238. The `vgdisplay` shows a similar finding.

[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name               vg02
System ID
Format                lvm2
Metadata Areas        3
Metadata Sequence No  11
VG Access             read/write
VG Status             resizable
MAX LV                0
Cur LV                1
Open LV               0
Max PV                0
Cur PV                3
Act PV                3
VG Size               89.98 GB
PE Size               4.00 MB
Total PE              23035
Alloc PE / Size       12797 / 49.99 GB
Free  PE / Size       10238 / 39.99 GB
VG UUID               x88xxx-xxXX-XxxX-xxXx-xxxX-xxXX-xxxxXX

Now there is 40GB added (39.99). We can extend the LV by that much. (use the number of Free PE instead of the size- that way you don’t get rounding errors and space that ends in the middle of a sector.)

[root@localhost ~]# lvextend -l +10238 /dev/mapper/vg02-appsvol
Extending logical volume appsvol to 89.98 GB
Logical volume appsvol successfully resized
Of course, now we need to extend the FS itself. This is the first thing we will have done that can actually be dangerous, so be careful. Unmount the volume and resize:   **EDIT**-See note at bottom if your FS needs to stay online…
[root@localhost ~]# umount /dev/mapper/vg02-appsvol
[root@localhost ~]# resize2fs /dev/mapper/vg02-appsvol

IF THERE has been recent improper shutdowns, reboots, disk snafu’s, or the disk has just reached it’s reboot count, it may prompt you to run a scan disk before it will let you extend the FS. If it asks, run the fsck and re-attempt to extend the LV. (this box did make me run it…)

[root@localhost ~]# lvextend -l +10238 /dev/mapper/vg02-appsvol
Extending logical volume appsvol to 89.98 GB
Logical volume appsvol successfully resized
[root@localhost ~]# resize2fs /dev/mapper/vg02-appsvol
resize2fs 1.35 (28-Feb-2004)
Please run 'e2fsck -f /dev/mapper/vg02-appsvol' first.
[root@localhost ~]# e2fsck -f /dev/mapper/vg02-appsvol
e2fsck 1.35 (28-Feb-2004)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/vg02-appsvol: 40159/6553600 files (8.1% non-contiguous), 1979539/13104128 blocks
[root@localhost ~]# resize2fs /dev/mapper/vg02-appsvol
resize2fs 1.35 (28-Feb-2004)
Resizing the filesystem on /dev/mapper/vg02-appsvol to 23587840 (4k) blocks.
The filesystem on /dev/mapper/vg02-appsvol is now 23587840 blocks long.
[root@localhost ~]# mount -a <—– This will remount your fstab. If necessary, mount manually
[root@localhost ~]# df -h
Filesystem                                     Size  Used Avail Use% Mounted on
/dev/mapper/vg01-rootvol                      5.0G  2.4G  2.4G  50%  /
/dev/sda1                                     190M   15M  166M   9%  /boot
none                                          2.0G     0  2.0G   0%  /dev/shm
/dev/mapper/vg01-varvol                       2.0G  616M  1.3G  33%  /var
/dev/mapper/vg02-appsvol                       89G  6.8G   80G   8%  /apps

That’s it.

Kling

**EDIT** When expanding the filesystem, it is assumed that the FS you are expanding is unmounted. You can extend a mounted (online) filesystem by adding the `-f` option to the resize2fs command. This also will skip the e2fsck if necessitated, but keep in mind, if the OS is asking you to run a scan disk, it’s probably a good idea to comply. Adding the space to an online filesystem is not “best practice”, but sometimes you just can’t take a filesystem offline and keep your job. Just an FYI… 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *