Lvm – lvreduce after lvextend without losing the ext4 partition inside it

ext4lvm

In a botched attempt to move my root partition from one disk to another I have done the following:

  • added new disk
  • partitioned it with parted (part #3 is now almost totally filling the disk)
  • initialized a physical volume

    $ pvcreate /dev/sdb3
    Physical volume "/dev/sdb3" successfully created
    
  • extended the volume group to include this new physical disk

    $ vgextend myvg /dev/sdb3
    Volume group "myvg" successfully extended
    
  • extended the logical volume (I think this is where I ballsed it up: I think I should have pvmove'ed stuff to the new pv in that group – can someone confirm?)

    $ lvextend /dev/mapper/myvg-root /dev/sdb3
    

I would now like to undo the lvextend and then proceed with the original plan of moving the content of the old physical volume over to the new physical volume. Can I reduce the logical volume (I have not yet touched the ext4 partition that sits in /dev/mapper/myvg-root with something like resizefs) without fear of damaging the ext4 filesystem? If so, how do I tell it to reduce by exactly the right amount?

$ lvreduce --by-exactly-the-amount-occupied-by-PV /ev/sdb3 /dev/mapper/myvg-root

Best Answer

To summarize, currently there is an ext4 filesystem (root) inside a logical volume (myvg-root) that sits in a volume group (myvg) that is made up of two physical volumes (one of which is the newly added sdb3).

You can inspect the mapping of logical extents to physical extents with lvdisplay

$ lvdisplay -m
  --- Logical volume ---
LV Name                /dev/myvg/root
VG Name                pfeifer
LV UUID                2LeFq8-zz3y-Y62m-931D-mD3m-XmK1-6Zp3Ir
LV Write Access        read/write
LV Status              available
# open                 1
LV Size                3.62 TiB
Current LE             947851
Segments               2
Allocation             inherit
Read ahead sectors     auto
- currently set to     256
Block device           251:0

--- Segments ---
Logical extent 0 to 470980:
  Type              linear
  Physical volume   /dev/sda3
  Physical extents  0 to 470980

Logical extent 470981 to 947850:
  Type              linear
  Physical volume   /dev/sdb3
  Physical extents  0 to 476869

You can simply reduce the size of this logical volume with lvreduce

$ lvreduce -l -476870 /dev/myvg/root
  WARNING: Reducing active and open logical volume to 1.80 TiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce root? [y/n]: y
  Reducing logical volume root to 1.80 TiB
  Logical volume root successfully resized

Note how the number of logical extents on sdb3 to be reduced is 476869+1 (counting starts at 0!). That's it. Now a quick check seems to confirm the original world order:

  --- Segments ---
  Logical extent 0 to 470980:
    Type                    linear
    Physical volume         /dev/sda3
    Physical extents        0 to 470980

Now a slow and tedious pvmove command should allow the moving of data over:

nohup pvmove -n /dev/myvg/root /dev/sda3 /dev/sdb3 &