3

Hej,

it is relatively easy to use a file for emulating a block-device using losetup in Linux:

Can anyone please give me a hint on what to look for in case I want to program my own block-device which is based on several files I'm taking content from? For your understanding, I would like to let's say take bytes 1-500 and 1.000-3.000 from file1 and bytes 501-999 and bytes 3.001 to 5.000 from file2 to offer them as a combined block-device. My prefered programming language is Python and I want to write my program in user-space as much as possible.

For Windows I found such an implementation. It's called FileDisk and HttpDisk and it can be found here:

Thanks in advance and regards, Rainer

Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
dubbaluga
  • 2,223
  • 5
  • 29
  • 38

2 Answers2

4

You don't need to program anything. You can use Linux's multi-device (a.k.a. md) subsystem to build yourself a block device which consists of a number of smaller devices.

For this to work you use mdadm to assemble a LINEAR raid device out of smaller devices.

Update
So here is what I did:

$ cd /images
$ dd if=/dev/zero bs=1M count=100 of=a.img
$ dd if=/dev/zero bs=1M count=50 of=b.img
$ dd if=/dev/zero bs=1M count=150 of=c.img
$ losetup -f
/dev/loop0
$ for i in a b c; do losetup -f $i.img; done
$ mdadm --build /dev/md0 -l linear -n 3 /dev/loop[012]
mdadm: array /dev/md0 built and started.
$ cat /proc/mdstat
Personalities : [linear] 
md0 : active linear loop2[2] loop1[1] loop0[0]
      307200 blocks super non-persistent 64k rounding

Note that I used $ as prompt to not confuse automatic syntax highlighting ;)

As easy as that.

Cheers.

PS: Now this really qualifies for superuser, doesn't it?

jimbo
  • 11,004
  • 6
  • 29
  • 46
Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
  • Hej, thanks for your answer. But I think this solution does not support the scenario that I described. Actually I forgot to mention that I wanted to program something like an overlay block-device which has some logic to decide from which file it takes its contents. So far I'll go for NBD. There also exist some Python implementations (e. g. http://lists.canonical.org/pipermail/kragen-hacks/2004-May/000397.html). – dubbaluga Dec 23 '10 at 20:56
  • dubbaluga: You're welcom. You talked about programming but I thought it would be way too much in face of md ;). Good luck! – Marcus Borkenhagen Dec 23 '10 at 21:11
2

If you want to stay completely in userspace, with a simple API, I highly recommend FUSE which would be relatively simple to do with Python.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • FUSE is a filesystem layer, not a block-device layer. – Marcus Borkenhagen Dec 23 '10 at 16:23
  • 2
    Yeah, FUSE is filesystem in user space. Well, CUSE allows for character device in user space now, but there's no BUSE. Usually people just hack NBD for that. – ephemient Dec 23 '10 at 16:47
  • Thank you very much for your suggestions! For now I'll go for NBD. That seems to be the easiest solution and as replied to Fritschy's post, there also exist Python implementations. – dubbaluga Dec 23 '10 at 21:00