Linux udev指定USB设备描述符

udev具体命令可以参考这个博客

https://www.cnblogs.com/fah936861121/p/6496608.html

我这里是嵌入式设备1043核心板

使用

dmesg 可以查看你插入usb时候的信息

或者使用

udevadm info -a -p $(udevadm info -q path -n /dev/sda1)

查询具体的信息

/etc/udev/rules.d/automount.rules

按照下面规格创建后,Usb1,访问的时候就是/dev/udisk

# There are a number of modifiers that are allowed to be used in some
# of the different fields. They provide the following subsitutions:
#
# %n the "kernel number" of the device.
#    For example, 'sda3' has a "kernel number" of '3'
# %e the smallest number for that name which does not matches an existing node
# %k the kernel name for the device
# %M the kernel major number for the device
# %m the kernel minor number for the device
# %b the bus id for the device
# %c the string returned by the PROGRAM
# %s{filename} the content of a sysfs attribute
# %% the '%' char itself
#

# Media automounting
SUBSYSTEM=="block", KERNELS=="1-1", SYMLINK+="udisk", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh udisk"
SUBSYSTEM=="block", KERNELS=="1-1", SYMLINK+="udisk", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh udisk"
SUBSYSTEM=="block", KERNELS=="1-1", SYMLINK+="udisk", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh udisk"

SUBSYSTEM=="block", KERNELS=="2-1", SYMLINK+="udisk", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh udisk"
SUBSYSTEM=="block", KERNELS=="2-1", SYMLINK+="udisk", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh udisk"
SUBSYSTEM=="block", KERNELS=="2-1", SYMLINK+="udisk", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh udisk"

SUBSYSTEM=="block", KERNELS=="3-1", SYMLINK+="cdisk", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh cdisk"
SUBSYSTEM=="block", KERNELS=="3-1", SYMLINK+="cdisk", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh cdisk"
SUBSYSTEM=="block", KERNELS=="3-1", SYMLINK+="cdisk", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh cdisk"

SUBSYSTEM=="block", KERNELS=="4-1", SYMLINK+="cdisk", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh cdisk"
SUBSYSTEM=="block", KERNELS=="4-1", SYMLINK+="cdisk", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh cdisk"
SUBSYSTEM=="block", KERNELS=="4-1", SYMLINK+="cdisk", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh cdisk"

SUBSYSTEM=="block", ACTION=="add"    RUN+="/etc/udev/scripts/mount.sh"
SUBSYSTEM=="block", ACTION=="remove" RUN+="/etc/udev/scripts/mount.sh"
SUBSYSTEM=="block", ACTION=="change", ENV{DISK_MEDIA_CHANGE}=="1" RUN+="/etc/udev/scripts/mount.sh"




 

/etc/udev/scripts/mount.sh

#!/bin/sh
#
# Called from udev
#
# Attempt to mount any added block devices and umount any removed devices


MOUNT="/bin/mount"
PMOUNT="/usr/bin/pmount"
UMOUNT="/bin/umount"

FLAG="$1"
#mount_dir="/mnt9/$FLAG"
#mkdir -p $mount_dir

#if [ $FLAG = "udisk" ];then
#	mount_dir="/mnt/$FLAG"
#	mkdir -p $mount_dir
#fi

for line in `grep -h -v ^# /etc/udev/mount.blacklist /etc/udev/mount.blacklist.d/*`
do
	if [ ` expr match "$DEVNAME" "$line" ` -gt 0 ];
	then
		logger "udev/mount.sh" "[$DEVNAME] is blacklisted, ignoring"
		exit 0
	fi
done

automount() {	
	name="`basename "$DEVNAME"`"

	! test -d "/run/media/$name" && mkdir -p "/run/media/$name"
	# Silent util-linux's version of mounting auto
	if [ "x`readlink $MOUNT`" = "x/bin/mount.util-linux" ] ;
	then
		MOUNT="$MOUNT -o silent"
	fi
	
	# If filesystem type is vfat, change the ownership group to 'disk', and
	# grant it with  w/r/x permissions.
	case $ID_FS_TYPE in
	vfat|fat)
		MOUNT="$MOUNT -o umask=007,gid=`awk -F':' '/^disk/{print $3}' /etc/group`"
		;;
	# TODO
	*)
		;;
	esac

	if ! $MOUNT -t auto $DEVNAME "/run/media/$name"
	then
		#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/run/media/$name\" failed!"
		rm_dir "/run/media/$name"
	else
		logger "mount.sh/automount" "Auto-mount of [/run/media/$name] successful"
		touch "/tmp/.automount-$name"
		if [ $FLAG = "udisk" ];then
			mount_dir="/mnt/$FLAG"
			! test -d $mount_dir && mkdir -p $mount_dir
			$MOUNT -t auto $DEVNAME $mount_dir
		elif [ $FLAG = "cdisk" ];then
			mount_dir="/mnt/$FLAG"
			! test -d $mount_dir && mkdir -p $mount_dir
			$MOUNT -t auto $DEVNAME $mount_dir
		fi
	fi



}
	
rm_dir() {
	# We do not want to rm -r populated directories
	if test "`find "$1" | wc -l | tr -d " "`" -lt 2 -a -d "$1"
	then
		! test -z "$1" && rm -r "$1"
	else
		logger "mount.sh/automount" "Not removing non-empty directory [$1]"
	fi
}

# No ID_FS_TYPE for cdrom device, yet it should be mounted
name="`basename "$DEVNAME"`"
[ -e /sys/block/$name/device/media ] && media_type=`cat /sys/block/$name/device/media`

