Tuesday, 9 March 2021

configure /dev/shm size of Linux

 

How to configure /dev/shm size of Linux?

To change the configuration for /dev/shm, add one line to /etc/fstab as follows.

tmpfs /dev/shm tmpfs defaults,size=8g 0 0

Here, the /dev/shm size is configured to be 8GB (make sure you have enough physical memory installed).

It will take effect next time Linux reboot. If you would like to make it take effect immediately, run

 

========

For many facilities there are system calls, others are hidden behind netlink interfaces, and even others are exposed via virtual file systems such as /proc or /sys. These file systems are programming interfaces, they are not actually backed by real, persistent storage. They simply use the file system interface of the kernel as interface to various unrelated mechanisms.


Now by default systemd assigns a certain part of your physical memory to these partitions as a threshold. But what if your requirement requires you to change tmpfs partition size?

For some of the tmpfs partitions, you can change the threshold size by using fstab. While for other partitions like (/run/user/) which are created runtime, you cannot use fstab to change tmpfs partition size for such runtime directories.

Below are the list of tmpfs partitions available in RHEL 7

Filesystem Size Used Avail Use% Mounted on
tmpfs      187G    0  187G   0% /dev/shm
tmpfs      187G  41M  187G   1%  /run
tmpfs      187G    0  187G   0% /sys/fs/cgroup
tmpfs       38G    0   38G   0% /run/user/1710
tmpfs       38G    0   38G   0% /run/user/0
NOTE:
You may notice that /etc/fstab does not contains entries for these tmpfs partitions but still df -h will show these partitions.

 

Change tmpfs partition size for /dev/shm

If an application is POSIX compliant or it uses GLIBC (2.2 and above) on a Red Hat Enterprise Linux system, it will usually use the /dev/shm for shared memory (shm_open, shm_unlink). /dev/shm is a temporary filesystem (tmpfs) which is mounted from /etc/fstab. Hence the standard options like "size" supported for tmpfs can be used to increase or decrease the size of tmpfs on /dev/shm (by default it is half of available system RAM).


For example, to set the size of /dev/shm to 2GiB, change the following line in /etc/fstab:

Default:

none     /dev/shm       tmpfs   defaults                0 0

To:

none     /dev/shm       tmpfs   defaults,size=2G        0 0

For the changes to take effect immediately remount /dev/shm:

# mount -o remount /dev/shm
NOTE:
A mount -o remount to shrink a tmpfs will succeed if there are not any blocks or inodes allocated within the new limit of the smaller tmpfs size. It is not possible to predict or control this, however a remount simply will not work if it cannot be done. In that case, stop all processes using tmpfs, unmount it, and remount it using the new size.

Lastly validate the new size

# df -h /dev/shm
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           2.0G     0  2.0G   0% /dev/shm

 

Change tmpfs partition size for /run

/run is a filesystem which is used by applications the same way /var/run was used in previous versions of RHEL. Now /var/run is a symlink to /run filesystem. Previously early boot programs used to place runtime data in /dev under numerous hidden dot directories. The reason they used directories in /dev was because it was known to be available from very early time during machine boot process. Because /var/run was available very late during boot, as /var might reside on a separate file system, directory /run was implemented.

 

By default you may not find any /etc/fstab entry for /run, so you can add below line

none     /run          tmpfs       defaults,size=600M        0 0

For the changes to take effect immediately remount /run:

# mount -o remount /run

lastly validate the new size

# df -h /run
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           600M  9.6M  591M   2% /run

 

Change tmpfs partition size for /run/user/$UID

/run/user/$UID is a filesystem used by pam_systemd to store files used by running processes for that user. In previous releases these files were typically stored in /tmp as it was the only location specified by the FHS which is local, and writeable by all users. However using /tmp can causes issues because it is writeable by anyone and thus access control was challenging. Using /run/user/$UID fixes the issue because it is only accessible by the target user.

IMPORTANT NOTE:
You cannot change tmpfs partition size for /run/user/$UID using /etc/fstab.

tmps partition size for /run/user/$UID is taken based on RuntimeDirectorySize value from /etc/systemd/logind.conf

# grep -i runtime /etc/systemd/logind.conf
RuntimeDirectorySize=10%

By default the default threshold for these runtime directory is 10% of the total physical memory.

From the man page of logind.conf

RuntimeDirectorySize=
      Sets the size limit on the $XDG_RUNTIME_DIR runtime directory for each user who logs in. Takes a size in bytes, optionally suffixed with the usual K, G, M, and T suffixes, to the base 1024 (IEC). Alternatively, a numerical percentage suffixed by "%" may be specified, which sets the size limit relative to the amount of physical RAM. Defaults to 10%. Note that this size is a safety limit only. As each runtime directory is a tmpfs file system, it will only consume as much memory as is needed.

Modify this variable to your required value, for example I have provided threshold of 100M

# grep -i runtime /etc/systemd/logind.conf
RuntimeDirectorySize=100M

Next restart the systemd-logind service

IMPORTANT NOTE:
A reboot of the node is required to activate the changes.

 

Change tmpfs partition size for /sys/fs/cgroup

/sys/fs/cgroup is an interface through which Control Groups can be accessed. By default there may or may not be /etc/fstab content for /sys/fs/cgroup so add a new entry

Current value for /sys/fs/cgroup

# df -h /sys/fs/cgroup
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            63G     0   63G   0% /sys/fs/cgroup

Add below line in your /etc/fstab to change the threshold to 2GB

none          /sys/fs/cgroup          tmpfs       defaults,size=2G         0 0

Remount the partition /sys/fs/cgroup

# mount -o remount /sys/fs/cgroup

Lastly validate the updated changes

# df -h /sys/fs/cgroup
Filesystem      Size  Used Avail Use% Mounted on
tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup

 

 

No comments:

Post a Comment