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

## 20.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`。

## 20.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: 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://handbook.bsdcn.org/di-20-zhang-cun-chu/20.9.-bei-fen-de-ji-chu-zhi-shi.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.