if [ "$ACTION" = "add" ] && [ -n "$DEVNAME" ] && [ -n "$ID_FS_TYPE" -o "$media_type" = "cdrom" ]; then
	if [ -x "$PMOUNT" ]; then
		$PMOUNT $DEVNAME 2> /dev/null
	elif [ -x $MOUNT ]; then
    		$MOUNT $DEVNAME 2> /dev/null
	fi
	
	# If the device isn't mounted at this point, it isn't
	# configured in fstab (note the root filesystem can show up as
	# /dev/root in /proc/mounts, so check the device number too)
	if expr $MAJOR "*" 256 + $MINOR != `stat -c %d /`; then
		grep -q "^$DEVNAME " /proc/mounts || automount
	fi
fi


if [ "$ACTION" = "remove" ] || [ "$ACTION" = "change" ] && [ -x "$UMOUNT" ] && [ -n "$DEVNAME" ]; then
	for mnt in `cat /proc/mounts | grep "$DEVNAME" | cut -f 2 -d " " `
	do
		$UMOUNT $mnt
	done
	
	# Remove empty directories from auto-mounter
	name="`basename "$DEVNAME"`"

	
#	if [ "$1" = "udisk" ]; then
#		mount_dir=/mnt/udisk
#	elif [ "$1" = "cdisk" ]; then#
#		mount_dir=/mnt/cdisk
#	fi
#	test -e "/tmp/.automount-$name" && rm_dir $mount_dir

	test -e "/tmp/.automount-$name" && rm_dir "/run/media/$name"
fi

一下内容曾经在6ul的开发板上实现过,后来在1043上没有是实现,由于项目着急,不进行后续修改了。

使用命令,可以查询具体的设备信息

udevadm info -a -p $(udevadm info -q path -n /dev/sda1)

 

有很多总规则可以实现,我选择了一个比较通用的,但是不知道是否有bug

先使用命令,找到主设备号

ls /dev/sd* -l

brwxrwxrwx 1 root disk 8, 0 Jan 28 23:59 /dev/sda
brwxrwxrwx 1 root disk 8, 1 Jan 28 23:59 /dev/sda1

我这里的主设备号就是8

在路径:

/etc/udev/rules.d/

建立规则文件名:

08-usb-hotplug.rules

确认 KERNELS后面使用

插拔一下你的U盘,然后使用dmesg查看u盘占用的位置

Linux udev指定USB设备描述符_第1张图片

 

如果是hub需要修改下面的代码为 KERNELS=="3-1.1"

 

文件内容如下

#使用命令ls /dev/sd* -l 查询主设备号, 用来设置规则文件名称前面的数字部分,我这里是8
#CF卡的HUB 只能认出为3-1 或者4-1
#更新内核的usb,只能认出为1-1或者2-1
SUBSYSTEMS=="usb", KERNELS=="3-1", MODE:="0777", SYMLINK+="hub_cf"
SUBSYSTEMS=="usb", KERNELS=="4-1", MODE:="0777", SYMLINK+="hub_cf"

SUBSYSTEMS=="usb", KERNELS=="2-1", MODE:="0777", SYMLINK+="usb1"
SUBSYSTEMS=="usb", KERNELS=="1-1", MODE:="0777", SYMLINK+="usb1"

#SUBSYSTEMS=="usb", KERNELS=="2-1:1.0", MODE:="0777", SYMLINK+="usb1"
#SUBSYSTEMS=="usb", KERNELS=="1-1", MODE:="0777", SYMLINK+="usb2"


#SUBSYSTEMS=="block", KERNEL=="sd[a-z][0-9]",KERNELS=="[4-99]-1", MODE:="0777", SYMLINK+="usb1"
#SUBSYSTEMS=="usb", ATTRS{idVendor}=="058f", ATTRS{idProduct}=="6387", MODE:="0777", SYMLINK+="rplidar"

重新插拔u盘后,或者重启你的板子,并查看是否成功,以后使用/dev/usb1即可

root@localhost:~# ls -l /dev/ | grep sda
brwxrwxrwx 1 root disk      8,   0 Jan 28 23:59 sda
brwxrwxrwx 1 root disk      8,   1 Jan 28 23:59 sda1
lrwxrwxrwx 1 root root           4 Jan 28 23:59 usb1 -> sda1

如果需要自动挂载,则需要修改代码

#路径 etc\udev\scripts\mount.sh


automount() {	
	name="`basename "$DEVNAME"`"

	! test -d "/media/$name" && mkdir -p "/media/$name"
	
	if ! $MOUNT -t auto $DEVNAME "/media/$name"
	then
		#logger "mount.sh/automount" "$MOUNT -t auto $DEVNAME \"/media/$name\" failed!"
		rm_dir "/media/$name"
	else
		logger "mount.sh/automount" "Auto-mount of [/media/$name] successful"
		touch "/tmp/.automount-$name"

#自己根据情况进行修改
		$MOUNT /dev/usb1 /mnt/usb1
		$MOUNT /dev/hub_cf /mnt/cf


	fi
}

 

 

 

 

网上资料有用下面的方法实现的,我没使用,见上面的注释部分

idVendor 和 idProduct 进行判断的

idVendor :058f

idProduct :6387

root@localhost:~# lsusb 
Bus 006 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 058f:6387 Alcor Micro Corp. Flash Drive
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

 

你可能感兴趣的:(linux)