4

in my bash script I use the syntax lsblk | grep sd in order to capture all disk in my HW machines ( not include flash card or rom )

I just worry that some disk device name will be diff from sd , and I will missed these disks

is it possible ?

lsblk | grep sd
sda                8:0    0   150G  0 disk
├─sda1             8:1    0   500M  0 part /boot
└─sda2             8:2    0 149.5G  0 part
sdb                8:16   0    20G  0 disk /id/sdb
sdc                8:32   0    20G  0 disk /id/sdc
sdd                8:48   0    20G  0 disk /id/sdd
sde                8:64   0    20G  0 disk /id/sde
sdf                8:80   0    20G  0 disk /id/sdf
sdg                8:96   0    20G  0 disk
sdh                8:112  0    20G  0 disk
sdi                8:128  0    20G  0 disk
sdj                8:144  0    20G  0 disk
sdk                8:160  0    20G  0 disk
George Udosen
  • 1,807
  • 13
  • 26
yael
  • 12,598
  • 51
  • 169
  • 303

3 Answers3

4

Most disk drivers use the sd prefix, but not all. Historically sd stood for “SCSI disk”, but most disks use a protocol which is close to SCSI, and most of Linux's disk drivers use the generic sd layer plus a controller-specific part. However, this is not an obligation, so you need to check with your hardware. For example, eMMC devices have the prefix mmcblk. Some hardware RAID drivers use a different prefix. Virtual machine disks may or may not be /dev/sd* depending on the virtualization technology.

Note that sd includes removable drivers as well. For example all USB drives have the sd prefix, regardless of whether they're hard disks, USB keys, SDcard readers, etc.

Note also that grep sd is very fragile since it matches sd anywhere on the line, for example in a disk or partition label. grep '^sd' would be less fragile.

All in all, grep '^sd' does something that isn't very useful, but may happen to work for you, depending on your hardware. If you migrate your installation to different hardware, it may stop working. So you should try to find something else. What else to use depends on what you mean by “all disk (…) (not include flash card or rom)”. Flash is a disk technology, after all, and there's no reason to distinguish it from rotating disks. And it's usually a bad idea to rely on the fact that a machine is or is not virtualized. And if you start using RAID, it isn't clear whether you're interested in the underlying hardware or in the partitions that are available for software.

If you want to see only non-removable drives, look in /sys/block/* and check which ones contain 0 in the removable file. This includes “non-hardware” block devices such as RAID/LVM holders and loop devices.

Under Linux, I recommend using LVM for non-removable media. It makes administration a lot easier. If you use LVM then either pvdisplay or lvdisplay probably shows the information you're after (but of course I can't tell for sure since you didn't tell what you're after).

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
1

At least (P)ATA, SATA and USB drives show up as /dev/sdX. If you have other hardware, you may want to check how they appear. /proc/partitions should show a human readable list of all mass storage devices the system has detected.

/dev/sd* was originally just for SCSI drives, but it has expanded in scope a bit. The IDE/ATA driver used in past times named the disks /dev/hd*, but it "has driven developers insane" and the current driver has them show up like SCSI disks, as /dev/sd*.

Some hardware RAID controllers have their own naming scheme, like /dev/cciss/c0d0p1 etc. for HP RAID controllers, and disks on some virtual machines may show up as /dev/xvd* (Xen) or /dev/vd* (KVM virtio drives).

Other than that, the most common ones are probably RAM disks, /dev/ram*; floppy disks /dev/fd*; and software RAID devices (multidevice disks), /dev/md*

ilkkachu
  • 133,243
  • 15
  • 236
  • 397
  • 1
    Things are even different for some SSD. Mine is `/dev/nvme0n1` – Patrick Mevzek Dec 24 '17 at 17:59
  • @PatrickMevzek, too bad all devices/drivers don't have fixed major/minor numbers any more, I tried to browse [Documentation/admin-guide/devices.txt](https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/devices.txt) for the device names, but it doesn't even have `/dev/vd*`, or that `/dev/nvme*`. – ilkkachu Dec 24 '17 at 18:25
  • Various cases are displayed here: https://en.wikipedia.org/wiki/NVM_Express#/media/File:IO_stack_of_the_Linux_kernel.svg (From wikipedia article on NVME) – Patrick Mevzek Dec 24 '17 at 20:39
0

Only devices which are managed by the psuedo scsi system are listed as sdX. Older block devices would show up as /dev/hda, /dev/hdb etc. SD cards show up as /dev/mmcblk0, /dev/mmcblk1 etc.

DAmann
  • 1
  • 1