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.
"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