# Filesystem mounted/umounted



## bbt (Feb 12, 2011)

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!


----------



## kisscool-fr (Feb 12, 2011)

You should take a look for periodic's daily reporting mails. It reports those sort of changes. It could helps you.


----------



## bbt (Feb 12, 2011)

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...


----------



## expl (Feb 13, 2011)

devd(8)
or
sysutils/hal
are what is available on FreeBSD.


----------



## bbt (Feb 13, 2011)

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!!


----------



## trasz@ (Feb 17, 2011)

Note that there is kqueue(2) support for filesystem events (EVFILT_FS); unfortunately, it's undocumented.


----------



## bbt (Feb 17, 2011)

Thanks trasz@!!! That is exactly what I was looking for. Works great!


----------



## wblock@ (Feb 17, 2011)

Please show what you did.  Was it through devd or something else?


----------



## bbt (Feb 18, 2011)

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):


```
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.


----------

