From 119e149cdc21cb244b21b8f0351c37deef57db32 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 26 Dec 2011 03:59:12 +0100 Subject: [PATCH] Added USBIP Project --- tunnel/usbip/README | 5 ++ tunnel/usbip/conf.py | 37 +++++++++ tunnel/usbip/listDevices.py | 14 ++++ tunnel/usbip/usbip.py | 154 ++++++++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+) create mode 100644 tunnel/usbip/README create mode 100644 tunnel/usbip/conf.py create mode 100755 tunnel/usbip/listDevices.py create mode 100755 tunnel/usbip/usbip.py diff --git a/tunnel/usbip/README b/tunnel/usbip/README new file mode 100644 index 0000000..c1ef3fe --- /dev/null +++ b/tunnel/usbip/README @@ -0,0 +1,5 @@ + +Installation +============ +You need + * python-pyudev diff --git a/tunnel/usbip/conf.py b/tunnel/usbip/conf.py new file mode 100644 index 0000000..c4d6c9d --- /dev/null +++ b/tunnel/usbip/conf.py @@ -0,0 +1,37 @@ +# ___________________________________________________ +# | | +# | usbip - ip over usbsticks | +# |___________________________________________________| + +import os + +Conf = { + # ======== network settings ======== + # ipsettings for the device + 'devname': '', + 'network': + { + 'address': '10.10.10.11', + 'netmask': '255.255.255.0', + #gateway: '', + 'mtu': 1400, + }, + + # ======== USB-Stick settings ======== + # udev attributes to choose a device on input + # make sure this only matches to mountable devices + 'udevAttrs': + { + 'ID_SERIAL': u'VBTM_Store__n__Go_0DE1A361400298C2-0:0', + 'UDISKS_PARTITION_NUMBER': u'1', + }, + # where to mount the usb stick + 'mountpoint': '/mnt/', + # relative path from mountpoint, where to write network files + 'usbNetworkDir': 'net/', + # prexif for all files, suffixed + 'networkFilePrefix': 'data', + # sync after unmount? + 'sync': True +} + diff --git a/tunnel/usbip/listDevices.py b/tunnel/usbip/listDevices.py new file mode 100755 index 0000000..8021837 --- /dev/null +++ b/tunnel/usbip/listDevices.py @@ -0,0 +1,14 @@ +#!/usr/bin/python + +import pyudev + +context = pyudev.Context() +monitor = pyudev.Monitor.from_netlink(context) +monitor.filter_by(subsystem='block') +for action, device in monitor: + if action == 'add': + print device + for k, v in device.items(): + print k.ljust(34), v + print "-" * 80 + diff --git a/tunnel/usbip/usbip.py b/tunnel/usbip/usbip.py new file mode 100755 index 0000000..a388e53 --- /dev/null +++ b/tunnel/usbip/usbip.py @@ -0,0 +1,154 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import logging +import os +import pyudev +import Queue +import subprocess +import sys +import threading +import time + +sys.path.append("../../../") + +from conf import Conf +from ether2any import Ether2Any +class USBWriter(threading.Thread): + def __init__(self, dev, networkQueue, writeLock): + threading.Thread.__init__(self) + self.daemon = True + self.quit = False + self.packetCounter = 0 + + self.dev = dev + self.writeLock = writeLock + self.networkQueue = networkQueue + + self.mountpoint = Conf.get("mountpoint") + self.usbNetworkDir = Conf.get("usbNetworkDir") + self.networkFilePrefix = Conf.get("networkFilePrefix") + + self.writePath = self.mountpoint + "/" + self.usbNetworkDir + "/" + + def getPacketCounter(self): + return self.packetCounter + + def resetPacketCounter(self): + self.packetCounter = 0 + + def run(self): + while not self.quit: + packet = self.networkQueue.get() + self.writeLock.acquire() + f = open(self.writePath + self.networkFilePrefix + "%09d" % self.packetCounter, "w") + f.write(packet) + f.close() + self.packetCounter += 1 + self.writeLock.release() + +class UdevHandler(threading.Thread): + def __init__(self, dev, usbwriter, writeLock): + threading.Thread.__init__(self) + self.daemon = True + self.quit = False + self.packetCounter = 0 + + self.dev = dev + self.usbwriter = usbwriter + self.writeLock = writeLock + self.context = pyudev.Context() + + self.mountpoint = Conf.get("mountpoint") + self.usbNetworkDir = Conf.get("usbNetworkDir") + self.networkFilePrefix = Conf.get("networkFilePrefix") + self.sync = Conf.get("sync") + + self.writePath = self.mountpoint + "/" + self.usbNetworkDir + "/" + self.attrs = Conf.get("udevAttrs") + self.devname = None + + def isUsableDevice(self, device): + for k, v in self.attrs.items(): + try: + if not device[k] == v: + return False + except KeyError: + return False + return True + + def readAndSweep(self): + counter = 0 + try: + for n in os.listdir(self.writePath): + if n == "." or n == "..": + continue + f = open(self.writePath + n, "r") + packet = f.read() + f.close() + os.unlink(self.writePath + n) + self.dev.write(packet) + counter += 1 + except OSError, o: + print " !! Error reading from directory:", o + + print " >> Read %d packet(s)" % counter + + def run(self): + monitor = pyudev.Monitor.from_netlink(self.context) + monitor.filter_by(subsystem='block') + for action, device in monitor: + if self.isUsableDevice(device): + self.devname = device['DEVNAME'] + if action == 'add': + p = subprocess.Popen(["/bin/mount", self.devname, self.mountpoint]) + ret = p.wait() + if ret == 0: + print " ++ Mounted stick (%s) at %s" % (self.devname, self.mountpoint) + if not os.path.exists(self.writePath): + os.mkdir(self.writePath) + self.readAndSweep() + self.writeLock.release() + raw_input(" ** Press any key to release usbstick...") + print " >> %d packet(s) written" % (self.usbwriter.getPacketCounter(),) + self.usbwriter.resetPacketCounter() + print " ** Releasing stick..." + self.writeLock.acquire() + subprocess.Popen(["/bin/umount", "-f", self.devname]) + if self.sync: + print " ** Syncing..." + subprocess.Popen("/bin/sync") + print " ** Unmounted" + else: + print " !! Error mounting stick (%s) at %s: Error id %d" % (self.devname, self.mountpoint, ret) + elif action == 'remove': + print " -- Stick (%s) removed." % self.devname + + +class USBIP(Ether2Any): + def __init__(self): + Ether2Any.__init__(self, tap=False) + + network = Conf.get("network", {'mtu': 1400}) + self.dev.ifconfig(**network) + self.dev.up() + + self.networkQueue = Queue.Queue() + self.writeLock = threading.Lock() + self.writeLock.acquire() + + self.usb = USBWriter(self.dev, self.networkQueue, self.writeLock) + self.usb.start() + + self.udev = UdevHandler(self.dev, self.usb, self.writeLock) + self.udev.start() + + + def sendToNet(self, packet): + self.networkQueue.put(packet) + +if __name__ == '__main__': + usbip = USBIP() + print "Starting ip over USB-Stick service..." + usbip.run() +