Creating a ramdisk

I’ve been using a ramdisk under Haiku on and off for a while now. The command has been in Haiku’s sources for a long time, and I just compiled it myself. Now it’s officially added to Haiku images and I updated the user guide accordingly. Until that update finds its way to the images and website, I reproduce the entry here.


A ramdisk is like a harddisk running only in the computer's memory. That makes it very fast but also volatile, because its contents vanishes when you shut down the computer, or it crashes or you experience a blackout.
To create a ramdisk of 1 GiB, format to the name "RAMses" and mount it, you enter this in Terminal or create a script of it:

ramdisk create -s 1gb
mkfs -q -t bfs /dev/disk/virtual/ram/0/raw RAMses
mountvolume RAMses

Note: When creating a ramdisk, the ramdisk command prints out the path to it. If you create several disks, that path /dev/disk/virtual/ram/0/raw will change!

To preserve the contents, at least if no calamity like a blackout etc. strikes, a ramdisk can be set up to read/write an image on the harddisk. For that, you need to supply a file of the desired size that will be read from every time you start your ramdisk, and written to when you unmount it.
To create an image file “RAMimage” of 500MiB and format it, do this:

dd if=/dev/zero of=RAMimage bs=500M count=1
mkfs -q -t bfs /dev/disk/virtual/ram/0/raw RAMimage

From now on, you start the ramdisk like this:

ramdisk create RAMimage
mountvolume RAMimage

It’s very important to always cleanly unmount you ramdisk, either from Tracker or with unmount /RAMimage, or the changes won’t be written back to the image file!

7 Likes

A little technical info on RAM usage.

The RAMdisk by default does not allocate the memory when created. It allocates new sectors as they are read or written. This means it will not eat 1GB of RAM immediately when mounted.

If you put a lot of data in the ramdisk, and then remove it, however, the memory will remain allocated. If you need to free some RAM for other uses, you can use the fstrim command to do so. It will release all blocks now unused by the filesystem.

4 Likes

It looks like one can automate the various steps using scripts:
-create the RAMDisk
-mounti it
-if a raw disk image exists, upload it to the RAMDisk
-use it
-if it is desired to keep changes, download the RAMDisk to to a raw disk image (create if not existing)
-free memory blocks

Is this correct?

You can create a ramdisk from an image file. If the image is already initialized, the filesystem will be detected and Tracker will automaticcaly offer to mount it (like when you plug an USB drive).

Downloading to the disk image is done when unmounting.

Freeing memory blocks is done when unmounting, or using fstrim: “fstrim /MyRAMDisk”

1 Like

Some cautions:

ramdisk delete without unmounting through Tracker will cause a KDL.

ramdisk create must use a size or file smaller than the free memory or it will also KDL.

That sounds like a bug… have you opened a ticket @ http://dev.haiku-os.org ?

Why not an article on the website?

1 Like

I want to try the ramdisk command, but i does not foud him on my system. Past that hrev it is included?

I have hrev 50443

It was added only recently in hrev50664.

Three years later…

Is there a way to have Haiku run entirely from within a RAMdisk at boot? How would this be accomplished, if possible?

You have 2 options here currently:

Quick n dirty PoC ran:
Made 3 gig ramdisk, copied iso and img file to it.
Changed paths in the qemu line. Totally runs/walks. (pushing 13 processor to limits) For a proof of concept.
Clean shutdown vm, retrieve and store image.
Can throw the RAM-drive into the trash.
very classic Fruitos :smiley: .

Store the humdinger device in your qemuscript, slows the initial startup down for a few seconds of filecopy…
Great fun, Thanks much!
.

1 Like