2

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.

2

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.

  • Probably worth mentioning that when the file is attached, mdconfig will, by default, output the name of the device created (without the preceding /dev), for example: md0. This is useful if you are doing the thing in a shell script. – Bob Eager Jul 8 '17 at 23:45
1

Linux

Create the file.

dd if=/dev/zero of=disk.ext3 bs=1024 count=131072

Create the filesystem

mke2fs disk.ext3

Mount the new file

mount disk.ext3 /mnt -o loop=/dev/loop0


FreeBSD

Create the file.

dd if=/dev/zero of=~/disk.img bs=1024 count=131072

Create the device /dev/md1

mdconfig -a -t vnode -f ~/disk.img -u 0
bsdlabel -w /dev/md0

Create the filesystem

newfs /dev/md0

Mount the new file

mount /dev/md0 /mnt

  • 1
    The questioner specifically asked how to do this on FreeBSD, not on Linux. – JdeBP Jun 23 '17 at 18:50
  • the only thing that might be lacking is mke2fs, this can be installed via "sysutils/e2fsprogs". cd /usr/ports/sysutils/e2fsprogs/ && make install clean – Jon Red Jun 24 '17 at 17:56
  • 1
    No. Your answer contains several Linuxisms and is outright wrong for FreeBSD. Badly papering over just one of the Linuxisms does not improve it. – JdeBP Jun 25 '17 at 16:24
  • 1
    Is the c after /dev/md0 intended in the 2 last occurrences? Because if I add it, I get the error message newfs: /dev/md0c: could not find special device. And I don't see why this file would exist. If I don't include the c, it seems to work. – UTF-8 Jun 26 '17 at 13:18
  • 1
    @JdeBP - dunno if the edit added the bsd info, but I like seeing how to do it on the system I'm used to (Linux) and what is new to me (the bsd) – ivanivan Jun 29 '17 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.