tmpfs: temporary file system
Last updated
Last updated
Assoc. Prof. Wiroon Sriborrirux, Founder of Advance Innovation Center (AIC) and Bangsaen Design House (BDH), Electrical Engineering Department, Faculty of Engineering, Burapha University
tmpfs is an implementation of a temporary file system. It is a memory-based file system with the following advantages:
Dynamically adjust virtual memory
Fast reading and writing speed
Another feature of tmpfs is that it does not exist on an underlying block device, but is directly built on virtual memory. You do not need to use mkfs to format it, you can directly mount it to create a tmpfs file system. Since data is stored in memory, the data will be lost after a power outage.
In RT-Thread, there is also a ramfs. The differences between tmpfs and ramfs are shown in the following table:
characteristic | tmpfs | ramfs |
Is the size fixed? | no | yes |
Whether to support folder operations | yes | no |
Volatile | yes | yes |
Currently, tmpfs already exists as a component in RT-Thread, located at rt-thread\components\dfs\filesystems\tmpfs
, and can be tailored and configured.
For the operation of tmpfs files, the application layer can directly use POSIX to access them. The access sequence diagram of the operation file is as follows:
The operations on files are:
open: open a file
close: close the file
read: read file data
write: write data to a file
lseek: Change the read and write pointer position when reading and writing a file
getdents: Get directory entries
stat: Get file status
The POSIX interface can be used to manage directories. Its access sequence diagram is similar to the file access sequence diagram. The operations on the directory are:
mkdir: Create a directory
rmdir: Remove a directory
opendir: open a directory
readdir: read directory
closedir: close directory
dirent: read directory
telldir: Get the read position of the directory stream
seekdir: Set the next directory read location
rewinddir: reset the directory read position to the beginning
You can use the DFS MSH command to operate tmpfs. The commonly used MSH commands for file system operations are shown in the following table:
MSH Commands | describe |
ls | Display information about files and directories |
cd | Enter the specified directory |
cp | Copying Files |
rm | Deleting a file or directory |
mv | Move or rename the file |
echo | Write the specified content to the specified file. If the file exists, write it to the file. If the file does not exist, create a new file and write it to it. |
cat | Display the contents of a file |
pwd | Print out the current directory address |
mkdir | Create a folder |
The following is a sequence diagram of the application layer using the DFS virtual file system API to access tmpfs:
Automatic file system initialization: After automatic initialization is turned on, both the virtual file system and the temporary file system will be automatically initialized without user execution.
Use dfs_mount() at the application layer to mount the tmpfs file system.
At the application layer, use the DFS virtual file system API to access tmpfs. For more API interfaces, see DFS Virtual File System .
Use QEMU to demonstrate the use of tmpfs, taking mounting it in the "/mnt/tmp" directory as an example.
In the rt-smart branch of the rt-thread source code, open the qemu-vexpress-a9 BSP, use menuconfig to configure tmpfs in the component, located in "RT-Thread Components → Device virtual file system", exit and save.
Note: If multiple file systems are used in the same system, pay attention to modify the values of the number of mountable file systems and the number of file system types on the same interface to support multiple file systems.
The code for mounting the file system using dfs_mount in mnt.c is as follows:
After compiling with the command scons
, enter qemu.bat
to run qemu, switch to the mnt directory and create a tmp directory (if you use fatfs for the first time, you need to format it with mkfs first)
Execute "ctrl+c" to exit qemu, and then execute again qemu.bat
to run qemu
Use the file system's MSH commands to manipulate files in the tmpfs file system: