FreeBSD/Memory Disk

From Devpit
Jump to: navigation, search

The quick command for creating a swap-backed memory filesystem that's 1GB is:

mdmfs -s 1g md /mnt

The -s option can alternatively specify the size in 512-byte blocks. The default is a swap-backed filesystem, which means if you run out of memory, FreeBSD will nicely move stuff to swap space, so it's okay to create a disk bigger than the available ram. A malloc-backed filesystem will not move stuff to swap, and therefore must be used with care.

To get rid of it:

umount /mnt && mdconfig -d -u md0

If you forget to run mdconfig -d, FreeBSD won't free the memory. Deleting files does not free the memory, since it's literally a raw device with a ufs2 filesystem, and once you've written to a block it's never really cleared.

Running mdmfs is a shortcut for running mdconfig, newfs, and mount. Alternatively:

mdconfig -a -t swap -s 1g
newfs -U /dev/md0  # Switch md0 to whatever mdconfig printed
mount /dev/md0 /mnt

Also, you can create an msdos filesystem on a memory device. (This is useful for creating a disk image, for example.) For example, the following creates an msdos filesystem on a file-backed memory device. Note that newfs_msdos can't get all the info it needs from a memory device, so you have to help it out by specifying the geometry. (XXX: I don't know whether this geometry matters. I suspect it is irrelevent because modern disks don't have any representation of their geometry. Can anyone verify?)

mdconfig -a -t swap -s 204800
newfs_msdos -S 512 -u 18 -h 2 -o 0 -F 32 /dev/md0
mount -t msdos /dev/md0 /mnt