int mount_main(int argc, char argv) MAIN_EXTERNALLY_VISIBLE; int mount_main(int argc UNUSED_PARAM, char argv) { char *cmdopts = xzalloc(1); char *fstype = NULL; char *O_optmatch = NULL; char *storage_path; llist_t *lst_o = NULL; const char *fstabname = "/etc/fstab"; FILE *fstab; int i, j; int rc = EXIT_SUCCESS; unsigned long cmdopt_flags; unsigned opt; struct mntent mtpair[2], *mtcur = mtpair; ... for (i = j = 1; argv[i]; i++) { if (argv[i][0] == '-' && argv[i][1] == '-') append_mount_options(&cmdopts, argv[i] + 2); else argv[j++] = argv[i]; } argv[j] = NULL; ... // Past this point, we are handling either "mount -a [opts]" // or "mount [opts] single_param" cmdopt_flags = parse_mount_options(cmdopts, NULL); if (nonroot && (cmdopt_flags & ~MS_SILENT)) // Non-root users cannot specify flags bb_error_msg_and_die(bb_msg_you_must_be_root); ... fstab = setmntent(fstabname, "r");------------------------------------------------fstabname默认指向/etc/fstab,打开fstab文件。 if (!fstab) bb_perror_msg_and_die("can't read '%s'", fstabname); // Loop through entries until we find what we're looking for memset(mtpair, 0, sizeof(mtpair)); for (;;) { struct mntent *mtother = (mtcur==mtpair ? mtpair+1 : mtpair); // Get next fstab entry if (!getmntent_r(fstab, mtcur, getmntent_buf----------------------------------从fstab中读取一条挂载信息放入mtcur中。 + (mtcur==mtpair ? GETMNTENT_BUFSIZE/2 : 0), GETMNTENT_BUFSIZE/2) ) { // End of fstab/mtab is reached mtcur = mtother; // the thing we found last time break; } if (argv[0]) { // Is this what we're looking for? if (strcmp(argv[0], mtcur->mnt_fsname) != 0 && strcmp(storage_path, mtcur->mnt_fsname) != 0 && strcmp(argv[0], mtcur->mnt_dir) != 0 && strcmp(storage_path, mtcur->mnt_dir) != 0 ) { continue; // no } mtcur = mtother; // If we're mounting all } else {-----------------------------------------------------------------------mount -a相关路径。 struct mntent *mp; // No, mount -a won't mount anything, // even user mounts, for mere humans if (nonroot) bb_error_msg_and_die(bb_msg_you_must_be_root); // Does type match? (NULL matches always) if (!fstype_matches(mtcur->mnt_type, fstype)) continue; // Skip noauto and swap anyway if ((parse_mount_options(mtcur->mnt_opts, NULL) & (MOUNT_NOAUTO | MOUNT_SWAP)) // swap is bogus "fstype", parse_mount_options can't check fstypes || strcasecmp(mtcur->mnt_type, "swap") == 0 ) { continue; } // Does (at least one) option match? // (NULL matches always) if (!match_opt(mtcur->mnt_opts, O_optmatch)) continue; resolve_mount_spec(&mtcur->mnt_fsname); // NFS mounts want this to be xrealloc-able mtcur->mnt_opts = xstrdup(mtcur->mnt_opts); mp = find_mount_point(mtcur->mnt_dir, /*subdir_too:*/ 0);------------------检查mtcur->mnt_dir目录是否已经被mount。 if (mp) { if (verbose) { bb_error_msg("according to %s, " "%s is already mounted on %s", bb_path_mtab_file, mp->mnt_fsname, mp->mnt_dir); } } else { // ...mount this thing if (singlemount(mtcur, /*ignore_busy:*/ 1)) {--------------------------执行mount操作。 // Count number of failed mounts rc++; } } free(mtcur->mnt_opts); } } ... //ret: if (ENABLE_FEATURE_CLEAN_UP) endmntent(fstab); if (ENABLE_FEATURE_CLEAN_UP) { free(storage_path); free(cmdopts); } //TODO: exitcode should be ORed mask of (from "man mount"): // 0 success // 1 incorrect invocation or permissions // 2 system error (out of memory, cannot fork, no more loop devices) // 4 internal mount bug or missing nfs support in mount // 8 user interrupt //16 problems writing or locking /etc/mtab //32 mount failure //64 some mount succeeded return rc; }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/11305.html