关于/dev/shm的文章
2015-11-26 21:25
1136 查看
https://wiki.archlinux.org/index.php/Tmpfs
http://www.walkernews.net/2010/05/04/how-to-resize-devshm-filesystem-in-linux/ http://www.xifenfei.com/page/142?dt_portfolio_category=projects-4
tmpfs is
a temporary filesystem that resides in memory and/or your swap partition(s), depending on how much you fill it up. Mounting directories as tmpfs can be an effective way of speeding up accesses to their files, or to ensure that their contents are automatically
cleared upon reboot.
Note: When using systemd, temporary files in tmpfs
directories can be recreated at boot by using tmpfiles.d.
[hide]
1 Usage
2 Examples
3 Disable
automatic mount
4 Troubleshooting
4.1 Opening
symlinks in tmpfs as root fails
5 See
also
Some directories where tmpfs is commonly used are /tmp, /var/lock and /var/run.
Do not use it on/var/tmp,
because that folder is meant for temporary files that are preserved across reboots.
Arch uses a tmpfs
existing as symlinks for compatibility. It is also used for
a specific configuration is needed.
glibc 2.2
and above expects tmpfs to be mounted at
shared memory. Mounting tmpfs at
so manual configuration in fstab is no longer necessary.
Generally, I/O intensive tasks and programs that run frequent read/write operations can benefit from using a tmpfs folder. Some applications can even receive a substantial gain by offloading some (or all) of
their data onto the shared memory. For example, relocating
the Firefox profile into RAM shows a significant improvement in performance.
By default, a tmpfs partition has its maximum size set to half your total RAM, but this can be customized. Note that the actual memory/swap consumption depends on how much you fill it up, as tmpfs partitions
do not consume any memory until it is actually needed.
To explicitly set a maximum size, in this example to override the default
use the
Here is a more advanced example showing how to add tmpfs mounts for users. This is useful for websites, mysql tmp files,
and more. It's important to try and get the ideal mount options for what you are trying to accomplish. The goal is to have as secure settings as possible to prevent abuse. Limiting the size, and specifying uid and gid + mode is very secure. For more information
on this subject, follow the links listed in the #See alsosection.
See the
in the man page is the
Reboot for the changes to take effect. Note that although it may be tempting to simply run
be safe to run
After applying changes, you may want to verify that they took effect by looking at
using
The tmpfs can also be temporarily resized without the need to reboot, for example when a large compile job needs to run soon. In this case, you can run:
Under systemd,
be automatically mounted as a tmpfs even though you have no entry for that in your
To disable the automatic mount, run:
Files will no longer be stored in a tmpfs, but your block device instead. The
will now be preserved between reboots, which you might not want. To regain the previous behavior and clean the
when restarting your machine, consider using
Considering
then create a file and create a symlink to that file in the same
the symlink, you will get a permission denied error. This is expected as
the sticky bit set.
This behaviour can be controlled via
via sysctl:
make this permanent.
Warning: Changing this behaviour can lead to security issues! Disable it only if you know what you are doing.
Linux
kernel documentation
http://www.walkernews.net/2010/05/04/how-to-resize-devshm-filesystem-in-linux/ http://www.xifenfei.com/page/142?dt_portfolio_category=projects-4
tmpfs
tmpfs is
a temporary filesystem that resides in memory and/or your swap partition(s), depending on how much you fill it up. Mounting directories as tmpfs can be an effective way of speeding up accesses to their files, or to ensure that their contents are automatically
cleared upon reboot.
Note: When using systemd, temporary files in tmpfs
directories can be recreated at boot by using tmpfiles.d.
Contents
[hide] 1 Usage
2 Examples
3 Disable
automatic mount
4 Troubleshooting
4.1 Opening
symlinks in tmpfs as root fails
5 See
also
Usage
Some directories where tmpfs is commonly used are /tmp, /var/lock and /var/run.Do not use it on/var/tmp,
because that folder is meant for temporary files that are preserved across reboots.
Arch uses a tmpfs
/rundirectory, with
/var/runand
/var/locksimply
existing as symlinks for compatibility. It is also used for
/tmpby the default systemd setup and does not require an entry in fstab unless
a specific configuration is needed.
glibc 2.2
and above expects tmpfs to be mounted at
/dev/shmfor POSIX
shared memory. Mounting tmpfs at
/dev/shmis handled automatically by systemd,
so manual configuration in fstab is no longer necessary.
Generally, I/O intensive tasks and programs that run frequent read/write operations can benefit from using a tmpfs folder. Some applications can even receive a substantial gain by offloading some (or all) of
their data onto the shared memory. For example, relocating
the Firefox profile into RAM shows a significant improvement in performance.
Examples
By default, a tmpfs partition has its maximum size set to half your total RAM, but this can be customized. Note that the actual memory/swap consumption depends on how much you fill it up, as tmpfs partitionsdo not consume any memory until it is actually needed.
To explicitly set a maximum size, in this example to override the default
/tmpmount,
use the
sizemount option:
/etc/fstab
tmpfs /tmp tmpfs nodev,nosuid,size=2G 0 0
Here is a more advanced example showing how to add tmpfs mounts for users. This is useful for websites, mysql tmp files,
~/.vim/,
and more. It's important to try and get the ideal mount options for what you are trying to accomplish. The goal is to have as secure settings as possible to prevent abuse. Limiting the size, and specifying uid and gid + mode is very secure. For more information
on this subject, follow the links listed in the #See alsosection.
/etc/fstab
tmpfs /www/cache tmpfs rw,size=1G,nr_inodes=5k,noexec,nodev,nosuid,uid=648,gid=648,mode=1700 0 0
See the
mountcommand man page for more information. One useful mount option
in the man page is the
defaultoption. At least understand that.
Reboot for the changes to take effect. Note that although it may be tempting to simply run
mount -ato make the changes effective immediately, this will make any files currently residing in these directories inaccessible (this is especially problematic for running programs with lockfiles, for example). However, if all of them are empty, it should
be safe to run
mount -ainstead of rebooting (or mount them individually).
After applying changes, you may want to verify that they took effect by looking at
/proc/mountsand
using
findmnt:
$ findmnt --target /tmp
TARGET SOURCE FSTYPE OPTIONS /tmp tmpfs tmpfs rw,nosuid,nodev,relatime
The tmpfs can also be temporarily resized without the need to reboot, for example when a large compile job needs to run soon. In this case, you can run:
# mount -o remount,size=4G,noatime /tmp
Disable automatic mount
Under systemd, /tmpmay
be automatically mounted as a tmpfs even though you have no entry for that in your
/etc/fstab.
To disable the automatic mount, run:
# systemctl mask tmp.mount
Files will no longer be stored in a tmpfs, but your block device instead. The
/tmpcontents
will now be preserved between reboots, which you might not want. To regain the previous behavior and clean the
/tmpfolder automatically
when restarting your machine, consider using
tmpfiles.d(5):
/etc/tmpfiles.d/tmp.conf
# see tmpfiles.d(5) # always enable /tmp folder cleaning D! /tmp 1777 root root 0 # remove files in /var/tmp older than 10 days D /var/tmp 1777 root root 10d # namespace mountpoints (PrivateTmp=yes) are excluded from removal x /tmp/systemd-private-* x /var/tmp/systemd-private-* X /tmp/systemd-private-*/tmp X /var/tmp/systemd-private-*/tmp
Troubleshooting
Opening symlinks in tmpfs as root fails
Considering /tmpis using tmpfs, change the current directory to
/tmp,
then create a file and create a symlink to that file in the same
/tmpdirectory. If you try to open the file you created via
the symlink, you will get a permission denied error. This is expected as
/tmphas
the sticky bit set.
This behaviour can be controlled via
/proc/sys/fs/protected_symlinksor simply
via sysctl:
sysctl -w fs.protected_symlinks=0. See Sysctl#Configuration to
make this permanent.
Warning: Changing this behaviour can lead to security issues! Disable it only if you know what you are doing.
See also
Linuxkernel documentation
相关文章推荐
- tmpfs /dev/shm
- 【问题记录】数据库打不开 报ORA-00845错误 /dev/shm设置过小
- Oracle 11g internals part 1: Automatic Memory Management
- Oracle 11g internals part 1: Automatic Memory Management
- LeetCode---Count and Say
- 字符串匹配算法总结
- 用JAVA写的一个只基于awt的计算器---比较简陋,继续加油!
- spring bean单例模式改成原型模式
- 提升进程权限为DEBUG权限
- 继承,初始化方法,便利构造器
- HTML常见的实体字符
- 【云快讯】HP 将关闭公有云服务,转售微软的 Azure 云服务
- Android Studio 导入github源码
- android 自定义标题栏和自定义下拉选项PopupWindow
- Delphi检测网络连接状态
- 日报2015/11/26(第一行代码读书笔记)
- dwz.cn接口java客户端实现
- 【云快讯】HP 将关闭公有云服务,转售微软的 Azure 云服务
- codeforces148e
- 【C语言】删除任意一个节点n(不知道头结点,且所删节点既不是头结点也不是尾节点)