Re: [systemd-devel] Masking mount units

2024-11-21 Thread Andrei Borzenkov

21.11.2024 20:47, Phillip Susi wrote:

Andrei Borzenkov  writes:


commit 142b8142d7bb84f07ac33fc00527a4d48ac8ef9f
Author: Tom Yan 
Date:   Wed Jan 9 23:35:24 2019 +0800

 mount/generators: do not make unit wanted by its device unit

 As device units will be reloaded by systemd whenever the
corresponding device generates a "changed" event, if the mount unit /
cryptsetup service is wanted by its device unit, the former can be
restarted by systemd unexpectedly after the user stopped them
explicitly. It is not sensible at all and can be considered dangerous.
Neither is the behaviour conventional (as `auto` in fstab should only
affect behaviour on boot and `mount -a`) or ever documented at all
(not even in systemd, see systemd.mount(5) and crypttab(5)).


Makes sense.  So is there any way to convince the older versions of
systemd to remove the WantedBy relationship?  It seems that systemctl
won't let you.


Only by explicitly using mount units instead of /etc/fstab. I am still 
really surprised it was finally dropped at all - because every time this 
was suggested it was vehemently opposed to. May be it did slip in by 
accident :)


Re: [systemd-devel] Masking mount units

2024-11-21 Thread Phillip Susi
Andrei Borzenkov  writes:

> commit 142b8142d7bb84f07ac33fc00527a4d48ac8ef9f
> Author: Tom Yan 
> Date:   Wed Jan 9 23:35:24 2019 +0800
>
> mount/generators: do not make unit wanted by its device unit
>
> As device units will be reloaded by systemd whenever the
> corresponding device generates a "changed" event, if the mount unit /
> cryptsetup service is wanted by its device unit, the former can be
> restarted by systemd unexpectedly after the user stopped them
> explicitly. It is not sensible at all and can be considered dangerous.
> Neither is the behaviour conventional (as `auto` in fstab should only
> affect behaviour on boot and `mount -a`) or ever documented at all
> (not even in systemd, see systemd.mount(5) and crypttab(5)).

Makes sense.  So is there any way to convince the older versions of
systemd to remove the WantedBy relationship?  It seems that systemctl
won't let you.


Re: [systemd-devel] Masking mount units

2024-11-19 Thread Andrei Borzenkov
On Tue, Nov 19, 2024 at 10:55 PM Phillip Susi  wrote:
>
> Furthermore, it seems that the undesirable auto mounting behavior only
> happens with systemd-239 and lower.  Newer versions of systemd seem to
> have dropped this behavior by not setting the WantedBy=foo.device on the
> mount point.  I assume this change was intentional?


commit 142b8142d7bb84f07ac33fc00527a4d48ac8ef9f
Author: Tom Yan 
Date:   Wed Jan 9 23:35:24 2019 +0800

mount/generators: do not make unit wanted by its device unit

As device units will be reloaded by systemd whenever the
corresponding device generates a "changed" event, if the mount unit /
cryptsetup service is wanted by its device unit, the former can be
restarted by systemd unexpectedly after the user stopped them
explicitly. It is not sensible at all and can be considered dangerous.
Neither is the behaviour conventional (as `auto` in fstab should only
affect behaviour on boot and `mount -a`) or ever documented at all
(not even in systemd, see systemd.mount(5) and crypttab(5)).


Re: [systemd-devel] Masking mount units

2024-11-19 Thread Lennart Poettering
On Di, 19.11.24 01:45, Nils Kattenbeck ([email protected]) wrote:

