> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/handbook/di-21-zhang-cun-chu/21.9.-nei-cun-pan.md).

# 21.9.内存盘

除了物理磁盘，FreeBSD 还支持创建和使用内存盘。内存盘的一个可能用途是访问 ISO 文件系统的内容，而无需先将其刻录到 CD 或 DVD，然后再挂载 CD/DVD 光盘。

在 FreeBSD 中，[md(4)](https://man.freebsd.org/cgi/man.cgi?query=md\&sektion=4\&format=html) 驱动程序提供对内存盘的支持。`GENERIC` 内核包含此驱动程序。如果使用定制内核配置文件，请确保包含以下行：

```sh
device md
```

## 21.9.1. 附加和分离现有镜像

要挂载现有的文件系统镜像，请使用 `mdconfig` 指定 ISO 文件的名称和空闲的单元编号。然后，引用该单元编号将其挂载到现有的挂载点。挂载后，ISO 文件中的文件将显示在挂载点中。此示例将 `diskimage.iso` 附加到内存设备 **/dev/md0**，然后将该内存设备挂载到 **/mnt**：

```sh
# mdconfig -f diskimage.iso -u 0
# mount -t cd9660 /dev/md0 /mnt
```

注意，`-t cd9660` 用于挂载 ISO 格式。如果没有使用 `-u` 指定单元编号，`mdconfig` 会自动分配未使用的内存设备，并输出分配的单元名称，例如 `md4`。有关此命令及其选项的更多详细信息，请参阅 [mdconfig(8)](https://man.freebsd.org/cgi/man.cgi?query=mdconfig\&sektion=8\&format=html)。

当内存盘不再使用时，应将其资源释放回系统。首先，卸载文件系统，然后使用 `mdconfig` 从系统中分离磁盘并释放其资源。继续此示例：

```sh
# umount /mnt
# mdconfig -d -u 0
```

要确定是否仍有内存盘附加到系统，请输入 `mdconfig -l`。

## 21.9.2. 创建基于文件或内存的内存盘

FreeBSD 还支持内存盘，其使用的存储可从硬盘或内存区域分配。第一种方法通常称为基于文件的文件系统，第二种方法称为基于内存的文件系统。两种类型都可使用 `mdconfig` 创建。

要创建新的基于内存的文件系统，请指定 `swap` 类型和要创建的内存盘大小。然后，格式化内存盘并像平常一样挂载。此示例创建了大小为 5MB 的内存盘，单元编号为 `1`。该内存盘随后被格式化为 UFS 文件系统，然后挂载：

```sh
# mdconfig -a -t swap -s 5m -u 1
# newfs -U md1
/dev/md1: 5.0MB (10240 sectors) block size 16384, fragment size 2048
        using 4 cylinder groups of 1.27MB, 81 blks, 192 inodes.
        with soft updates
super-block backups (for fsck -b #) at:
 160, 2752, 5344, 7936
# mount /dev/md1 /mnt
# df /mnt
Filesystem 1K-blocks Used Avail Capacity  Mounted on
/dev/md1        4718    4  4338     0%    /mnt
```

要创建新的基于文件的内存盘，首先从磁盘中分配一个区域。此示例创建了名为 `newimage` 的空白 5MB 文件：

```sh
# dd if=/dev/zero of=newimage bs=1k count=5k
5120+0 records in
5120+0 records out
```

接下来，将该文件附加到内存盘，给内存盘打上标签，并将其格式化为 UFS 文件系统，挂载内存盘，并验证基于文件的磁盘大小：

```sh
# mdconfig -f newimage -u 0
# bsdlabel -w md0 auto
# newfs -U md0a
/dev/md0a: 5.0MB (10224 sectors) block size 16384, fragment size 2048
        using 4 cylinder groups of 1.25MB, 80 blks, 192 inodes.
super-block backups (for fsck -b #) at:
 160, 2720, 5280, 7840
# mount /dev/md0a /mnt
# df /mnt
Filesystem 1K-blocks Used Avail Capacity  Mounted on
/dev/md0a       4710    4  4330     0%    /mnt
```

使用 `mdconfig` 创建基于文件或内存的文件系统需要几个命令。FreeBSD 还配有 `mdmfs`，它可自动配置内存盘，格式化为 UFS 文件系统，并挂载它。例如，在使用 `dd` 创建 *newimage* 后，下面这个命令相当于运行上面显示的 `bsdlabel`、`newfs` 和 `mount` 命令：

```sh
# mdmfs -F newimage -s 5m md0 /mnt
```

如果要使用 `mdmfs` 创建新的基于内存的内存盘，请使用这个命令：

```sh
# mdmfs -s 5m md1 /mnt
```

如果没有指定单元编号，`mdmfs` 将自动选择未使用的内存设备。有关 `mdmfs` 的更多详细信息，请参阅 [mdmfs(8)](https://man.freebsd.org/cgi/man.cgi?query=mdmfs\&sektion=8\&format=html)。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/handbook/di-21-zhang-cun-chu/21.9.-nei-cun-pan.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
