Linux – tmpfs usage and resizing

linuxmemorystoragetmpfs

I am trying to understand the stuff.
I have a machine with 80G storage.
It looks like that:

Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   50G  7.1G   43G  15% /
devtmpfs                 3.9G     0  3.9G   0% /dev
tmpfs                    3.9G  1.4M  3.9G   1% /dev/shm
tmpfs                    3.9G  409M  3.5G  11% /run
tmpfs                    3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/sda1                494M  125M  370M  26% /boot
/dev/mapper/centos-home   26G   23G  3.5G  87% /home
tmpfs                    782M     0  782M   0% /run/user/0

Now, from what I read the tmpfs doesn't take physical storage, but uses the virtual memory of the machine. Is it correct? Does it affect the physical storage in any way?

Is there a reality where the tmpfs will be written to the physical storage?
Next, do all the mounted (/dev/sda1, /dev/sda1, etc…) dirs share the tmpfs? Or each of them gets a different one?

Also, I tried to resize the tmpfs.
I did :

 mount -o remount,size=1G /dev/shm

On restart it went back to original size.
I changed /etc/fstab like this:

tmpfs      /dev/shm      tmpfs   defaults,size=1G

And then:

mount -o remount /dev/shm

it did the trick, but on restart it again went to it's original size.
I think I am missing something.

Best Answer

Now, from what I read the tmpfs doesn't take physical storage, but uses the virtual memory of the machine. Is it correct?

Correct. tmpfs appears as a mounted file system, but it's stored in volatile memory instead of a persistent storage device. So this could answer your other questions.

In reality you cannot assign physical storage to tmpfs since it only relies on virtual memory. Everything stored in tmpfs is temporary in the sense that no files will be created on the hard drive. Swap space is used as backing store in case of low memory situations. On reboot, everything in tmpfs will be lost.

Many Unix distributions enable and use tmpfs by default for the /tmp branch of the file system or for shared memory.

Depending of your distribution you can use tmpfs for the /tmp. By default, a tmpfs partition has its maximum size set to half of the available RAM, however it is possible to overrule this value and explicitly set a maximum size. In this example, to override the default /tmp mount, use the size mount option:

/etc/fstab
tmpfs   /tmp         tmpfs   nodev,nosuid,size=2G          0  0

source: https://wiki.archlinux.org/index.php/tmpfs

Related Question