# Emulating Tape Devices



## alexp789 (Jul 27, 2012)

Hi All,
I'm looking to write a userspace application which will emulate a tape device, and then do various magic with the data that's written.  I've had a look through some of the standard documentation for FreeBSD, but I was wondering if someone could point me in the correct direction for some examples of code which is doing this, or good resources I should read up on to save some time?

Thanks in advance!

Alex


----------



## vermaden (Jul 27, 2012)

I do not have direct code references, but these are various virtual tape devices with various features, taken from man 4 sa manpage:

```
/dev/[n][e]sa[0-9]  general form:
     /dev/sa0            Rewind on close
     /dev/nsa0           No rewind on close
     /dev/esa0           Eject on close (if capable)
     /dev/sa0.ctl        Control mode device (to examine state while another program is accessing the device, e.g.).
```


----------



## alexp789 (Jul 27, 2012)

*Thanks*

Hey vermaden,
Thanks for the info I'll have a read up on that, I'm not sure if its going to set me in the right direction or not though.  Ideally I'm either looking for some example code, showing creating the device etc, or an example of an existing project that's doing this sort of thing, so I can look at to make a start 'the bsd way' ;-)

Thanks!

Alex


----------



## Maelstorm (Jul 28, 2012)

You best bet for example code would be to look at the actual sa(4) driver source code in /usr/src/sys/cam/scsi/scsi_sa.c.  However, take a look at device(9), driver(9), and some of the others in the see also sections.


----------



## wblock@ (Jul 28, 2012)

http://nostarch.com/bsddrivers.htm will help.


----------



## alexp789 (Jul 28, 2012)

*Thanks*

Hey Maelstorm & wblock@,
Thanks for those pointers, the book looks good, and the links to the source code look to be exactly what I need ;-)  Time for some reading up I think!

Cheers for the help!

Alex


----------



## Maelstorm (Jul 28, 2012)

I found some more info for you.  In the BSD Rootkit book, starting on page 14, it talks about creating a character device.  Check out make_dev(9) kernel call.  You also need to fill out a struct cdevsw structure to map calls over to your functions.


----------



## alexp789 (Aug 5, 2012)

*Block vs Character*

Hey all,
Thanks for the brilliant help so far, I do have one question though.  Block vs Character devices... For something like a virtual tape, which would be best?  A lot of places are really bashing block devices, should I be creating a character device?...

Thanks again

Alex


----------



## wblock@ (Aug 5, 2012)

http://www.freebsd.org/doc/en_US.ISO8859-1/books/arch-handbook/driverbasics-block.html


----------



## alexp789 (Aug 6, 2012)

Hey wblock@,
Yeah thats the documentation I'm referring to... A block device would seem to make sense for a tape, fixed size, fill it from start to end etc... Does a character device have the same characteristics?  Or would a character device just act as a 'sink' for the data?  ie the app just fires data to it, and it swallows it?  I'm punching above my weight on the kernel development here ;-)

Alex


----------



## wblock@ (Aug 6, 2012)

Block devices have been gone for a long time.  Character devices have replaced them, being able to write to devices with fixed or variable block sizes anyway.  Consider dd(1) writing to a disk; the disk has 512-byte blocks, but that's not a problem for dd(1).

For what you're doing, consider taking the source for sa(4), giving it a new name, and just removing all the code that actually deals with hardware.  It might be easier than that, and maybe some fakery with /dev/null would be adequate.


----------

