Freebsd – Create filesystem in regular file using FreeBSD

disk-imagefilesystemsfreebsd

When using Linux, one can easily create a regular file of some size and then create a filesystem in it. How can one do the same thing when using FreeBSD?

I tried this:

root@:/tmp/test # newfs -U ~/disk
newfs: /root/disk: not a character-special device: No error: 0
newfs: no valid label found

I didn't find any relevant information on this (e.g.: "Use switch -i to allow the filesystem to be created on a regular file instead of only on a character device." on the (fairly short) man page of newfs.

Best Answer

Create the file; "1g" stands for one gigabyte:

truncate -s 1g disk.img

Attach the file as a virtual memory disk; this will print the allocated device name, eg "md0":

mdconfig disk.img

Create a filesystem on that memory disk:

newfs /dev/md0

And finally mount it:

mount /dev/md0 /mnt

You can use mdconfig -lv to show currently attached memory disks. Also note that the memory disk - md0 in this case, http://http://man.freebsd.org/md - is a GEOM provider, so for all practical intents and purposes behaves as a disk. Which means, if you do an image of a physical disk, and attach that image using mdconfig(8), GEOM will automatically probe partitions, so you'll get /dev/md0p1, /dev/md0p2 etc. You can also use geli(8) to encrypt its contents, or create a zpool on them.

Related Question