在Linux中编写USB设备安装脚本的更好方法 [英] Better way to script USB device mount in Linux

查看:78
本文介绍了在Linux中编写USB设备安装脚本的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为与用户提供的USB记忆棒进行交互的设备编写python模块.用户可以将USB记忆棒插入设备的USB插槽中,并且该设备无需用户干预即可将数据转储到该记忆棒中.如果用户插入USB记忆棒时设备正在运行,则说明我已经挂接到D-Bus,并已完成所有自动安装例程.新问题是,如果在关闭设备电源的情况下插入了操纵杆,该怎么办?打开设备电源后,没有发生D-Bus插入事件,也没有任何有关记忆棒的相关信息.

I'm writing a python module for a device that interacts with a user supplied USB memory stick. The user can insert a USB memory stick in the device USB slot, and the device will dump data onto the memory stick without user intervention. If the device is running when the user inserts the USB stick, I have hooked into D-Bus and have an auto mount routine all worked out. The new issue is, what if the stick is inserted while the device is powered off? I get no D-Bus insertion event, or any the associated nuggets of information about the memory stick after the device is powered on.

我已经找到了一种方法,可以通过调用以下命令来从/proc中的USB设备扫描中导出设备节点(/dev/sd?):

I have worked out a way to derive the device node ( /dev/sd? ) from scanning the USB devices in /proc, by calling:

ls /proc/scsi/usb-storage

如果为该文件夹中的每个文件添加了目录,则会提供scsi设备信息.

this gives the scsi device info if you cat each of the files in that folder.

然后我从USB存储记录中获取供应商",产品"和序列号"字段,生成一个标识符字符串,然后在

I then take the Vendor, Product, and Serial Number fields from the usb-storage records, generate an identifier string that I then use in

ll /dev/disc/by-id/usb_[vendor]_[product] _ [序列号] -0:0

ll /dev/disc/by-id/usb_[vendor]_[product]_[serial_number]-0:0

所以我可以解析结果以获得相对路径

So I can parse through the result to get the relative path

../../sdc

然后,我可以安装USB记忆棒.

Then, I can mount the USB stick.

这是一个繁琐的过程,几乎所有基于文本的过程,而且当有人引入奇怪的字符或非标准的序列号字符串时,也很容易出现错误.它可以与我拥有的所有2个USB记忆棒一起使用.我试图映射/var/log/messages的输出,但是最终还是文本比较. lsusb,fdisk,udevinfo,lsmod和其他文件的输出仅显示所需数据的一半.

This is a cumbersome procedure, pretty much all text based, and ready for bugs when someone introduces a weird character, or non-standard serial number string. It works with all 2 of the USB memory sticks I own. I have tried to map output from /var/log/messages but that ends up being text comparisons as well. Output from lsusb, fdisk, udevinfo, lsmod, and others only show half of the required data.

我的问题:在没有D-Bus消息的情况下,如何确定分配给USB记忆棒的/dev设备,而无需用户干预或事先知道所插入设备的详细信息?

My question: how do I determine, in the absence of a D-Bus message, the /dev device assigned to a USB memory stick without user intervention, or knowing in advance the specifics of the inserted device?

谢谢,对这本小说感到抱歉.

Thanks, sorry about the novel.

推荐答案

这似乎结合了/proc/partitions和ephimient采用的/sys/class/block方法.

This seems to work combining /proc/partitions and the /sys/class/block approach ephimient took.

#!/usr/bin/python
import os
partitionsFile = open("/proc/partitions")
lines = partitionsFile.readlines()[2:]#Skips the header lines
for line in lines:
    words = [x.strip() for x in line.split()]
    minorNumber = int(words[1])
    deviceName = words[3]
    if minorNumber % 16 == 0:
        path = "/sys/class/block/" + deviceName
        if os.path.islink(path):
            if os.path.realpath(path).find("/usb") > 0:
                print "/dev/%s" % deviceName

我不确定它的便携性或可靠性,但是它可以用于我的USB记忆棒.当然,可以将find("/usb")制成更严格的正则表达式.进行mod 16可能也不是找到磁盘本身并筛选出分区的最佳方法,但到目前为止对我仍然有效.

I'm not sure how portable or reliable this is, but it works for my USB stick. Of course find("/usb") could be made into a more rigorous regular expression. Doing mod 16 may also not be the best approach to find the disk itself and filter out the partitions, but it works for me so far.

这篇关于在Linux中编写USB设备安装脚本的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