USB drive status detection under Linux

Source: Internet
Author: User

Linux file system is asynchronous, that is, write a file is not immediately saved to the media (hard disk, USB stick, etc.), but into the buffer, and so accumulate to a certain extent and then saved to the media. If there is no umount to illegally unplug the U disk, the program is not known, fopen,fwrite and other functions are still returned to the correct, know that the operating system to write media, will prompt I/O error. However, a lot of data will be lost due to this not-so-timely error report.
In fact, the USB driver has made changes to the system configuration file when a USB drive is plugged in and unplugged.
For example, the USB drive driver will be inserted or unplugged in the/proc/scsi/usb-storage-0/0 to remember Attached:yes or No
By looking at this file it is not difficult to detect whether the USB stick is plugged in or unplugged.

Note: Whether the USB drive is plugged in is not related to mount (Mount).

Of course this is not the only way to detect the state of the U-disk. Depending on the settings of the kernel, there may be no/PROC/SCSI directory, but the/sys directory will have a USB directory, plug the U-disk, the directory will be automatically created and disappear, if the above method is not feasible, then you can detect these directories to determine the existence of the U-disk or SD card storage device status,/ sys/directory found in the relevant USB or SD card, there is more detailed device information, including the device factory ID, transmission speed, capacity and so on.

USB drive status detection under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.