CONTENTS

Home
Updates
Software
Electronics
Android / iOS
Videos
Music
Resume
Contact



HTTPS VERSION


Netdisk Linux Kernel Module

Posted: August 19, 2008

Introduction

So here's my first ever Linux kernel module other than a worthless "Hello World!" character device I did a while ago. The goal of this module for me was to learn how block devices work and how to open up a socket in a kernel module. I have to give credit to this article: http://lwn.net/Articles/58719/ for pretty much guiding me through the block device part and to the khttpd project which led me to sock_create() and many good links from Google.

So what exactly is this... basically I wanted to create a block device that would look like a floppy but instead of storing the data locally it would send/receive each sector from another computer. I was originally going to write a small server that would fseek() in a disk image and read/write from it, but I decided to do it with a webserver using PHP. The reason for PHP was because 1) it's a really small simple script and 2) I find it humorous using PHP for the backend of a kernel module :). The obvious disadvantage of PHP here is the extra overhead of having HTTP headers around the sector data and the fact that I decided not to do a connection keep-alive on the socket, so every sector read/write will have to reestablish the TCP connection.

TODO and Possible Improvments

  • Connection: Keep-alive could make virtual disk more responsive, but make the kernel module code more complicated.
  • Instead of giving a Content-Length for the entire multipart form, I think I could just give a content length for the sector (which would always be 512) in the form data section and simplify the headers a bit.
  • Server IP address is hardcoded in the source code. Allow user to specify server on the command line.
  • Some hardcoded values in the module restrict the size of the disk to the size of a floppy. Adding a way to query the server for the size of the image could fix this.
  • Kernel module puts a lot of warnings in dmesg during the socket connections. Need to figure out why.

How To

First thing needed to test this out is make sure your computer has gcc installed and the Linux kernel source tree for your current running kernel. You will also need a web server with PHP installed to run the server part. The server doesn't have to be Linux since the backend to this Linux module is PHP. In order to format the disk image that will sit on the web server side, you will need mkfs.vfat. Actually, technically you should be able to format the disk with any valid filesystem, I just used vfat here. Please note this is a beta kernel module and if it crashes, your computer will crash with it. Use at your own risk. To test this out, simply do the following:

  • Create a directory on your web server called netdisk and place netdisk.php in there.
  • Edit the source code of netdisk.c and replace the part that says:
      static unsigned char address[4]={ 10, 1, 1 , 1 }; with the IP address of your web server.
  • Type: make
  • On the computer with the web server, type:
      dd if=/dev/zero of=/tmp/netdisk.img bs=512 count=2880
      mkfs.vfat /tmp/netdisk.img
  • On the computer with the netdisk module, type:
      insmod netdisk.ko
      mkdir /mnt/netdisk
      mount /dev/netdisk0 /mnt/netdisk

You should now be able to read/write from /mnt/netdisk which will actually be reading and writing from the /tmp/netdisk.img image on the computer with the web server.

Download

netdisk-2008-08-19.tar.gz

Copyright 1997-2018 - Michael Kohn