1

I have a need to shrink lvm2 partition, that was created by CentOS 7.4 installer. I am confused with what I see by lvm commands:

pvdisplay

root@ubuntu:/home/ubuntu# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sdb2
  VG Name               sample-group
  PV Size               28.32 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              7250
  Free PE               0
  Allocated PE          7250
  PV UUID               xutuoL-6546d-rttert-n7tH-eeXu-24t3-dgf33

vgdisplay

root@ubuntu:/home/ubuntu# vgdisplay
  --- Volume group ---
  VG Name               sample-group
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               28.32 GiB
  PE Size               4.00 MiB
  Total PE              7250
  Alloc PE / Size       7250 / 28.32 GiB
  Free  PE / Size       0 / 0  
  VG UUID               ytjytj-QWT5-sdf-vSMj-rKtj-gfhdfh-76885

lvdisplay

root@ubuntu:/home/ubuntu# lvdisplay
  --- Logical volume ---
  LV Path                /dev/sample-group/swap
  LV Name                swap
  VG Name                sample-group
  LV UUID                JIZL0D-kNfL-aEsR-y6Rl-1mzx-wWpO-ngcElh
  LV Write Access        read/write
  LV Creation host, time cube, 2018-04-20 07:15:29 +0000
  LV Status              available
  # open                 0
  LV Size                2.93 GiB
  Current LE             751
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/sample-group/root
  LV Name                root
  VG Name                sample-group
  LV UUID                4OIJBb-6MSa-y5Ua-62bI-O6Lf-pjWZ-Qnz4Gv
  LV Write Access        read/write
  LV Creation host, time cube, 2018-04-20 07:15:30 +0000
  LV Status              suspended
  # open                 0
  LV Size                25.39 GiB
  Current LE             6499
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

From the above output we see that there is totally no free space on the disk, but this is what I see in file-manager:

enter image description here

Almost the whole disk is empty, but why it lies me in console?

I cannot estimate how much space do I have and cannot shrink it. Tried with kvpm and it shows the same.

UPDATE: After the Emmanuel's manipulations on parted resizepart step it throws

 Error: can't have the end before the start (start sector=2099200 length=-2073809)

And here is what pvs/vgs shows:

 PV         VG           Fmt  Attr PSize  PFree
 /dev/sdb2  sample-group lvm2 a--  4.00g  1.06g

UPDATE 2:

Successfully resized the partition, but now xfsdump refuses to restore my filesystem:

xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.6 (dump format 3.0) - type ^C for status and control

xfsdump: WARNING: most recent level 0 dump was interrupted, but not resuming that dump since resume (-R) option not specified
xfsdump: level 0 dump of ubuntu:/mnt/root
xfsdump: dump date: Sat May 26 18:14:53 2018
xfsdump: session id: 2c905a06-6e7d-400e-8a03-5e0e20ea4583
xfsdump: session label: "new"
xfsdump: ino map phase 1: constructing initial dump list
xfsdump: ino map phase 2: skipping (no pruning necessary)
xfsdump: ino map phase 3: skipping (only one dump stream)
xfsdump: ino map construction complete
xfsdump: estimated dump size: 20800 bytes
xfsdump: positioned at media file 0: dump 0, stream 0
xfsdump: ERROR: media contains valid xfsdump but does not support append
xfsdump: dump size (non-dir files) : 0 bytes
xfsdump: NOTE: dump interrupted: 8 seconds elapsed: may resume later using -R option
xfsdump: Dump Summary:
xfsdump:   stream 0 /media/ubuntu/SYSTEM/root.backup ERROR (operator error or resource exhaustion)
xfsdump: Dump Status: INTERRUPT
  • LVM does not deal with any filesystems on it. – Ignacio Vazquez-Abrams May 20 '18 at 17:51
  • But here they say one can use those commands for showing free space. What does PFree coumn is used for? – Suncatcher May 20 '18 at 17:53
  • Free space in the LVM structures. The filesystems are not part of that structure. – Ignacio Vazquez-Abrams May 20 '18 at 17:55
  • So what would be the correct way to shrink those partition? I tried GParted but it doesn't support LVM. – Suncatcher May 20 '18 at 18:00
  • 2
    First you shrink the filesystems, then you shrink the LVs, then you shrink the VG, then you shrink the PV, then you shrink the partition. – Ignacio Vazquez-Abrams May 20 '18 at 18:03
