Centos – Can’t open /dev/xxx exclusively. Mounted filesystem. How to create volume groups and lvm on mounted disk

centoslvmpartition

I've 3 partitions sdb1 sdb2 and sdb3.
I wanna to create a volume group and lvms in the sdb3 which is the mounted linux partition where is located my root folder.

I'm typing:

pvcreate /dev/sdb3 

but i get:

Can't open /dev/sdb3 exclusively. Mounted filesystem.

So well, I know that my partition is mounted in fact I'm just working on it. Is there a way to create the volume group without asking my provider to reformat my partitions. I mean through SSH ?

Best Answer

You can do that, but it is hard.

The steps are essentially the following:

  1. Clone or create a rescue system on a ramfs, and start it. It must be enough big to be able to get new packages, if you forgot something.
  2. Start it in a chrooted environment.
  3. Start his own ssh daemon on it (which will be run on a port different from 22, of course). Of course, you should test if you can log into this ssh daemon.
  4. Here is the key. It works only on linux, it is named pivot_root. This syscall/tool can change the root partition of your currently running system.
  5. pivot_root /mnt/rescuesys, where your chrooted ramfs rescue system is running. If it succedded, from this point your original system will run in chroot, and your rescue system will run as the real root system.
  6. With a telinit q command restart the init (the process 1) to run also from your real (now ramfs) system and not from the sdb3-based one. Maybe telinit Q, telinit u or telinit U could be also needed for that, it depends on your init system, too.
  7. With an lsof -n|grep /mnt/<oldsysroot> command you can check, if there is any existing open fiedescriptor to your old system. If yes, kill, stop them one by one.
  8. Umount, swapoff, until nothing remains from your old system. If you did everything okay, now you can umount your old sdb3. If not, goto 7 (or even 6).
  9. Backup the contents of your sdb3. You could do this even with a little bit of filesystem resizing, repartitioning, copying, as you have place.
  10. Create your lvm volume with your pvcreate, etc. commands, create the lvs, mount them, and copy your root system back.
  11. Do the 1-9 points again, including the pivot_root. From this point, your real root system is soon the logical volume, which you originally wanted to have.
  12. Destroy your rescue system (as 6-8 point did) to free memory. Don't forget to enable swap. In ideal, security-optimized case it should also live on an encrypted logical volume, despite it degrades speed seriously.
  13. Fix /etc/fstab, reconfigure/reinstall grub, and any similar.
  14. Because you can't be sure, that your system is really bootable, until you didn't booted it, do everything as you can, but try to make your next reboot if you are physically by your server (or, have at least sufficient remote management capabilities, which can be a remote management service of your hosting provider).
Related Topic