reverse extending the LVM group volume

9,965

Solution 1

First, make backups and test that they work. While this shouldn't erase any data, you could lose everything to a typo.

Since you've only extended the logical volume and not the filesystem, all you need to do is shrink the logical volume. Find out the size of the filesystem on /dev/myvg/rootvol. If it's an ext4 filesystem, tune2fs -l /dev/myvg/rootvol will tell you. Note that it's critical to get it right: if you shrink the logical volume below the size of the filesystem, you'll lose your data. Then run

lvreduce -L NNNNNNNk /dev/myvg/rootvol

where NNNNNNN is the size of the filesystem in kB.

Now run pvmove to free up the data on /dev/sda3. After this you can remove the physical volume.

pvmove /dev/sda3
vgreduce myvg /dev/sda3
pvremove /dev/sda3

Solution 2

Since I did NOT resize the FS to fill the full VG extents, and the new physical volume was empty, I was able to reduce the extents on my volume group simply with:

lvreduce -l3913721 /dev/myvg/rootvol
vgreduce rootvol /dev/sds
pvremove /dev/sds

where 3913721 were the OLD extents

Share:
9,965
Raza
Author by

Raza

Updated on September 18, 2022

Comments

  • Raza
    Raza almost 2 years

    I accidentally extended the volume to utilized 100% free disk space on LVM group volume. I need it to utilized only /dev/sdb disk but it took free space from both disk, since they were on the same group. Is there a way to reverse this.

    [root@server]# pvscan
      PV /dev/sda3   VG myvg   lvm2 [13.84 GB / 13.84 GB free]
      PV /dev/sdb3   VG myvg   lvm2 [22.06 GB / 8.22 GB free]
      Total: 2 [35.91 GB] / in use: 2 [35.91 GB] / in no VG: 0 [0   ]
    
    [root@server]# lvextend -l +100%FREE /dev/myvg/rootvol
      Extending logical volume rootvol to 35.91 GB
      Logical volume rootvol successfully resized
    
    [root@server]# pvscan
      PV /dev/sda3   VG myvg   lvm2 [13.84 GB / 0    free]
      PV /dev/sdb3   VG myvg   lvm2 [22.06 GB / 0    free]
      Total: 2 [35.91 GB] / in use: 2 [35.91 GB] / in no VG: 0 [0   ]
    

    I actually need to remove /dev/sda3.

    This is on RHEL 5.9 system.

    Please note that I do not want to loss the data

    Updated:

    If I use temporary disk then I will need about the same size of the disk, if I pvmove only one volume then how can I make sure there there is nothing in other disk and the data will not get corrupted.

    Updated:

    I added a new larger disk and pvmove /dev/sda3 and /dev/sdb3 to /dev/sdc3 (new disk). I am not sure why df still showing newly added free space on root volume.

     [root@server]# pvscan
       PV /dev/sdc3   VG myvg            lvm2 [47.06 GB / 11.16 GB free]
       PV /dev/sda3                      lvm2 [13.85 GB]
       PV /dev/sdb3                      lvm2 [22.07 GB]
       Total: 3 [82.98 GB] / in use: 1 [47.06 GB] / in no VG: 2 [35.92 GB]
    
     [root@server]# lvscan
       ACTIVE            '/dev/myvg/rootvol' [35.91 GB] inherit
    
     [root@server]# df -kh .
     Filesystem            Size  Used Avail Use% Mounted on
     /dev/mapper/myvg-rootvol
                            14G   12G  1.3G  91% /
    
    • Bratchley
      Bratchley almost 11 years
      You can shrink the volume with lvreduce, then use pvmove to move all extents associated with that volume off or towards any physical volume you specify. Also, be aware that you'll have unmount the logical volume before you do the reduce. BTRFS can handle online shrinking but not ext3/ext4.
    • Bratchley
      Bratchley almost 11 years
      OK, I'm seeing now where you did this to the root volume. There are two ways to approach this: If you absolutely need the space on this HDD back and can't do downtime, you might add some temporary space to the VG and do the online pvmove off that drive. The other option is to boot into rescue mode without searching for or mounting RHEL installations, and shrink the root LV from there.
    • Bratchley
      Bratchley almost 11 years
      Also, what do you mean by "I do need to secure the data"?
  • Raza
    Raza almost 11 years
    thanks for your response. I added a new larger disk and run pvmove new disk but it is not reflecting changes on df. Do I need to run resize2fs /dev/myvg/rootvol. Is it safe to run? I have updated the question as well.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' almost 11 years
    @Salton Did you resize the filesystem or not after enlarging the volume? If you did, you need to shrink the filesystem before shrinking the LV. The filesystem is 14GB, so it looks like it has the old size, not the new size. What surprises you in the output of df?
  • Raza
    Raza almost 11 years
    my bad.. please ignore my previous comment
  • psusi
    psusi about 7 years
    You don't need to deactivate the logical volume to shrink it, nor use a live cd.