> On Tue, Nov 19, 2024 at 1:00 AM Lennart Poettering
>  wrote:
> >
> > On Do, 14.11.24 14:25, Phillip Susi ([email protected]) wrote:
> >
> > > Lennart Poettering  writes:
> > >
> > > > the BLKFLSBUF ioctl() works fine on block device fds open for read only.
> > >
> > > Oh, I might have to change that to use a read only open then.
> > >
> > > > I am not following anymore. As *long* *as* *the* lock is taken the
> > > > auto mounting doesn't happen. Once you release the lock then
> > > > everything will be reprobed and work as it always worked, including
> > > > auto-mounting.
> > >
> > > It is not about delaying the auto mounting, but *preventing it*
> > > entirely.
> >
> > It *is* prevented. While you keep the lock open *no* events are
> > propagated from udev to its clients, hence no automounts will take
> > place. Any kernel reported event that happen while you keep the lock
> > will basically be eaten up by udev and not propagated on to rest of
> > userspace. They are sent to /dev/null if you so will. When you release
> > the lock once you are fully done (which might as well be 3h later, if
> > you like) however, then a single *new* event is *synthesized*, to get
> > the rest of userspace updated again on things. And yes, that *might*
> > cause userspace to automount things, and other stuff. But that's fine,
> > you explicit indicated you are now done with the device, hence others
> > can consume it again.
>
> I think you have been talking past each other for the last dozen mails.
> I am quite sure that Philip Susi not only wants to prevent auto-mounts
> while gparted is working with the filesystem but also afterwards,
> i.e. they also want to suppress the synthetic event you are talking
> about.

Huh? Even after? Like, *forever*??? What's the point of that? Is
gparted supposed to break your system for good and render your block
devices unusable?

I thought it was a partitioner, but I might have gotten that wrong?

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-11-19 Thread Phillip Susi
Lennart Poettering  writes:

> Huh? Even after? Like, *forever*??? What's the point of that? Is
> gparted supposed to break your system for good and render your block
> devices unusable?
>
> I thought it was a partitioner, but I might have gotten that wrong?

Just because you move or resize a partition does not mean that it should
be auto mounted, even if it is delayed until after you close gparted.
That is just terrible UX.

Over the years gparted has gone through many different things to disable
auto mounting while it runs; hal-lock, udisks-inhibit, and currently it
masks all mount units.

Furthermore, it seems that the undesirable auto mounting behavior only
happens with systemd-239 and lower.  Newer versions of systemd seem to
have dropped this behavior by not setting the WantedBy=foo.device on the
mount point.  I assume this change was intentional?  I am a bit confused
as to why systemd no longer does auto mount yet you are shocked that
anyone would not want systemd to auto mount.

I would say that gparted should just drop the masking of mounts and be
done with it since it is no longer an issue, but they still wish to
support even older distros that are still supported, which right now
means Rocky Linux 8, which still has systemd-239, and still tries to
auto mount a moved partition unless gparted masks the mounts.



Re: [systemd-devel] Masking mount units

2024-11-18 Thread Nils Kattenbeck
On Tue, Nov 19, 2024 at 1:00 AM Lennart Poettering
 wrote:
>
> On Do, 14.11.24 14:25, Phillip Susi ([email protected]) wrote:
>
> > Lennart Poettering  writes:
> >
> > > the BLKFLSBUF ioctl() works fine on block device fds open for read only.
> >
> > Oh, I might have to change that to use a read only open then.
> >
> > > I am not following anymore. As *long* *as* *the* lock is taken the
> > > auto mounting doesn't happen. Once you release the lock then
> > > everything will be reprobed and work as it always worked, including
> > > auto-mounting.
> >
> > It is not about delaying the auto mounting, but *preventing it*
> > entirely.
>
> It *is* prevented. While you keep the lock open *no* events are
> propagated from udev to its clients, hence no automounts will take
> place. Any kernel reported event that happen while you keep the lock
> will basically be eaten up by udev and not propagated on to rest of
> userspace. They are sent to /dev/null if you so will. When you release
> the lock once you are fully done (which might as well be 3h later, if
> you like) however, then a single *new* event is *synthesized*, to get
> the rest of userspace updated again on things. And yes, that *might*
> cause userspace to automount things, and other stuff. But that's fine,
> you explicit indicated you are now done with the device, hence others
> can consume it again.

I think you have been talking past each other for the last dozen mails.
I am quite sure that Philip Susi not only wants to prevent auto-mounts
while gparted is working with the filesystem but also afterwards,
i.e. they also want to suppress the synthetic event you are talking about.

