Documentation/translations/zh_TW/filesystems/tmpfs.rst
Source file repositories/reference/linux-study-clean/Documentation/translations/zh_TW/filesystems/tmpfs.rst
File Facts
- System
- Linux kernel
- Corpus path
Documentation/translations/zh_TW/filesystems/tmpfs.rst- Extension
.rst- Size
- 6678 bytes
- Lines
- 148
- Domain
- Support Tooling And Documentation
- Bucket
- Documentation
- Inferred role
- Support Tooling And Documentation: documentation
- Status
- atlas-only
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
- No C-style include directives detected by the generator.
Detected Declarations
- No top-level syscall, struct, function, initcall, or export declaration detected by the generator.
Annotated Snippet
.. SPDX-License-Identifier: GPL-2.0
.. include:: ../disclaimer-zh_TW.rst
:Original: Documentation/filesystems/tmpfs.rst
translated by Wang Qing<wangqing@vivo.com>
=====
Tmpfs
=====
Tmpfs是一個將所有文件都保存在虛擬內存中的文件系統。
tmpfs中的所有內容都是臨時的,也就是說沒有任何文件會在硬盤上創建。
如果卸載tmpfs實例,所有保存在其中的文件都會丟失。
tmpfs將所有文件保存在內核緩存中,隨着文件內容增長或縮小可以將不需要的
頁面swap出去。它具有最大限制,可以通過“mount -o remount ...”調整。
和ramfs(創建tmpfs的模板)相比,tmpfs包含交換和限制檢查。和tmpfs相似的另
一個東西是RAM磁盤(/dev/ram*),可以在物理RAM中模擬固定大小的硬盤,並在
此之上創建一個普通的文件系統。Ramdisks無法swap,因此無法調整它們的大小。
由於tmpfs完全保存於頁面緩存和swap中,因此所有tmpfs頁面將在/proc/meminfo
中顯示爲“Shmem”,而在free(1)中顯示爲“Shared”。請注意,這些計數還包括
共享內存(shmem,請參閱ipcs(1))。獲得計數的最可靠方法是使用df(1)和du(1)。
tmpfs具有以下用途:
1) 內核總有一個無法看到的內部掛載,用於共享匿名映射和SYSV共享內存。
掛載不依賴於CONFIG_TMPFS。如果CONFIG_TMPFS未設置,tmpfs對用戶不可見。
但是內部機制始終存在。
2) glibc 2.2及更高版本期望將tmpfs掛載在/dev/shm上以用於POSIX共享內存
(shm_open,shm_unlink)。添加內容到/etc/fstab應注意如下:
tmpfs /dev/shm tmpfs defaults 0 0
使用時需要記住創建掛載tmpfs的目錄。
SYSV共享內存無需掛載,內部已默認支持。(在2.3內核版本中,必須掛載
tmpfs的前身(shm fs)才能使用SYSV共享內存)
3) 很多人(包括我)都覺的在/tmp和/var/tmp上掛載非常方便,並具有較大的
swap分區。目前循環掛載tmpfs可以正常工作,所以大多數發佈都應當可以
使用mkinitrd通過/tmp訪問/tmp。
4) 也許還有更多我不知道的地方:-)
tmpfs有三個用於調整大小的掛載選項:
========= ===========================================================
size tmpfs實例分配的字節數限制。默認值是不swap時物理RAM的一半。
如果tmpfs實例過大,機器將死鎖,因爲OOM處理將無法釋放該內存。
nr_blocks 與size相同,但以PAGE_SIZE爲單位。
nr_inodes tmpfs實例的最大inode個數。默認值是物理內存頁數的一半,或者
(有高端內存的機器)低端內存RAM的頁數,二者以較低者爲準。
========= ===========================================================
這些參數接受後綴k,m或g表示千,兆和千兆字節,可以在remount時更改。
size參數也接受後綴%用來限制tmpfs實例佔用物理RAM的百分比:
未指定size或nr_blocks時,默認值爲size=50%
如果nr_blocks=0(或size=0),block個數將不受限制;如果nr_inodes=0,
inode個數將不受限制。這樣掛載通常是不明智的,因爲它允許任何具有寫權限的
用戶通過訪問tmpfs耗盡機器上的所有內存;但同時這樣做也會增強在多個CPU的
場景下的訪問。
Annotation
- Atlas domain: Support Tooling And Documentation / Documentation.
- Implementation status: atlas-only.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.