From the course: Linux Device Drivers

Create and use device files - Linux Tutorial

From the course: Linux Device Drivers

Start my 1-month free trial

Create and use device files

- [Instructor] Device files are the means for processes to interact with character and block device drivers. So device files are special. Let's see how they're special. To talk more about device files, we need to talk about the /dev directory. And we need to talk about kinds of device files and how do we or how are device files created? And that leads to a discussion of how device files are often automatically created. The /dev directory these days is usually a Ram-based temp file system. So it's not on disc. Device files are recreated dynamically when the system is booted and as modules are loaded. So, the Linux kernel itself and privileged users and certain services can add entries to the dev directory, can dynamically create device files. It can also manipulate device files like change their permission modes. So device files are typically in /dev or one of its sub-directories, although they could be elsewhere. It is common though, that for security reasons, things like removable media that could be mounted by an ordinary non-privileged user, that media not be allowed to have device files because permissions on device files means something. And a non-privileged user creating a device file and giving themselves write permission, could allow a non-privileged user to write to a device when we don't want to allow that. And then the dev directory, notice that contains device files and there's two kinds, character and block device files. Plus the dev directory usually has some soft links and sub-directories to help organize things. If we look how the dev directory is mounted, by typing mount and searching for space /dev space, we can see udev is mounted on /dev. So udev is an internal name thing, and what important here is what's mounted on /dev is the thing of type devtmpfs. And this devtmps is populated by the kernel. Like we said, there were two kinds of device files, character and block. And depending on the type, that defines what sort of operations you could do with that device file. Character device files are very analogous to what you think of as ordinary files. You can read them, write them, maybe seek on 'em and so forth. Block device files, an easier way to think of them is usually a block device file, there's something you can mount a file system on. A common block device file would be for a hard disc then. If we go to /dev and we do a long list, the first character in the long list there is an indication by Ellis to what type something is. The letter c means character device file. D means directory, l means soft link. We scroll down a little bit further, we start seeing some block device files. Like loop zero, loop one, and so forth. So we're in /dev. If we say, find here everything of type c character device file and we count, we see there's 230 on my system. If we see how many are block, there's 30. Of course this really varies depending on how many disks you have on your system. And just as an interesting little aside, let's look at dev null, it's a character device file. And if we go to /sys and we look for character device files, we actually find one. It's a character device file, and it's actually equal to dev null. Let's talk about how device files get created. Well, first of all a device file does not have to be underneath /dev, we saw one underneath sys. You could potentially put a device file on your home directory. Whether you can use them, depends on mount options, and also some types of file systems might not support a device file type. So they can't just go anywhere, but they can go lots of places. It's a privilege operation to create a device file. And when you create a device file, it gets some permissions, just like any other file that's created. So be careful with that. So you could have removable media like a USB stick that you plug in one computer where your route and you create a device file on it, and you give everybody write permission. Then you take out the USB stick and you put it in a computer where you don't have route permission, and that stick gets mounted, and now it's got the device file on it. You can use that device file if the USB stick got mounted, allowing for that. But generally Linux systems are configured to not allow device files to be used on stuff like USB discs. So you can make a device file with a command, mknod or mknod, the name of the file, then either a c or a b for character or block, and then a number which will be known as the major device number and another number known as the minor. Traditionally, the major number identifies which device driver the file corresponds to, and the minor number is used by the device driver to say, distinguish instances of device. Like different serial porch say or different partitions on a disc. When you do a mknod, it creates a file and that file's going to have some permission modes, so be careful to check that. Use your umask or changemod to set the permission modes the way you want 'em. So let's look at creating device files. We'll go to the tmp directory. We're going to set our umask to zero. So we're not going to mask out any permission modes, and let's make a device file. Let's call it x_cdev and it'll be a character and we'll give it one, one. We do a long list on that and we see it as read-write, read-write, read-write. That might not be what you want. Now let's make a block device file and check its permission modes. And we'll look at that. It's also read-write, read-write, read-write. So, whether it's a character or a block here the mknod is creating it with read and write permission for everyone. Now you can create device files for devices that don't exist. As long as the number's within a reasonable range, you can create a device file with those numbers. That doesn't mean that there's actually a device driver that will correspond to operations with that device file. We're going to talk more about that later. So device files can be created automatically and needs days. They almost always are. Except during development for you writing device driver, you might want to create your own device files. At boot time, the kernel is going to create this devtempfs these days and populate it with all the device files that correspond to the drivers that have been statically linked in the kernel. And then as modules are loaded, additional device files get created. So you could have a demon running udevd or systemd, that can also affect the device files. There are configuration files underneath Etsy you can use to have these demons rename a device file, or change its permissions, and some other stuff. Years ago in Unix days, device files were always created by hand by a system administrator. You added a new disc, you had to create a device file for it. And in those days you knew what numbers to use. These days, the Linux kernel generally generates the numbers for the device files. So, creating it by hand ahead of time isn't even possible. Because these numbers are an association between the driver and the file. The file has to have the number that the driver says it's going to handle. And if the driver doesn't say a number, if the driver is assigned a number, say when the module's loaded, then you can't create the device file until after the module has been assigned a number. So it's really important that these are created automatically. Right, so you should have a much better understanding of device files and they're important. (chuckles) That's how user space is going to interact with the drivers.

Contents