# 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.

### [File Management](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/tmpfs?id=%e6%96%87%e4%bb%b6%e7%ae%a1%e7%90%86) <a href="#wen-jian-guan-li" id="wen-jian-guan-li"></a>

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:

![image-20221101172133720](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/figures/uml-file.png)

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

### [Catalog Management](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/tmpfs?id=%e7%9b%ae%e5%bd%95%e7%ae%a1%e7%90%86) <a href="#mu-lu-guan-li" id="mu-lu-guan-li"></a>

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

### [MSH Commands](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/tmpfs?id=msh-%e5%91%bd%e4%bb%a4) <a href="#msh-ming-ling" id="msh-ming-ling"></a>

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                                                                                                                                                |

### [How to use](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/tmpfs?id=%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8) <a href="#ru-he-shi-yong" id="ru-he-shi-yong"></a>

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](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/filesystem/filesystem) .

![image-20221102174841105](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/figures/uml-1.png)

### [Usage Examples](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/tmpfs?id=%e4%bd%bf%e7%94%a8%e7%a4%ba%e4%be%8b) <a href="#shi-yong-shi-li" id="shi-yong-shi-li"></a>

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.

![image-20221101155404497](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/figures/menuconfig.png)

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.

![image-20221103101448013](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/figures/dfs-config.png)

The code for mounting the file system using dfs\_mount in mnt.c is as follows:

```c
    /* 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.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)

```shell
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.bat`to run qemu

![image-20221101155816447](https://www.rt-thread.org/document/site/rt-thread-version/rt-thread-standard/programming-manual/tmpfs/figures/qemu-run.png)

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

```shell
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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.aic-eec.com/recommended_by_aic/rt-thread-university-program/components/network-components/tmpfs-temporary-file-system.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