> Or in other words: if you must, just take the frickin' lock the
> *entire* time gparted runs, and nothing else will automount or
> otherwise touch device while gparted runs.
>
> Hence, again: just take the frickin' lock. It's the API you should be
> using. Don't mask units or anything, that's entirely besides the
> point and seriously broken.
>
> Did I get through now? I really fail to see why the concept of a lock
> is so difficult to grok?
>
> Lennart
>
> --
> Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-11-18 Thread Lennart Poettering
On Do, 14.11.24 14:25, Phillip Susi ([email protected]) wrote:

> Lennart Poettering  writes:
>
> > the BLKFLSBUF ioctl() works fine on block device fds open for read only.
>
> Oh, I might have to change that to use a read only open then.
>
> > I am not following anymore. As *long* *as* *the* lock is taken the
> > auto mounting doesn't happen. Once you release the lock then
> > everything will be reprobed and work as it always worked, including
> > auto-mounting.
>
> It is not about delaying the auto mounting, but *preventing it*
> entirely.

It *is* prevented. While you keep the lock open *no* events are
propagated from udev to its clients, hence no automounts will take
place. Any kernel reported event that happen while you keep the lock
will basically be eaten up by udev and not propagated on to rest of
userspace. They are sent to /dev/null if you so will. When you release
the lock once you are fully done (which might as well be 3h later, if
you like) however, then a single *new* event is *synthesized*, to get
the rest of userspace updated again on things. And yes, that *might*
cause userspace to automount things, and other stuff. But that's fine,
you explicit indicated you are now done with the device, hence others
can consume it again.

Or in other words: if you must, just take the frickin' lock the
*entire* time gparted runs, and nothing else will automount or
otherwise touch device while gparted runs.

Hence, again: just take the frickin' lock. It's the API you should be
using. Don't mask units or anything, that's entirely besides the
point and seriously broken.

Did I get through now? I really fail to see why the concept of a lock
is so difficult to grok?

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-11-14 Thread Phillip Susi
Lennart Poettering  writes:

> the BLKFLSBUF ioctl() works fine on block device fds open for read only.

Oh, I might have to change that to use a read only open then.

> I am not following anymore. As *long* *as* *the* lock is taken the
> auto mounting doesn't happen. Once you release the lock then
> everything will be reprobed and work as it always worked, including
> auto-mounting.

It is not about delaying the auto mounting, but *preventing it*
entirely.

I seem to remember at one point auto mounting was handled by udisksd and
there was some hint environment variable a udev rule could set to tell
it whether a given filesystem should be auto mounted or not, and there
was a udev rule that set this hint only for USB disks.  It seems this is
no longer the case these days.

After some additional testing, Mike Fleetwood has found that auto
mounting does not happen on most recent distros, at least for sata
disks, but it does still happen back on Rocky Linux 8.  At first he said
he saw two udev attributes set when not specifying the noauto option in
/etc/fstab:

RequiredBy=local-fs.target
WantedBy=dev-sdb1.device

In my testing, I only see the RequiredBy, not the WantedBy, and don't
see any auto mounting being triggered.  I would think that systemd auto
mounting would be triggered by the WantedBy relationship.  What causes
that to be set?

It also seems that you can not set this relationship manually.  I think
when I tried to add the wants relationship, I got an error that the
device unit does not exist.  I guess that is because there is no actual
.device file in the filesystem?  I suggested trying to add a runtime
removal of the WantedBy relationship to temporarily disable auto mounting
while gparted is running.  Mike tried this but apparently you can't
change most properties this way.

See here for the discussion we've been having trying to figure out how
to get rid of the old masking mounts workaround:

https://gitlab.gnome.org/GNOME/gparted/-/issues/255#note_2263098



Re: [systemd-devel] Masking mount units

2024-11-11 Thread Lennart Poettering
On Mo, 11.11.24 08:34, Phillip Susi ([email protected]) wrote:

