Resize LVM without data loss

I have Cloud Linux Serwer with LVM with two disk (40 + 50). For today, I do not need a second drive and I would like to unplug it by keeping LVM for the future.

# vgdisplay
  --- Volume group ---
  VG Name               system
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  20
  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               89.75 GiB
  PE Size               4.00 MiB
  Total PE              22977
  Alloc PE / Size       22977 / 89.75 GiB
  Free  PE / Size       0 / 0
  VG UUID               QkkfoN-Ftor-suCL-VF4J-huV2-lhWp-9K5cMx

# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/system-root ext4       88G   21G   63G  25% /
none                    tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
udev                    devtmpfs  485M  4.0K  485M   1% /dev
tmpfs                   tmpfs     100M  412K   99M   1% /run
none                    tmpfs     5.0M     0  5.0M   0% /run/lock
none                    tmpfs     496M     0  496M   0% /run/shm
none                    tmpfs     100M     0  100M   0% /run/user
/dev/vda1               ext2      236M   98M  127M  44% /boot

# lsblk
NAME                     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0                       11:0    1  1024M  0 rom
vda                      253:0    0    40G  0 disk
├─vda1                   253:1    0   243M  0 part /boot
├─vda2                   253:2    0     1K  0 part
└─vda5                   253:5    0  39.8G  0 part
  ├─system-root (dm-0)   252:0    0  88.8G  0 lvm  /
  └─system-swap_1 (dm-1) 252:1    0     1G  0 lvm  [SWAP]
vdb                      253:16   0    50G  0 disk
└─vdb1                   253:17   0    50G  0 part
  └─system-root (dm-0)   252:0    0  88.8G  0 lvm  /

# pvdisplay --maps
  --- Physical volume ---
  PV Name               /dev/vda5
  VG Name               system
  PV Size               39.76 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              10178
  Free PE               0
  Allocated PE          10178
  PV UUID               aUQpTn-xi1T-ygwu-emIh-I2CT-w2tp-cRUNjl

  --- Physical Segments ---
  Physical extent 0 to 9921:
    Logical volume      /dev/system/root
    Logical extents     0 to 9921
  Physical extent 9922 to 10177:
    Logical volume      /dev/system/swap_1
    Logical extents     0 to 255

  --- Physical volume ---
  PV Name               /dev/vdb1
  VG Name               system
  PV Size               50.00 GiB / not usable 2.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              12799
  Free PE               0
  Allocated PE          12799
  PV UUID               5IbHi7-bS0j-ZDEQ-pPfY-BGW4-PAk0-pEV8VH

  --- Physical Segments ---
  Physical extent 0 to 12798:
    Logical volume      /dev/system/root
    Logical extents     9922 to 22720

And now if he wants to use command lvresize

# lvresize --size -50G /dev/mapper/system-root
  WARNING: Reducing active and open logical volume to 38.75 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)

The system warns me about the loss of data. How can I reduce space by disconnecting the disk without losing data?


You have to shrink the ext4 filesystem before to reduce the logical volume size. You have to unmount the root partition so in order to shrink the filesystem you have to boot the server with a USB/DVD Linux iso and execute the rescue mode.

Example: https://www.thegeekdiary.com/centos-rhel-how-to-shrink-lvm-root-file-system/


From the link offered by NoNoNo above, the resize2fs + lvreduce commands can be done in one liner, which I think is best. Especially because the lvreduce will otherwise spit out a really bad warning.

BEFORE RUNNING THOSE INSTRUCTIONS, MAKE SURE TO BACKUP YOU IMPORTANT DATA.

The instructions can be resumed to:

  1. boot on a live CD/DVD (Linux or GParted Live)

  2. make sure your root partition is not mounted

    umount /dev/[partition-name]
    
  3. make sure LVM is up (probably not required, at least on GParted Live)

    vgchange -ay
    
  4. clean up

    e2fsck -f /dev/[volumegou]/[logicalvolume]
    

    Repeat until no more errors occur to increase chances that things will work as expected while resizing the file system.

  5. reduce partition

    lvresize --resizefs -L [newlvsize] /dev/[volumegroup]/[logicalvolume]
    

    Because of the --resizefs the lvresize will take a long time (assuming you have a large partition, my 1.8Tb took about 5 hours to reduce to about 1.1Tb). You should be getting some feedback in your console.

    Note: The LVM environment offers three commands to resize: lvresize, lvreduce, and lvextend. The resize let you change the size (bigger or smaller), the reduce only allows smaller sizes (i.e. at most X Gb) and the extend only allows larger sizes (i.e. at least X Gb).