Skip to content

Lvm resize root volume

wordpress meta

title: 'LVM resize root volume'
date: '2018-05-21T13:41:13-05:00'
status: publish
permalink: /lvm-resize-root-volume
author: admin
excerpt: ''
type: post
id: 1214
category:
    - LVM
tag: []
post_format: []

Since this is a root/boot volume we are talking about anything on this page is VERY risky. Be warned this post may provide you with a strategy or hints but you are completely responsible for your system.

Of course the best route to extend a root volume in an unmounted state is to boot of a Live CD. In some cases and even more relevant a lot of cloud instances you do not have normal physical or virtualization methods to access server consoles.

I have been trying a couple ideas. First one is using dracut to resize the root volume on system bootup. I have had success with this method. The second method is using pivot_root. On this method it may be slightly easier on systemd servers but for my paritcular need I have an older Centos 6 flavor without systemd and so far I could not get pivot_root and resize online to work.

Below is notes on method 1 (dracut resize):

Specs:
VirtualBox VM CentOS6.4
Disk layout 15G
- /boot 200M ext4
- / LV 13G ext4
- /u01 LV 2G ext4

GOAL:
1. Resize / file system smaller without boot CD only SSH access. Using dracut bootup.
2. Extend /u01 with the additional space in the VG

[root@localhost ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroupSys-LogVolRoot
                       11G  2.1G  8.7G  20% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             194M   29M  155M  16% /boot
/dev/mapper/VolGroupSys-LogVolU01
                      2.1G   68M  2.0G   4% /u01
[root@localhost ~]# pvs
  PV         VG          Fmt  Attr PSize  PFree
  /dev/sda2  VolGroupSys lvm2 a--  14.80g 1.70g

[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  VolGroupSys   1   2   0 wz--n- 14.80g 1.70g

[root@localhost ~]# lvs
  LV         VG          Attr      LSize  Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVolRoot VolGroupSys -wi-ao--- 11.00g                                             
  LogVolU01  VolGroupSys -wi-ao---  2.11g    

# pwd
/usr/share/dracut/modules.d/95rootfs-block

# vi mount-root.sh
[..]
if [ -n "$root" -a -z "${root%%block:*}" ]; then
    ## custom code testing lvresize on root vol
    lvm vgchange -ay --config " global {locking_type=1} "
    rm -f /etc/lvm/lvm.conf
    e2fsck -C 0 -f /dev/VolGroupSys/LogVolRoot
    resize2fs -p -f /dev/VolGroupSys/LogVolRoot 8G
    lvm lvresize -f /dev/VolGroupSys/LogVolRoot -L 8G
    resize2fs -p -f /dev/VolGroupSys/LogVolRoot
    e2fsck -C 0 -f /dev/VolGroupSys/LogVolRoot

    mount -t ${fstype:-auto} -o "$rflags" "${root#block:}" "$NEWROOT" \
[..]

# dracut -f --install 'resize2fs e2fsck'

# reboot

Verify after the reboot

# pvs
  PV         VG          Fmt  Attr PSize  PFree
  /dev/sda2  VolGroupSys lvm2 a--  14.80g 4.70g

# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  VolGroupSys   1   2   0 wz--n- 14.80g 4.70g

# lvs
  LV         VG          Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVolRoot VolGroupSys -wi-ao--- 8.00g                                             
  LogVolU01  VolGroupSys -wi-ao--- 2.11g                                             

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroupSys-LogVolRoot
                      7.9G  2.1G  5.8G  26% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             194M   30M  155M  16% /boot
/dev/mapper/VolGroupSys-LogVolU01
                      2.1G   68M  2.0G   4% /u01

** remove custom code from mount-root.sh

# dracut -f
# reboot

#################################################################
### EXTEND u01
#################################################################

# lvextend -L+2G /dev/VolGroupSys/LogVolU01 
  Extending logical volume LogVolU01 to 4.11 GiB
  Logical volume LogVolU01 successfully resized

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

# vgs
  VG          #PV #LV #SN Attr   VSize  VFree
  VolGroupSys   1   2   0 wz--n- 14.80g 2.70g

# lvs
  LV         VG          Attr      LSize Pool Origin Data%  Move Log Cpy%Sync Convert
  LogVolRoot VolGroupSys -wi-ao--- 8.00g                                             
  LogVolU01  VolGroupSys -wi-ao--- 4.11g                                             

# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroupSys-LogVolRoot
                      7.9G  2.1G  5.8G  26% /
tmpfs                 1.9G     0  1.9G   0% /dev/shm
/dev/sda1             194M   29M  155M  16% /boot
/dev/mapper/VolGroupSys-LogVolU01
                      4.1G   69M  3.8G   2% /u01

#################################################################                     
### APPENDIX: 
#################################################################

** Use /var/messages/boot.log to check dracut failures on our custom code