LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   How to list the hard disks in the system? (https://www.linuxquestions.org/questions/linux-hardware-18/how-to-list-the-hard-disks-in-the-system-4175463455/)

amishera 05-25-2013 11:01 PM

How to list the hard disks in the system?
 
Hi,

I need to write a script that will list all the hard disks in the system. The output of the script will be fed into another script. Hence the output has to be machine readable.

I have tried

lshw -class disk -short

which shows both the usb and the disk

I have also tried

ls /dev/{hs}d?

but again it shows both the hard disk and usb drive.

Hence the problem is to get rid of usb from the output. The usb will always be there as the scripts will be run from a live usb.

I tried google search but none of the solutions seem to address my problem.

So please help and would appreciate greatly.

Thanks

syg00 05-25-2013 11:09 PM

"ls /dev/disk/by-id"

Soapm 05-25-2013 11:10 PM

removed... probably not the best advice.

replica9000 05-26-2013 09:39 AM

Is the script being run as root? And does it need to output physical disks or mounted/unmounted partitions?

This might help. /dev/sd[a-d] are internal SATA disk, /dev/sdi is a USB HDD, and I have a USB flash drive that doesn't show in this list.

Code:

$ /usr/sbin/smartctl --scan
/dev/sda -d scsi # /dev/sda, SCSI device
/dev/sdb -d scsi # /dev/sdb, SCSI device
/dev/sdc -d scsi # /dev/sdc, SCSI device
/dev/sdd -d scsi # /dev/sdd, SCSI device
/dev/sdi -d sat # /dev/sdi [SAT], ATA device


unSpawn 05-26-2013 10:55 AM

Unfortunately "ls /dev/disk/by-id" won't work because USB drives use SCSI emulation so they're marked as /dev/sd* devices as well. That's also the reason the OP's
Code:

lshw -class disk -short
doesn't work (and no,
Code:

lshw -disable usb -class disk -short
doesn't work either). I'd say
Quote:

Originally Posted by replica9000 (Post 4959217)
Code:

/usr/sbin/smartctl --scan

would be a better option compared to mine, even though what I came up with can print only hard disks
Code:

#!/bin/sh --
for UDI in $(/usr/bin/hal-find-by-capability --capability storage); do
 DEVICE=$(hal-get-property --udi $UDI --key block.device)
 [[ $(hal-get-property --udi $UDI --key storage.bus) != "usb" ]] && printf "${DEVICE}\n"
done; exit 0

but this requires udev / HAL (or finding a way to achieve the same with DeviceKit).

jefro 05-26-2013 02:46 PM

Use lsusb to subtract from list.


It took many years to get a usb to act like a scsi to linux. It is a feature that has proved very useful.

syg00 05-26-2013 05:25 PM

Quote:

Originally Posted by unSpawn (Post 4959258)
Unfortunately "ls /dev/disk/by-id" won't work because USB drives use SCSI emulation so they're marked as /dev/sd* devices as well.

Hmmm - interesting.
I did a quick check prior to posting with a USB key, and it showed as "usb-...".
Out of curiosity I just tested a USB hard drive and it did likewise.

Fedora 17.

unSpawn 05-26-2013 05:41 PM

Quote:

Originally Posted by syg00 (Post 4959453)
Hmmm - interesting.
I did a quick check prior to posting with a USB key, and it showed as "usb-...".

Uh. You're right. I must have been thinking of one of the other /dev/disk/by-*/...
Code:

env GLOBIGNORE=*usb-* /bin/ls -1 /dev/disk/by-id/*

amishera 05-26-2013 10:29 PM

Thanks guys for the quick replies. The output of

ls /dev/{hs}d?

was different in case of virtual machine:

ata-VMware_Virtual_IDE_CDROM_Drive_10000000000000000001
dm-name-vgbip-volbip1
dm-uuid-LVM-fqAYJKNxQJeTW8OBsfxoJ5k215KBRd9W83of4hByz2cmz7UnKjUDdc6ZWBYoB7X5

Hence I guess that command has limitations.

ls /dev/disks/by-id

shows

dm-....-LVM

kind of entries while run on a RHEL virtual machine.

The smartctl command seems to work fine in opensuse virtual machine but I could not download it for ubuntu using apt-get. But anyhow I can try to find a suitable package for ubuntu.

Is there any other solution better than smartctl using standard commands generally available in all distros (like ls, find, etc)?

Thanks

syg00 05-27-2013 12:58 AM

Quote:

Originally Posted by amishera (Post 4959001)
I need to write a script that will list all the hard disks in the system.

We (I at least) took you at your word - I don't see any mention of virtual in your initial post.
And LVM on top of that apparently - which adds another block-device layer on top of all the other software layers.

The commands are providing you with what you asked for - and what the hipervisor is designed to allow the guest(s) to see. Not a limitation in the command IMHO.

replica9000 05-27-2013 07:12 AM

I would assume the package name for Ubuntu is the same for Debian. apt-get install smartmontools

schneidz 05-27-2013 07:52 AM

arent all hard disks (internal/external/usb/ide/sata/camera/phone/mp3 player/...) treated the same. i think fdisk -l should give you what you want and you can grep out the content you dont want.

i am pretty sure îf you are running a live-usb then /dev/sda will be the usb.


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