butchie3980 has asked for the wisdom of the Perl Monks concerning the following question:

I wanted to make a block device (i.e. "/dev/myblkdev") that will use my script to handle the data coming in and out.
I think mknod is where I start, but I don't know where to go from there.
I'm at a loss here. I've seen FUSE, but that's for filesystems, not block devices. Thanks, butchie3980

Replies are listed 'Best First'.
Re: imitation block device in linux
by zentara (Cardinal) on Mar 18, 2011 at 14:44 UTC
    I may be a bit rusty on my kernel design, but I believe kernel code becomes involved when you use block devices. You may need to write a device driver for the kernel and learn about major and minor devices. Read "man makedev".

    IIRC there is a mini-tutorial somewhere out there in the cyber fields, for writing your own linux kernel driver.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      If the OP wants to take that road then the O'Reilly book Linux Device Drivers is available. The text is under a liberal open source licence, so you can read it online for free.

      Another approach would be to create a FUSE filing system, and create an artificial block device or mountable file-system image inside.

      Having said that, I doubt that the OP really needs to write a block device.

        Yeah, I'm trying to imitate a block device, one that can be accessed by programs like dd, or even mounted with the mount command. My intention is to intercept read/write/seek operations and manage the data as it goes back and forth (logging, encryption, etc). I was hoping to stay with perl, but it looks like I might have to spend some learning curve on C?
Re: imitation block device in linux
by chrestomanci (Priest) on Mar 18, 2011 at 12:56 UTC

    Are you sure you want a block device? (Like a hard disc), Perhaps you actually need a fifo or a character device. (Like a serial port or a unix named pipe.) I think you need to tell us a bit more about what you are attempting to do.

    Named pipes are easy to create, just call mkfifo <filename> in your unix shell, or call mkfifo() from the POSIX module. With a named pipe you can setup your script to write into the pipe while other processes read, or vice versa. Pipe are uni-directional, but you can easily create a pair.

    FUSE is for setting up filing systems. (With files, directories, access controls etc). There are perl modules that will help you setup a FUSE filesystem, and let you create virtual files that give access to information when they are read.

Re: imitation block device in linux
by salva (Canon) on Mar 18, 2011 at 21:27 UTC
    NBD
      NBD looks very interesting. How would I use perl to make an NBD server that clients can mount?
        Basically, you write a network server implementing the NBD protocol and then use nbd-client to connect and link it to some nbd device.

        There is little documentation about the NBD protocol, you will have to read the C source code for the nbd-server program. It is a very simple protocol. After some simple handshaking, there are only two kind of messages you have to support: read and write.

        Another option would be to write a custom nbd-client able to fork and run the server through an anonymous socket (think socketpair).