2

Let's say I created the following Linux LVM volume group and logical volumes:

$ sudo vgcreate vg1 /dev/sdb1
$ sudo lvcreate -L 10G -n lv1 vg1
$ sudo lvcreate -L 10G -n lv2 vg1
$ sudo lvcreate -L 10G -n lv3 vg1

$ sudo mkfs.ext4 /dev/mapper/vg1-lv1
$ sudo mkfs.ext4 /dev/mapper/vg1-lv2
$ sudo mkfs.ext4 /dev/mapper/vg1-lv3

I end up with the following, which would all be using ext4 filesystem:

$ sudo lvs
  LV     VG       Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv1    vg1      -wi-a-----  10.00g                                                    
  lv2    vg1      -wi-a-----  10.00g                                                    
  lv3    vg1      -wi-a-----  10.00g                                                    

I would like to reduce the lv1 size from 10G to 5G, and grow the lv2 size from 10G to 15G. This means that lv2 would need to grow towards the front of the disk.

$ sudo lvreduce --resizefs --size 5G /dev/mapper/vg1-lv1
fsck from util-linux 2.36.1
/dev/mapper/vg1-lv1: clean, 11/655360 files, 66753/2621440 blocks
resize2fs 1.46.3 (27-Jul-2021)
Resizing the filesystem on /dev/mapper/vg1-lv1 to 1310720 (4k) blocks.
The filesystem on /dev/mapper/vg1-lv1 is now 1310720 (4k) blocks long.

  Size of logical volume vg1/lv1 changed from 10.00 GiB (2560 extents) to 5.00 GiB (1280 extents).
  Logical volume vg1/lv1 successfully resized.

Reducing lv1 seems to have worked. How do I now grow lv2?

1
  • just use lvexpand to grow lvm2 and resize4fs to expand the filesystem.
    – davidgo
    Jan 6 at 23:07

1 Answer 1

1

Yes its possible. A main point of lvm is it abstracts the disk away from the partitions so even if you free up blocks at the end of the disk (or in this case before the start of the next partition) lvm will allow you to add it to an existing partition because it maps the blocks internally. The only tricky bit herr is shrinking lv1, brcause it already has a filesystem on iit, and ext4 filesystems cant be shrunk online.

I consider all block device operations to carry a level of risk, and advise a backup prior to attempting them as a matter of best practice.

There is no songle correct solution, and im not at my pc so I cant test my dyntax, but the way I would do yhis would be -

  1. Unmount lvm1 / get it into an unmounted state ( if its a root partition you may need to use a usb disk )

  2. Shrink the filesystem on it smaller then it should finally be (you can use resize2fs/resize4fs to zhrink unmounted volumes)

  3. use lvreduce yo shrink lvm1 to its desired size. Tjis must not be smaller then the resized ext4 partition.

  4. Resize4fs (resize2fs) to expand the filesystem on lvm1.

(Strictly speaking if your math is spot on you dont need to resize the volume smaller then expand it, but I find this much easier yhen working with 512/1024/2048 byte blocks). At this point the hard part is done.

  1. Use lvexpand to grow lvm2 and resize4fs to grow the filesystem. This can be done online.
2
  • When i started writing this the question did not advise lvm1 was already resized.
    – davidgo
    Jan 6 at 23:06
  • The key is that LVM remaps blocks, which I didn't realize. Thanks!
    – Roxy
    Jan 6 at 23:09

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.