0

With ls -l, I found a series of devices like ram0, ram1 etc. I looked at the output closely and (for example)

brw------- 1 root root 1, 3 Jan  6 11:34 /dev/ram3
crw-rw-rw- 1 root root 1, 3 Jul 15  1970 /dev/null

Hmmm, they only differs by device type (block and character). I tried reading and writing to ram3, but to my surprise, I can read a 8 MiB block of zero bytes from it, and I can't write more than 8 MiB to it, as I thought I could immediately receive an EOF when reading and could write infinitely to it.

I then tried other rams, but more surprisingly they all behave the same. /dev/ram8 doesn't generate random stuffs like /dev/random do, /dev/ram7 doesn't report "disk full" like /dev/full...

Then I tried mknod-ing everything in my working directory (mount type: ext4), and the results are the same: character devices behave the same as /dev/null,full,zero,random and block devices same as /dev/ramX

So I started wondering:

  • How is /dev/ramX different from /dev/<device> with the same device number?
  • How to explain the 8 MiB empty stuff (all zero) when read and 8 MiB write limit for those block devices?

I guess the answers should apply to all Linux-kernel systems.

(Side note: 8 MB = 8,000,000 bytes and 8 MiB = 8,388,608 bytes)

1

The block devices are completely separate from character devices. The fact that the device major/minor numbers are equal means nothing at all: you can think of the block/character device bit as an extra, most significant bit in the device numbers.

Conceptually, the device numbers are indexes into the kernel's big table of devices. Historically there used to be one table for character devices and another for block devices. I think Linus Torvalds regards this division as just a historical artifact and might eventually want to literally treat the block/character device bit as just an extra binary digit for the device number. But for now, some old software assumes that any Unix-like system has block devices and character devices and that their natures are fundamentally different, and thus some semblance of this old design will remain.

The /dev/ram* devices are defined in the kernel configuration with the CONFIG_BLK_DEV_RAM* compile-time options:

  • CONFIG_BLK_DEV_RAM determines if these devices are built-in to the kernel, compiled as a module (brd.ko) or completely omitted. Debian 9 has it as a module.
  • CONFIG_BLK_DEV_RAM_COUNT determines the number of /dev/ram* devices to create. Kernel "factory default" is 16 devices.
  • CONFIG_BLK_DEV_RAM_SIZE will define the size of each /dev/ram* device: kernel "factory default" is 4 MiB per device, Debian 9 uses 16 MiB. Your distribution apparently uses 8 MiB.

If the RAM disk driver is compiled as a kernel module, you can modify the number and size of /dev/ram* devices by using module parameters.

The /dev/ram* devices are usually used by Linux installers and such, to allow for setting up something reasonably similar to a normal system when there is no real disk available.

They are RAM-based block devices, so they behave generally like disks: they have a finite size, you can create filesystems on them. You could create a partition table on them, but there are no standard device numbers allocated for RAM disk partitions. Should you need a partitioned RAM disk, you could use the kpartx command to easily work around this: the resulting partition devices for /dev/ramX would typically be named like /dev/mapper/ramXpY. Obviously anything you store onto the /dev/ram* devices will be lost at reboot or power loss - or when you unload the brd.ko module, if applicable.

I think some distributions (used to?) use the /dev/ram* devices when creating the initramfs/initrd. (Whenever a new kernel package is installed, a new initramfs file is usually created for it automatically. It cannot come pre-packaged, as the initramfs file needs to include several settings specific to your system.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.