fstab-generator.c revision c3834f9b881f2b1a68dc7d797c134f0b66b47b57
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
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 <stdio.h>
#include <mntent.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "log.h"
#include "util.h"
#include "unit-name.h"
#include "path-util.h"
#include "mount-setup.h"
#include "special.h"
#include "mkdir.h"
#include "fileio.h"
#include "generator.h"
#include "strv.h"
static const char *arg_dest = "/tmp";
static bool arg_fstab_enabled = true;
unsigned long r;
if (!pri)
return 0;
pri += 4;
errno = 0;
if (errno > 0)
return -errno;
return -EINVAL;
*ret = (int) r;
return 1;
}
bool noauto;
int r, pri = -1;
if (r < 0) {
log_error("Failed to parse priority");
return pri;
}
if (!name)
return log_oom();
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);
if (pri >= 0)
fprintf(f,
"Priority=%i\n",
pri);
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!noauto) {
if (!lnk)
return log_oom();
return -errno;
}
}
return 0;
}
return
}
return
}
static int add_mount(
const char *what,
const char *where,
const char *type,
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;
automount = false;
noauto = false;
nofail = false;
}
if (!name)
return log_oom();
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);
fprintf(f,
"Before=%s\n",
post);
if (passno != 0) {
if (r < 0)
return r;
}
fprintf(f,
"\n"
"[Mount]\n"
"What=%s\n"
"Where=%s\n"
"Type=%s\n",
what,
type);
fprintf(f,
"Options=%s\n",
opts);
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!noauto) {
if (post) {
if (!lnk)
return log_oom();
return -errno;
}
}
}
if (automount) {
if (!automount_name)
return log_oom();
if (!automount_unit)
return log_oom();
fclose(f);
if (!f) {
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);
if (post)
fprintf(f,
"Before=%s\n",
post);
fprintf(f,
"[Automount]\n"
"Where=%s\n",
where);
fflush(f);
if (ferror(f)) {
return -errno;
}
if (!lnk)
return log_oom();
return -errno;
}
}
return 0;
}
static int parse_fstab(bool initrd) {
const char *fstab_path;
int r = 0;
if (!f) {
return 0;
return -errno;
}
int k;
continue;
if (!what)
return log_oom();
if (!where)
return log_oom();
else {
const char *post;
if (initrd)
else if (mount_in_initrd(me))
else if (mount_is_network(me))
else
post, fstab_path);
}
if (k < 0)
r = k;
}
return r;
}
static int parse_new_root_from_proc_cmdline(void) {
char *w, *state;
size_t l;
int r;
r = proc_cmdline(&line);
if (r < 0)
if (r <= 0)
return 0;
return log_oom();
/* root= and roofstype= may occur more than once, the last instance should take precedence.
* In the case of multiple rootflags= the arguments should be concatenated */
_cleanup_free_ char *word;
if (!word)
return log_oom();
if (!what)
return log_oom();
if (!type)
return log_oom();
char *o;
if (!o)
return log_oom();
opts = o;
char *o;
if (!o)
return log_oom();
opts = o;
}
}
if (!what) {
log_debug("Could not find a root= entry on the kernel commandline.");
return 0;
}
if (what[0] != '/') {
return 0;
}
return (r < 0) ? r : 0;
}
int r;
r = parse_boolean(value);
if (r < 0)
else
arg_fstab_enabled = r;
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 (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
return EXIT_FAILURE;
/* Always honour root= in the kernel command line if we are in an initrd */
if (in_initrd())
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;
}