LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 09-04-2019, 12:40 AM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,970

Rep: Reputation: 271Reputation: 271Reputation: 271
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.
 
Old 09-04-2019, 02:07 AM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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.
 
Old 09-04-2019, 02:39 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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
 
Old 09-04-2019, 02:47 AM   #4
timl
Member
 
Registered: Jan 2009
Location: Sydney, Australia
Distribution: Fedora,CentOS
Posts: 750

Rep: Reputation: 156Reputation: 156
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
 
1 members found this post helpful.
Old 09-04-2019, 02:53 AM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by timl View Post
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.
 
Old 09-04-2019, 04:05 AM   #6
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,832
Blog Entries: 17

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
Quote:
Originally Posted by RandomTroll View Post
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 View Post
You should not use scripts that require sda sdb etc.
This is good advice regarding the script part.

Last edited by zeebra; 09-04-2019 at 04:07 AM.
 
Old 09-04-2019, 07:25 AM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by timl View Post
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?
 
Old 09-04-2019, 09:03 AM   #8
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,970

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Quote:
Originally Posted by berndbausch View Post
sda, sdb, sdc and so on are not persistent names.
Duh.

Quote:
Originally Posted by berndbausch View Post
I suggest to educate yourself about persistent naming.
That's an argument never to ask any question.

Quote:
Originally Posted by Firerat View Post
What if you have 2 usb drives...?
Then I know that and act appropriately.


Quote:
Originally Posted by Firerat View Post
/dev/disk/by-uuid
Is probably what you want
Turns out not to be the case.


Quote:
Originally Posted by timl View Post
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 View Post
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 View Post
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.
 
Old 09-04-2019, 09:14 AM   #9
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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.

Last edited by Firerat; 09-04-2019 at 09:16 AM.
 
Old 09-04-2019, 10:13 AM   #10
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
Quote:
Originally Posted by RandomTroll View Post
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.
 
Old 09-04-2019, 01:08 PM   #11
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by berndbausch View Post
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.

Last edited by rknichols; 09-04-2019 at 01:12 PM. Reason: Add: That's assuming ...
 
Old 09-04-2019, 07:05 PM   #12
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,970

Original Poster
Rep: Reputation: 271Reputation: 271Reputation: 271
Quote:
Originally Posted by Firerat View Post
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 View Post
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.
 
Old 09-05-2019, 03:06 AM   #13
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Quote:
Originally Posted by RandomTroll View Post
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 View Post
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 View Post
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.
 
Old 09-05-2019, 04:53 AM   #14
zeebra
Senior Member
 
Registered: Dec 2011
Distribution: Slackware
Posts: 1,832
Blog Entries: 17

Rep: Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638Reputation: 638
Quote:
Originally Posted by Firerat View Post
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?
 
Old 09-05-2019, 05:17 AM   #15
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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 )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to read USB1-1 device descriptor of the plugged-in device singh.shashank Linux - Embedded & Single-board computer 2 10-20-2010 01:40 AM
How to detect programatically if a USB device is is plugged-in / plugged out? franc Linux - Software 3 02-01-2007 04:01 AM
Un-plugged PS/2 mouse then plugged it into usb. mouse now dead deathman Mandriva 2 11-20-2006 07:35 AM
How does Linux decide what device in /dev to use for a piece of hardware? BrianHenderson Linux - Hardware 4 08-30-2004 04:06 PM
Why does PC hang when USB device is plugged in? DeepSPiN Fedora 1 12-03-2003 10:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 04:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration