[pve-devel] r6468 - in pve-storage/pve2: . PVE PVE/API2/Storage

svn-commits at proxmox.com svn-commits at proxmox.com
Fri Aug 12 12:13:41 CEST 2011


Author: dietmar
Date: 2011-08-12 12:13:41 +0200 (Fri, 12 Aug 2011)
New Revision: 6468

Modified:
   pve-storage/pve2/ChangeLog
   pve-storage/pve2/PVE/API2/Storage/Scan.pm
   pve-storage/pve2/PVE/Storage.pm
Log:
impl scan_usb


Modified: pve-storage/pve2/ChangeLog
===================================================================
--- pve-storage/pve2/ChangeLog	2011-08-12 10:11:32 UTC (rev 6467)
+++ pve-storage/pve2/ChangeLog	2011-08-12 10:13:41 UTC (rev 6468)
@@ -1,3 +1,7 @@
+2011-08-12  Proxmox Support Team  <support at proxmox.com>
+
+	* PVE/Storage.pm (scan_usb): imp.
+
 2011-08-05  Proxmox Support Team  <support at proxmox.com>
 
 	* changelog.Debian: increase release number to 2.0-4

Modified: pve-storage/pve2/PVE/API2/Storage/Scan.pm
===================================================================
--- pve-storage/pve2/PVE/API2/Storage/Scan.pm	2011-08-12 10:11:32 UTC (rev 6467)
+++ pve-storage/pve2/PVE/API2/Storage/Scan.pm	2011-08-12 10:13:41 UTC (rev 6468)
@@ -38,6 +38,7 @@
 	    { method => 'lvm' },
 	    { method => 'iscsi' },
 	    { method => 'nfs' },
+	    { method => 'usb' },
 	    ];
 
 	return $res;
@@ -146,4 +147,44 @@
 	return PVE::RESTHandler::hash_to_array($res, 'vg');
     }});
 
+__PACKAGE__->register_method ({
+    name => 'usbscan', 
+    path => 'usb', 
+    method => 'GET',
+    description => "List local USB devices.",
+    protected => 1,
+    proxyto => "node",
+    parameters => {
+    	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	},
+    },
+    returns => {
+	type => 'array',
+	items => {
+	    type => "object",
+	    properties => { 
+		busnum => { type => 'integer'},
+		devnum => { type => 'integer'},
+		port => { type => 'integer'},
+		usbpath => { type => 'string', optional => 1},
+		level => { type => 'integer'},
+		class => { type => 'integer'},
+		vendid => { type => 'string'},
+		prodid => { type => 'string'},
+		speed => { type => 'string'},
+
+		product => { type => 'string', optional => 1 },
+		serial => { type => 'string', optional => 1 },
+		manufacturer => { type => 'string', optional => 1 },
+	    },
+	},
+    },
+    code => sub {
+	my ($param) = @_;
+
+	return PVE::Storage::scan_usb();
+    }});
+
 1;

Modified: pve-storage/pve2/PVE/Storage.pm
===================================================================
--- pve-storage/pve2/PVE/Storage.pm	2011-08-12 10:11:32 UTC (rev 6467)
+++ pve-storage/pve2/PVE/Storage.pm	2011-08-12 10:13:41 UTC (rev 6468)
@@ -2211,6 +2211,63 @@
     raise_param_exc({ portal => "unable to resolve portal address '$portal'" });
 }
 
+# idea is from usbutils package (/usr/bin/usb-devices) script
+sub __scan_usb_device {
+    my ($res, $devpath, $parent, $level) = @_;
+
+    return if ! -d $devpath;
+    return if $level && $devpath !~ m/^.*[-.](\d+)$/;
+    my $port = $level ? int($1 - 1) : 0;
+
+    my $busnum = int(file_read_firstline("$devpath/busnum"));
+    my $devnum = int(file_read_firstline("$devpath/devnum"));
+
+    my $d = {
+	port => $port,
+	level => $level,
+	busnum => $busnum,
+	devnum => $devnum,
+	speed => file_read_firstline("$devpath/speed"),
+	class => hex(file_read_firstline("$devpath/bDeviceClass")),
+	vendid => file_read_firstline("$devpath/idVendor"),
+	prodid => file_read_firstline("$devpath/idProduct"),
+    };
+
+    if ($level) {
+	my $usbpath = $devpath;
+	$usbpath =~ s|^.*/\d+\-||;
+	$d->{usbpath} = $usbpath;
+    }
+
+    my $product = file_read_firstline("$devpath/product");
+    $d->{product} = $product if $product;
+    
+    my $manu = file_read_firstline("$devpath/manufacturer");
+    $d->{manufacturer} = $manu if $manu;
+
+    my $serial => file_read_firstline("$devpath/serial");
+    $d->{serial} = $serial if $serial;
+
+    push @$res, $d;
+
+    foreach my $subdev (<$devpath/$busnum-*>) {
+	next if $subdev !~ m|/$busnum-[0-9]+(\.[0-9]+)*$|;
+	__scan_usb_device($res, $subdev, $devnum, $level + 1);
+    }
+
+};
+
+sub scan_usb {
+
+    my $devlist = [];
+
+    foreach my $device (</sys/bus/usb/devices/usb*>) {
+	__scan_usb_device($devlist, $device, 0, 0);
+    }
+
+    return $devlist;
+}
+
 sub scan_iscsi {
     my ($portal_in) = @_;
 



More information about the pve-devel mailing list