<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Sysfs on Roberto Selbach</title><link>https://rselbach.com/tags/sysfs/</link><description>Recent content in Sysfs on Roberto Selbach</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Fri, 27 May 2005 18:55:00 +0000</lastBuildDate><atom:link href="https://rselbach.com/tags/sysfs/index.xml" rel="self" type="application/rss+xml"/><item><title>A Stable Name for a Restless USB Disk</title><link>https://rselbach.com/a-stable-name-for-a-restless-usb-disk/</link><pubDate>Fri, 27 May 2005 18:55:00 +0000</pubDate><guid>https://rselbach.com/a-stable-name-for-a-restless-usb-disk/</guid><description>&lt;p&gt;My backup script failed because the USB disk appeared under a different device node after a reboot. The script had one job and had apparently outsourced it to probe order.&lt;/p&gt;
&lt;p&gt;I first added a loop that searched &lt;code&gt;/dev/sd*&lt;/code&gt; and selected the first disk of roughly the right size. That is not identification; it is speed dating with block devices. One wrong match would turn a backup problem into a restoration problem.&lt;/p&gt;
&lt;p&gt;The better route starts with the event. When the kernel discovers hardware, hotplug information reaches user space. sysfs exposes attributes and the device hierarchy. udev applies naming policy and creates nodes or symbolic links. A rule can therefore match something intrinsic to this disk, such as a serial attribute, rather than its temporary kernel name.&lt;/p&gt;
&lt;p&gt;Before writing the rule, I inspected the sysfs path associated with the block device and walked upward to understand where the useful attribute lived. This distinction matters: a rule may receive attributes from the device itself and from parent devices, but blindly matching an attribute seen somewhere in &lt;code&gt;/sys&lt;/code&gt; is an excellent way to create a rule that never fires.&lt;/p&gt;
&lt;p&gt;The result was a stable symbolic link under &lt;code&gt;/dev&lt;/code&gt; while the ordinary &lt;code&gt;sd&lt;/code&gt; name remained available. My script uses the stable link and checks the filesystem before mounting it. udev supplies naming policy; it does not absolve the script from verifying what it is about to write.&lt;/p&gt;
&lt;p&gt;I tested by unplugging the disk, attaching another one first, and reconnecting in different orders. Watching the hotplug environment and udev&amp;rsquo;s diagnostics was much faster than repeatedly editing a rule and staring at &lt;code&gt;/dev&lt;/code&gt; with wounded optimism.&lt;/p&gt;
&lt;p&gt;Cold-plug startup deserved the same test. A useful rule must behave consistently whether the disk is present during boot or attached later.&lt;/p&gt;
&lt;p&gt;The division of responsibility is clean. The kernel reports what appeared and where it sits in the device model. User space decides what local name is useful. My machine&amp;rsquo;s nickname for a disk does not belong in a kernel driver.&lt;/p&gt;
&lt;p&gt;Dynamic device management still has an intimidating number of moving parts, but the principle is reliable: match stable attributes, create a stable link, and keep transient enumeration out of scripts. Hardware order is an observation, not a promise.&lt;/p&gt;</description></item><item><title>Following the Device Through sysfs</title><link>https://rselbach.com/following-the-device-through-sysfs/</link><pubDate>Fri, 19 Nov 2004 18:40:00 +0000</pubDate><guid>https://rselbach.com/following-the-device-through-sysfs/</guid><description>&lt;p&gt;A USB card reader prompted today&amp;rsquo;s trip through &lt;code&gt;/sys&lt;/code&gt;. I wanted one answer: which physical device sits behind the block-class entry &lt;code&gt;/sys/block/sda&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;The useful relationship is the &lt;code&gt;device&lt;/code&gt; symlink. &lt;code&gt;/sys/block/sda&lt;/code&gt; presents the disk as a member of the block class; its &lt;code&gt;device&lt;/code&gt; link points into the physical device hierarchy. Those are two views of one kernel object, not two devices.&lt;/p&gt;
&lt;p&gt;I started at the block entry and followed its device link rather than guessing from the node name:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-console" data-lang="console"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;$ readlink -f /sys/block/sda/device
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;/sys/devices/pci0000:00/0000:00:10.4/usb2/2-2/2-2:1.0/host1/1:0:0:0
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The exact target is machine-specific. Here it says that the block disk came through a SCSI host attached to USB interface &lt;code&gt;2-2:1.0&lt;/code&gt;, itself below a PCI USB controller. On another machine the chain will differ, but the link still joins the class view to the device that implements it.&lt;/p&gt;
&lt;p&gt;Walking upward from that target lets me inspect the parents that actually own bus attributes. The block entry doesn&amp;rsquo;t need to copy USB details into its own directory; the parent links already express the relationship.&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t parse the punctuation in the resolved path. Directory names and topology are useful while diagnosing a machine, but exported links and attributes are the interface. Hard-coding every component would just trade &lt;code&gt;/dev/sda&lt;/code&gt; for a longer accidental name.&lt;/p&gt;
&lt;p&gt;This also explains why &lt;code&gt;/dev/sda&lt;/code&gt; isn&amp;rsquo;t a hardware biography. The node names a block interface; sysfs supplies the link back to the device model when I need to ask where that interface came from.&lt;/p&gt;
&lt;p&gt;The service corridors are a little dim, but this one symlink answers the question without guesswork.&lt;/p&gt;</description></item><item><title>Letting udev Name the Devices</title><link>https://rselbach.com/letting-udev-name-the-devices/</link><pubDate>Thu, 13 May 2004 18:15:00 +0000</pubDate><guid>https://rselbach.com/letting-udev-name-the-devices/</guid><description>&lt;p&gt;I have been trying &lt;code&gt;udev&lt;/code&gt; on a Linux 2.6 test installation because I want removable devices to receive useful permissions without maintaining a giant static &lt;code&gt;/dev&lt;/code&gt;. The project is still young, so this is an experiment rather than my new universal recipe.&lt;/p&gt;
&lt;p&gt;The mechanism is pleasantly divided. The kernel discovers hardware and emits a hotplug event. Sysfs exposes the device and its attributes under &lt;code&gt;/sys&lt;/code&gt;. A userspace helper uses that information and its rules to create or remove the appropriate node in &lt;code&gt;/dev&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;This means policy lives in userspace. The kernel does not need to decide which local group may open a scanner or what friendly name I prefer for a particular disk. It reports identity and major/minor numbers; &lt;code&gt;udev&lt;/code&gt; applies the administrator&amp;rsquo;s rule.&lt;/p&gt;
&lt;p&gt;For debugging, I start with sysfs rather than guessing a rule from the device node:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;find /sys/class -name dev -print
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The &lt;code&gt;dev&lt;/code&gt; attribute contains the major and minor number for devices that have one. Following the device links reveals parent hardware and useful attributes. The exact early rule syntax and available helper programs are changing, so I keep rules small and check the documentation shipped with the installed version instead of copying an example for some other release.&lt;/p&gt;
&lt;p&gt;My test sequence is equally small. I boot with a conventional device setup available as a fallback, start &lt;code&gt;udev&lt;/code&gt;, attach one known USB device, and watch both the system log and &lt;code&gt;/dev&lt;/code&gt;. I verify the node type, major/minor number, owner, group, and mode before opening it from an application.&lt;/p&gt;
&lt;p&gt;Permissions are the easiest part to get subtly wrong. Creating the correct node with mode &lt;code&gt;0666&lt;/code&gt; may make the test pass while giving every local user access to hardware that should be restricted. A group-based rule is usually a better policy. Convenience should not arrive disguised as an accidental security decision.&lt;/p&gt;
&lt;p&gt;Persistent naming is the more interesting promise. Kernel names can depend on discovery order. Userspace can use stable attributes to select a consistent name for a known device. But choose attributes that are genuinely stable and unique; a generic product string shared by every unit is neither.&lt;/p&gt;
&lt;p&gt;I like the architecture because it keeps detection in the kernel and local policy outside it. I remain cautious because early boot needs device nodes before much userspace is running, and distributions must integrate the pieces carefully. For now I am testing one class of device at a time. Dynamic &lt;code&gt;/dev&lt;/code&gt; is useful, but a missing root disk remains impressively dynamic in the wrong direction.&lt;/p&gt;</description></item><item><title>sysfs Is Not a New proc</title><link>https://rselbach.com/sysfs-is-not-a-new-proc/</link><pubDate>Thu, 08 Jan 2004 18:40:00 +0000</pubDate><guid>https://rselbach.com/sysfs-is-not-a-new-proc/</guid><description>&lt;p&gt;I keep hearing &lt;code&gt;/sys&lt;/code&gt; described as &amp;ldquo;the new &lt;code&gt;/proc&lt;/code&gt;.&amp;rdquo; This is close enough to create the wrong expectation.&lt;/p&gt;
&lt;p&gt;Procfs primarily exposes processes, plus a historical collection of kernel information and controls that accumulated there because it was available. Sysfs exposes the kernel device model. Its directories represent devices, buses, drivers, and classes, with symbolic links expressing how those objects relate.&lt;/p&gt;
&lt;p&gt;For example, &lt;code&gt;/sys/class/net/eth0&lt;/code&gt; is the network-class view of an interface. Its &lt;code&gt;device&lt;/code&gt; link leads to the underlying hardware in &lt;code&gt;/sys/devices&lt;/code&gt;, and that device may have a &lt;code&gt;driver&lt;/code&gt; link identifying what controls it.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;readlink /sys/class/net/eth0/device
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;readlink /sys/class/net/eth0/device/driver
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That relationship is the useful bit. I no longer need to combine a line from &lt;code&gt;lspci&lt;/code&gt;, a module name from &lt;code&gt;lsmod&lt;/code&gt;, and a hopeful guess.&lt;/p&gt;
&lt;p&gt;Sysfs attributes are generally small files containing one value. This is much easier to consume than parsing a pretty table, but I would not make scripts depend on every path visible today. Use the stable, documented attributes provided for the job. The filesystem mirrors kernel objects, and not every implementation detail is a promise.&lt;/p&gt;
&lt;p&gt;The distinction also explains why sysfs matters to &lt;code&gt;udev&lt;/code&gt;. Userspace receives a device event, follows the structured information in &lt;code&gt;/sys&lt;/code&gt;, and applies naming or permission policy. &lt;code&gt;/proc&lt;/code&gt; was never a clean database for that purpose.&lt;/p&gt;
&lt;p&gt;So I mount both. &lt;code&gt;/proc&lt;/code&gt; answers process and assorted kernel questions; &lt;code&gt;/sys&lt;/code&gt; answers device-model questions. Linux has not replaced one miscellaneous cupboard with another. It has, at last, labelled a new set of shelves.&lt;/p&gt;</description></item><item><title>Reading the Device Tree in sysfs</title><link>https://rselbach.com/reading-the-device-tree-in-sysfs/</link><pubDate>Thu, 06 Nov 2003 17:55:00 +0000</pubDate><guid>https://rselbach.com/reading-the-device-tree-in-sysfs/</guid><description>&lt;p&gt;I needed to answer a simple question on a 2.6 test machine: which driver owns this network device? My old habit was to rummage through boot messages, &lt;code&gt;/proc&lt;/code&gt;, and module output until the answer surrendered. The new device model gives us a less archaeological method.&lt;/p&gt;
&lt;p&gt;First, mount &lt;code&gt;sysfs&lt;/code&gt; if the installation has not done it already:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;mount -t sysfs none /sys
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The interesting part is not that &lt;code&gt;/sys&lt;/code&gt; contains files. Linux can turn almost anything into files if left unattended. The useful part is the structure. Devices appear according to their physical or logical parentage under &lt;code&gt;/sys/devices&lt;/code&gt;. Buses, such as PCI and USB, have views under &lt;code&gt;/sys/bus&lt;/code&gt;, while &lt;code&gt;/sys/class&lt;/code&gt; groups devices by function, such as network interfaces or block devices.&lt;/p&gt;
&lt;p&gt;These are views of the same kernel objects, joined by symbolic links. For an Ethernet interface named &lt;code&gt;eth0&lt;/code&gt;, this is a useful start:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-sh" data-lang="sh"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;readlink /sys/class/net/eth0/device
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;ls -l /sys/class/net/eth0/device/driver
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The first link leads back into the device hierarchy. The &lt;code&gt;driver&lt;/code&gt; link, when present, identifies the bound driver. Following parent links tells me which PCI device contains the interface and where that device sits.&lt;/p&gt;
&lt;p&gt;This differs from &lt;code&gt;/proc&lt;/code&gt;. Procfs grew as a place for process information and gradually acquired assorted kernel knobs and device facts. Sysfs is an exported representation of the kernel&amp;rsquo;s device model. One attribute per file is the usual convention, which makes simple scripts possible without parsing a decorative essay from the kernel.&lt;/p&gt;
&lt;p&gt;I am deliberately cautious about those scripts. The 2.6 series is not final yet, and sysfs details can change as interfaces settle. Scripts should consume documented attributes, not depend on directory ordering or infer meaning from every internal-looking name. A readable tree is still an interface, not an invitation to marry the first implementation detail one encounters.&lt;/p&gt;
&lt;p&gt;The larger implication is device management in userspace. If the kernel exposes device identity and events cleanly, a userspace program can create names and permissions according to policy rather than a huge static &lt;code&gt;/dev&lt;/code&gt; or a kernel naming scheme. That work is still young, but sysfs supplies the map it needs.&lt;/p&gt;
&lt;p&gt;For now, it has already replaced several minutes of log-diving with two symbolic links. I consider that progress, even if it has added another virtual filesystem to my vocabulary.&lt;/p&gt;</description></item></channel></rss>