/***
This file is part of systemd.
Copyright 2012 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <mntent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "alloc-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "fstab-util.h"
#include "generator.h"
#include "log.h"
#include "mkdir.h"
#include "mount-setup.h"
#include "mount-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
#include "special.h"
#include "stat-util.h"
#include "string-util.h"
#include "strv.h"
#include "unit-name.h"
#include "util.h"
#include "virt.h"
static bool arg_fstab_enabled = true;
static int add_swap(
const char *what,
bool noauto,
bool nofail) {
int r;
return 0;
}
if (detect_container() > 0) {
return 0;
}
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
if (!unit)
return log_oom();
if (!f) {
log_error("Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
else
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
"[Swap]\n"
"What=%s\n",
what);
r = fflush_and_check(f);
if (r < 0)
/* use what as where, to have a nicer error message */
if (r < 0)
return r;
if (!noauto) {
if (!lnk)
return log_oom();
}
return 0;
}
}
}
usec_t u;
int r;
if (r < 0)
return log_warning_errno(r, "Failed to parse options: %m");
if (r == 0)
return 0;
if (r < 0) {
return 0;
}
return 0;
}
char **s;
int r;
assert(f);
if (r < 0)
return log_warning_errno(r, "Failed to parse options: %m");
if (r == 0)
return 0;
char *x;
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
r = strv_consume(&units, x);
if (r < 0)
return log_oom();
}
if (units) {
if (!res)
return log_oom();
}
return 0;
}
int r;
assert(f);
if (r < 0)
return log_warning_errno(r, "Failed to parse options: %m");
if (r == 0)
return 0;
if (!res)
return log_oom();
return 0;
}
static int add_mount(
const char *what,
const char *where,
const char *fstype,
const char *opts,
int passno,
bool noauto,
bool nofail,
bool automount,
const char *post,
const char *source) {
_cleanup_free_ char
int r;
return 0;
return 0;
}
if (mount_point_is_api(where) ||
return 0;
if (noauto)
log_warning("Ignoring \"noauto\" for root device");
if (nofail)
log_warning("Ignoring \"nofail\" for root device");
if (automount)
log_warning("Ignoring automount option for root device");
}
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
if (!unit)
return log_oom();
if (!f) {
log_error("Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
else
return -errno;
}
fprintf(f,
"# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
source);
r = write_requires_after(f, opts);
if (r < 0)
return r;
r = write_requires_mounts_for(f, opts);
if (r < 0)
return r;
}
if (passno != 0) {
if (r < 0)
return r;
}
fprintf(f,
"\n"
"[Mount]\n"
"What=%s\n"
"Where=%s\n",
what,
where);
if (r < 0)
return r;
r = fflush_and_check(f);
if (r < 0)
if (!noauto) {
if (!lnk)
return log_oom();
}
if (automount) {
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
if (!automount_unit)
return log_oom();
fclose(f);
if (!f)
fprintf(f,
"# Automatically generated by systemd-fstab-generator\n\n"
"[Unit]\n"
"SourcePath=%s\n"
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
source);
if (opts) {
r = write_requires_after(f, opts);
if (r < 0)
return r;
r = write_requires_mounts_for(f, opts);
if (r < 0)
return r;
}
fprintf(f,
"[Automount]\n"
"Where=%s\n",
where);
if (r < 0)
return r;
r = fflush_and_check(f);
if (r < 0)
if (!lnk)
return log_oom();
}
return 0;
}
const char *fstab_path;
int r = 0;
if (!f) {
return 0;
return -errno;
}
int k;
continue;
if (!what)
return log_oom();
continue;
}
if (!where)
return log_oom();
log_debug("Found entry what=%s where=%s type=%s nofail=%s noauto=%s",
else {
bool automount;
const char *post;
"comment=systemd.automount\0"
"x-systemd.automount\0");
if (initrd)
else if (mount_is_network(me))
else
me->mnt_passno,
post,
}
if (k < 0)
r = k;
}
return r;
}
static int add_sysroot_mount(void) {
const char *opts;
if (isempty(arg_root_what)) {
log_debug("Could not find a root= entry on the kernel command line.");
return 0;
}
if (!what)
return log_oom();
if (!arg_root_options)
else if (arg_root_rw >= 0 ||
else
"/sysroot",
opts,
false,
false,
false,
}
static int add_sysroot_usr_mount(void) {
const char *opts;
return 0;
if (arg_root_what && !arg_usr_what) {
if (!arg_usr_what)
return log_oom();
}
if (arg_root_fstype && !arg_usr_fstype) {
if (!arg_usr_fstype)
return log_oom();
}
if (arg_root_options && !arg_usr_options) {
if (!arg_usr_options)
return log_oom();
}
if (!arg_usr_what)
return 0;
if (!path_is_absolute(what)) {
return -1;
}
if (!arg_usr_options)
else
opts,
1,
false,
false,
false,
}
int r;
/* root=, usr=, usrfstype= and roofstype= may occur more than once, the last
* instance should take precedence. In the case of multiple rootflags=
* or usrflags= the arguments should be concatenated */
r = parse_boolean(value);
if (r < 0)
else
arg_fstab_enabled = r;
return log_oom();
return log_oom();
char *o;
o = arg_root_options ?
if (!o)
return log_oom();
arg_root_options = o;
return log_oom();
return log_oom();
char *o;
o = arg_usr_options ?
if (!o)
return log_oom();
arg_usr_options = o;
arg_root_rw = true;
arg_root_rw = false;
return 0;
}
int r = 0;
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1)
log_open();
umask(0022);
if (r < 0)
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
/* Always honour root= and usr= in the kernel command line if we are in an initrd */
if (in_initrd()) {
r = add_sysroot_mount();
if (r == 0)
r = add_sysroot_usr_mount();
}
if (arg_fstab_enabled) {
int k;
k = parse_fstab(false);
if (k < 0)
r = k;
if (in_initrd()) {
k = parse_fstab(true);
if (k < 0)
r = k;
}
}
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}