LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   How does the kernel decide what device to create when a new drive is plugged in? (https://www.linuxquestions.org/questions/linux-kernel-70/how-does-the-kernel-decide-what-device-to-create-when-a-new-drive-is-plugged-in-4175660296/)

RandomTroll 09-04-2019 12:40 AM

How does the kernel decide what device to create when a new drive is plugged in?
 
I have 1 permanent drive. I often plug in a USB drive. Usually it's assigned /dev/sdb . Sometimes it's assigned /dev/sdc even though sdb is available. Why? I have scripts that expect /dev/sdb.

berndbausch 09-04-2019 02:07 AM

sda, sdb, sdc and so on are not persistent names. I can't tell you what causes your particular disk to be sometimes sdb, sometimes sdc, but in any case it's risky (as you just learned :)) to believe that the disk will always get the same device name.

Your disk has various properties that your system uses to generate persistent names, such as UUIDs, filesystem or swap LABELs, hardware paths, SCSI addresses and so on. In most distros, udev creates symbolic links under /dev/disk whose names are guaranteed to always refer to the same disk.

(not quite actually - when you clone a disk, the clone will receive all UUIDs and LABELs, and you can connect the same disk to different hardware paths. As long as you know what you are doing, though, the persistent naming mechanisms work reliably)

Since I don't know your application, I don't have a full solution for your problem, but I suggest to educate yourself about persistent naming.

Firerat 09-04-2019 02:39 AM

you might see a switch from sdb to sdc if the status of the drive changed in some way.

You should not use scripts that require sda sdb etc.
What if you have 2 usb drives , how does your script know you plugged the right one in?

