LVM extend file system and partition

There are plenty resource on the web on how to do this. For example, see http://theducks.org/2009/11/expanding-lvm-partitions-in-vmware-on-the-fly/ or see http://www.tldp.org/HOWTO/LVM-HOWTO/extendlv.html. I constantly forget the commands, so I summarize it here–again….

Situation is that the disk is full and someone already changed the real disk size in the (virtual) machine.

At first the physical partition must be extended. This can be done with fdisk by first deleting the partition and then recreating it with the same start sector:

[root@dev-jenkins-slave5-bc ~]# fdisk -u /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c').

Command (m for help): p

Disk /dev/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders, total 73400320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005fc17

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2         1026048    33554431    16264192   8e  Linux LVM
/dev/sda3        33554432    67103504    16774536+  83  Linux

Command (m for help): d
Partition number (1-4): 3

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First sector (63-73400319, default 63): 33554432
Last sector, +sectors or +size{K,M,G} (33554432-73400319, default 73400319):
Using default value 73400319

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)

Command (m for help): p

Disk /dev/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 cylinders, total 73400320 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0005fc17

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     1026047      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2         1026048    33554431    16264192   8e  Linux LVM
/dev/sda3        33554432    73400319    19922944   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@dev-jenkins-slave5-bc ~]# reboot

Note that after deleting the partition, the new one is created at exactly the same position: 33554432. I rebooted the machine so that the new size is recognized.

Next step is to resize the disk and filesystem. First the lvm physical volume is resized and then the filesystem.

[root@dev-jenkins-slave5-bc ~]# pvresize /dev/sda3
  Physical volume "/dev/sda3" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

See in vgdisplay that there is now new space (31.5G are allocated, but 34.5G are there):

[root@dev-jenkins-slave5-bc ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  7
  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               34.50 GiB
  PE Size               4.00 MiB
  Total PE              8833
  Alloc PE / Size       8065 / 31.50 GiB
  Free  PE / Size       768 / 3.00 GiB
  VG UUID               k6r2rS-vAeB-olIh-evY8-vL1Y-Z7lq-AZKX2L

Then extend the logical volume size, now that the underlying physical volume has been extended and, finally, the filesystem is resized:

[root@dev-jenkins-slave5-bc ~]# lvextend -L+3G /dev/VolGroup/lv_root
  Size of logical volume VolGroup/lv_root changed from 29.91 GiB (7656 extents) to 32.91 GiB (8424 extents).
  Logical volume lv_root successfully resized

[root@dev-jenkins-slave5-bc ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 3
Performing an on-line resize of /dev/VolGroup/lv_root to 8626176 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 8626176 blocks long.

[root@dev-jenkins-slave5-bc ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       33G   28G  2.9G  91% /
tmpfs                 2.9G     0  2.9G   0% /dev/shm
/dev/sda1             477M   25M  427M   6% /boot

The resize2fs command uses the full disk if no size parameter is specified. So it extends the filesystem to the new maximum size.

Date: 2016-07-06

Author: Eike

Created: 2016-07-08 Fr 23:33

Validate