1 介绍/proc/mounts
如何利用/proc/mounts知道已经挂载上的设备呢,我们先来看看/proc/mounts都有啥东西
解释一下,第一列是设备路径,比如说/dev/sda1 第二列是挂载点(即设备挂载到的目录) 第三列是以什么文件系统挂载
2 编代码读取前3列
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int read_proc_mounts()
- {
- FILE *fp = NULL;
- int i = 0;
- int nSscanfNum = 0;
- char chDevicePath[255] = {0};
- char chMountPath[255] = {0};
- char chFsTypeName[255]= {0};
- char chBuffer[1024] = {0};
- fp = fopen("/proc/mounts","r");
- if (NULL == fp)
- {
- printf("\n read /proc/mounts error! \n");
- return -1;
- }
- while(1)
- {
- memset(chBuffer,0,sizeof(chBuffer));
- if(NULL == fgets(chBuffer,sizeof(chBuffer),fp))
- {
- break;
- }
- memset(chDevicePath,0,sizeof(chDevicePath));
- memset(chMountPath,0,sizeof(chMountPath));
- memset(chFsTypeName,0,sizeof(chFsTypeName));
- nSscanfNum = sscanf(chBuffer ,"%[^' '] %[^' '] %[^' ']",chDevicePath,chMountPath,chFsTypeName );
- if(3 != nSscanfNum)
- {
- continue;
- }
- printf("\nchDevicePath[%s],chMountPath[%s],chFstypeName[%s]\n",chDevicePath,chMountPath,chFsTypeName);
- }
- fclose(fp);
- return 0;
- }
- int main()
- {
- read_proc_mounts();
- return 0;
- }
3.运行结果
-bash-3.2$
-bash-3.2$ gcc test_proc_mounts.c
-bash-3.2$
-bash-3.2$
-bash-3.2$ ./a.out
chDevicePath[rootfs],chMountPath[/],chFstypeName[rootfs]
chDevicePath[/dev/root],chMountPath[/],chFstypeName[ext3]
chDevicePath[/dev],chMountPath[/dev],chFstypeName[tmpfs]
chDevicePath[/proc],chMountPath[/proc],chFstypeName[proc]
chDevicePath[/sys],chMountPath[/sys],chFstypeName[sysfs]
chDevicePath[/proc/bus/usb],chMountPath[/proc/bus/usb],chFstypeName[usbfs]
chDevicePath[devpts],chMountPath[/dev/pts],chFstypeName[devpts]
chDevicePath[/dev/sda6],chMountPath[/home],chFstypeName[ext3]
chDevicePath[/dev/sda5],chMountPath[/var],chFstypeName[ext3]
chDevicePath[/dev/sda1],chMountPath[/boot],chFstypeName[ext3]
chDevicePath[tmpfs],chMountPath[/dev/shm],chFstypeName[tmpfs]
chDevicePath[/dev/sdb1],chMountPath[/homesec],chFstypeName[ext4]
chDevicePath[/dev/sda6],chMountPath[/home_mount],chFstypeName[ext3]
chDevicePath[/dev/sdb1],chMountPath[/homesec_mount],chFstypeName[ext4]
chDevicePath[none],chMountPath[/proc/sys/fs/binfmt_misc],chFstypeName[binfmt_misc]
chDevicePath[sunrpc],chMountPath[/var/lib/nfs/rpc_pipefs],chFstypeName[rpc_pipefs]
chDevicePath[nfsd],chMountPath[/proc/fs/nfsd],chFstypeName[nfsd]
chDevicePath[/etc/auto.misc],chMountPath[/misc],chFstypeName[autofs]
chDevicePath[-hosts],chMountPath[/net],chFstypeName[autofs]
chDevicePath[/root],chMountPath[/root],chFstypeName[esn_cfs]
chDevicePath[/home],chMountPath[/home],chFstypeName[esn_cfs]
chDevicePath[/homesec],chMountPath[/homesec],chFstypeName[esn_cfs]
-bash-3.2$
联系客服