10

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.

Paulo Tomé
  • 3,782

3 Answers3

4

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.

Rich
  • 313
1

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.

Fox
  • 466
-1

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.

HalosGhost
  • 4,790
spuk
  • 368
  • I think you completely missed the point of the question. OP is asking how to create a virtual block device, that can be accessed through a block-device inode (e.g. something you'd find in /dev/…, and do all the fops from userspace. – mmap is an entirely different thing. Some block devices do support mmap; others don't. Also you can mmap without a backing file (see MAP_ANONYMOUS; strictly speaking the kernel will create an anonymous file out of the I/O cache in-situ). But all of this is on the opposite end of what OP was asking for. -1 – datenwolf Aug 23 '21 at 09:34
  • you're right, I assumed his root problem was "changes being written to disk"; thanks for the MAP_ANONYMOUS info – spuk Aug 30 '21 at 22:26