Linux people reach for losetup. FreeBSD has had md(4) since the 4.x days, and mdconfig(8) is the whole interface: take a file, some RAM or some swap, and present it as a block device. It is the cheapest lab bench in the system, and nobody uses it because they forgot it exists.
The three backings
truncate -s 1g /tmp/disk.img
mdconfig -a -t vnode -f /tmp/disk.img # prints md0
mdconfig -a -t malloc -s 256m # RAM, prints md1
mdconfig -a -t swap -s 256m # swap-backed, prints md2
mdconfig -lv # list what you have
mdconfig -d -u 0 # detach md0
vnode is a file. malloc is wired kernel memory and gone at reboot. swap is pageable memory, so it can be pushed out under pressure instead of squatting on RAM. For anything over a few hundred megabytes I use swap, because a malloc disk is memory you cannot get back.
Use truncate, not dd if=/dev/zero, for the file. A sparse file costs nothing until written, which matters in a minute.
Mounting an ISO without burning anything
mdconfig -a -t vnode -f FreeBSD-15.1-RELEASE-amd64-disc1.iso
mount -t cd9660 /dev/md0 /mnt
...
umount /mnt && mdconfig -d -u 0
Add -o readonly to mdconfig if you want the device itself to refuse writes. Forget the -d and the file stays pinned by the device; I have found three-year-old md devices on boxes that had been rebooted twice, which tells you how much attention I give my own advice.
mdmfs: the one-liner scratch filesystem
mdmfs wraps attach, newfs and mount into one command:
mdmfs -s 512m md /mnt/scratch
That gives you a swap-backed UFS you can thrash for a build or a test run and throw away. On FreeBSD, tmpfs(5) does much of the same job with less ceremony (mount -t tmpfs tmpfs /mnt/scratch), so reach for mdmfs when you specifically want a real UFS or a size cap enforced by a device. Check mdmfs(8) for the flags on your release; I am not going to pretend I remember every one.
Rehearsing ZFS failures on files
This is why I wrote the post. ZFS accepts regular files as vdevs, provided you give absolute paths. So you can run a disaster drill with no spare hardware:
truncate -s 512m /tmp/a.img /tmp/b.img
zpool create drill mirror /tmp/a.img /tmp/b.img
zfs create drill/data
dd if=/dev/random of=/drill/data/blob bs=1m count=100
zpool offline drill /tmp/b.img
zpool status drill # DEGRADED, as designed
truncate -s 512m /tmp/c.img
zpool replace drill /tmp/b.img /tmp/c.img
zpool status drill # watch the resilver
zpool scrub drill
Now you know what DEGRADED looks like, what the resilver output says, and which command you typed, before it is 3 a.m. and a real disk is the one making the noise. The same pool is a safe target for practising send and receive replication and for trying bookmarks and holds without a production dataset anywhere near it.
To corrupt a vdev on purpose, write garbage into the middle of one file with dd conv=notrunc while the pool is exported, import it again, and scrub. Watch the checksum errors get repaired from the mirror. That demonstration persuades people faster than any slide.
Partitioned images
An image can carry a partition table like any disk:
mdconfig -a -t vnode -f /tmp/disk.img
gpart create -s gpt md0
gpart add -t freebsd-ufs -s 600m md0
newfs -U /dev/md0p1
This is how I build and inspect VM and jail images offline, and it pairs well with the jails on ZFS setup when you want to stage a root before it goes live.
Where it bites
- A vnode device is only as fast as the filesystem under the file. Benchmarking a “disk” that is a file on the same pool you are testing tells you about the layering, not the disk. Do not publish those numbers.
- File-backed ZFS pools are for drills, not data. The stack works, but every write goes through the host filesystem as well. Do not leave a pool you care about on
/tmp. - malloc devices vanish on reboot, and so does whatever you put on them. That is the feature. Do not put the only copy of anything there.
- Detach properly. Unmount, then
mdconfig -d. A busy device will refuse;-o forceexists and exists to teach you a lesson. - Persistence is your job. md devices do not come back after a reboot. If you want one at boot, that is an
rc.confmdconfig_md0entry, and the knobs are inrc.conf(5).
I spent years assuming the loop device on the other OS was the only trick of its kind, then reread mdconfig(8) and found I had been carrying a drawer of free test disks the whole time. Irritating. It beats borrowing a spare drive and a SATA cable to learn what DEGRADED means.
More FreeBSD material lives in the FreeBSD and OpenBSD hub and the ZFS and storage hub.
