Search code examples
ubuntu-18.04lvm

How to move mounted and mapped logical volume from one volume group/physical volume to another


I know that there are many similar questions but none of the solutions works for me as it is a little complicated in my case. I'm sorry if I overlooked one of them.

So I have a Ubuntu server with 2 physical volumes, one is a 4TB HDD and another one is a 250GB SSD.

PV /dev/sda3   VG ssd             lvm2 [<217.43 GiB / <197.43 GiB free]
PV /dev/sdb1   VG hdd             lvm2 [<3.64 TiB / 3.63 TiB free]
Total: 2 [3.85 TiB] / in use: 2 [3.85 TiB] / in no VG: 0 [0   ]

They are mapped into 2 volume groups HDD and SSD respectively.

Reading volume groups from cache.
Found volume group "ssd" using metadata type lvm2
Found volume group "hdd" using metadata type lvm2

The volume groups are mounted to different paths:

ACTIVE            '/dev/ssd/usr' [5.00 GiB] inherit
ACTIVE            '/dev/ssd/var' [5.00 GiB] inherit
ACTIVE            '/dev/ssd/home' [5.00 GiB] inherit
ACTIVE            '/dev/ssd/opt' [5.00 GiB] inherit
ACTIVE            '/dev/hdd/data' [4.00 GiB] inherit

They are also mounted/mapped like this

Filesystem            Size  Used Avail Use% Mounted on
udev                   32G     0   32G   0% /dev
tmpfs                 6.3G  7.6M  6.3G   1% /run
/dev/sda1             3.9G  745M  3.2G  19% /
/dev/ssd/usr          4.8G  1.7G  2.9G  37% /usr
tmpfs                  32G     0   32G   0% /dev/shm
tmpfs                 5.0M   20K  5.0M   1% /run/lock
tmpfs                  32G     0   32G   0% /sys/fs/cgroup
/dev/mapper/ssd-opt   4.8G  840M  3.8G  19% /opt
/dev/mapper/ssd-home  4.8G   10M  4.6G   1% /home
/dev/mapper/ssd-var   4.8G  944M  3.7G  21% /var
tmpfs                 6.3G     0  6.3G   0% /run/user/0
/dev/mapper/hdd-data  3.9G  8.0M  3.6G   1% /data

I want to move everything to HDD and divide all available spaces for the logical volumes. I didn't setup the LVM initially so I don't know how to do this.

I have tried using vgextend and it gave this error

Can't open /dev/sdb1 exclusively.  Mounted filesystem?

And I tried using pvmove and it gave this error

Failed to find device for physical volume "hdd".

I also tried using vgmerge and this error came out

Logical volumes in "ssd" must be inactive

And if I tried to deactivate the logical volume this happened:

Logical volume ssd/usr contains a filesystem in use.
Can't deactivate volume group "ssd" with 4 open logical volume(s)

So I tried to unmount the filesystem and it won't let me

umount: /usr: target is busy.

And I don't think I can stop everything that is using the directories without breaking the server... It seems like I need to shut down the server to do that but then I can't run any command if the server is down...

As you can see I am stuck and scratching my head. Any help would be very much appreciated.


Solution

  • So I did eventually find a way to get it done... In case anyone that comes across this rare case, here's how I did it:

    So as stated in the question, I am unable to unmount the logical volume or do anything with it while it is mounted and actively being used. So the only option for me is to boot into Recovery Mode where everything will be stopped and only critical stuff is running. That means I can now unmount the logical volume.

    So once in the Recovery Mode, I created a new logical volume using lvcreate on the other volume group that is on another physical volume (its complicated I know).

    Then I copy everything in the existing volume (yes haven't unmounted it yet) to the new volume using cp -rp (yes there are other ways to do that and you can also just straight-up move them).

    Now, I can unmount both volumes and delete the old volume using lvremove.

    And finally, we go to change the mounting point in the /etc/fstab (open with text editor) and it is done.

    Reboot back to normal operation mode and it should work perfectly 😎

    Hope this helps... but its a rare situation so probably not very helpful 😅