如何设置tmpfs的大小
https://access.redhat.com/solutions/2843
How do I modify the size of tmpfs?
环境
- Red Hat Enterprise Linux 7
- Red Hat Enterprise Linux 6
- Red Hat Enterprise Linux 5
- Red Hat Enterprise Linux 4
问题
- How can an in-ram temporary filesystem be created?
- To increase the default size of tmpfs
决议
Modifying the size of tmpfs on /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 1GiB, change the following line in /etc/fstab:
Default:
none /dev/shm tmpfs defaults 0 0
Changed:
none /dev/shm tmpfs defaults,size=1G 0 0 <<== add "size=1G"
For the changes to take effect immediately remount /dev/shm:
# mount -o remount /dev/shm
Creating a new tmpfs mount
- Example of mounting tmpfs on the fly with no options on a system with 4GB of RAM
# mkdir /mnt/tmp
# mount -t tmpfs tmpfs /mnt/tmp
# df -h /mnt/tmp
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.9G 0 1.9G 0% /mnt/tmp
- Example of mounting two tmpfs with specific sizes via fstab
Add the following lines to /etc/fstab:
tmpfs /tmp1 tmpfs size=200m 0 0
tmpfs /tmp2 tmpfs size=1g 0 0
Run the following commands:
# mkdir /tmp{1,2}
# mount -a
# df -h /tmp?
Filesystem Size Used Avail Use% Mounted on
tmpfs 200M 0 200M 0% /tmp1
tmpfs 1.0G 0 1.0G 0% /tmp2
Additional notes
-
tmpfs is mounted solely in memory, allowing the kernel to avoid any disk I/O layers altogether; reads and writes go directly to the page cache. Following from this, all data in a tmpfs is lost when the tmpfs is unmounted.
-
By default, tmpfs are created with half of the active RAM size. If this value requires changing, use the optional
size=mount option as seen above to set the desired size. -
Note that tmpfs is backed by swap space -- if swap is available, tmpfs pages might be swapped out to disk.
-
See the "Mount options for tmpfs" section of the mount(8) manual page (
man mount) for a full explanation of the available mount options. Further details regarding tmpfs is available in/usr/share/doc/kernel-doc-<version>/Documentation/filesystems/tmpfs.txtfile. -
A
mount -o remountto 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. -
A
mount -o remountto grow a tmpfs will always succeed.
WARNINGS
- The
mountcommand allows the creation of a tmpfs larger than available RAM. This is usually a bad idea, and should absolutely be avoided if system swap is unavailable. - If no system swap is available, make sure the system has enough RAM to accommodate the size and usage of any tmpfs being created.
- In Red Hat Enterprise Linux 6.0 - 6.3, use of the "
size=" mount option for a tmpfs mount in/etc/fstabhas no affect at boot time. See tmpfs size option specified in fstab is ignored during boot proccess for more details.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
浙公网安备 33010602011771号