LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2009, 04:57 PM   #1
ahz
Member
 
Registered: Oct 2004
Posts: 58

Rep: Reputation: 15
programmatically identify removable vs fixed drives


I need to tell apart a fixed drive (IDE HDD, SATA) vs removable drive (USB, floppy, CD-ROM, DVD) vs network (NFS/CIFS).

Looking at the device is not reliable because /dev/sdb can either be a CD-ROM on IDE interface or an IDE HDD.

The file system (ext3, vfat) is a hint, but I need a more precise method.

I'm using Python but will take advice in other languages.

Last edited by ahz; 08-07-2009 at 05:04 PM.
 
Old 08-08-2009, 11:23 AM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 356

Rep: Reputation: 43
Try udevinfo as an external system program from within python.

Internal SATA:
Code:
[radar@msi-k8t ~]$ udevinfo -a -p /sys/block/sda
~
~
DRIVERS=="sata_promise"
~
~
USB Drive:
Code:
[radar@msi-k8t ~]$ udevinfo -a -p /sys/block/sde
~
~
DRIVERS=="usb-storage"
~
~
These are just examples of information you can pull out of the results
 
Old 08-15-2009, 06:25 PM   #3
ahz
Member
 
Registered: Oct 2004
Posts: 58

Original Poster
Rep: Reputation: 15
Thanks, that may be what I need. BTW, on Fedora 11 it's a little different:

Code:
$ udevinfo -a -p /sys/block/sda |grep DRIV
the program '/bin/bash' called 'udevinfo', it should use 'udevadm info <options>', this will stop working in a future release
    DRIVER==""
    DRIVERS=="sd"
    DRIVERS==""
    DRIVERS==""
    DRIVERS=="sata_via"
    DRIVERS==""

$ udevadm info -a -p /sys/block/sda | grep DRIVERS
    DRIVERS=="sd"
    DRIVERS==""
    DRIVERS==""
    DRIVERS=="sata_via"
    DRIVERS==""
 
Old 08-15-2009, 06:41 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
udevinfo -q path -n /dev/sdb1
/devices/pci0000:00/0000:00:13.5/usb1/1-9/1-9:1.0/host6/target6:0:0/6:0:0:0/block/sdb/sdb1

The "usb" in the line is a good hint that it is removable.

Also check this out:
grep '[[:digit:]]' /sys/block/sd*/removable
/sys/block/sda/removable:0
/sys/block/sdb/removable:1

By the way, Python seems a popular choice for dbus and hald helper programs.

Last edited by jschiwal; 08-15-2009 at 07:30 PM.
 
Old 08-15-2009, 08:21 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You can get a lot of information about devices from the /sys filetree. For example "cat /sys/block/<device>/removable". 0=fixed; 1=removable.

For network shares you can always read from /etc/mtab, if the matching mount line contains nfs or cifs, you know what type of share it is.

Edit: aargh. beaten to the punch again.
(I really need to figure out which script filter is blocking that "live topic" alert from running. ).

Last edited by David the H.; 08-15-2009 at 08:25 PM.
 
  


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
command to identify drives yusufs Linux - Newbie 2 09-18-2008 05:07 PM
how to format the hard disks(removable & fixed) ? himy Linux - Newbie 11 09-09-2007 06:23 PM
how to identify the bootloader programmatically sanjeevsk Linux - General 2 07-30-2007 11:18 PM
auto mount removable drives mallyooh Slackware 6 05-09-2006 09:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:07 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