Filesystem mounted/umounted

I want to detect when a new filesystem has been mounted or a previously mounted filesystem has been umounted. Is there any way to watch for this events?

Thanks in advance!
 
Thanks for your answer. Periodic reports use a simple diff with previous status, I don't want to be pooling for status changes.

What I'm looking for is an "instant" notification from the system (using something like a kevent), but I have not found anything related to mount status...
 
I thought devd only gives information about devices, no mounts. I will give it a try.
If not I'll go with hal.
Thanks for your help!!
 
Sorry, I thought it was clear I used kqueue with undocumented EVFILT_FS. devd is useful when a device is attached or deattached, but it won't show anything when for example a filesystem is mounted using nullfs.

This is the code used to test (it is in FreePascal, but should be easy to translate to C or other languages):

Code:
program mounts;

{$mode objfpc}{$H+}

uses
	BSD;

const
	NOTE_MOUNTED = $0008;
	NOTE_UMOUNTED = $0010;

var
	ke: TKEvent;
	kq: Longint;

begin
	kq := kqueue();
	if kq < 0 then
	begin
		WriteLn('ERROR: kqueue()');
		exit;
	end; { if }

	FillByte(ke, SizeOf(ke), 0);
	EV_SET(@ke, 0, EVFILT_FS, EV_ADD or EV_CLEAR, 0, 0, nil);

	if kevent(kq, @ke, 1, nil, 0, nil) = -1 then
	begin
		WriteLn('ERROR: kevent()');
		exit;
	end; { if }

	FillByte(ke, SizeOf(ke), 0);
	while true do
	begin
		if kevent(kq, nil, 0, @ke, 1, nil) > 0 then
		begin
			if (ke.FFlags and NOTE_MOUNTED <> 0) then
				WriteLn('Filesystem mounted')
			else if (ke.FFlags and NOTE_UMOUNTED <> 0) then
			begin
				WriteLn('Filesystem umounted');
				break;
			end; { else if }
		end; { if }
	end; { while }
end.

The constants NOTE_MOUNTED and NOTE_UMOUNTED were retrieved via observation (ke.FFlags value), I was not able to find them in FreeBSD source code with the rest of NOTE_* constants.

Value of EVFILT_FS is -9, although it was already defined in FreePascal.
 
Back
Top