D
D
dlinyj2014-05-27 16:37:23
linux
dlinyj, 2014-05-27 16:37:23

How to get a list of physical hard drives in the system and their mount locations?

I am writing a program for testing hardware, and to test hard drives, you need to get a list of hard drives and their correspondence to mount points (if any) and, of course, to a device file like
/dev/sd*
For these purposes, I used the convenient lsblk program , which, when requested, gives all they ask is:

lsblk -io KNAME,TYPE,SIZE,MODEL,MOUNTPOINT
KNAME TYPE   SIZE MODEL            MOUNTPOINT
sda   disk 465.8G WDC WD5000AZRX-0 
sda1  part 457.9G                  /
sda2  part     1K                  
sda5  part   7.9G                  [SWAP]
sr0   rom   1024M iHAS124   W

You can conveniently embed it in the program code and then parse its output and torture the corresponding hard disk (selecting the line containing disk)
FILE * fp = popen("lsblk -io KNAME,TYPE,SIZE,MODEL,MOUNTPOINT", "r");
...// проверка на ошибки
  while(fgets(buf, sizeof(buf) -1,fp)) {
    if (strstr(buf, "disk")) {
      //пошли тут тестировать
    }

The trouble came from where they did not expect. This program is not on the debian assembly on which the test will be carried out (not in turnips either). Assembly cannot be changed. Are there any alternative and easy ways to get such a list, for example via /proc or /sys?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
K
klirichek, 2014-05-27
@dlinyj

Running strace lsbkl tells it to read /sys/block and interpret it somehow.
Can you try the same, directly?
Well, or look at the source code, in the end.

I
Ilya, 2014-05-27
@Gorily

See this thread: unix.stackexchange.com/questions/90290/how-to-disp...

S
sonik_spb, 2014-05-27
@sonik_spb

fdisk -l ?

R
Rsa97, 2014-05-27
@Rsa97

df | grep /dev/sd
mount | grep /dev/sd

L
lemingg, 2014-05-28
@lemingg

df -h
not?

A
Alexey Timofeev, 2014-05-27
@alextimofeyev7

blockdev --report
Show block devices
And mount shows what is mounted and where.

A
Alexey Lesovsky, 2014-05-27
@lesovsky

lsblk has been shipped with util-linux since version 2.19. finding a deb package should not be difficult.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question