> Lennart Poettering  writes:
>
> >> I'm reading between the lines a bit, but my guess is that libparted
> >> always opens the device writable in case you start issuing actual
> >> partitioning commands.
> >
> > That sounds like a bug though. They should open the device for write
> > only when they need to make changes.
>
> It opens all of the partitions in write mode in order to flush the
> cache.  I believe this was put in as a workaround to the Linux cache
> coherency problem that caused reads through the whole disk not to see the
> recently written filesystem superblock after running mkfs.

the BLKFLSBUF ioctl() works fine on block device fds open for read only.

> >> To support "parted print doesn't trigger udev
> >> events", I think you'd have to change libparted so that it opens the
> >> device for read first (potentially with a shared lock) and then
> >> reopens it for write (with an exclusive lock) when needed (e.g. when
> >> using the mkpart command in the interactive session). Without that, I
> >> think you're always going to need workarounds such as masking mount
> >> units.
> >
> > Yeah, what you are proposing sounds like it is the way to go indeed,
> > if parted really is not doing this already right now.
>
> The bsd lock only *delays* the auto mounting, it does not prevent it, so
> it seems that there is nothing libparted can do to prevent auto mounting
> other than getting rid of the cache flushes and accepting the old
> problem with cache cohernecy.

I am not following anymore. As *long* *as* *the* lock is taken the
auto mounting doesn't happen. Once you release the lock then
everything will be reprobed and work as it always worked, including
auto-mounting.

But the time window how long auto mounting shall not take place is
under parted's control: as long as it keeps the lock taken,
automounting won't take place.

> I thought that perhaps a runtime override of RequiredBy=local-fs.target
> and WantedBy=dev-sdb1.device ( equivalent to the noauto mount option in
> /etc/fstab ) would do it, but apparently you can't actully set most
> properties at runtime.  Why is that?
>
> Also, again, why does a udev CHANGE event on sdb1.device trigger auto
> mount?  Shouldn't that only be done for ADD?

So that newly placed file systems can be probed and the rest of the
stack can learn about them.

But I am really puzzled by what the whole misunderstanding here is. Do
you understand what a "lock" in computer science means? I have the
suspicion you don't? As long as you keep the lock on the block device
you basically have exclusive access at lesat in regards to everyone
else following the lock protocol, of which udev is one.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-11-11 Thread Phillip Susi
Lennart Poettering  writes:

>> I'm reading between the lines a bit, but my guess is that libparted
>> always opens the device writable in case you start issuing actual
>> partitioning commands.
>
> That sounds like a bug though. They should open the device for write
> only when they need to make changes.

It opens all of the partitions in write mode in order to flush the
cache.  I believe this was put in as a workaround to the Linux cache
coherency problem that caused reads through the whole disk not to see the
recently written filesystem superblock after running mkfs.

>> To support "parted print doesn't trigger udev
>> events", I think you'd have to change libparted so that it opens the
>> device for read first (potentially with a shared lock) and then
>> reopens it for write (with an exclusive lock) when needed (e.g. when
>> using the mkpart command in the interactive session). Without that, I
>> think you're always going to need workarounds such as masking mount
>> units.
>
> Yeah, what you are proposing sounds like it is the way to go indeed,
> if parted really is not doing this already right now.

The bsd lock only *delays* the auto mounting, it does not prevent it, so
it seems that there is nothing libparted can do to prevent auto mounting
other than getting rid of the cache flushes and accepting the old
problem with cache cohernecy.

I thought that perhaps a runtime override of RequiredBy=local-fs.target
and WantedBy=dev-sdb1.device ( equivalent to the noauto mount option in
/etc/fstab ) would do it, but apparently you can't actully set most
properties at runtime.  Why is that?

Also, again, why does a udev CHANGE event on sdb1.device trigger auto
mount?  Shouldn't that only be done for ADD?


Re: [systemd-devel] Masking mount units

2024-11-01 Thread Lennart Poettering
On Do, 31.10.24 12:08, Dan Nicholson ([email protected]) wrote:

