1

(Inspired by https://unix.stackexchange.com/a/254254/320598)

In Linux ram disks like /dev/ram0 created by the brd driver aren't removable according to /sys/block/ram0/removable, and I wonder: Why?

One reason I could think of is: If you remove a "normal" removable disk, the data is still there, and it can be re-inserted/re-connected, but when ejecting a ram disk, the data is lost. A related problem may be how to create ram disks one-by-one, and remove those one-by-one again. Using blockdev --flushbufs to de-allocate a ram disk had been discussed as bad design already (usually flushing disk buffers does not remove the data from the disk). See also:

U. Windl
  • 1,095
  • 7
  • 21
  • 1
    I'd like to point out ramfs and tmpfs to you. The old ramdisk mechanism uses a fixed size RAM area for a block device onto which a file system is created. When you read files from this "disk" the kernel copies the data to the page cache, just like with a normal disk based file, which results in unnecessary copying and on top of that the file data is stored twice in RAM. The new (only about 15 year old :) ramfs skips the RAM block device and serves the files directly out of the caches (page cache and dentry cache). To free the memory, just delete the files. – Johan Myréen Oct 27 '20 at 15:30
  • @JohanMyréen How would I create a block device using `ramfs` or `tmpfs`? Or are thise just "filesystems" instead of "block devices"? For example I see `tmpfs` is mounted on `/dev`, but where is the block device? In my use case I use the ram disk with `dmsetup`. – U. Windl Oct 30 '20 at 07:43
  • Yes, ramfs and tmpfs are just filesystems. There is no backing store or block device. As ramfs and tmpfs are already dynamically resizable, there is no need for a mechanism like device mapper that collects physical devices of limited and fixed size into a virtual block device. If your use case is to test device mapper with ramdisks, then tmpfs is not for you. My comment was also not intended to be an answer to you question (it is a comment, after all), but just to point out the newer alternatives to the old ramdisk mechanism. – Johan Myréen Oct 30 '20 at 09:44
  • @JohanMyréen To explain what I'm doing with those RAM disks: I need a test disk for testing error handling mechanisms in my application, so I use dmsetup to "inject" I/O errors and read delays into those RAM disks, and after the test I want to "recycle" them. – U. Windl Dec 14 '20 at 10:27

0 Answers0