I'm not sure what I should be googling or if FUSE does this (I suspect not). I'd like to create a virtual block device for which all forms of access, for example reads and writes, go directly to my app.

I know I can create a file be used as a block device by doing

dd if=/dev/zero of=~/test count=100k

then create a loopback to it using

losetup /dev/loop0 ~/test`

But I would like accesses going directly to my app instead of to a file. I hope this question is fairly clear.

  • you might use linux kernel namespaces to do a private mount. look here for a brief overview to see if it might meet your needs. – mikeserv Oct 23 '14 at 9:34
  • 1
    You can do it with NBD or FUSE, see How to write a userspace linux block device driver – lemonsqueeze Oct 23 '14 at 9:36
  • @lemonsqueeze: So do I create a block device with FUSE? or do I create a file with a loopback? I'm going to look at FUSE this weekend for this – user4069 Oct 23 '14 at 9:54
  • 1
    With FUSE you can create a filesystem that just exposes one file and answer read/writes to this file as you like. It'll look like a regular file from the outside, so will have to be mounted with -o loop. – lemonsqueeze Oct 23 '14 at 11:11

You can use NBD. Using nbdkit you can even write virtual block devices in shell script or other scripting languages (although stick to C if you want the best performance). I gave a talk about this topic at FOSDEM 2019 where I did a live demo writing a Linux kernel block device in shell script.

| improve this answer | |

As mentioned in the comments one of the possible ways is NBD. BUSE might help you getting started with that. It actually uses a Unix socket, so it should be reasonably performant.

| improve this answer | |
  • 1
    or maybe you can use iSCSI – Skaperen Apr 25 '15 at 0:26

Aren't you looking for mmap()?

I'm not sure how you'd mmap without having an actual file backing it, however, but you might have an arbitrarily big, zeroed, file (see falocate, truncate) and mmap() it with flag MAP_PRIVATE, so that writes to it are visible to your application only, and not carried through to disk.

| improve this answer | |

Your Answer

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