From the course: Linux Device Drivers

Use block device files - Linux Tutorial

From the course: Linux Device Drivers

Start my 1-month free trial

Use block device files

- [Instructor] Let's talk about block drivers, drivers that are controlled by the block layer and you normally use indirectly via file system. Note though, that file systems don't need block devices, for example, procfs and sysfs are not on block devices, but slash is probably on a block device on your disk. So we'll talk about block device files, quick little list of kinds of block drivers and review mounting. So like character device drivers, block device drivers have a device file. LS shows the type as B and like character device files, there's a major and minor number. For block devices, the minor number is typically an indication of which disk and which partition. So there's kind of a jump between numbers because as you go to a new disk, you jump a bunch of minors and block devices don't have to be on fixed media on secondary storage, they just have to act like that. So there's a cool block device, the network block device, where it acts like a disk, but instead of storing to disk, it writes it out over the network to a server. So you can format it and mount it just like a disk. So a block device is something that acts like a block device, it doesn't have to physically be a block device. There are, RAM disks also that act like a disk, but it all happens in RAM. And by the way, that's different than RAM fs. All right, let's take a quick look at block device files. We'll look at the ones that start with SD on my system. You could see, I have quite a few. I've got SDA, SDB, and so forth. They all have major number eight, but if you look at the minor numbers, you can see they skip. B starts at 16, C starts at 32, and so forth. That leaves some minor numbers in there for partitions. So we saw with NBD and so forth, that there's different kinds of block devices or block drivers for devices. Another interesting one is loop. You use the loop block driver when you're going to mount into your file system, a file that has a file system on it. Usually you mount with a block device file. But if you don't have a block device file because your file's on a file system, you need one, so that's what the loop one provides. It provides a block device file for you. SCSI is the most common block driver these days. That's what most disks are, the SD stuff. RAM disk used to be pretty common. It used to be common that Linux would mount with an initial RAM disk, now it uses an initial Ram file system. One of the key things about a disk is it uses the block layer for caching and that's in RAM. And when you had a Ram disk in RAM, and you had the block layer in RAM, you had like two copies of stuff in RAM, wasteful. RAM file system just uses the block cache itself. Another kind of block device you might be familiar with is RAID where you can have multiple physical disks appear like a single one or even Logical Volume Manager that does a similar sort of thing. So, you want to be reminded about mounting, right? A mounting I say is associating a file system with a directory in the system file system. So you mount a directory on top of a file system. Often that hides what was there before, if it wasn't an empty directory, but there are interests just in cases where stuff can show through in effect. So you mount with block device files, which are usually disks, and the device file usually corresponds to a partition because each partition is formatted with a file system. So you want a device file that corresponds to a file system. So you have what's known then as a physical file system. Each partition has a physical file system, and then you have your logical file system that starts at slash. So underneath slash you can have lots of separate physical disks mounted and their file systems are mounted and then you have different types of file systems. When an application works with a file, it opens up the file and reads and writes it, those are actually operations implemented by the file system, not by the block device. It's the block layer that really interacts with the device driver and asks the device driver to do I/O. Let's just take a quick look at an example with mounting. Let's do an example where we do a loop back device. So let's go to slash temp. We're going to create a temporary file. We'll just do DD, input file will be the device file dev zero, which you can read from as much as you want, dev zero never gives end of file, just gives you binary zeros. Many devices you asked for, it gives you that many zeros. Output a file, we'll create a file. We'll say the block size is one meg. So we'll copy one meg at a time and we'll do that 10 times. So we'll create a file that's 10 meg, really fast. Let's format that because we need a file system if we're going to mount it and we'll just do a simple non-journaling file system, ext2 on there, that's really fast. So now we have a file system in a file and when we want to mount something we need a directory so let's make an empty directory, and then we want to mount it. So ordinarily with mounting, we would put a device file like dev SDA or something, SDA one, say, but we want to use the loop back device file so we're going to use the dash O loop to mount and it'll get us a loop device. And then we're going to mount afile on adir and we go to adir and we do a DF minus H and it says it's 9.7 meg, there's a little bit of overhead there, we've used 24K even though we haven't put anything in it, because a little bit of overhead. And if we do mount grep adir we see there, afile is mounted on temp adir, even though afile is not a block device. That's because of the magic of loop.

Contents