{"id":11442,"date":"2019-03-14T07:39:46","date_gmt":"2019-03-14T07:39:46","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11442"},"modified":"2019-03-14T07:39:46","modified_gmt":"2019-03-14T07:39:46","slug":"how-to-use-udev-for-device-detection-and-management-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/how-to-use-udev-for-device-detection-and-management-in-linux\/","title":{"rendered":"How to Use Udev for Device Detection and Management in Linux"},"content":{"rendered":"<p><strong>Udev<\/strong>\u00a0(userspace\u00a0<strong>\/dev<\/strong>) is a Linux sub-system for dynamic device detection and management, since kernel version\u00a0<strong>2.6<\/strong>. It\u2019s a replacement of\u00a0<strong>devfs<\/strong>\u00a0and\u00a0<strong>hotplug<\/strong>.<\/p>\n<p>It dynamically creates or removes device nodes (an interface to a device driver that appears in a file system as if it were an ordinary file, stored under the\u00a0<strong>\/dev<\/strong>\u00a0directory) at boot time or if you add a device to or remove a device from the system. It then propagates information about a device or changes to its state to user space.<\/p>\n<p>It\u2019s function is to 1) supply the system applications with device events, 2) manage permissions of device nodes, and 3) may create useful symlinks in the\u00a0<strong>\/dev<\/strong>\u00a0directory for accessing devices, or even renames network interfaces.<\/p>\n<p>One of the pros of\u00a0<strong>udev<\/strong>\u00a0is that it can use persistent device names to guarantee consistent naming of devices across reboots, despite their order of discovery. This feature is useful because the kernel simply assigns unpredictable device names based on the order of discovery.<\/p>\n<p>In this article, we will learn how to use\u00a0<strong>Udev<\/strong>\u00a0for device detection and management on Linux systems. Note that most if not all mainstream modern Linux distributions come with\u00a0<strong>Udev<\/strong>\u00a0as part of the default installation.<\/p>\n<h3>Learn Basics of Udev in Linux<\/h3>\n<p>The\u00a0<strong>udev<\/strong>\u00a0daemon,\u00a0<strong>systemd-udevd<\/strong>\u00a0(or\u00a0<strong>systemd-udevd.service<\/strong>) communicates with the kernel and receives device\u00a0<strong>uevents<\/strong>\u00a0directly from it each time you add or remove a device from the system, or a device changes its state.<\/p>\n<p><strong>Udev<\/strong>\u00a0is based on rules \u2013 it\u2019s rules are flexible and very powerful. Every received device event is matched against the set of rules read from files located in\u00a0<strong>\/lib\/udev\/rules.d<\/strong>\u00a0and\u00a0<strong>\/run\/udev\/rules.d<\/strong>.<\/p>\n<p>You can write custom rules files in the\u00a0<strong>\/etc\/udev\/rules.d\/<\/strong>\u00a0directory (files should end with the\u00a0<code>.rules<\/code>extension) to process a device. Note that rules files in this directory have the highest priority.<\/p>\n<p>To create a device node file,\u00a0<strong>udev<\/strong>\u00a0needs to identify a device using certain attributes such as the\u00a0<strong>label<\/strong>,\u00a0<strong>serial number<\/strong>, its major and minor number used, bus device number and so much more. This information is exported by the\u00a0<strong>sysfs<\/strong>\u00a0file system.<\/p>\n<p>Whenever you connect a device to the system, the kernel detects and initializes it, and a directory with the device name is created under\u00a0<strong>\/sys\/<\/strong>\u00a0directory which stores the device attributes.<\/p>\n<p>The main configuration file for udev is\u00a0<strong>\/etc\/udev\/udev.conf<\/strong>, and to control the runtime behavior the udev daemon, you can use the\u00a0<strong>udevadm<\/strong>\u00a0utility.<\/p>\n<p>To display received kernel\u00a0<strong>events<\/strong>\u00a0(<strong>uevents<\/strong>) and\u00a0<strong>udev events<\/strong>\u00a0(which udev sends out after rule processing), run\u00a0<strong>udevadm<\/strong>\u00a0with the monitor command. Then connect a device to your system and watch, from the terminal, how the device event is handled.<\/p>\n<p>The following screenshot shows an excerpt of an\u00a0<strong>ADD<\/strong>\u00a0event after connecting a USB flash disk to the test system:<\/p>\n<pre>$ udevadm monitor \r\n<\/pre>\n<div id=\"attachment_31084\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/monitor-device-events.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31084\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/monitor-device-events.png\" sizes=\"auto, (max-width: 932px) 100vw, 932px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/monitor-device-events.png 932w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/monitor-device-events-768x252.png 768w\" alt=\"Monitor Device Events in Linux\" width=\"932\" height=\"306\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Monitor Device Events in Linux<\/p>\n<\/div>\n<p>To find the name assigned to your USB disk, use the\u00a0<a href=\"https:\/\/www.tecmint.com\/find-linux-filesystem-type\/\" target=\"_blank\" rel=\"noopener\">lsblk utility<\/a>\u00a0which reads the\u00a0<strong>sysfs<\/strong>\u00a0filesystem and\u00a0<strong>udev db<\/strong>\u00a0to gather information about processed devices.<\/p>\n<pre> \r\n$ lsblk\r\n<\/pre>\n<div id=\"attachment_31085\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/list-block-devices.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31085\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/list-block-devices.png\" alt=\"List Block Devices in Linux\" width=\"662\" height=\"363\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Block Devices in Linux<\/p>\n<\/div>\n<p>From the output of the previous command, the USB disk is named\u00a0<code>sdb1<\/code>\u00a0(absolute path should be\u00a0<code>\/dev\/sdb1<\/code>). To query the device attributes from the udev database, use the\u00a0<strong>info command<\/strong>.<\/p>\n<pre>$ udevadm info \/dev\/sdb1\r\n<\/pre>\n<div id=\"attachment_31086\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/query-device-attributes-from-udev-db.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31086\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/query-device-attributes-from-udev-db.png\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/query-device-attributes-from-udev-db.png 802w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/query-device-attributes-from-udev-db-768x439.png 768w\" alt=\"Query Device Attributes from Udev DB in Linux\" width=\"802\" height=\"458\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Query Device Attributes from Udev DB in Linux<\/p>\n<\/div>\n<h3>How to Work with Udev Rules in Linux<\/h3>\n<p>In this section, we will briefly discuss how to write\u00a0<strong>udev<\/strong>\u00a0rules. A rule comprises of a comma-separated list of one or more key-value pairs. Rules allow you to rename a device node from the default name, modify permissions and ownership of a device node, trigger execution of a program or script when a device node is created or deleted, among others.<\/p>\n<p>We will write a simple rule to launch a script when a USB device is added and when it is removed from the running system.<\/p>\n<p>Let\u2019s start by creating the two scripts:<\/p>\n<pre>$ sudo vim \/bin\/device_added.sh\r\n<\/pre>\n<p>Add the following lines in the\u00a0<code>device_added.sh<\/code>\u00a0script.<\/p>\n<pre>#!\/bin\/bash\r\necho \"USB device added at $(date)\" &gt;&gt;\/tmp\/scripts.log\r\n<\/pre>\n<p>Open the second script.<\/p>\n<pre>$ sudo vim \/bin\/device_removed.sh\r\n<\/pre>\n<p>Then add the following lines to\u00a0<code>device_removed.sh<\/code>\u00a0script.<\/p>\n<pre>#!\/bin\/bash\r\necho \"USB device removed  at $(date)\" &gt;&gt;\/tmp\/scripts.log\r\n<\/pre>\n<p>Save the files, close and make both scripts executable.<\/p>\n<pre>$ sudo chmod +x \/bin\/device_added.sh\r\n$ sudo chmod +x \/bin\/device_removed.sh\r\n<\/pre>\n<p>Next, let\u2019s create a rule to trigger execution of the above scripts, called\u00a0<strong>\/etc\/udev\/rules.d\/80-test.rules<\/strong>.<\/p>\n<pre>$ vim \/etc\/udev\/rules.d\/80-test.rules\r\n<\/pre>\n<p>Add these two following rules in it.<\/p>\n<pre>SUBSYSTEM==\"usb\", ACTION==\"add\", ENV{DEVTYPE}==\"usb_device\",  RUN+=\"\/bin\/device_added.sh\"\r\nSUBSYSTEM==\"usb\", ACTION==\"remove\", ENV{DEVTYPE}==\"usb_device\", RUN+=\"\/bin\/device_removed.sh\"\r\n<\/pre>\n<p>where:<\/p>\n<ul>\n<li><code>\"==\"<\/code>: is an operator to compare for equality.<\/li>\n<li><code>\"+=\"<\/code>: is an operator to add the value to a key that holds a list of entries.<\/li>\n<li><strong>SUBSYSTEM<\/strong>: matches the subsystem of the event device.<\/li>\n<li><strong>ACTION<\/strong>: matches the name of the event action.<\/li>\n<li><strong>ENV{DEVTYPE}<\/strong>: matches against a device property value, device type in this case.<\/li>\n<li><strong>RUN<\/strong>: specifies a program or script to execute as part of the event handling.<\/li>\n<\/ul>\n<p>Save the file and close it. Then as root, tell\u00a0<strong>systemd-udevd<\/strong>\u00a0to reload the rules files (this also reloads other databases such as the kernel module index), by running.<\/p>\n<pre>$ sudo udevadm control --reload\r\n<\/pre>\n<p>Now connect a USB drive into your machine and check if the\u00a0<code>device_added.sh<\/code>\u00a0script was executed. First of all the file\u00a0<strong>scripts.log<\/strong>\u00a0should be created under\u00a0<strong>\/tmp<\/strong>.<\/p>\n<pre>$ ls -l \/tmp\/scripts.log\r\n<\/pre>\n<div id=\"attachment_31087\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/check-scripts-log-after-adding-a-usb-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31087\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/check-scripts-log-after-adding-a-usb-device.png\" alt=\"Check Scripts Log After Adding USB\" width=\"622\" height=\"97\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Scripts Log After Adding USB<\/p>\n<\/div>\n<p>Then the file should have an entry such as \u201c<strong>USB device removed at date_time<\/strong>\u201d, as shown in the screenshot.<\/p>\n<pre>$ cat \/tmp\/scripts.log\r\n<\/pre>\n<div id=\"attachment_31088\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/check-scripts-log-after-removing-usb-device.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-31088\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2018\/11\/check-scripts-log-after-removing-usb-device.png\" alt=\"Check Scripts Log After Removing USB\" width=\"622\" height=\"78\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Check Scripts Log After Removing USB<\/p>\n<\/div>\n<p>For more information on how to write udev rules and manage udev, consult the\u00a0<strong>udev<\/strong>\u00a0and\u00a0<strong>udevadm<\/strong>\u00a0manual entries respectively, by running:<\/p>\n<pre>$ man udev\r\n$ man udevadm\r\n<\/pre>\n<h5>Summary<\/h5>\n<p><strong>Udev<\/strong>\u00a0is a remarkable device manager that provides a dynamic way of setting up device nodes in the\u00a0<code>\/dev<\/code>directory. It ensures that devices are configured as soon as they are plugged in and discovered. It propagates information about a processed device or changes to its state, to user space.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/udev-for-device-detection-management-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Udev\u00a0(userspace\u00a0\/dev) is a Linux sub-system for dynamic device detection and management, since kernel version\u00a02.6. It\u2019s a replacement of\u00a0devfs\u00a0and\u00a0hotplug. It dynamically creates or removes device nodes (an interface to a device driver that appears in a file system as if it were an ordinary file, stored under the\u00a0\/dev\u00a0directory) at boot time or if you add a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/14\/how-to-use-udev-for-device-detection-and-management-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Use Udev for Device Detection and Management in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11442","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11442","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11442"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11442\/revisions"}],"predecessor-version":[{"id":11443,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11442\/revisions\/11443"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11442"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11442"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11442"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}