Free Hosting Online for WorkStations

< Previous | Contents | Next >

Determining Device Names

It's sometimes difficult to determine the name of a device. Back in the old days, it wasn't very hard. A device was always in the same place and it didn't change. Unix-like systems like it that way. Back when Unix was developed, “changing a disk drive” involved using a forklift to remove a washing machine-sized device from the computer room. In recent years, the typical desktop hardware configuration has become quite dynamic and Linux has evolved to become more flexible than its ancestors.

In the examples above we took advantage of the modern Linux desktop's ability to “au- tomagically” mount the device and then determine the name after the fact. But what if we are managing a server or some other environment where this does not occur? How can we figure it out?

First, let's look at how the system names devices. If we list the contents of the /dev di- rectory (where all devices live), we can see that there are lots and lots of devices:


[me@linuxbox ~]$ ls /dev

[me@linuxbox ~]$ ls /dev


The contents of this listing reveal some patterns of device naming. Here are a few:


Table 15-2: Linux Storage Device Names


Pattern Device

Pattern Device

/dev/fd* Floppy disk drives.


/dev/hd* IDE (PATA) disks on older systems. Typical motherboards contain two IDE connectors or channels, each with a cable with two attachment points for drives. The first drive on the cable is called the master device and the second is called the slave device. The device names are ordered such that /dev/hda refers to the master device on the first channel, /dev/hdb is the slave device on the first channel; /dev/hdc, the master device on the second channel, and so on. A trailing digit indicates the partition number on the device. For example, /dev/hda1 refers to the first partition on the first hard drive on the system while

/dev/hda refers to the entire drive.


/dev/lp* Printers.


/dev/sd* SCSI disks. On modern Linux systems, the kernel treats all disk- like devices (including PATA/SATA hard disks, flash drives, and USB mass storage devices such as portable music players, and digital cameras) as SCSI disks. The rest of the naming system is similar to the older /dev/hd* naming scheme described above.


/dev/sr* Optical drives (CD/DVD readers and burners).



In addition, we often see symbolic links such as /dev/cdrom, /dev/dvd, and

/dev/floppy, which point to the actual device files, provided as a convenience.

If you are working on a system that does not automatically mount removable devices, you can use the following technique to determine how the removable device is named when it is attached. First, start a real-time view of the /var/log/messages or

/var/log/syslog file (you may require superuser privileges for this):


[me@linuxbox ~]$ sudo tail -f /var/log/messages

[me@linuxbox ~]$ sudo tail -f /var/log/messages


The last few lines of the file will be displayed and then pause. Next, plug in the remov- able device. In this example, we will use a 16 MB flash drive. Almost immediately, the kernel will notice the device and probe it:



Jul 23 10:07:53 linuxbox kernel: usb 3-2: new full speed USB device using uhci_hcd and address 2

Jul 23 10:07:53 linuxbox kernel: usb 3-2: configuration #1 chosen

from 1 choice

Jul 23 10:07:53 linuxbox kernel: scsi3 : SCSI emulation for USB Mass Storage devices

Jul 23 10:07:58 linuxbox kernel: scsi scan: INQUIRY result too short (5), using 36

Jul 23 10:07:58 linuxbox kernel: scsi 3:0:0:0: Direct-Access Easy

Disk 1.00 PQ: 0 ANSI: 2

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] 31263 512-byte hardware sectors (16 MB)

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Write Protect is off

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Assuming drive cache: write through

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] 31263 512-byte hardware sectors (16 MB)

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Write Protect is off

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Assuming drive cache: write through

Jul 23 10:07:59 linuxbox kernel: sdb: sdb1

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: Attached scsi generic sg3 type 0

Jul 23 10:07:53 linuxbox kernel: usb 3-2: new full speed USB device using uhci_hcd and address 2

Jul 23 10:07:53 linuxbox kernel: usb 3-2: configuration #1 chosen

from 1 choice

Jul 23 10:07:53 linuxbox kernel: scsi3 : SCSI emulation for USB Mass Storage devices

Jul 23 10:07:58 linuxbox kernel: scsi scan: INQUIRY result too short (5), using 36

Jul 23 10:07:58 linuxbox kernel: scsi 3:0:0:0: Direct-Access Easy

Disk 1.00 PQ: 0 ANSI: 2

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] 31263 512-byte hardware sectors (16 MB)

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Write Protect is off

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Assuming drive cache: write through

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] 31263 512-byte hardware sectors (16 MB)

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Write Protect is off

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Assuming drive cache: write through

Jul 23 10:07:59 linuxbox kernel: sdb: sdb1

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: Attached scsi generic sg3 type 0


After the display pauses again, press Ctrl-c to get the prompt back. The interesting parts of the output are the repeated references to “[sdb]” which matches our expectation of a SCSI disk device name. Knowing this, two lines become particularly illuminating:



Jul 23 10:07:59 linuxbox kernel: sdb: sdb1

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk

Jul 23 10:07:59 linuxbox kernel: sdb: sdb1

Jul 23 10:07:59 linuxbox kernel: sd 3:0:0:0: [sdb] Attached SCSI removable disk


This tells us the device name is /dev/sdb for the entire device and /dev/sdb1 for the first partition on the device. As we have seen, working with Linux is full of interest- ing detective work!


Tip: Using the tail -f /var/log/messages technique is a great way to watch what the system is doing in near real-time.


With our device name in hand, we can now mount the flash drive:


[me@linuxbox ~]$ sudo mkdir /mnt/flash [me@linuxbox ~]$ sudo mount /dev/sdb1 /mnt/flash [me@linuxbox ~]$ df


Filesystem

1K-blocks

Used

Available

Use%

Mounted on

/dev/sda2

15115452

5186944

9775164

35%

/

/dev/sda5

59631908

31777376

24776480

57%

/home

/dev/sda1

147764

17277

122858

13%

/boot

tmpfs

776808

0

776808

0%

/dev/shm

/dev/sdb1

15560

0

15560

0%

/mnt/flash


The device name will remain the same as long as it remains physically attached to the computer and the computer is not rebooted.


Top OS Cloud Computing at OnWorks: