LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to get device from my HDD serial ? (https://www.linuxquestions.org/questions/linux-software-2/how-to-get-device-from-my-hdd-serial-905427/)

pmorin 09-28-2011 09:52 AM

How to get device from my HDD serial ?
 
Hi !

I'm creating a script that will make a backup of my server files directly by plugging my external HDD on my server.

I found out how to run a script when I plug my HDD on my server (here) and now I want to do this with my scripts :
1. mount all the partitions of the external HDD
2. make a backup (that is already OK)
3. unmount all the partitions of the external HDD (I just need to run some "umount" to do this, so that should not be a problem)
4. eject the external HDD (safely remove hardware) : I found a script to do this here, but I need to get the device

So I have problems for points 1. and 4. :
1. I can't use "mount -a" : I don't know why, but it's not working
4. I don't have the device name (in my case, now, it's /dev/sdc, but it could be something else)

In both cases, I need to find what is the device name of my external HDD. I know the name of the partitions of the HDD, and its serial.
How can I find the device from the HDD serial ?

I have some leads, like using "sudo lshw -short", but I'm not an expert of regex (to parse its output).

Can somebody help me, please ?

Thanks !
Pierre

theNbomr 09-28-2011 02:07 PM

The mount command can only mount partitions if run with root privileges.
UUIDs and labels are good for unambiguously identifying partitions.
See a LQ thread on the subject for a good jumping off point on the subject.
--- rod.

pmorin 09-29-2011 02:52 AM

Thank you for your help.

For root privileges, that's not a problem.
For UUIDs of partitions, that could be good, but I would prefer using HDD serial : I need the device name of the HDD, not its partitions ones.

But you gave me an idea : I could try adding the partitions to fstab.

Anyway, I found another method to get the device name of my HDD : using "fdisk -l" with "grep" and ["sed"/"awk"/something else that use regex].
I know that my HDD has a capacity of 2000398934016 Bytes, so I could us this :
Quote:

sudo fdisk -l | grep -Hn "2000398934016" | sed -r 'XXXXX'
I don't know what to replace "XXXXX" with. Can anybody help, please ?

Thanks !

pmorin 09-29-2011 02:59 AM

I found this, but there may be better ways to do :
Quote:

sudo fdisk -l | grep "2000398934016" | sed -r 's/:.*//' | sed -r 's/.*\s//'

theNbomr 09-29-2011 10:02 AM

In order to read the serial number, you need to know the block device name, which puts you right back to the same problem.
--- rod.

pmorin 09-29-2011 10:06 AM

Thank you for your response.

I don't need to read the serial number : I already have it and it wouldn't change, so I can "hard-code" it directly in my script.


All times are GMT -5. The time now is 05:48 PM.