Find the answer to your Linux question:
Results 1 to 7 of 7
How can I detect if there are USB sticks on the machine and how is it possible to mount them? I did use a libusb for detection of MASS_STORAGE_DEVICE but ...
  1. #1
    Just Joined!
    Join Date
    Oct 2006
    Posts
    6

    detect and mount usb stick with C?

    How can I detect if there are USB sticks on the machine and how is it possible to mount them?
    I did use a libusb for detection of MASS_STORAGE_DEVICE but it can also be a DVD-Player...not only USB stick... and I still have no idea how to get data from the USB stick??

    Please, help!
    Thanks in advance.

  2. #2
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    dmesg -> after plugin
    lsusb -> after plugin
    cat /etc/fstab
    mount /dev/usb -> may be called differently, like /mnt/memory or /media/memory or ...

    My system would work if I type: mount /dev/sda1
    Can't tell an OS by it's GUI

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Well, I'm not sure how to go about identifying a USB stick with libusb, unfortunately. Maybe someone else will have thoughts on that.

    Anyway, once you have identified it, you can use the mount() function. Check "man 2 mount" for the documentation on it. Once it's mounted, you can access the data just as you would any file.
    DISTRO=Arch
    Registered Linux User #388732

  4. #4
    Just Joined!
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by Cabhan View Post
    Well, I'm not sure how to go about identifying a USB stick with libusb, unfortunately.
    This is the problem I have no idea how to get information if my usb stick is sda, sda1, sdb, sdd...or something else... and I don't know which filesystem on it...

    If I could get this information, I could surely mount it using mount().

    I can only get info if there is some MASS_STORAGE_DEVICE on the machine, but it can also be a DVD/CDROM or HARDDRIVE...and it still doesn't give me any info about /dev/sd*...so I'm sitll unable to mount it...

  5. #5
    Linux Engineer Freston's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    1,047
    Quote Originally Posted by charmik24
    I can only get info if there is some MASS_STORAGE_DEVICE on the machine, but it can also be a DVD/CDROM or HARDDRIVE...and it still doesn't give me any info about /dev/sd*...so I'm sitll unable to mount it...
    You'll find mounting is very easy and can't go wrong. But you'll need to combine the output from the commands I gave you.

    Some commands need rot:
    <dmesg> gives the kernel response to the plugging in of a new device. The kernel assigns it with an address.
    <lsusb> gives a list of all plugged in USB devices, mounted or not (optional to the process).
    <cat /etc/fstab> assigns who may mount where. Therefore contains useful information on where certain devices are expected.
    And mount of course. To do the valuable stuff.
    Can't tell an OS by it's GUI

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Freston, the problem is that he's trying to do this through C, not Bash.

    However, Freston does give me an idea. Assuming that you have a USB stick mount point in your /etc/fstab and that you know the block name or mount point name, you can actually search your fstab and check for the existence of that device with libusb (or just mount it straightaway). Check "man getfsent" for the documentation of these functions.
    DISTRO=Arch
    Registered Linux User #388732

  7. #7
    Just Joined!
    Join Date
    Oct 2006
    Posts
    6
    Quote Originally Posted by Cabhan View Post
    However, Freston does give me an idea. Assuming that you have a USB stick mount point in your /etc/fstab and that you know the block name..
    Thanks for answers, but it doesn't solve my problem. The problem is - I don't know the block name of the device!
    Any ideas how can I find it out?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...