LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-17-2011, 07:39 PM   #1
stympman
LQ Newbie
 
Registered: Oct 2008
Posts: 16

Rep: Reputation: 0
Associating /dev/sdX with a drive.


In Ubuntu 11.10 desktop, Disk Utility shows all of the drives that are connected and their info such as size, controller and /dev/sdX association without mounting them. Is there are command line program that can do the same. I'm at least looking for /dev/sdX mapping to some drive info like size or name without having to mount the drive. Thanks
 
Old 12-17-2011, 07:41 PM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
Code:
fdisk -l | less
 
1 members found this post helpful.
Old 12-17-2011, 09:50 PM   #3
Yesmaster
LQ Newbie
 
Registered: Dec 2011
Location: Texas
Distribution: Mint 10 now and have used many
Posts: 9

Rep: Reputation: Disabled
Try 'df'
 
Old 12-17-2011, 09:55 PM   #4
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 193Reputation: 193
FWIW df will only show mounted filesystems.

Quote:
df displays the amount of disk space available on the file system containing each file name argument. If no file name is given, the space available on all currently mounted file systems is shown.
 
Old 12-17-2011, 10:18 PM   #5
Yesmaster
LQ Newbie
 
Registered: Dec 2011
Location: Texas
Distribution: Mint 10 now and have used many
Posts: 9

Rep: Reputation: Disabled
Oops. I guess I misunderstood the question. Sorry.
 
Old 12-17-2011, 11:57 PM   #6
stympman
LQ Newbie
 
Registered: Oct 2008
Posts: 16

Original Poster
Rep: Reputation: 0
Thanks fukawi1
 
Old 12-18-2011, 01:18 AM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
What information do you want to know? It is all there, under /sys/block/device/. In Bash:
Code:
for SYS in /sys/block/sd[a-z] ; do
    SIZE=$[ ($(cat "$SYS/size" 2>/dev/null)-0)/2 ]  # kilobytes
    VENDOR="$(cat "$SYS/device/vendor" 2>/dev/null)"
    MODEL="$(cat "$SYS/device/model" 2>/dev/null)"
    SCSI="$(find "$SYS/device/scsi_disk/" -maxdepth 1 -name '*:*' -printf '%f\n' 2>/dev/null)"
    echo "Device /dev/${SYS##*/}:"
    echo "  Vendor: $VENDOR"
    echo "   Model: $MODEL"
    echo "    Size: $SIZE k"
    echo "    SCSI: $SCSI"
    echo ""
done
You can determine which partitions have been mounted from the disk. It is a bit complicated to do fully, because the partitions may participate in a software-RAID (md) and/or LVM volume.

Anyway, here is a quick script I cobbled together that lists all SCSI/SATA devices (not hardware RAID devices, but including USB storage devices), and whether they're mounted (and where):
Code:
#!/bin/bash

# Create a temporary working directory,
WORK="$(mktemp -d)" || exit $?

# and remove it when this script exits.
trap "rm -rf '$WORK'" EXIT

( # Generate list of md and dm devices,
  for SYS in /sys/block/md[0-9]* ; do
    echo -n "/dev/${SYS##*/}"
    for D in "$SYS/md/rd"[0-9]* ; do
        DEV="$(readlink "$D")"
        echo -n " /${DEV//-//}"
    done
    echo ""
  done
  for SYS in /sys/block/dm-* ; do
    echo -n "/dev/mapper/$(cat "$SYS/dm/name")"
    for D in "$SYS/slaves/"* ; do
        echo -n " /dev/${D##*/}"
    done
    echo ""
  done
) | awk '
     { for (i = 2; i <= NF; i++)
           dev[$i] = dev[$i] " " $1
     }
 END { changes = 1
       while (changes) {
           changes = 0
           for (d in dev) {
               sub(/^ +/, "", dev[d])
               val = ""
               n = split(dev[d], old)
               for (i = 1; i <= n; i++)
                   if (old[i] in dev) {
                       changes = 1
                       val = val " " dev[old[i]]
                   } else
                       val = val " " old[i]
               sub(/^ +/, "", val)
               dev[d] = val
           }
       }
       for (d in dev) {
           b = d
           sub(/[0-9]*$/, "", b)
           printf("%s: %s\n", b, dev[d])
       }
     }' > "$WORK/devices"

# Scan known SCSI/SATA block devices.
echo ""
for DEV in /sys/block/sd* ; do
    SIZE=$[ ( $(cat "$DEV/size") -0 )/2 ]   # in units of 1024 bytes
    VENDOR="$(cat "$DEV/device/vendor")"
    MODEL="$(cat "$DEV/device/model")"
    SCSI="$(find "$DEV/device/scsi_disk/" -maxdepth 1 -name '*:*:*:*' -printf '%f')"
    PARTS="/dev/${DEV##*/} $(sed -ne 's|^/dev/'"${DEV##*/}"': *\(.*\)$|\1|p' "$WORK/devices" | tr -s '\n ' '  ') "
    MOUNTS=""
    while read MDEV MROOT MTYPE MIGNORE ; do
        MDEV="${MDEV%%[0-9]}"
        MDEV="${MDEV%%[0-9]}"
        for PART in $PARTS ; do
            USED=0
            [ "$PART" = "$MDEV" ] && USED=1 && break
        done
        [ $USED -ne 0 ] && MOUNTS="$MOUNTS $MROOT"
    done < /proc/mounts
    MOUNTS="${MOUNTS# }"
    [ -z "$MOUNTS" ] && MOUNTS="(none)"

    echo "Device /dev/${DEV##*/}:"
    echo "     Size: $SIZE k ($[SIZE/1024] M) ($[SIZE/1048576].$[((SIZE*10)/1048576)%10] G)"
    echo "   Vendor: $VENDOR"
    echo "    Model: $MODEL"
    [ -n "$SCSI" ] && echo "     SCSI: $SCSI"
    echo "  Mounted: $MOUNTS"
    echo ""
done 2>/dev/null
 
1 members found this post helpful.
  


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
udev rule to rename /dev/sdx as /dev/sdr does not work :-( on OS 11.2 tkmbe SUSE / openSUSE 2 05-10-2012 01:53 AM
Force /dev/sdX? to remain? tbeehler Linux - Software 2 05-08-2008 04:29 PM
/dev/sdx question Fill Linux - Newbie 3 10-05-2007 06:31 AM
How do I change the /dev/sdx number of a drive ? Christof999 Linux - Hardware 1 06-08-2007 03:29 PM
USB Drive - Static /dev/sdx Nodes in 2.4.x? Ruler2112 Linux - Hardware 1 01-26-2007 10:20 AM

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

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