在 C++ 中编辑 /etc/fstab 条目

发布于 2024-10-06 13:32:50 字数 791 浏览 0 评论 0原文

我正在尝试使用 C++ 编辑 CentOS 安装上的 /etc/fstab 文件。我的想法是,基于另一个配置文件,我将添加 fstab 中不存在的条目,或者编辑 fstab 文件中安装点相同的条目。这使我们可以在初始启动时正确设置系统。

我发现 setmntent()getmntent() 用于迭代现有条目,因此我可以轻松检查 fstab 中的条目是否也存在于我的配置文件中。然后我可以使用 addmntent() 添加任何尚不存在的条目 - 文档没有提及能够编辑条目,仅在文件末尾添加一个新条目。似乎无法编辑现有条目或删除条目。奇怪的是这个功能不存在,只有 CRUD 的 CR 而没有 UD。

如果我能帮忙的话,我宁愿不必编写自己的解析器。

我的另一个选择是:

  • 使用 setmntent() 打开文件 使用
  • getmentent() 将整个 fstab 读取到内存中,并执行任何添加和/或编辑
  • 使用关闭文件endmntent()
  • 打开/etc/fstab进行写入
  • 关闭/etc/fstab(从而清空文件)
  • 使用setmntent打开fstab ()
  • 循环遍历我之前读入的条目,并使用 addmntent() 将它们写出来,

虽然可能没问题,但看起来有点混乱。

I'm trying to edit the /etc/fstab file on a CentOS installation using C++. The idea being that based on another config file I will add entries that don't exist in the fstab, or edit entries in the fstab file where the mount point is the same. This lets us set the system up properly on initial bootup.

I've found setmntent() and getmntent() for iterating over the exiting entries so I can easily check whether an entry in fstab also exists in my config file. And I can then use addmntent() to add any entry that doesn't already exist - the documentation says nothing about this being able to edit an entry, only add a new entry to the end of the file. There seems to be no way to edit an existing entry or delete an entry. It seems odd that this feature doesn't exist, only the CR and not the UD of CRUD.

I'd rather not have to write my own parser if I can at all help it.

My other alternative is to:

  • open the file using setmntent()
  • read the whole of fstab into memory using getmentent() and perform any additions and/or edits
  • close the file using endmntent()
  • open /etc/fstab for writing
  • close /etc/fstab (thus emptying the file)
  • open the fstab using setmntent()
  • loop through the entries I read in previously and write them out using addmntent()

Which although probably fine, just seems a bit messy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-10-13 13:32:50

修改系统配置文件(例如 /etc/fstab)时,请记住这些是关键状态,如果您的“编辑”因断电而中断,可能会导致重新启动失败。
处理这个问题的方法是:

  1. 创建一个空输出:
    FILE* out = setmntent("/etc/fstab.new", "rw");
  2. 打开原文进行输入:
    FILE* in = setmntent("/etc/fstab", "r");
  3. 复制内容:
    while (m = getmntent(in)) { addmntent(out, m); } }
  4. 确保输出包含所有内容:
    fflush(输出);结束(出); endmntent(in);
  5. 原子地替换 /etc/fstab:
    rename("/etc/fstab.new", "/etc/fstab");

留给读者的练习是更改 while 循环的主体以对现有元素进行修改,以替换专门制作的 mntent 或其他内容。如果您对此有具体问题,请询问。

rename() 的 UN*X 语义保证即使在断电的情况下,您也将拥有原始版本或新的更新版本。

没有 modifymntent() 是有原因的 - 因为这会鼓励不良编程/更改系统关键文件的不良方式。您在帖子末尾说“...可能很好...” - 。更改系统配置文件的唯一安全方法是编写完整的修改副本,将其同步到安全存储,然后使用重命名来替换旧的。

When modifying system configuration files such as /etc/fstab keep in mind that these are critical state and, should your "edit" be interrupted by a power loss, might result in a failure to reboot.
The way to deal with this is:

  1. create an empty output:
    FILE* out = setmntent("/etc/fstab.new", "rw");
  2. open the original for input:
    FILE* in = setmntent("/etc/fstab", "r");
  3. copy the contents:
    while (m = getmntent(in)) { addmntent(out, m); }
  4. make sure the output has it all:
    fflush(out); endmntent(out); endmntent(in);
  5. atomically replace /etc/fstab:
    rename("/etc/fstab.new", "/etc/fstab");

It's left as an exercise to the reader to change the body of the while loop to make a modification to an existing element, to substitute a specifically crafted mntent or whatever. If you have specific questions on that please ask.

UN*X semantics for rename() guarantee that even in the case of power loss, you'll have either the original version or your new updated one.

There's a reason why there is no modifymntent() - because that would encourage bad programming / bad ways of changing system critical files. You say at the end of your post "... probably fine ..." - not. The only safe way to change a system configuration file is to write a complete modified copy, sync that to safe storage, and then use rename to replace the old one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文