1

I create a RAM Disk on Ubuntu 16.10 (running within a VM) with the following commands

sudo mkdir -p /media/RAMDisk
sudo mount -t tmpfs -o size=2048M tmpfs /media/RAMDisk

After this, if I go into the RAMDisk and do this

df .

I get the following

tmpfs            2097152     0   2097152   0% /media/RAMDisk

So far so good.

Then I reboot Ubuntu (which reboots the VM running via Workstation Pro).

After reboot, I expect either the /media/RAMDisk folder to not exist at all, or to exist as a tmpfs file system with no contents in it.

The /media/RAMDisk folder does exist with no contents, but surprisingly as a /dev/sda1 file system. When I do df . with /media/RAMDisk, I see this:

    /dev/sda1       17413904 9577448   6928836  59% /

Now, I could easily put the commands within my .bashrc and have them run and do the right things everytime, but I need to know what is going on.

Also, could my virtual machine manager -- Workstation Pro -- be doing something?

1

What is going on?

Nothing unusual.

df /media/RAMDisk, when it refers to /dev/sda1, tells you the directory at the moment belongs to the filesystem on /dev/sda1 which is mounted on /.

This is because the effect of your mount command is not permanent, it doesn't survive reboot. On the other hand the mkdir command created a directory inside the filesystem of /dev/sda1 and this filesystem is mounted after every reboot so the directory itself survives, but as a part of /dev/sda1 filesystem.

You need to mount RAMDisk after (or at) every boot somehow.


How to mount?

Running mount from within .bashrc is not a good idea because this file may be sourced multiple times during a single session. Since you need sudo anyway, it will be better to use /etc/rc.local which is run once when OS starts.

But the even better way may be to add the following line to your /etc/fstab:

tmpfs /media/RAMDisk tmpfs defaults,nosuid,nodev,size=2048M 0 0

Investigate user and noauto options (see man 5 fstab) and maybe you would like to use them and invoke mount /media/RAMDisk on demand only.


systemd?

This site makes me believe you can run a systemd unit when a given user logs in for the first time and terminate it as soon as the last session for the user is closed. I'm not very familiar with systemd, so I can't tell you how (if) you can do it with .mount unit.

But if you use systemd then you should already have a personal tmpfs mounted on /run/user/<UID>. The system-wide one should be on /dev/shm.


About /media/

Also note /media/ is used by Ubuntu to create mountpoints e.g. for external USB drives (I believe udisks2 is responsible). They may look like /media/<username>/<label> so I guess /media/RAMDisk is not going to collide with anything. In general I would avoid using this location though. In my systems (Debian, Kubuntu, Raspbian, OpenWRT) I use /mnt/<something> and I have never had any issues.

  • Thanks for this very helpful answer. I will try the options you suggested. – treecoder Oct 3 '17 at 23:31

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.