tmpfs: temporary file system

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:

    /* romfs 挂载在 / 下 */
    /* fatfs 挂载在 /mnt 下 */
    /* tmpfs 挂载在 /mnt/tmp 下 */
    if (dfs_mount(RT_NULL, "/mnt/tmp", "tmp", 0, NULL) != 0)
    {
        rt_kprintf("Dir /tmp mount failed!\n");
        return -1;
    }copymistakeCopy Success

After compiling with the command scons, enter qemu.batto 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)

msh />cd mnt            # 使用 cd 命令切换目录
msh /mnt>mkdir tmp      # 使用 mkdir 创建新目录
msh /mnt>ls             # 使用 ls 命令查看当前目录信息
Directory /mnt:
tmp                 <DIR>copymistakeCopy Success

Execute "ctrl+c" to exit qemu, and then execute again qemu.batto run qemu

Use the file system's MSH commands to manipulate files in the tmpfs file system:

msh /mnt/tmp>echo "RT-Thread"         # 将字符串输出到标准输出
RT-Thread

msh /mnt/tmp>mkdir test               # 创建 test 目录
msh /mnt/tmp>echo "rtt" tmpfile.txt   # 使用 echo 命令将输入的字符串输出到指定输出位置
msh /mnt/tmp>ls                       # 使用 ls 命令查看当前目录信息
Directory /mnt/tmp:
tmpfile.txt         3
test                <DIR>

msh /mnt/tmp>cat tmpfile.txt          # 使用 cat 命令查看文件内容
rttcopymistakeCopy Success

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