Friday, November 7, 2008

Monitor Linux File System Events with Inotify

Monitor Linux File System Events with Inotify

By Eli M. Dow

The first step in installing inotify is to determine whether the Linux kernel you are using supports it. The simplest way to check your distribution is to look for the existence of a /dev/inotify device. If this device is present, you may move on to the section Using inotify in a simple application.

At the time this article is being written, inotify is included in Andrew Morton's Linux 2.6-mm tree, and several Linux distributions are providing inotify-enabled kernels (including Gentoo and Ubuntu) or have supplemental kernel packages with support (for example, Fedora and SuSE). Since Andrew can remove inotify support from his tree if he sees fit and because inotify releases are still frequent at this stage in development, patching from scratch is highly recommended.

If the device is missing, you may need to patch your kernel and create the device.

Patching your kernel for inotify
You can obtain inotify patches from the Linux Kernel Archives (see the Resources section for a link).

You should apply the patch that has the highest version number for your specific kernel. Each distribution handles kernel installation a bit differently, but the following is a generic guideline to follow: Note: Obtain your distribution's 2.6 Linux kernel source or, if appropriate, the latest stable release from the Linux Kernel Archives.

Begin by going into your kernel source directory:

bash:~$ cd /usr/src

Since you installed the kernel source earlier, you now need to unpack it:

bash:~$ sudo tar jxvf linux-source-

Now, make your symlink to the new source tree:

bash:~$ sudo ln -sf linux-source- linux

Change your current directory to the kernel source directory you just made:

bash:~$ cd linux

Copy the inotify patch:

bash:~$ sudo cp ~/inotify* /usr/src

Patch the kernel:

bash:~$ sudo patch -p1 < ../inotify*.patch

Build your kernel:

bash:~$ sudo make menuconfig

Configure your kernel as you normally would, making sure to enable the inotify functionality. Add this new kernel to your bootloader as necessary, remembering to maintain your old kernel image and bootloader options. This step varies across bootloaders (see the Resources section for more information about your specific bootloader). Reboot your machine and select your new inotify enabled kernel. Test your new kernel to make sure it functions properly before continuing with this process.

Creating the inotify device
Next, you need to ensure the /dev/inotify device gets created. The following steps will walk you through the process. Important note: The minor number can change, so you must be vigilant about ensuring that it is up to date! If your Linux installation supports udev functionality, it will be kept up to date automatically.

After rebooting into your new kernel, you must obtain the minor number:

bash:~$ dmesg | grep ^inotify

An example of what is returned follows:

inotify device minor=63

Since inotify is a misc device, the major is 10. Create the device node as the root user by executing the following command:

bash:~$ mknod /dev/inotify c 10 63

Note: Replace the "63" with your appropriate minor number as necessary.

Optionally, you should set the permissions as you like. A sample permission set is shown below:

bash:~$ chown root:root /dev/inotify
bash:~$ chmod 666 /dev/inotify

You are now ready to use the inotify device for file system monitoring.

No comments: