> 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/ask/flat/chapter-25-storage-management/di-25.2-jie-xu-ni-nei-cun-pan.md).

# 25.2 Virtual Memory Disks

In addition to physical disks, FreeBSD also supports creating and using virtual memory disks. One typical use of virtual memory disks is to access the contents of an ISO file system without first burning it to a CD or DVD and then mounting the CD/DVD media. See the "Installing Software from DVD" section for more information.

FreeBSD also supports creating memory-based or file-based virtual memory disks using the `mdconfig` command, allocating storage from memory regions or hard disks respectively. The former is a memory-based virtual memory disk, and the latter is a file-based virtual memory disk.

## Creating a Memory-Based Virtual Memory Disk

To create a memory-based virtual memory disk, use `-t swap` to specify the memory disk type, and specify the size of the virtual memory disk to create.

Then format the virtual memory disk and mount it. This example creates a virtual memory disk with a size of 50MB, numbered `1`.

```sh
# mdconfig -a -t swap -s 50m -u 1
```

Check the memory disk number:

```sh
# mdconfig -l
md1
```

Format the virtual memory disk as a UFS file system, then mount it:

```sh
# newfs -U md1
/dev/md1: 50.0MB (102400 sectors) block size 32768, fragment size 4096
	using 4 cylinder groups of 12.53MB, 401 blks, 1664 inodes.
	with soft updates
super-block backups (for fsck_ffs -b #) at:
 192, 25856, 51520, 77184
# mount /dev/md1 /mnt
```

Check the memory disk file system:

```sh
# df -h  /mnt
Filesystem    Size    Used   Avail Capacity  Mounted on
/dev/md1       48M    8.0K     44M     0%    /mnt
```

## Creating a File-Based Virtual Memory Disk

To create a file-based virtual memory disk, first allocate a region from the disk. This example creates a 50MB sparse file **newimage**:

```sh
# truncate -s 50M newimage
```

Next, attach the file to a virtual memory disk:

```sh
# mdconfig -f newimage -u 0
```

Assign a GPT partition table to the virtual memory disk:

```sh
# gpart create -s GPT md0
# gpart add -t freebsd-ufs md0
```

Format it as a UFS file system:

```sh
# newfs -U md0p1
/dev/md0p1: 50.0MB (102320 sectors) block size 32768, fragment size 4096
	using 4 cylinder groups of 12.50MB, 400 blks, 1664 inodes.
	with soft updates
super-block backups (for fsck_ffs -b #) at:
 192, 25792, 51392, 76992
```

Mount the virtual memory disk:

```sh
# mount /dev/md0p1 /mnt
```

Verify the file-backed disk size:

```sh
# df -h /mnt
Filesystem    Size    Used   Avail Capacity  Mounted on
/dev/md0p1     48M    8.0K     44M     0%    /mnt
```


---

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

```
GET https://book.bsdcn.org/ask/flat/chapter-25-storage-management/di-25.2-jie-xu-ni-nei-cun-pan.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.
