Mounting a file on FreeBSD

I always forget how to mount a file or how to create a file system in a file on FreeBSD. Therefore this post. There is no “-o loop” option for mount like it is on GNU/Linux. Insteed you need to manually create a device that binds the file to the device system. You can do this via

mdconfig -f

This will bind the file to the device /dev/md0 (or the first free /dev/mdX device). You then can operate on that device like on an ordinary device. You might have to load the kernel module before. In order to clean up run

mdconfig -d -u

where is the number of the md device you want to remove.