I'm trying to setup a Linux system that runs from an LVM-formatted image file. After some tinkering with the initramfs and boot options I managed to make it up and running by mounting the host file system to /run/initramfs/host, losetuping the image to /dev/loop0 and making sure the kernel and udev detect the LVM (and the root LV) in there. So far so good.

The problem is that when shutting down (or rebooting, or …) the system neither the root file system nor the host are unmounted properly, because of a chicken-and-egg scenario: the root (or /oldroot, as it's referred to by the shutdown script) cannot be unmounted, because /oldroot/run/initramfs/host is still mounted, and the host cannot be unmounted, because doing so would make /oldroot inaccessible.

Unclean shutdowns aren't the end of the world, because both file systems are journaled, so during the next boot fsck simply replays the journals, but obviously clean shutdowns would be better.

So the question is: is it somehow possible to arrange the shutdown sequence (I can modify the shutdown script), or the bootup sequence (perhaps by moving the host mount point to a different place) so that both file systems can be cleanly unmounted?

  • Why losetup under lvm? Lvm use dmsetup wich is a kind of more featured losetup. – F. Hauri Jan 13 '13 at 16:55
  • See mount --move – F. Hauri Jan 13 '13 at 16:57
  • @F.Hauri: losetup, because it's the first thing I thought of that turns a file into a block device. Also, the chicken-and-egg problem at hand is not really related to LVM, is it? – user30223 Jan 13 '13 at 17:42
  • Could you tell me where is the shutdown script? I have the same problem, so I need to manage it. Thank you! – Antonio Petricca Mar 14 '18 at 13:16

In case somebody has the same problem:

All I needed was to move the mount point of the host file system to a place outside the root file system in the shutdown script (that's fine, because it runs in a tmpfs pivot root) before any unmounting takes place:

mount --move /oldroot/run/initramfs/host /host

This allows /oldroot to unmount cleanly. The host file system can be then unmounted with a simple

umount /host
  • Would you mind explaining where this shutdown script might be placed? Thanks! – user541686 Jul 1 '17 at 16:36
  • Please could you explain how to write that shutdown script? – Antonio Petricca Mar 18 '18 at 16:04

There is some linux trick to do this kind of work:

  • mount --move which let you swap filesystem on mount point
  • pivot_root which work with chroot for switch / root filesystem

Initialy, the feature's goal was:

  1. booting kernel with an initramdisk as root filesystem (reserving some RAM for uncompressed initrd).
  2. All needed modules and scripts to access real root device are initiated in this 1'st boot step. (This could contain network initialisation and network devices mounting)
  3. After preparing, checking real root device and mounting as a ready root filesytem. Running pivot_root (and chroot), so after operation, the ramdisk will become an unused ram disk mounted at /initrd.
  4. So from there, it is possible to umount /initrd and freeing memory.

Have a look at man mount and man pivot_root!

  • +1 for mount --move – user30223 Jan 13 '13 at 18:16
  • As I don't know your hierarchy and not sure to clearly understand your goal, I can't offer a more detailed solution (than you could find in man mount ... and I hate to simply cut'n past from man pages) – F. Hauri Jan 13 '13 at 18:25
  • That's okay, you pointed me in the right direction by mentioning mount --move. – user30223 Jan 13 '13 at 18:27
  • Just for my comprehension: Why did you need to umount /old before umount /old/blah/host ?? – F. Hauri Jan 13 '13 at 18:35
  • Because /old is actually (a part of) a file stored in /old/blah/host. – user30223 Jan 13 '13 at 18:45

To mark a filesystem as cleanly unmounted without making the files on it unavailable, remount it as read-only:

mount -o remount,ro /mount/point

This can be done even if there are files open for reading, running executables, current directories or active mount points on that filesystem. Only files open for writing prevent remounting read-only.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy