On Linux, how can a RAM disk be created and attached without being mounted? Moreover, how can the device name like /dev/sdb assigned to the RAM disk be found? Finally, how can the RAM disk (which is not mounted) be detached?

I would like to create and attach a RAM disk, but I do not want to mount it. I want to leave it unmounted.

On Mac OS X, the following command creates and attaches a 100MiB RAM disk without mounting it.

hdiutil attach -nomount ram://204800

# In terms of the number of 512-byte sectors,
# 100 MiB is equivalent to 204800 sectors.

Upon successful completion, the above command returns the device name such as /dev/disk1 assigned to the RamDisk. However, later when you forget the device name, you can find it by the following OSX commands (as well as the GUI application Disk Utility).

# Displays the list of attached disks.
diskutil list

# Displays the details of a specific disk.
diskutil info /dev/disk1

Finally, on Mac OS X, to detach the RAM disk, no matter whether it has been mounted, the following command (as well as the GUI application Disk Utility) works. This command automatically unmounts it first if it has been mounted.

hdiutil detach /dev/disk1

On Linux, how can all the above three operations be achieved? (If the operations I am asking for may depend on distros, then I am particularly interested in Debian 9.)

One of the reasons that I do not want the RAM disk to be mounted is that I want to run some experiments on the dd command reading and writing directly on a disk (or device), bypassing the filesystem. In fact, the direct writing mode of dd requires the volume on the device to be unmounted, but the device to be still attached and not ejected.

On Linux, the following mount command creates, attaches and mounts a 100MiB RAM disk.

mkdir /mnt/ramdisk
mount -t tmpfs -o size=100m tmpfs /mnt/ramdisk

However, I do not know how to disable the "mounting" part.

By the way, for those who want to know how to mount, in addition to creating and attaching, a RAM disk on Mac OS X, here is the command.

diskutil erasevolume HFS+ "RamD" $(hdiutil attach -nomount ram://204800)

# 204800 is the number of 512-byte sectors,
# resulting in 100 MiB.
# "RamD" is a volume name or label.
# "erasevolume" means to format the volume, 
# but it does not necessarily write zeros.

The problem in your case is that with tmpfs there is no RAM based block device to mount. Normally, when Linux reads from a file system, the files reside on a block device, typically a hard disk partition. When files are read from disk (the backing store), the data is cached in RAM.

Tmpfs cuts out the backing store: writes go to the cache and reads read the written data back from the cache. This means there is no block device you can dd to.

For your need I recommend using a loop device, which enables you to mount a disk image stored in a file as a filesystem. For example:

losetup /dev/loop0 filesys.image

where filesys.image is a file containing a filesystem image. This makes /dev/loop0 look like a block device with the file filesys.image as backing store. See man losetup.

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.