Usage of uvent,bind and unbind interface in /sys

來源:互聯網
上載者:User

In /sys, there are many kobject which have the attribute of uevent. Mainly thay are used as the interface for communication between kenel and udev. To udev and the netlink protocol, they doesn’t have to access uevent file. But via the interface of the uevent
file, udevmonitor can send message from kernel to udevd, and it also can be used to check the environment variable of netlink messages which are support by this device. This attribute can be used by developer to debug udev script, and udevtrigger is also implemented
based on this interface.

These uevent file usually are writable, In /sys/device, there are many of them. In the latest version kernel, these files are also readable.

#find /sys –type f –name uevent -ls

11    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10   /sys/devices/platform/uevent

1471    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10  /sys/devices/platform/pcspkr/uevent

3075    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10  /sys/devices/platform/vesafb.0/uevent

3915    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10  /sys/devices/platform/serial8250/uevent

3941    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10  /sys/devices/platform/serial8250/tty/ttyS2/uevent

3950    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10 /sys/devices/platform/serial8250/tty/ttyS3/uevent 5204    0 -rw-r–r–   1 root     root         4096 Jan 12 21:10 /sys/devices/platform/i8042/uevent[...]

912    0 -rw-r–r–   1 root     root         4096 Jan 12 21:17  /sys/devices/pci0000:00/0000:00:02.5/uevent

All these uevent file are writable and readable. The last item is uevent file of SCSI controller. The parameter which can be written are “add”, “remove”, “change”, “move”, “online” and “offline”.By writing “add”, it can sent a netlink message to udevd and
let udevd refresh the udev script file. This is very helpfult for developer to debug the udev script file.

# echo add > /sys/devices/pci0000:00/0000:00:02.5/uevent

In the /sys, there are many bind, unbind and new_id file for PCI device. Every device driver specified in the program which hardware this driver for, for example in the source code of driver, the MODULE_DEVICE_TABLE specifies the PCI hardware
PCI device id. If the hardware which is designed in future still can work in the mode of backward comptiable, then it still can use current driver. But before the interface of bind and unbind were implemented, we have to modify the DEVICE_TABLE of the source
code and recompile it. Besides this, there is no other solution. After the bind and unbind were added to kernel 2.6, we can bind the device driver and the hardware manually without the recompilation of the code.

In addition, to some hardware, there are several device drivers available, but at any certain time, there is only one driver running. We can use bind and unbind to decide which driver is running or not( For the choose of multiple drivers,the better way is
use modprobe.conf. It needs to be effective after reboot, but bind/unbind provide a temporary method witheout reboot). Via the interface of bind and unbind we can forcefully decide a device use certain driver or not.

#find /sys/-type f ( -name bind -or -name unbind -or -name new_id ) -ls

69    0 -rw-r–r–   1 root     root         4096 Jan 12 22:12

/sys/devices/virtual/vtconsole/vtcon0/bind

3072    0 –w——-   1 root     root         4096 Jan 12 22:15

/sys/bus/platform/drivers/vesafb/unbind

[...]

6489    0 –w——-   1 root     root         4096 Jan 12 22:09

/sys/bus/pci/drivers/8139too/unbind

6490    0 –w——-   1 root     root         4096 Jan 12 22:09

/sys/bus/pci/drivers/8139too/bind

6491    0 –w——-   1 root     root         4096 Jan 12 22:15

/sys/bus/pci/drivers/8139too/new_id

# find /sys/bus/pci/drivers/8139too/ -ls

  6435    0 drwxr-xr-x   2 root     root            0 Jan 12 22:08

/sys/bus/pci/drivers/8139too/

  6436    0 lrwxrwxrwx   1 root     root            0 Jan 12 22:08

/sys/bus/pci/drivers/8139too/0000:00:0e.0 ->  ../../../../devices/pci0000:00/0000:00:0e.0

  6485    0 lrwxrwxrwx   1 root     root            0 Jan 12 22:08

/sys/bus/pci/drivers/8139too/module -> ../../../../module/8139too

  6488    0 –w——-   1 root     root         4096 Jan 12 22:08

/sys/bus/pci/drivers/8139too/uevent

  6489    0 –w——-   1 root     root         4096 Jan 12 22:08

/sys/bus/pci/drivers/8139too/unbind

  6490    0 –w——-   1 root     root         4096 Jan 12 22:08

/sys/bus/pci/drivers/8139too/bind

  6491    0 –w——-   1 root     root         4096 Jan 12 22:08

/sys/bus/pci/drivers/8139too/new_id

# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc

pfifo_fast state

UNKNOWN qlen 1000

    link/ether 00:14:2a:d1:16:72 brd ff:ff:ff:ff:ff:ff

    inet 192.168.1.102/24 brd 192.168.1.255 scope global eth0

3: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN

    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff

# echo -n 0000:00:0e.0 > /sys/bus/pci/drivers/8139too/unbind

# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

3: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN

    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff

# echo -n 0000:00:0e.0 > /sys/bus/pci/drivers/8139too/bind

# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN

    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

    inet 127.0.0.1/8 scope host lo

3: bond0: <BROADCAST,MULTICAST,MASTER> mtu 1500 qdisc noop state DOWN

    link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff

4: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state

DOWN qlen 1000

link/ether 00:14:2a:d1:16:72 brd ff:ff:ff:ff:ff:ff

  This example shows how to bind and unbind the driver 8139too to device “0000:00:0e.0″ forcefully.

Write bus_id to unbind to cancal  binding.

Write bus_id to bind to implement binding

Notes: Bus id is used for writing, for PCI Device, it meets following rules and it can be retrieved from the kobject node. To device of other kinds bus, it meets the rules of this kind of bus.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.