4

To shrink your LVM partition you'll need to:

  1. Boot with a Linux live CD/DVD
  2. Backup your XFS filesystem (shrinking XFS is not supported)
  3. Shrink your logical volume
  4. Shrink your physical volume
  5. Shrink your partition
  6. Restore your XFS filesystem

NOTICE: Before you begin check your /etc/fstab to see how the root filesystem device is being mounted. If it's /dev/sample-group/root you're good to go. But if it's using a filesystem label you need to make sure you use the -L option with mkfs.xfs to specify the same label. If it's using a UUID or something like that you'll need to adjust your /etc/fstab once you have your new ID.

Backup your XFS filesystem

Backup your filesystem -ideally to another device- using xfsdump, like this:

mkdir /mnt/backup
mkdir /mnt/root
mount /dev/sample-group/root /mnt/root
mount /dev/some-backup-device /mnt/backup
xfsdump -f /mnt/backup/root.backup /mnt/root
umount /mnt/root

"Shrink" your logical and physical volumes, and partition

While it is possible to shrink the root logical volume with lvreduce, it's just easier to delete it and recreate it using the desired size. So, you'll delete the logical volume, shrink the physical volume to be just large enough to contain your remaining logical volume (swap), shrink the partition to the desired size, resize the physical volume so it uses up all the space in the partition, and finally recreate the logical volume. I know that seems like a lot, but it avoids having to specify the resizing units accurately, which when mismatched can lead to errors.

In this example, the target size of the new logical volume is ~10GB (3GB swap + 10GB root = 13GB partition):

lvremove /dev/sample-group/root
pvresize --setphysicalvolumesize 4GB /dev/sdb2
parted /dev/sdb resizepart 2 13GiB
pvresize /dev/sdb2
lvcreate -ay -l 100%FREE -n root sample-group

The 100%FREE tells lvcreate to use all the space remaining in the volume group, which in this example amounts to ~10GB. Adjust as necessary.

Restore your XFS filesystem

Now you're ready to restore your XFS filesystem:

wipefs /dev/sample-group/root
mkfs.xfs /dev/sample-group/root
mount /dev/sample-group/root /mnt/root
xfsrestore -f /mnt/backup/root.backup /mnt/root
umount /mnt/root
umount /mnt/backup

Finally, cross your fingers and reboot.

  • Thanks you a lot, Emmanuel! I appreciate your effort. I am stuck at resizing part parted /dev/sdb resizepart 2 13 Gib. I updated the question. – Suncatcher May 26 '18 at 2:57
  • OK, so the end parameter in the resizepart command is an offset from the beginning of the disk. I can't tell you which offset to use, so you'll have to run parted /dev/sdb to enter interactive mode. Then enter the print command to display your partitions; Note this won't show your logical volumes. Using the start and end numbers displayed for each partition you'll have to calculate what the new end number should be to give you the correct partition size. Then, run resizepart [PARTITION_NUMBER] [OFFSET]. See gnu.org/software/parted/manual/html_node/unit.html#unit – Emmanuel Rosa May 26 '18 at 15:46
  • Thanks again. Now stuck on the last step, see my update. – Suncatcher May 26 '18 at 18:17
  • Oh, sorry that should have been xfsrestore not xfsdump: xfsrestore -f /mnt/backup/root.backup /mnt/root – Emmanuel Rosa May 26 '18 at 20:07
  • Finally I've succeeded! Thanks a ton! And thanks you for your patience! – Suncatcher May 27 '18 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.