vermaden / automount

Simple devd(8) based automounter for FreeBSD
66 stars 20 forks source link

Check if device is already mounted before mounting it again #37

Open probonopd opened 2 years ago

probonopd commented 2 years ago

On the helloSystem Live ISO which is heavily based on automount, some volumes (e.g., MONKEYPATCH in the picture) need to be mounted very early in the boot process, before automount is started. This results to these being mounted twice (see MONKEYPATCH-2).

It would be good to check if a geom is already mounted and if so, exit without trying to mount it again.

image

probonopd commented 2 years ago

Note that MONKEYPATCH can be addressed as /dev/da1 and /dev/msdosfs/MONKEYPATCH.

In this case, /dev/msdosfs/MONKEYPATCH was already mounted before automount was started, and then was mounted again from /dev/da1.

% mount
/dev/iso9660/LIVE on / (cd9660, local, read-only)
(...)
/dev/msdosfs/MONKEYPATCH on /media/MONKEYPATCH (msdosfs, local)
(...)
/dev/da1 on /media/MONKEYPATCH-2 (msdosfs, local, noatime, read-only)
vermaden commented 2 years ago

Thats the 'price' for alternative approach with 'read only' mount.

How would you know if a device is mounted? Check output of mount(8) command.

After /dev/msdosfs/MONKEYPATCH was mounted ... does /dev/da1 appear in the mounted list? No. So its not mounted to the automount. Then automount tries to mount it read write and it fails because its already mounted so it tries to mount it read only and that works.

Can you do not use /dev/msdosfs path for mounting and just use 'plain' devices as da0/da1/...?

probonopd commented 2 years ago

We can't trust that user would always do this, so we need to find a solution.

So I propose, when e.g., /usr/local/sbin/automount da1 attach is invoked, to check using something like

dmesg | grep "Label for provider da1 is" | tail -n 1 | cut -d " " -f 7 | rev | cut -c 2- | rev

in __check_already_mounted(). And then check whether it is still mounted (because it could have been unmounted since the message appeared in dmesg).

Seems to work for me:

% dmesg | grep "Label for provider da1 is" | tail -n 1 | cut -d " " -f 7 | rev | cut -c 2- | rev
msdosfs/MONKEYPATCH

So from there the next check would be:

  mount | grep -e "^/dev/msdosfs/MONKEYPATCH on"

and if that returns nothing, then assume that it was not mounted.

vermaden commented 2 years ago

That would also require additional checks for UFS labels, GPT labels, GLABEL labels, ext2/3/4 labels, and so on. It will make supporting automount(8) a nightmare ...

probonopd commented 2 years ago

Would you consider it if I come up with a grep one-liner?

vermaden commented 2 years ago

One grep(1) one liner would not solve that. You have to use multiple utilities to get the labels of many types of filesystems.

Just do not use /dev/msdosfs as the path for automount(8) daemon.

Use da(4) and ada(4) and md(4) and mmcsd(4) devices.

probonopd commented 2 years ago

That does not work for me. The volume with this label may be on different types of disks. I want to address my volumes by their name, not depending on the bus they happen to be connected to.

Anyhow, I'll see what I can do because I need this in helloSystem anyway.

vermaden commented 2 years ago

I will try to add that 'check' for label at least for FAT fs.

That would be sufficient for your needs, yes?

probonopd commented 2 years ago

Unfortunately no. Do you know what else there is besides msdosfs, iso9660, ufs, uzip or where to look that up? I haven't given up on the idea of an one-liner entirely yet and would like to try.

vermaden commented 2 years ago

Also /dev/gpart and /dev/label for sure.

probonopd commented 2 years ago

Relevant docs:

https://docs.freebsd.org/en/books/handbook/geom/#geom-glabel

By permanently labeling the partitions on the boot disk, the system should be able to continue to boot normally, even if the disk is moved to another controller or transferred to a different system.

This is the reason why we want to use the disk labels for everything in helloSystem.

probonopd commented 2 years ago

Looking at man glabel(8) it seems like we need to check:

vermaden commented 2 years ago

Yeah, that is why I stick to the 'raw' devices and not their logical clones.

I have other idea. I will add an option to the automount.conf config file that will disallow read only mounts - do each volume will be again mounted once.

probonopd commented 2 years ago

mount | grep -e "^/dev/(label|ufs|ufsid|msdosfs|iso9660|ext2fs|reiserfs|ntfs|gpt|gptid|diskid)/MONKEYPATCH on"

probonopd commented 2 years ago

I will add an option to the automount.conf config file that will disallow read only mounts

Who would want that? I will need read-only mounts a lot for sure, especially now that double-clicking an ISO file mounts it!

vermaden commented 2 years ago

But WHERE that grep(1) expression would help us?

probonopd commented 2 years ago

Before doing any mounting, run <that grep expression> && exit 0

(of course with the correct patition/volume/disk label instead of MONKEYPATCH ;-))

vermaden commented 1 year ago

Check with latest version.

Thanks.