9

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.

4
  • 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 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. Oct 23 '14 at 11:11
2

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.

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.

1
  • 1
    or maybe you can use iSCSI
    – Skaperen
    Apr 25 '15 at 0:26
-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.

2
  • 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 9: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

Your Answer

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