2

Can i check if devices like sda , sdb naming in /dev/ are persistent by default

or

there are some udev rules in place to create persistent naming for them ?

Does udev itself create device naming dynamically in /dev but use rules to ensure presistent naming ?

fixer1234
  • 27,486
Noob
  • 1,595

1 Answers1

4

Device naming is performed as the devices are initialised by the kernel - so no, naming is not persistent. Commonly UUIDs are used to identify disks, as /dev/sdX is subject to change. One can use udev to recognise disks by their serial number (or some other attribute) and create a symlink in /dev , for example /dev/mydisk1.

There are a number of ways that /dev is populated; most commonly (nowdays) the kernel uses devtmpfs to dynamically populate /dev on boot. Udev then takes over and handles device addition, removal, scripting, etc.

From the Gentoo Wiki page on uDev

The kernel detects devices asynchronously, udev mirrors the kernel's sysfs filesystem and so the devices are named and numbered in order of detection. So by default udev provides no persistent device names. However there are mechanisms for some device classes to provide these

Here's a recent response to a similar question describing the process of setting up a persisent name, with links to further relevant answers...

EDIT: For fstab it is reccommended to set your devices using the UUID instead of setting up a udev symlink; run the following

blkid /dev/sda1 -s UUID -o value

The output will be the partition's UUID, which you can use in fstab. Instead of /dev/sda1 use UUID=thelonguuidabove123456

EDIT 2: The sequence of events during booting will be something like this, depending on how your kernel / userspace is set up:

  1. Kernel initialises a bare root filesystem containing an empty /dev folder. This folder is populated by the kernel using a devtmpfs, a dynamic pseudo-filesystem containing entries for each hardware device. This is a dynamic process and devices are subject to change. At this point we are in a ramdisk and kernel space, there is no udev / root filesystem
  2. On top of this is then mounted the initrd / initramfs filesystem, which provides more 'stuff' to get 'other stuff' up and running - for example it may contain fsck tools, kernel modules, stuff like that. /dev is still managed by devtmpfs.
  3. On top of this is then mounted the root filesystem proper, init has been run and is now starting services in userspace, one of which is udev. udev then takes over the /dev directory and remounts devtmpfs into is, and from hereon in udev manages the contents of /dev, using events from the kernel to 'know' when devices are added or removed and running scripts as appropriate.

The takehome message here is that /dev will inherit any names initially given by the kernel, and will then manage adding / removal. With no hardware changes the device descriptors in /dev probably won't change. However there are plenty of circumstances where they may change - point being, is that the names in /dev are not to be wholly relied upon.

hygri
  • 484
  • Thanks for the explanation. Do you mean if i have disk1 and disk2 which is assigned sda and sdb accordingly. And if i change their disk location physically, disk1 can become sdb and disk2 sda ? Or even without any physical / configuration changes -> their naming is still randomized – Noob Jun 28 '15 at 05:13
  • Yes,exactly. The device name is assigned in order of detection, so usually the disk on the first SATA port, for example, will be sda, the second sdb and so on. It's less common for devices to move about when there has been no phyiscal change, however two SATA RAID / Host controllers might be initialised in a different order between reboots, so it's still entireley possible! P.S. If you've found the above answer useful it'd be great if you'd 'accept' it! – hygri Jun 29 '15 at 07:33
  • the answers are certainly useful. Actually, i am have a mountpoint on the entire device (e.g. disk2) and the mapping in fstab is pointing directly to /dev/sdb . if according to what we mentioned earlier that devices name are randomized to a certain extend -> does that mean that what i am doing is wrong or risky ? – Noob Jun 29 '15 at 10:14
  • It'll work, but it's not reccommended due to the dynamic names as you say. Better thing is to use the UUID, I've editied the above post to explain. – hygri Jun 29 '15 at 11:20
  • thanks hoji. but can i confirm that given the dynamic naming of udev, it might not work (if i choose to stick to the actual device naming) ? also you mentioned about using UDID in fstab instead of symlink, i am using /dev/sdb/ -> can i check if /dev/sdb populated by udev ? i don't think it is a symlink though. – Noob Jun 29 '15 at 11:43
  • Yes, it might go wring at some point, especially if you add a new disk. I'm not sure what you mean by /dev/sdb populated by udev though. – hygri Jun 29 '15 at 12:29
  • sorry for the confusion. what i meant is , if "sdb" is added into /dev/ by udev ? Is it possible for you to elaborate why adding a new disk will likely change the device naming ? (if according to what you have mentioned the devices name are assigned in order of detection (1st sata port, 2nd sata port, 3rd sata port) ... and there is no physical swapping of disks – Noob Jun 29 '15 at 13:41
  • The devices are initially inserted into /dev by the kernel, not udev - udev then 'sits on top' of /dev looking for changes, running scripts, doing clever stuff. If there is no change in the hardware on the PC then the /dev/sdX names will probably be static between reboots - however this can be hardware dependent. An old motherboard I have here initialises the SATA and PATA in a random order, so the PATA and SATA disks frequently change. It's good practice to use UUIDs in fstab, but you can certainly get away with using device names in a single disk system. – hygri Jun 29 '15 at 13:48
  • i am not sure how it actually works, but i think it is the kernel that detects the devices , but it is still udev that put the device name into /dev/ looking at this https://www.linux.com/news/hardware/peripherals/180950-udev on Kernel Device Event Management - at the beginning it mentioned "Udev supplies a dynamic device directory containing only the nodes for devices which are connected to the system. It creates or removes the device node files in the /dev directory as they are plugged in or taken out. " – Noob Jun 29 '15 at 14:35
  • HoJi you still around ? are you able to elaborate abit further on why udev is unable to provide persistent naming by default -> it is mentioned that the kernel detect devices asynchronously, but if that's the case how come we are able to get the correct device naming for internal disks most / all of the time ? – Noob Jun 30 '15 at 14:08
  • Hello, I'm still here! I'll edit the answer again, just because this box is too small! – hygri Jul 01 '15 at 11:22
  • I've edited the above. The reason that disks are mostly consistently named is that the hardware is initialised in the same order at every boot, therefore without hardware changes the /dev entries are less likely to change. Point being that the kernel is what is detecting the devices and populating /dev, but it does this via udev which allows all sorts of other magic to be instigated, scripts and the like. – hygri Jul 01 '15 at 11:36
  • -> does it apply to external connected storage like SAN storage (whereby LUNs are assigned to the server) reason being i am actually setting a oracle database (asm), but the manual has inform us to use udev, which i am thinking why doesn't the same apply for internal storages – Noob Jul 02 '15 at 01:08
  • I'm not familiar with the way SAN is handled. However if it's anything like LUKS / LVM then there will be a pseudo-static entry in /dev/mapper - but I'm not the one to advise you on that :) – hygri Jul 02 '15 at 17:15