I'm looking for a way to have a tmpfs-like file system that can be unlimited in size, but will use a specified amount of RAM after which the "oversize" data will be stored on another disk-backed filesystem


I'm running on a SSD-only system, with low available space (usually < 3 GB), so I don't want to reserve any space for SWAP or similar (that's my main requirement)

Do you know of any solution that would fit my use-case ?

  • The functionality you desire is already the default behaviour. Linux automatically caches files in RAM for you. Maybe you should describe your use case in more detail. – frostschutz Apr 22 '16 at 22:32

You can use dmsetup to set a linear device made of a /dev/ramX plus your /dev/sdX disk-device.

For instance, from the command prompt type:

dmsetup create mydev << EOF
> 0 65536 linear /dev/ram0 0
> 65536 262144 linear /dev/sdb 0

The above creates a logical device named mydev made of a 32MB (65536 sectors) ramdisk plus the first 128MB (262144 sectors) of /dev/sdb. Of course replace /dev/sdb with the actual device file for your disk.

You may also want to use the exact size (or a bit less) of your disk or partition, and for that you need to know that size expressed in 512-bytes sectors. In order to know that for e.g. the /dev/sdb1 device you may do:

grep sdb1 /proc/partitions

and then use the big number that appears on the left of the device name, multiplied by 2. (because that big number is expressed in 1024-bytes blocks).

Then you have to format mydev with your file-system type of choice. For instance:

mkfs.ext4 /dev/mapper/mydev

Finally mount it with:

mount /dev/mapper/mydev /mnt

Replace /mnt with the actual directory you want to use as mount point, and enjoy.

To remove everything, do:

umount /mnt
dmsetup remove mydev

However it’s worth saying that this setup is less efficient than direct tmpfs filesystems, because tmpfs is already a (virtual) file-system on top of Linux's virtual memory manager, whereas ramdisks need an additional operation of memory-pages copying, plus they still require a regular file-system with its overhead on top of them.

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.