Skip to content

Commit

Permalink
Allow overriding /etc/fstab with $SYSTEMD_FSTAB
Browse files Browse the repository at this point in the history
  • Loading branch information
keszybz committed Nov 13, 2019
1 parent 32c6237 commit ed4ad48
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
3 changes: 3 additions & 0 deletions docs/ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion src/cryptsetup/cryptsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;

Expand Down
28 changes: 15 additions & 13 deletions src/fstab-generator/fstab-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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))) {
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/remount-fs/remount-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions src/shared/fstab-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions src/shared/fstab-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit ed4ad48

Please sign in to comment.