Automatic mounting and unmounting of U disk in polling mode under Linux

There is a better way to automatically mount a USB flash drive under Linux. I have recorded a relatively simple and crude way.

1. Find whether there is a USB flash drive device

1.1 Check whether there is “sd” string in the /proc/diskstats file. If it exists, proceed to the next step.

1.2 Obtain the disk partition through the command fdisk -l |grep “/dev/sd”

As shown below, what is obtained is “/dev/sda”

The obtained partitions may be a, b, etc. As shown in the figure below, if two USB flash drives are inserted, there will be b, or if the USB flash drive is not unmounted after being mounted, the query will also become b if the USB flash drive is unplugged and then plugged in again.

2. Check whether the USB disk has been mounted

2.1 Check whether there is “/media/usb” (directory to be mounted) string in the /proc/mounts file

3. Mount U disk

3.1 Executed when the U disk device exists and is not mounted

First create the folder to be mounted through the command mkdir -p /media/usb. Here I mount it under /media/usb

Mount through the command mount -t auto -o iocharset=utf8,umask=0 /dev/sda1 /media/usb

Note: After obtaining the disk partition, it is usually on 1, so “/dev/sda1” is mounted.

4. Uninstall the USB disk

4.1 Executed when the U disk device does not exist and is mounted

Uninstall through the command umount /media/usb (for the above mount, you can also use umount /dev/sda1 to uninstall)

5. Specific code implementation

/********************************************** *************************************************** **
** Function name: pub_tool_excute_shell_cmd_popen
** Function description: Use popen to execute shell commands
** Input parameters: *cmd: script command
** buff_len: buffer length
** Output parameters: *buff: information returned by executing the command
** Return parameters: Return 0 to indicate successful script command execution, return -1 to indicate execution failure.
*************************************************** ************************************************/
INT32S pub_tool_excute_shell_cmd_popen(INT8S *cmd, INT8S *buff, INT32S buff_len)
{
    FILE *fp;
    INT32S len;
    
    if (NULL == (fp = popen((INT8T *)cmd,"r"))) {
        return -1;
    }

    fseek(fp, 0, SEEK_SET);

    if (NULL != buff & amp; & amp; 0 < buff_len) {
        memset(buff,0,buff_len);
        len = fread(buff, buff_len - 1, 1, fp);
    }

    len = len;
    pclose(fp);

    return 0;
}

/****************************************************** *************************************************
** Function name: check_diskstats
** Function description: Find whether there is a specified string in the diskstats file
** Input parameters: devPoint - the string to be found
** Output parameters: none
** Return parameters: -1 - failed; 0 - found
*************************************************** ************************************************/
static INT32S check_diskstats(char *devPoint)
{
    char line[1024];
    FILE *fp;
    int ret = -1;
    
    fp = fopen("/proc/diskstats", "r");
    if(fp == NULL)
        return -1;
    
    while(!feof(fp)){
        memset(line, 0, sizeof(line));
        fgets(line, 1023, fp);
        if (strlen(line) > 0){
            if (strstr(line, devPoint) != NULL){
                ret =0;
                break;
            }
        }
    }
    
    fclose(fp);
    
    return ret;
}

/****************************************************** *************************************************
** Function name: check_fdisk
** Function description: Find whether there is a specified device in the disk partition table
** Input parameters: devPoint - the string to be found
** Output parameters: devName - the name of the device
** Return parameters: -1 - failed; 0 - found
*************************************************** ************************************************/
static INT32S check_fdisk(char *devPoint, char *devName)
{
    INT8T cmd[64];
    INT8T buffer[1024];
    INT8T *p_start;
    INT8T *p_end;
    INT8T *p;
    INT8U offset;

    if(!devPoint){
        return -1;
    }

    memset(cmd,0,sizeof(cmd));
    sprintf((char*)cmd,"fdisk -l |grep "%s"",devPoint);
    memset(buffer, 0, sizeof(buffer));
    if( pub_tool_excute_shell_cmd_popen((INT8S*)cmd, (INT8S*)buffer, sizeof(buffer)) != 0 ){
        return -1;
    }
    offset = 0;
    p_start = buffer;
    while(1){
        if(strlen(p_start)>0){
            p = strstr(p_start + offset, "Disk");
            if (p != NULL){
                p_start = p;
                offset = 1;
                continue;
            }
            p = p_start;
            p_start = strstr(p, " ");
            p_start + + ;
            p_end = strstr(p_start, ":");
            if ((p_end != NULL) & amp; & amp;(p_start != NULL)){
                memcpy(devName, p_start, p_end - p_start);
                devName[p_end - p_start] = 0;
            }
            
            if (access(devName, F_OK) == 0){
                return 0;
            }
            break;
        }else{
            break;
        }
    }
return -1;
}


/****************************************************** *************************************************
** Function name: check_mount
** Function description: Find whether the device has been mounted
** Input parameters: devPoint - device name
** Output parameters: None
** Return parameters:-1 - not mounted; 0 - mounted;
*************************************************** ************************************************/
static INT32S check_mount(char *devPoint)
{
    char line[1024];
    FILE *fp;
    int ret =-1;
    
    fp = fopen("/proc/mounts", "r");
    if(fp == NULL)
        return -1;
    
    while(!feof(fp)){
        memset(line, 0, sizeof(line));
        fgets(line, 1023, fp);
        if (strlen(line) > 0){
            if (strstr(line, devPoint) != NULL){
                ret =0;
                break;
            }
        }
    }
    fclose(fp);
    
    return ret;
}

/****************************************************** *************************************************
** Function name: usb_flash_disk_check
** Function description: U disk detection
** Input parameters: none
** Output parameters: None
** Return parameters:
*************************************************** ************************************************/
static void usb_flash_disk_check(void)
{
    char devName[100];
    INT8T cmd[100];
    int checkDev = -1; /*Whether there is a device*/
    int checkDevMount = -1; /*Whether the device is mounted*/

    memset(devName, 0, sizeof(devName));
    if (check_diskstats("sd") == 0){
        checkDev = check_fdisk("/dev/sd", devName);
    }
    checkDevMount = check_mount("/media/usb");
    if ((checkDev == 0) & amp; & amp;(checkDevMount == -1)){
        /*Mount U disk*/
        memset(cmd, 0, sizeof(cmd));
        sprintf(cmd, "umount %s1", devName);
        pub_tool_excute_shell_cmd((INT8S*)cmd);
        
        memset(cmd, 0, sizeof(cmd));
        pub_tool_excute_shell_cmd((INT8S*)"mkdir -p /media/usb");
        sprintf(cmd, "mount -t auto -o iocharset=utf8,umask=0 %s1 /media/usb", devName);
        pub_tool_excute_shell_cmd((INT8S*)cmd);
        
    }else if ((checkDev == -1) & amp; & amp;(checkDevMount == 0)){
        /*Uninstall U disk*/
        pub_tool_excute_shell_cmd((INT8S*)"umount /media/usb");
    }
}