0

Let's say I have a HDD with one partition at /dev/sdb1 with the following contents:

% sudo mount /dev/sdb1 /mnt
% cd /mnt
% ls
osA/ osB/
% ls *

osA:
bin  boot/  dev/  etc/  home/  lib  lib64  lost+found/  mnt/
opt/ proc/  root/ run/  sbin   srv/ sys/   tmp/         usr/  var/

osB:
bin  boot/  dev/  etc/  home/  lib  lib64  lost+found/  mnt/
opt/ proc/  root/ run/  sbin   srv/ sys/   tmp/         usr/  var/

I'd like to somehow have the directory osA on /dev/sdb1 mounted as / during boot through /etc/fstab (possibly) and to be able to change that later to be osB if I need that. I'd like this done on ext4 fs (if possible). Is this doable?

Another way of wording:

I'd like a partition to host multiple "root file system directories" and to be able to mount either directory to be / at boot time.

I always dedicated a whole partition to /. Not sure how to go about it when it's a subdirectory on a partition.

My current /etc/fstab entry (whole partition is dedicated to /):

UUID=0a7baeae-857d-11eb-8dcd-0242ac130003 / ext4 rw,relatime 0 1

I'm running Archlinux.

Motivation:

  • Sometimes I have to reinstall the system because of problems with the package manager. Thought that perhaps this approach could be quicker than setting everything up from scratch, with a way to go back to previous setup.
  • Seeing this as a possible way to try distributions and package managers by just switching what / is mounted to
  • By having only one partition on /dev/sdb I would not have to resize OS and data partitions, could just have data as a "data" folder alongside "osA" and "osB"

Edit1:

If you have suggestions on how I could ask the question more clearly - please let me know.

These questions seem somewhat related although the first one seems to delve into some unrelated things, second received no answer, checking third: 39423 374806 206493 613179

I think I'm looking to see if I can bind-mount / in /etc/fstab once /dev/sdb1 is already mounted somewhere (never done this before, don't know if it will work - can try this and post results - insights welcome).

Edit2:

Realised that to bind-mount I need a mount point first, and can't have one without the / itself.

  • Reading your motivation, this would be better solved by taking snapshot of the root volume, and going it back in case of problems. You would need to use lvm (here I suggest to use [lvm thin pools](https://wiki.archlinux.org/index.php/LVM#Thin_Provisioning)) and put ext4 over it, or move to [btrfs](https://wiki.archlinux.org/index.php/Btrfs). – marbu Mar 16 '21 at 08:52
  • @marbu lvm snapshots may indeed be a good way to go about it. I'd like to see first if I can accomplish what I want by tweaking settings in `/boot/grub/grub.cfg` and `/etc/fstab` – troubleshoot Mar 16 '21 at 10:42

1 Answers1

-1

I think you've dismissed the first question's answer too quickly. I was about to writeup pretty much the same answer. So for the discussion of exactly how to do this I would follow that answer.

Why you need to do this with initramfs...

Most mount points are defined in /etc/fstab. However this is [virtually always] stored on the root file system (/). You can't configure which partition is root in a file stored on root. You need something to configure which partition is mounted as root before /etc/fstab can be read. It's the job of initramfs to mount root (/) and then chroot into it.

It's important to understand your system is already mounting a different file system as it's first root file system, then mounting your chosen root filesystem elsewhere, then calling chroot into that file system.

As the other answer says, there's not [commonly] any configuration option in initramfs to make it chroot into a subdirectory. But it's simple enough to modify the scripts /init runs and thus modify where the script will chroot into.

Philip Couling
  • 17,591
  • 5
  • 42
  • 82
  • The first questions's answer gives an example for Debian, I don't have a `/etc/initramfs-tools/` folder on Archlinux so can't follow along the steps. Good point about `/etc/fstab` not being available before `/` itself is mounted. I don't have an `/init` directory either. Trying to find how I specified the partition when setting up GRUB back when installing the system. – troubleshoot Mar 16 '21 at 10:14
  • The menu entry in `/boot/grub/grub.cfg` seems to contain the UUID of the partition "0a7baeae..." so that must be it. – troubleshoot Mar 16 '21 at 10:25
  • I'll see if I can find how this is setup under Arch. `/init` is not on your root file system. It's on initramfs. To be clearer, initramfs is a file (typically in the same directory as the kernel) which is loaded by the bootloader when it also loads the kernel. This is then directly monted by the kernel from memory as it's first filesystem. – Philip Couling Mar 16 '21 at 10:55
  • The UUID in menu entries in `/boot/grub/grub.cfg` is passed by grub to the kernel. The kernel makes this (and other params) available to initramfs. – Philip Couling Mar 16 '21 at 10:57