tmpfs usage and resizing

28,253

Solution 1

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

Solution 2

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?

Well, it fills the swap area, to which it is limited. See tmpfs does not overflow to swap

Next, do all the mounted (/dev/sda1, /dev/sda1, etc...) dirs share the tmpfs? Or each of them gets a different one?

This depends on where the tmpfs is mounted. But all tmpfss should share the same memory, if that's what you meant.

I think I am missing something.

Maybe https://superuser.com/questions/45342/when-should-i-use-dev-shm-and-when-should-i-use-tmp:

Recent 2.6 Linux kernel builds have started to offer /dev/shm as shared memory in the form of a ramdisk, more specifically as a world-writable directory that is stored in memory with a defined limit in /etc/default/tmpfs. /dev/shm support is completely optional within the kernel config file. It is included by default in both Fedora and Ubuntu distributions, where it is most extensively used by the Pulseaudio application. [emphasis removed and added]

Share:
28,253

Related videos on Youtube

matisa
Author by

matisa

Updated on September 18, 2022

Comments

  • matisa
    matisa over 1 year

    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.

  • matisa
    matisa almost 6 years
    I did this: tmpfs /dev/shm tmpfs defaults,size=1G Then: mount -o remount /dev/shm And it worked, but on restart went to original size - why?
  • jdwolf
    jdwolf almost 6 years
    @matisa Because changes to mountpoints are not persistent unless added to fstab or configured in systemd (some are also autodetected by systemd)
  • matisa
    matisa almost 6 years
    I think I am missing shomething. Thats what I did, I added the fstab and set size to 1G.
  • klerk
    klerk almost 6 years
    @matisa which OS distro you are using ?
  • winterisComing
    winterisComing over 3 years
    @matisa - I wonder if you need to rebuild the "init ramdisk". It is possible that when the "init ramdisk" is rebuilt, /etc/fstab is copied into the "init ramdisk". That would explain why changes to /etc/fstab did not take effect on the next reboot. On Ubuntu, you rebuild the init ramdisk with the command update-initramfs -c -k all. I don't know the command on Centos.