> On Thu, Oct 31, 2024 at 10:23 AM Lennart Poettering
>  wrote:
> >
> > On Do, 31.10.24 09:03, Phillip Susi ([email protected]) wrote:
> >
> > > Lennart Poettering  writes:
> > >
> > > Yes, but then it reads the disk and auto mounts a partition just because
> > > someone ran parted print.  Printing the partition table should not
> > > trigger auto mount.
> >
> > NO!
> >
> > I don't know parted, but why would it open device for *WRITE* if it
> > only wants to show contents of it? It should open it for *READ* then,
> > and thus not trigger any events.
>
> I'm reading between the lines a bit, but my guess is that libparted
> always opens the device writable in case you start issuing actual
> partitioning commands.

That sounds like a bug though. They should open the device for write
only when they need to make changes.

> To support "parted print doesn't trigger udev
> events", I think you'd have to change libparted so that it opens the
> device for read first (potentially with a shared lock) and then
> reopens it for write (with an exclusive lock) when needed (e.g. when
> using the mkpart command in the interactive session). Without that, I
> think you're always going to need workarounds such as masking mount
> units.

Yeah, what you are proposing sounds like it is the way to go indeed,
if parted really is not doing this already right now.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-10-31 Thread Andrei Borzenkov

31.10.2024 16:03, Phillip Susi wrote:

Lennart Poettering  writes:


Doing the locking on the fd you use for writing makes things a lot
easier, because as mentioned udev will automatically retrigger block
devices if an inotify event on it is seen that indicates
"close-after-write". If you deal with multiple fds you need to make


That is exactly what I DON'T want to happen.  If udev retriggers when
the fd is closed, then that results in the partition being automatically
mounted just because libparted read the partition table.



Way partition is mounted? What mounts it?

systemd stopped pulling mount units by device in 242:


* The .device units generated by systemd-fstab-generator and other
  generators do not automatically pull in the corresponding .mount unit
  as a Wants= dependency. This means that simply plugging in the device
  will not cause the mount unit to be started automatically.


It sounds like you are shooting the messenger.


Re: [systemd-devel] Masking mount units

2024-10-31 Thread Dan Nicholson
On Thu, Oct 31, 2024 at 10:23 AM Lennart Poettering
 wrote:
>
> On Do, 31.10.24 09:03, Phillip Susi ([email protected]) wrote:
>
> > Lennart Poettering  writes:
> >
> > Yes, but then it reads the disk and auto mounts a partition just because
> > someone ran parted print.  Printing the partition table should not
> > trigger auto mount.
>
> NO!
>
> I don't know parted, but why would it open device for *WRITE* if it
> only wants to show contents of it? It should open it for *READ* then,
> and thus not trigger any events.

I'm reading between the lines a bit, but my guess is that libparted
always opens the device writable in case you start issuing actual
partitioning commands. To support "parted print doesn't trigger udev
events", I think you'd have to change libparted so that it opens the
device for read first (potentially with a shared lock) and then
reopens it for write (with an exclusive lock) when needed (e.g. when
using the mkpart command in the interactive session). Without that, I
think you're always going to need workarounds such as masking mount
units.

---
Dan


Re: [systemd-devel] Masking mount units

2024-10-31 Thread Lennart Poettering
On Do, 31.10.24 09:03, Phillip Susi ([email protected]) wrote:

> Lennart Poettering  writes:
>
> > Doing the locking on the fd you use for writing makes things a lot
> > easier, because as mentioned udev will automatically retrigger block
> > devices if an inotify event on it is seen that indicates
> > "close-after-write". If you deal with multiple fds you need to make
>
> That is exactly what I DON'T want to happen.  If udev retriggers when
> the fd is closed, then that results in the partition being automatically
> mounted just because libparted read the partition table.

Two things:

1. The inotify is installed for close-after-*write*. Just opening the
   device for *read* and closing it will thus *not* result in the
   device to be retriggered and any uevents to be sent out. So if
   libparted just "reads the partition table" this should not result
   in an event.

2. And of course, this is a *lock*. Keep the fd you are operating with
   open and locked *as* *long* *as* you don#t want udev to reprobe it,
   and issue uevents for it.

