Allow overriding /etc/fstab with $SYSTEMD_FSTAB

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-11-13 17:36:46 +01:00
parent 32c6237a7c
commit ed4ad48897
6 changed files with 29 additions and 18 deletions

View File

@ -41,6 +41,9 @@ All tools:
debugging, in order to test generators and other code against specific kernel
command lines.
* `$SYSTEMD_FSTAB` — if set, use this path instead of /etc/fstab. Only useful
for debugging.
* `$SYSTEMD_CRYPTTAB` — if set, use this path instead of /etc/crypttab. Only
useful for debugging. Currently only supported by systemd-cryptsetup-generator.

View File

@ -15,6 +15,7 @@
#include "device-util.h"
#include "escape.h"
#include "fileio.h"
#include "fstab-util.h"
#include "log.h"
#include "main-func.h"
#include "mount-util.h"
@ -302,7 +303,7 @@ static char *disk_mount_point(const char *label) {
if (asprintf(&device, "/dev/mapper/%s", label) < 0)
return NULL;
f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return NULL;

View File

@ -112,14 +112,16 @@ static int add_swap(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
r = generator_open_unit_file(arg_dest, "/etc/fstab", name, &f);
r = generator_open_unit_file(arg_dest, fstab_path(), name, &f);
if (r < 0)
return r;
fputs("[Unit]\n"
"SourcePath=/etc/fstab\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
"[Swap]\n", f);
fprintf(f,
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
"[Swap]\n",
fstab_path());
r = write_what(f, what);
if (r < 0)
@ -340,7 +342,7 @@ static int add_mount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
r = generator_open_unit_file(dest, "/etc/fstab", name, &f);
r = generator_open_unit_file(dest, fstab_path(), name, &f);
if (r < 0)
return r;
@ -463,7 +465,7 @@ static int add_mount(
f = safe_fclose(f);
r = generator_open_unit_file(dest, "/etc/fstab", automount_name, &f);
r = generator_open_unit_file(dest, fstab_path(), automount_name, &f);
if (r < 0)
return r;
@ -515,19 +517,19 @@ static int add_mount(
static int parse_fstab(bool initrd) {
_cleanup_endmntent_ FILE *f = NULL;
const char *fstab_path;
const char *fstab;
struct mntent *me;
int r = 0;
fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
log_debug("Parsing %s...", fstab_path);
fstab = initrd ? "/sysroot/etc/fstab" : fstab_path();
log_debug("Parsing %s...", fstab);
f = setmntent(fstab_path, "re");
f = setmntent(fstab, "re");
if (!f) {
if (errno == ENOENT)
return 0;
return log_error_errno(errno, "Failed to open %s: %m", fstab_path);
return log_error_errno(errno, "Failed to open %s: %m", fstab);
}
while ((me = getmntent(f))) {
@ -606,7 +608,7 @@ static int parse_fstab(bool initrd) {
me->mnt_passno,
makefs*MAKEFS | growfs*GROWFS | noauto*NOAUTO | nofail*NOFAIL | automount*AUTOMOUNT,
post,
fstab_path);
fstab);
}
if (r >= 0 && k < 0)

View File

@ -9,6 +9,7 @@
#include "env-util.h"
#include "exit-status.h"
#include "fstab-util.h"
#include "log.h"
#include "main-func.h"
#include "mount-setup.h"
@ -86,10 +87,10 @@ static int run(int argc, char *argv[]) {
umask(0022);
f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f) {
if (errno != ENOENT)
return log_error_errno(errno, "Failed to open /etc/fstab: %m");
return log_error_errno(errno, "Failed to open %s: %m", fstab_path());
} else
while ((me = getmntent(f))) {
/* Remount the root fs, /usr, and all API VFSs */

View File

@ -19,7 +19,7 @@ int fstab_has_fstype(const char *fstype) {
_cleanup_endmntent_ FILE *f = NULL;
struct mntent *m;
f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return errno == ENOENT ? false : -errno;
@ -39,7 +39,7 @@ int fstab_is_mount_point(const char *mount) {
_cleanup_endmntent_ FILE *f = NULL;
struct mntent *m;
f = setmntent("/etc/fstab", "re");
f = setmntent(fstab_path(), "re");
if (!f)
return errno == ENOENT ? false : -errno;

View File

@ -31,3 +31,7 @@ static inline bool fstab_test_yes_no_option(const char *opts, const char *yes_no
}
char *fstab_node_to_udev_node(const char *p);
static inline const char* fstab_path(void) {
return secure_getenv("SYSTEMD_FSTAB") ?: "/etc/fstab";
}