LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 05-25-2013, 11:01 PM   #1
amishera
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Rep: Reputation: Disabled
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
 
Old 05-25-2013, 11:09 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
"ls /dev/disk/by-id"
 
Old 05-25-2013, 11:10 PM   #3
Soapm
Member
 
Registered: Dec 2012
Posts: 182

Rep: Reputation: Disabled
removed... probably not the best advice.

Last edited by Soapm; 05-25-2013 at 11:12 PM.
 
Old 05-26-2013, 09:39 AM   #4
replica9000
Senior Member
 
Registered: Jul 2006
Distribution: Debian Unstable
Posts: 1,130
Blog Entries: 2

Rep: Reputation: 260Reputation: 260Reputation: 260
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

Last edited by replica9000; 05-26-2013 at 09:49 AM.
 
1 members found this post helpful.
Old 05-26-2013, 10:55 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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 View Post
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).
 
Old 05-26-2013, 02:46 PM   #6
jefro
Moderator
 
Registered: Mar 2008
Posts: 22,001

Rep: Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629Reputation: 3629
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.
 
Old 05-26-2013, 05:25 PM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Quote:
Originally Posted by unSpawn View Post
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.
 
Old 05-26-2013, 05:41 PM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by syg00 View Post
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/*
 
Old 05-26-2013, 10:29 PM   #9
amishera
LQ Newbie
 
Registered: Dec 2011
Posts: 8

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-27-2013, 12:58 AM   #10
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,140

Rep: Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123Reputation: 4123
Quote:
Originally Posted by amishera View Post
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.
 
Old 05-27-2013, 07:12 AM   #11
replica9000
Senior Member
 
Registered: Jul 2006
Distribution: Debian Unstable
Posts: 1,130
Blog Entries: 2

Rep: Reputation: 260Reputation: 260Reputation: 260
I would assume the package name for Ubuntu is the same for Debian. apt-get install smartmontools
 
Old 05-27-2013, 07:52 AM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

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


Reply

Tags
hard disks



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
not able to boot from ssd with multiple hard disks system kapsule Linux - Newbie 6 05-03-2011 05:57 AM
Using old hard disks in new system hjazz6 Linux - Hardware 3 11-10-2010 08:42 PM
Is there a list of a system's installed hard drives? ojaiguy Linux - Hardware 20 06-16-2008 01:15 PM
Hitachi SATA Hard Drive not showing in Disks List in QTParted Elim_Garak Linux - Hardware 3 03-04-2007 03:20 PM
tried to remove linux and system cant detect hard disks? twinkie General 4 01-22-2006 12:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 12:28 AM.

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