Programming a multi-file loopback device on Linux

Hey

It is relatively easy to use a file to emulate a block device using losetupon Linux:

Can someone give me a hint about what to look for if I want to program my own block device based on several files from which I take content? For your understanding, I would like to say that take bytes 1-500 and 1.000-3.000 from file1 and bytes 501-999 and bytes from 3.001 to 5.000 from file2 to offer them as a combined block device. My preferred 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 is called FileDisk and HttpDisk, and can be found here:

Thanks in advance and welcome Rainer

+3
source share
2 answers

You do not need to program anything. You can use the Linux multi-device (aka md) subsystem to create a block device that consists of several smaller devices.

To do this, you can use mdadmto build a raid device LINEARfrom 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

Please note that I used $as a hint so as not to confuse automatic syntax highlighting;)

So simple.

Greetings.

PS: , ?

+4

, API, FUSE, Python.

+2

Source: https://habr.com/ru/post/1781848/


All Articles