> > sure to close the fd used for writing *after* having released the
> > lock on the other one, because if you do it the other way round, udev
> > will retigger the device, but then not be able to lock device and not
> > actually do anything, and then no further inotify is seen and things
> > will remain out ofdate.
>
> That is what I WANT to happen.  Well, at least I don't want a partition
> to be automatically mounted.  It would be nice if udevdb could still
> be updated.

Why? Again, if parted is modifyig the partition table, changing
superblocks and whatnot, then udev should *not* read anything off the
disk while it is doing so, because it would read half-written info
potentially.

> >> Then again, this will also prevent the udevdb from being updated.  Maybe
> >> that is a bigger hammer than you really want when the goal is just to
> >> stop auto mounting?
> >
> > If you change the superblock or part table of your disk, udev
> > shouldn't read half-written data, so yes, it's *good* it doesn't
> > update its udevdb. That's the whole point of the exercise!
> >
> > I am not sure I grok what the problem is supposed to be here...
>
> Right, you don't want it to read half updated data, but if I
> intentionally prevent udev from looking at the disk _at all_ ( so that
> partitions don't get auto mounted ), then it also means that it never
> looks at the finished updates either and updates udevdb.

I am not following anymore?

It's a lock! Open the device, lock it, make your changes, once you are
done close (and thus implicitly release lock). That's all. That means,
while you are making your choices udev won't do anything with the
device, it will not probe it, not send out uevents and so on.

Just keep the lock as long as you aren't done.

> > parted should just open the main block device for writing, take an
> > exclusive BSD lock on it, then do its thing, and close the fd. It's
> > very easy, and will bock generation of udev's uevents on the thing,
> > will block udev from reading half-written data and everything else.
>
> Yes, but then it reads the disk and auto mounts a partition just because
> someone ran parted print.  Printing the partition table should not
> trigger auto mount.

NO!

I don't know parted, but why would it open device for *WRITE* if it
only wants to show contents of it? It should open it for *READ* then,
and thus not trigger any events.

> Hrm... now that I think about it, why does a CHANGE event trigger auto
> mount?  Shouldn't that only be done on ADD?

No. CHANGE and ADD are both "positive" events that indicate the device
exists now. We generally want that most apps and rules treat
"positive" events the same way.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-10-31 Thread Phillip Susi
Lennart Poettering  writes:

> Doing the locking on the fd you use for writing makes things a lot
> easier, because as mentioned udev will automatically retrigger block
> devices if an inotify event on it is seen that indicates
> "close-after-write". If you deal with multiple fds you need to make

That is exactly what I DON'T want to happen.  If udev retriggers when
the fd is closed, then that results in the partition being automatically
mounted just because libparted read the partition table.

> sure to close the fd used for writing *after* having released the
> lock on the other one, because if you do it the other way round, udev
> will retigger the device, but then not be able to lock device and not
> actually do anything, and then no further inotify is seen and things
> will remain out ofdate.

That is what I WANT to happen.  Well, at least I don't want a partition
to be automatically mounted.  It would be nice if udevdb could still be updated.

>> Then again, this will also prevent the udevdb from being updated.  Maybe
>> that is a bigger hammer than you really want when the goal is just to
>> stop auto mounting?
>
> If you change the superblock or part table of your disk, udev
> shouldn't read half-written data, so yes, it's *good* it doesn't
> update its udevdb. That's the whole point of the exercise!
>
> I am not sure I grok what the problem is supposed to be here...

Right, you don't want it to read half updated data, but if I
intentionally prevent udev from looking at the disk _at all_ ( so that
partitions don't get auto mounted ), then it also means that it never
looks at the finished updates either and updates udevdb.

> parted should just open the main block device for writing, take an
> exclusive BSD lock on it, then do its thing, and close the fd. It's
> very easy, and will bock generation of udev's uevents on the thing,
> will block udev from reading half-written data and everything else.

Yes, but then it reads the disk and auto mounts a partition just because
someone ran parted print.  Printing the partition table should not
trigger auto mount.

Hrm... now that I think about it, why does a CHANGE event trigger auto
mount?  Shouldn't that only be done on ADD?


Re: [systemd-devel] Masking mount units

2024-10-30 Thread Lennart Poettering
On Di, 29.10.24 08:28, Phillip Susi ([email protected]) wrote:

> Lennart Poettering  writes:
>
> > It prevents it fully. However, udev installs an inotify watch on all
> > relevant block devices, which watches for IN_CLOSE_WRITE events, and
> > then triggers the device again. IN_CLOSE_WRITE is the event that is
> > generated when an inode is closed after it was open for writing. For
> > block devices this means that when tools such as fdisk open the device
> > for writing and then close it, the devices will be fully automatically
> > reprobed afterwards (unless a BSD file lock is still in place then, by
> > somebody else).
> >
> > IN_CLOSE_WRITE is strictly speaking not the event we *actually* want
> > to watch for retriggering the device (that would be an event triggered
> > by the release of the file lock, but linux does not support generating
> > a lock then), but it's really close, because "close-after-write" and
> > "release lock" typically coincide, or are even the very same
> > operation.
>
> So if you want to prevent automounting entirely you have to... open the
> disk device read only and take the bsd lock, and hold it until after
> closing the write file descriptor?  Plus probably a little more time to
> give udev a chance to try and fail to take the lock?

Why keep it longer? The bsd file lock is effectively an rwmutex. As
long as your app has the lock, udev won't check it.

Ideally you take the lock on the fd you also use for writing. I am not
sure why you'd involve two fds?

Doing the locking on the fd you use for writing makes things a lot
easier, because as mentioned udev will automatically retrigger block
devices if an inotify event on it is seen that indicates
"close-after-write". If you deal with multiple fds you need to make
sure to close the fd used for writing *after* having released the
lock on the other one, because if you do it the other way round, udev
will retigger the device, but then not be able to lock device and not
actually do anything, and then no further inotify is seen and things
will remain out ofdate.

> Then again, this will also prevent the udevdb from being updated.  Maybe
> that is a bigger hammer than you really want when the goal is just to
> stop auto mounting?

If you change the superblock or part table of your disk, udev
shouldn't read half-written data, so yes, it's *good* it doesn't
update its udevdb. That's the whole point of the exercise!

I am not sure I grok what the problem is supposed to be here...

parted should just open the main block device for writing, take an
exclusive BSD lock on it, then do its thing, and close the fd. It's
very easy, and will bock generation of udev's uevents on the thing,
will block udev from reading half-written data and everything else.

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-10-29 Thread Dan Nicholson
On Tue, Oct 29, 2024 at 6:28 AM Phillip Susi  wrote:
>
> So if you want to prevent automounting entirely you have to... open the
> disk device read only and take the bsd lock, and hold it until after
> closing the write file descriptor?  Plus probably a little more time to
> give udev a chance to try and fail to take the lock?

It sounds like parted is already opening the device writable in order
to issue ioctls.
If so, the pattern is, open, flock exclusive, do whatever you need,
close. The lock
is implicitly dropped when the file descriptor is closed. When the
file descriptor is
closed, udev will receive an inotify event and probe the device again.
I don't know
why you'd want to wait for udev to try and fail to take the lock.

> Then again, this will also prevent the udevdb from being updated.  Maybe
> that is a bigger hammer than you really want when the goal is just to
> stop auto mounting?

You started this by saying that you wanted to prevent udev from
probing the device,
which is exactly what will happen if you take an exclusive lock. Regardless of
automounting, this sounds like exactly what parted would want to ensure it has
exclusive access to the device while it potentially makes partition
table changes.
While changes are being made that cause change events, udev will be prevented
from probing the device since it can't obtain a shared lock. Once
parted closes the
device, udev will immediately reprobe the device and everyone will see
the updated
attributes. Sounds like a good scheme to me.

---
Dan


Re: [systemd-devel] Masking mount units

2024-10-29 Thread Phillip Susi
Lennart Poettering  writes:

> It prevents it fully. However, udev installs an inotify watch on all
> relevant block devices, which watches for IN_CLOSE_WRITE events, and
> then triggers the device again. IN_CLOSE_WRITE is the event that is
> generated when an inode is closed after it was open for writing. For
> block devices this means that when tools such as fdisk open the device
> for writing and then close it, the devices will be fully automatically
> reprobed afterwards (unless a BSD file lock is still in place then, by
> somebody else).
>
> IN_CLOSE_WRITE is strictly speaking not the event we *actually* want
> to watch for retriggering the device (that would be an event triggered
> by the release of the file lock, but linux does not support generating
> a lock then), but it's really close, because "close-after-write" and
> "release lock" typically coincide, or are even the very same
> operation.

So if you want to prevent automounting entirely you have to... open the
disk device read only and take the bsd lock, and hold it until after
closing the write file descriptor?  Plus probably a little more time to
give udev a chance to try and fail to take the lock?

Then again, this will also prevent the udevdb from being updated.  Maybe
that is a bigger hammer than you really want when the goal is just to
stop auto mounting?



Re: [systemd-devel] Masking mount units

2024-10-25 Thread Phillip Susi
Lennart Poettering  writes:

> systemd is downstream to udev. If udev doesn't process a block device
> because of the taken BSD file lock on the main block device, then
> systemd (and other downstreams of udev) won't get such notificaitons
> either. And thus won't automount.

The BSD lock does not prevent the udev processing, it only delays it
until after the lock is released doesn't it?


Re: [systemd-devel] Masking mount units

2024-10-25 Thread Lennart Poettering
On Do, 24.10.24 15:43, Phillip Susi ([email protected]) wrote:

> Lennart Poettering  writes:
>
> > systemd is downstream to udev. If udev doesn't process a block device
> > because of the taken BSD file lock on the main block device, then
> > systemd (and other downstreams of udev) won't get such notificaitons
> > either. And thus won't automount.
>
> The BSD lock does not prevent the udev processing, it only delays it
> until after the lock is released doesn't it?

It prevents it fully. However, udev installs an inotify watch on all
relevant block devices, which watches for IN_CLOSE_WRITE events, and
then triggers the device again. IN_CLOSE_WRITE is the event that is
generated when an inode is closed after it was open for writing. For
block devices this means that when tools such as fdisk open the device
for writing and then close it, the devices will be fully automatically
reprobed afterwards (unless a BSD file lock is still in place then, by
somebody else).

IN_CLOSE_WRITE is strictly speaking not the event we *actually* want
to watch for retriggering the device (that would be an event triggered
by the release of the file lock, but linux does not support generating
a lock then), but it's really close, because "close-after-write" and
"release lock" typically coincide, or are even the very same
operation.

This is all pretty well documented btw, I'd recommend having a look:

https://systemd.io/BLOCK_DEVICE_LOCKING

Lennart

--
Lennart Poettering, Berlin


Re: [systemd-devel] Masking mount units

2024-10-24 Thread Lennart Poettering
On Mi, 23.10.24 14:32, Phillip Susi ([email protected]) wrote:

> A few years ago you mentioned that gparted should not be masking mount
> units and should instead take a bsd lock:
>
> https://lists.freedesktop.org/archives/systemd-devel/2020-January/043924.html
>
> I brought this issue back up recently.  It seems like taking a bsd lock
> would allow gparted to make sure that partition probing and auto
> mounting does not happen say, between the time the partition is created,
> and the time mkfs is run on it, but apparently that is not the real
> issue why gparted masks all mount units.
>
> Simply having libparted *read* the partition table from the disk
> triggers change events on all of the partitions, causing them to be auto
> mounted.  This is because libparted issues a BLKFLUSH ioctl to all of
> the partition devices, I believe, as a workaround for the fact that
> Linux does not unify the block cache for the partition and the whole
> disk, so the partition is flushed to make sure that any recent mkfs is
> visible when libparted tries to probe the filesystem type by reading the
> raw disk block device.
>
> Can you think of any other way to prevent this unwanted auto mounting
> besides masking the mount units?

systemd is downstream to udev. If udev doesn't process a block device
because of the taken BSD file lock on the main block device, then
systemd (and other downstreams of udev) won't get such notificaitons
either. And thus won't automount.

Lennart

--
Lennart Poettering, Berlin