LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Linux API to find out the number of USB disks connected to the machine and the files (https://www.linuxquestions.org/questions/linux-newbie-8/linux-api-to-find-out-the-number-of-usb-disks-connected-to-the-machine-and-the-files-638631/)

poomas 04-29-2008 07:09 AM

Linux API to find out the number of USB disks connected to the machine and the files
 
Hi,

I have to write a Linux Program to write a file to USB disks connected to a machine.

Steps:
->Find out the number of USB disks connected to the machine and the filesystem/partition corresponding to the USB disks
->Mount all the USB disks
->Write the file

I have followed the following approach:

-> dmesg | grep 'removable disk'

The output will be as follows:

“Attached scsi removable disk sdb at scsi8, channel 0, id 0, lun 0”

From this take out fifth field to get the Device name(sda) of Usb disk

-> From /proc/partitions file get the corresponding filesystem name say "/dev/sdb1"

But, I got to know that “dmesg” output may sometimes have different formats(?) and its better to use any available standard Linux API.

The APIs with Libusb library are used to find out the details of USB disk such as bus-name/device-name , device-id/product-id, manufacturer etc.,
I didnt find any API to find out the Usb disk filesystem.

Is there any API or any other standard way for doing this?

Thanks,
Poomathi

fancylad 04-29-2008 07:55 AM

Is this program just for your machine, or is it supposed to be portable? I'm not a programmer so I can't help you with any libraries or functions, but I'm you want to know the filesystem on an unmounted device use either fdisk or sfdisk.

Quote:

matt@matt-laptop:~$ /sbin/fdisk -l /dev/sda

Disk /dev/sda: 1998 MB, 1998585856 bytes
16 heads, 32 sectors/track, 7624 cylinders
Units = cylinders of 512 * 512 = 262144 bytes

Device Boot Start End Blocks Id System
/dev/sda1 1 7624 1951728 e W95 FAT16 (LBA)
This will tell you the partition type which should be the same as the file system format.

sundialsvcs 04-29-2008 09:04 AM

The general approach would be to execute a suitable command (mount, perhaps?), pipe its output and grep it to extract the meaningful lines. Then pipe that output to the next stage whatever-it-is.

Or perhaps you want to read "/etc/fstab" which gives a list of all the filesystems that could be mounted.

General-purpose scripting languages like Perl, Python and PHP are commonly used for these tasks. "Shell scripting" can be used too, but with these other more-powerful tools at your beck and call I see little reason to do so.


All times are GMT -5. The time now is 10:45 AM.