hint:
[code]
ls -l /dev/disk/*
]/code]

/dev/disk/by-uuid
Is probably what you want
But I have no idea what you have scripted, so that is a complete guess.

Code:

lsblk -O -J | jq .
for something your script can work with

timl 09-04-2019 02:47 AM

following the comments above...I get around this by creating a mount point and adding an entry to /etc/fstab with the mount point and the UUID of the drive. I use the mount point in my scripts. If the USB drive changes, change fstab to recognise this

The fact that my learned friends have not already mentioned this makes me wonder...am I missing the point

Firerat 09-04-2019 02:53 AM

Quote:

Originally Posted by timl (Post 6032929)
The fact that my learned friends have not already mentioned this makes me wonder...am I missing the point

no idea what the script is doing
maybe it is putting a fs on the partition.


all we know is some script needs /dev/sdb

Which tells me said script needs to be re-written or at least fixed.

zeebra 09-04-2019 04:05 AM

Quote:

Originally Posted by RandomTroll (Post 6032900)
I have 1 permanent drive. I often plug in a USB drive. Usually it's assigned /dev/sdb . Sometimes it's assigned /dev/sdc even though sdb is available. Why? I have scripts that expect /dev/sdb.

I've experienced this. I think it has something to do with timing. If sdb is available, it will always be assigned sdb. In some cases sdb WAS assigned (and probably not properly unmounted), then next time (shortly after) it was assigned sdc instead of sdb although sdb appeared to be available.

I don't know the root cause, but I can confirm the "issue". And personally I think it has something to do with inproper unmount of a device and device timeout.

Quote:

Originally Posted by Firerat (Post 6032928)
You should not use scripts that require sda sdb etc.

This is good advice regarding the script part.

berndbausch 09-04-2019 07:25 AM

Quote:

Originally Posted by timl (Post 6032929)
following the comments above...I get around this by creating a mount point and adding an entry to /etc/fstab with the mount point and the UUID of the drive. I use the mount point in my scripts. If the USB drive changes, change fstab to recognise this

The fact that my learned friends have not already mentioned this makes me wonder...am I missing the point

Absolutely. For example, what if there is no file system on the disk? Or not even a UUID?

RandomTroll 09-04-2019 09:03 AM

Quote:

Originally Posted by berndbausch (Post 6032920)
sda, sdb, sdc and so on are not persistent names.

Duh.

Quote:

Originally Posted by berndbausch (Post 6032920)
I suggest to educate yourself about persistent naming.

That's an argument never to ask any question.

Quote:

Originally Posted by Firerat (Post 6032928)
What if you have 2 usb drives...?

Then I know that and act appropriately.


Quote:

Originally Posted by Firerat (Post 6032928)
/dev/disk/by-uuid
Is probably what you want

Turns out not to be the case.


Quote:

Originally Posted by timl (Post 6032929)
following the comments above...I get around this by creating a mount point and adding an entry to /etc/fstab with the mount point and the UUID of the drive. I use the mount point in my scripts. If the USB drive changes, change fstab to recognise this

Good idea. I hadn't thought of this. Unfortunately it requires an entry for every device, more of an inconvenience than I have now.

Quote:

Originally Posted by Firerat (Post 6032931)
Which tells me said script needs to be re-written or at least fixed.

It already works. I was just curious about what is persisting in the mind of the kernel that keeps /dev/sdb unavailable.

Quote:

Originally Posted by berndbausch (Post 6032997)
Absolutely. For example, what if there is no file system on the disk? Or not even a UUID?

What if a fire has broken out?

I don't have a problem, but a question.

Firerat 09-04-2019 09:14 AM

Your script doesn't work, the reason you started the thread proves that.
Edit: "is broken" is a better fit than "doesn't work"


if you are going to troll, at least be smart about it.

berndbausch 09-04-2019 10:13 AM

Quote:

Originally Posted by RandomTroll (Post 6033031)
What if a fire has broken out?

Then sdb is still not viable, and UUIDs only work for a subset of all disks, and mounts for a subset of that.

rknichols 09-04-2019 01:08 PM

Quote:

Originally Posted by berndbausch (Post 6032997)
For example, what if there is no file system on the disk? Or not even a UUID?

There's /dev/disk/by-id/, which contains entries that include the drive's model and serial number, e.g.:
Code:

/dev/disk/by-id/ata-ST1000DM003-1ER162_S4Y0Y819
/dev/disk/by-id/ata-ST1000DM003-1ER162_S4Y0Y819-part1
/dev/disk/by-id/ata-ST1000DM003-1ER162_S4Y0Y819-part2
/dev/disk/by-id/ata-ST1000DM003-1ER162_S4Y0Y819-part3
/dev/disk/by-id/ata-ST1000DM003-1ER162_S4Y0Y819-part4

That's assuming you have the needed udev rules in place to set up those entries, of course.

RandomTroll 09-04-2019 07:05 PM

Quote:

Originally Posted by Firerat (Post 6033037)
Your script doesn't work, the reason you started the thread proves that.

No it doesn't. My script works. Sometimes it finds /dev/sdb unavailable even though it doesn't exist. It tries /dev/sdc next.

Quote:

Originally Posted by Firerat (Post 6033037)
Edit: "is broken" is a better fit than "doesn't work"

Both your phrases; I used neither.

It turned out to be an orphaned entry in /etc/mtab.

Firerat 09-05-2019 03:06 AM

Quote:

Originally Posted by RandomTroll (Post 6032900)
I have 1 permanent drive. I often plug in a USB drive. Usually it's assigned /dev/sdb . Sometimes it's assigned /dev/sdc even though sdb is available. Why? I have scripts that expect /dev/sdb.

Quote:

Originally Posted by Firerat (Post 6033037)
Your script doesn't work, the reason you started the thread proves that.
Edit: "is broken" is a better fit than "doesn't work"


if you are going to troll, at least be smart about it.

Quote:

Originally Posted by RandomTroll (Post 6033233)
No it doesn't. My script works. Sometimes it finds /dev/sdb unavailable even though it doesn't exist. It tries /dev/sdc next.

not something you said in the OP

If you want to carry on using error prone, flaky scripts on your slackware
It won't make too much difference.

zeebra 09-05-2019 04:53 AM

Quote:

Originally Posted by Firerat (Post 6033343)
not something you said in the OP

If you want to carry on using error prone, flaky scripts on your slackware
It won't make too much difference.

If the behaviour of sdx is predictable, then there should not be an issue in writing such a script. sdx SHOULD be predictable if you use it to be predictable, and thus the script SHOULD work.

The issue here is the behavious of sdx IS NOT predictable despite using sdx in a way that it SHOULD be predictable. The question is why. I added my view and have experienced the same behaviour, and I think the conclusion of the thread is pretty much the same with "the orphan in /etc/mtab".

The more important question is, why does that happen?

My theory had to do with inproper unmount and timeout. After x time, you will be assigned sdb in either case, but factor y causes sdc "issue" before x time factor, due to cause z.

Is cause z inproper unmount? I don't know. Is there an x timeout factor? I'm not sure. z=y?

Firerat 09-05-2019 05:17 AM

I don't know what the script does

it should not expect /dev/sdb since you can not guarantee that will exist or that it will be correct.

see the OP !

The Script should either, rely on user input when running the script
Code:

./script.sh /dev/sdb
or, the UUID ( the Filesystem ) / PARTUUID ( the specific drives partition )


All times are GMT -5. The time now is 02:12 PM.