fstab是什么?被谁用?怎么写?
关键词:fstab、mount -a、fsck等等。
1. fstab是干什么的?
fstab是file system table的意思,即文件系统表。
它在开机的时候告诉系统挂载哪些分区、挂载点是什么、以什么格式挂载、挂载的选项等等。
然后系统在开机的时候就根据fstab内容,执行挂载操作。在挂载完之后,就可以在文件系统上进行操作。
将需要挂载的分区放入fstab之后,就不需要手动挂载。
2. fstab被谁调用?
init进程在启动的时候会解析/etc/inittab文件,并执行其中选项。
# /etc/inittab
# Format for each entry: <id>:<runlevels>:<action>:<process>
#
# id == tty to run on, or empty for /dev/console
# runlevels == ignored
# action == one of sysinit, respawn, askfirst, wait, and once
# process == program to run
...
::sysinit:/bin/mount -a
...
::shutdown:/bin/umount -a -r
其中mount -a执行Mount all filesystems in fstab,mount命令会解析fstab中内容,根据fstab配置执行相应关在操作。
参考文档:《busybox启动流程简单解析:从init到shell login》。
3. fstab如何生效?
fstab是被mount命令解析,然后根据解析内容执行操作。
mount命令将fstab中配置解析成一个struct mntent实例:
struct mntent {
char *mnt_fsname;---------------/* 挂载分区名称*/
char *mnt_dir;------------------/* 挂载点*/
char *mnt_type;-----------------/* 文件系统类型:ufs、nfs 等*/
char *mnt_opts;-----------------/* 选项,以逗号为分隔符*/
int mnt_freq;-------------------/* Dump 的频率(以天为单位)*/
int mnt_passno;-----------------/* fsck检查的次序*/
};
操作struct mntent函数包括:
setmntent():是打开包含挂载点项目的文件, 其中的 filename 参数是要打开的文件名, type 参数就像 fopen() 的第二个参数, 代表只读、只写, 或读写皆可的存取模式 。返回FILE*。
getmntent():则是循序读取整个档案,传回指向 static struct mntent 结构的指针,结构中会填入适当的值。
addmntent():可以在已开启档案的末端加上资讯,它原本是给 mount 使用的。
endmntent():的功用是关闭打开的文件。这不能只是呼叫 fclose() 而已,因为可能还有其它与FILE * 有关的内部资料结构需要清理。
这里主要分析mount -a的操作。
int mount_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mount_main(int argc UNUSED_PARAM, char **argv)
{
char *cmdopts = xzalloc();
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[], *mtcur = mtpair;
...
for (i = j = ; argv[i]; i++) {
if (argv[i][] == '-' && argv[i][] == '-')
append_mount_options(&cmdopts, argv[i] + );
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, , sizeof(mtpair));
for (;;) {
struct mntent *mtother = (mtcur==mtpair ? mtpair+ : mtpair); // Get next fstab entry
if (!getmntent_r(fstab, mtcur, getmntent_buf----------------------------------从fstab中读取一条挂载信息放入mtcur中。
+ (mtcur==mtpair ? GETMNTENT_BUFSIZE/ : ),
GETMNTENT_BUFSIZE/)
) { // End of fstab/mtab is reached
mtcur = mtother; // the thing we found last time
break;
}
if (argv[]) { // Is this what we're looking for?
if (strcmp(argv[], mtcur->mnt_fsname) !=
&& strcmp(storage_path, mtcur->mnt_fsname) !=
&& strcmp(argv[], mtcur->mnt_dir) !=
&& strcmp(storage_path, mtcur->mnt_dir) !=
) {
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") ==
) {
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:*/ );------------------检查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:*/ )) {--------------------------执行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;
}
singlemount()根据从fstab中解析的struct mntent,进行实际的mount操作。
大部分类型的文件系统通过mount_it_now()进行挂载。
4. fstab如何编写?
fstab文件一行表示一个挂载操作,包含6列信息:<file system>、<mount pt>、<type>、<options>、<dump>、<pass>。
# <file system> <mount pt> <type> <options> <dump> <pass>
/dev/root / ext2 rw,noauto
proc /proc proc defaults
<file system>:表示磁盘文件、设备的Label或者UUID。设备的Label和UUID都可以通过dumpe2fs -h /dev/xxxx查看。使用UUID可以唯一标识一个分区,不会因为磁盘插槽顺序改变错误挂载。
<mount pt>:是设备挂载点,就是设备要挂载到哪个目录下。
<type>:待挂载的文件系统格式。
<options>:挂载时的选项。
| Async/sync | 设置是否为同步方式运行,默认为async |
| auto/noauto | 当下载mount -a 的命令时,此文件系统是否被主动挂载。默认为auto |
| rw/ro | 是否以以只读或者读写模式挂载 |
| exec/noexec | 限制此文件系统内是否能够进行”执行”的操作 |
| user/nouser | 是否允许用户使用mount命令挂载 |
| suid/nosuid | 是否允许SUID的存在 |
| Usrquota | 启动文件系统支持磁盘配额模式 |
| Grpquota | 启动文件系统对群组磁盘配额模式的支持 |
| Defaults | 同时具有rw,suid,dev,exec,auto,nouser,async等默认参数的设置 |
<dump>:是否被dump命令使用:
| 0 | 代表不要做dump备份 |
| 1 | 代表要每天进行dump的操作 |
| 2 | 代表不定日期的进行dump操作 |
<pass>:标识是否检验扇区,在开机过程中,系统默认会以fsck检查系统是否完整。
| 0 | 不要检验 |
| 1 | 最早检验(一般根目录会选择) |
| 2 | 1级别检验完成之后进行检验 |
5. 小结
fstab被mount -a命令解析,然后针对不同文件系统类型将设备挂载到指定目录。
如果需要将某一个设备在开机是挂在到系统中,需要通过编写fstab即可,共6列需要配置。
修改fstab之后,通过mount -a立即生效,或者通过重启生效。
参考文档:《A complete fstab guide》、《Linux命令-自动挂载文件/etc/fstab功能详解[转]》。
fstab是什么?被谁用?怎么写?的更多相关文章
- Linux文件系统管理 开机自动挂载及fstab文件修复
概述 开机自动挂载及fstab文件修复 开机自动挂载 实现开机后自动挂载,就需要修改系统的自动挂载文件 /etc/fstab.因为系统就是依赖这个文件决定启动时加载的文件系统的.通过vi 打开/etc ...
- 【转】RHCE 7系列—RHCE考试
本篇主要以RHCE练习题为线索,介绍其中涉及的知识点. 红色引用的字为题目要求(不是正式题目,难度略低于正式题目) In serverX or desktopX 1. (lab teambridge ...
- <实训|第十三天>linux中ACL权限控制以及磁盘配额,附编译属于自己的linux内核
[root@localhost~]#序言 首先讲讲昨天关于缩容失败,开不机的解决方法:ACL权限也算是一个很重要的知识点,不难,但是很实用:磁盘配额一般不需要自己弄,但是要懂得原理.剩下的就是编译属于 ...
- 迁移/home目录至新硬盘分区总结--无备份情况下
搞了一天,终于成功迁移.由于一开始就没备份过程实在很曲折. 希望本篇对那些没有备份习惯的朋友们有所帮助. 准备工作: sudo vim /etc/fstab 在文件中加入: /dev/sdb8 ...
- Linux学习 -- 文件系统管理
1 分区和文件系统 分区类型 主分区:<= 4个 扩展分区:只能有一个,也算主分区的一种 不能存储数据和格式化,只能用来包含逻辑分区 逻辑分区:扩展分区中划分的 IDE--最多59个 ...
- RHCSA考试真题
2018年 RHCSA考试真题... ------------ 考前需要做的基础 破解root密码 KVM虚拟机与VM虚拟机 主机名:station.domain1.example.comIP地址:1 ...
- 二、RHCSA试题解析
一.设置YUM仓库 YUM的软件库源地址为:http://content.example.com/rhel7.0/x86_64/dvd,将此配置为操作系统的默认软件仓库. 方法一(修改配置文件): v ...
- 磁盘格式化/磁盘挂载/手动增加swap空间
4.5/4.6 磁盘格式化 4.7/4.8 磁盘挂载 4.9 手动增加swap空间 磁盘格式化 查看centos7支持的文件系统格式 cat /etc/filesystem,centos7默认的文件 ...
- RHCSA和RHCE
RHCSA: 红帽考试流程: RHCE(上下午) 上午:RHCSA(红帽认证系统管理员)下午:RHCE (红帽认证系统工程师) 上午考试通过,下午未通过(RHCSA)上午考试未通过,下午考试通过(没有 ...
- Android 4.4系统获取root权限的方法
1. 准备工作: 准备一台ubuntu机器,将boot.img复制到该机器上,下载必要的工具sudo apt-get install abootimggit clone https://github. ...
随机推荐
- springboot(二):bootstrap和application有什么区别?
“SpringBoot专注于快速方便的开发单个个体微服务. SpringCloud是关注全局的微服务协调整理治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来, 为各个服务之间提供 ...
- C#中的时间戳
来源:https://blog.guoqianfan.com/2019/11/24/timestamp-in-csharp/ 什么是时间戳 时间戳默认是Unix时间戳. 首先要清楚JavaScript ...
- MySQL修改数据库时区
--查看数据库时区设置mysql> show variables like "%time_zone%"; +------------------+--------+ | Va ...
- c#中的跳转语句
break:跳出循环,执行循环外的语句:continue:跳出此次循环,进入下一次循环: goto:不建议使用 return:终止它所在的方法的执行,并将控制权返回给调用方法.
- 012.MongoDB读写分离
一 读写分离概述 1.1 读写分离描述 从应用程序角度来看,使用Replica Set 和使用单台mongo很像.默认的驱动程序会连接primary节点,并且将所有读写请求都路由到主节点.但也可以通过 ...
- 《移动WEB前端高级开发实践@www.java1234.com.pdf》——2
5.3 作用域.闭包和this let 声明的变量只存在于其所在的代码块中 由于 JS 是基于词法(静态)作用域的语言,词法作用域的含义是在函数定义时就确定了作用域,而不是函数执行时再确定 calcu ...
- 集合系列 Set(七):LinkedHashSet
LinkedHashSet 继承了 HashSet,在此基础上维护了元素的插入顺序. public class LinkedHashSet<E> extends HashSet<E& ...
- 你必须知道的Docker数据卷(Volume)
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 一.将Docker数据挂载到容器 在Docker中,要想实现数据的持久化(所谓 ...
- C语言程序设计100例之(24):数制转换
例24 数制转换 题目描述 请你编一程序实现两种不同进制之间的数据转换. 输入格式 共三行,第一行是一个正整数,表示需要转换的数的进制n(2≤n≤16),第二行是一个n进制数,若n>10则用 ...
- 前端小白webpack学习(一)
俗话说得好,好记性不如烂笔头. 之前就在学习中看过webpack的教程,然而一段时间没用,火速的忘光了.写这篇博文,做个总结,也让自己以后有个地方回顾. 看webpack之前,我先去看了一下官方文档, ...