Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

How do you get a list of all harddrives, usb drives etc. in c++ on linux?

I'm writing a custom file tree widget and need to display all currently connected storage devices. With the win32 api, this is very easy, as I can iterate over all volumes with FindNextVolumeW of the file api. On linux however, this seems to be quite a bit more complicated, as you can mount any anything at any given path. However, there should be at least a way to get all storage device (no cd/floppy drives though I guess, as the partition on the physical drive would have to be mounted somewhere on another storage device) as file managers are able to display them in their menus.

I tried reading all current mounts from /proc/mounts, but even after filtering only /dev/[sdX][nvme] devices, I'm still left with non storage devices like /boot/efi, loop devices and some other stuff.

The second attempt was to read /etc/fstab, but since there are only devices that are manually mounted at a custom path and the main partition, it does not provide a full list of all currently connected storage devices (eg. flash drives are completely missing).

The third attempt was to read /dev/disk/by-id, but I'm not really sure how to filter the actually used partitions, and it seems to even have duplicates. I currently have 2 nvme m.2 drives, one sata harddisk drive and a usb flash drive connected, each having a single "real" partition, besides the normal system reserved partitions windows or linux need on the nvme drives. This is the content of the folder:

drwxr-xr-x 2 root root 480 Jan 27 23:19 .
drwxr-xr-x 8 root root 160 Jan 27 11:40 ..
lrwxrwxrwx 1 root root   9 Jan 27 11:40 ata-ST1500DL003-9VT16L_5YD5PM99 -> ../../sda
lrwxrwxrwx 1 root root  10 Jan 27 11:40 ata-ST1500DL003-9VT16L_5YD5PM99-part1 -> ../../sda1
lrwxrwxrwx 1 root root  13 Jan 27 11:40 nvme-eui.0025385271b0ce2b -> ../../nvme1n1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.0025385271b0ce2b-part1 -> ../../nvme1n1p1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.0025385271b0ce2b-part2 -> ../../nvme1n1p2
lrwxrwxrwx 1 root root  13 Jan 27 11:40 nvme-eui.002538bc01b07e00 -> ../../nvme0n1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.002538bc01b07e00-part1 -> ../../nvme0n1p1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.002538bc01b07e00-part2 -> ../../nvme0n1p2
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.002538bc01b07e00-part3 -> ../../nvme0n1p3
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-eui.002538bc01b07e00-part4 -> ../../nvme0n1p4
lrwxrwxrwx 1 root root  13 Jan 27 11:40 nvme-Samsung_SSD_960_EVO_500GB_S3EUNX0J217275R -> ../../nvme1n1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_960_EVO_500GB_S3EUNX0J217275R-part1 -> ../../nvme1n1p1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_960_EVO_500GB_S3EUNX0J217275R-part2 -> ../../nvme1n1p2
lrwxrwxrwx 1 root root  13 Jan 27 11:40 nvme-Samsung_SSD_980_PRO_1TB_S5GXNF0NC26238D -> ../../nvme0n1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_980_PRO_1TB_S5GXNF0NC26238D-part1 -> ../../nvme0n1p1
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_980_PRO_1TB_S5GXNF0NC26238D-part2 -> ../../nvme0n1p2
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_980_PRO_1TB_S5GXNF0NC26238D-part3 -> ../../nvme0n1p3
lrwxrwxrwx 1 root root  15 Jan 27 11:40 nvme-Samsung_SSD_980_PRO_1TB_S5GXNF0NC26238D-part4 -> ../../nvme0n1p4
lrwxrwxrwx 1 root root   9 Jan 27 23:19 usb-SanDisk_Ultra_0401e72b70214457f9024196cfe7310c5cd6b2abba0d130404df526e5fbf585ef0650000000000000000000057816446009b071881558107b8a62b4f-0:0 -> ../../sdb
lrwxrwxrwx 1 root root  10 Jan 27 23:19 usb-SanDisk_Ultra_0401e72b70214457f9024196cfe7310c5cd6b2abba0d130404df526e5fbf585ef0650000000000000000000057816446009b071881558107b8a62b4f-0:0-part1 -> ../../sdb1
lrwxrwxrwx 1 root root   9 Jan 27 11:40 wwn-0x5000c5003dbd1150 -> ../../sda
lrwxrwxrwx 1 root root  10 Jan 27 11:40 wwn-0x5000c5003dbd1150-part1 -> ../../sda1

The actual used partitions are sda1 (random storage drive), nvme0n1p3 (windows drive), nvme1n1p2 (linux drive) and sdb1 (flash drive). I know the distinction between "used" and "not used" is not clearly defined and can be interpreted differently depending on the user, but what I basically want is the list of devices the file manager (in my case thunar) displays in this screenshot.

list of all devices in file manager thunar

"Dateisystem" is the main linux drive, so /dev/nvme1n1p2, "Datentr?ger 1000 GB" is the main windows drive, so /dev/nvme0n1p3, "ESD-USB" is the flash drive, so /dev/sdb1. Note that /dev/sda1 is missing here, as it has a custom mount under /Files.

What would be a way to get exactly the same list of storage devices in c++?

question from:https://stackoverflow.com/questions/65928862/how-do-you-get-a-list-of-all-harddrives-usb-drives-etc-in-c-on-linux

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...