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

2,142

Solution 1

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).

Solution 2

What you suggest shouldn't be done online, as it's error prone and possibly dangerous. You need to resize partitions to do that, you likely lose data when trying to mess with the partition your system's root filesystem is placed on. This is what that check is for.

Use a livecd with gparted to resize your partitions, then setup an encrypted filesystem in the freed up space.

Share:
2,142

Related videos on Youtube

TruMan1
Author by

TruMan1

Updated on September 18, 2022

Comments

  • TruMan1
    TruMan1 over 1 year

    The TabView seems to stretch to its maximum height, but I'd like it to shorten to height as much as it requires instead of empty space. This is what it looks like even tho there are Spacer views around it:

    enter image description here

    struct ContentView: View {
        var body: some View {
            Text("Some text")
            Text("Some text")
            Text("Some text")
            Spacer()
            TabView {
                GroupBox(label: Text("This is the title 1")) {
                    Label("Some item 1", systemImage: "checkmark.circle.fill")
                    Label("Some item 2", systemImage: "checkmark.circle.fill")
                    Label("Some item 3", systemImage: "checkmark.circle.fill")
                    Label("Some item 4", systemImage: "checkmark.circle.fill")
                }
                .padding()
                GroupBox(label: Text("This is the title 2")) {
                    Label("Some item 1", systemImage: "checkmark.circle.fill")
                    Label("Some item 2", systemImage: "checkmark.circle.fill")
                    Label("Some item 3", systemImage: "checkmark.circle.fill")
                    Label("Some item 4", systemImage: "checkmark.circle.fill")
                }
                .padding()
                GroupBox(label: Text("This is the title 3")) {
                    Label("Some item 1", systemImage: "checkmark.circle.fill")
                    Label("Some item 2", systemImage: "checkmark.circle.fill")
                    Label("Some item 3", systemImage: "checkmark.circle.fill")
                    Label("Some item 4", systemImage: "checkmark.circle.fill")
                }
                .padding()
            }
            .tabViewStyle(PageTabViewStyle())
            .background(Color.yellow)
            Spacer()
            Text("Some text")
            Text("Some text")
            Text("Some text")
        }
    }
    

    Is there a way to dynamically tighten the height to what's needed by its children?

    • fuero
      fuero over 9 years
      What are you trying to do? This seems like a bad, bad idea.
    • peterh
      peterh over 9 years
      You tried to reformat your root partition??? Is it real? And now you ask, why it doesn't work? You had to be thankful to God, that the lvm developers put this check in their code!
    • Claudio Ferraro
      Claudio Ferraro over 9 years
      I'm trying to divide my partition in many LVMs since I need 1 free volume to encrypt my data.
    • Claudio Ferraro
      Claudio Ferraro over 9 years
      sdb3 is the root of the current linux installation.
    • Aaron Copley
      Aaron Copley over 9 years
      You are trying to change the foundation that the house is already built on top of. Surely you can understand why this is a problem?
    • fuero
      fuero over 9 years
      @ClaudioFerraro get the gparted livecd and use that.
    • Claudio Ferraro
      Claudio Ferraro over 9 years
      to run gparted I need access to the server. So I need to call my provider . or not ?
    • mahan
      mahan about 3 years
      Did my answer fix the issue?
    • TruMan1
      TruMan1 about 3 years
      It caused weird layout issues, probably more related to my specific app. But did get me closer.. Thx!
  • peterh
    peterh over 9 years
    Currently I am writing him, how can it be done.
  • fuero
    fuero over 9 years
    edited the answer to suggest that doing it online is dangerous.
  • peterh
    peterh over 9 years
    Of course. Very.
  • fuero
    fuero over 9 years
    +1 for pivot_root, didn't know that.
  • Duc Dang
    Duc Dang over 1 year
    not working, my tabview disappeared
  • Peter Lapisu
    Peter Lapisu over 1 year
    this does no longer work