2012-05-22 19:23:33 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2012 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
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"
|
2013-02-14 12:26:13 +01:00
|
|
|
#include "fileio.h"
|
2014-03-05 22:26:10 +01:00
|
|
|
#include "generator.h"
|
2014-03-06 17:05:55 +01:00
|
|
|
#include "strv.h"
|
2014-03-11 17:57:15 +01:00
|
|
|
#include "virt.h"
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
static const char *arg_dest = "/tmp";
|
2014-03-05 22:26:10 +01:00
|
|
|
static bool arg_fstab_enabled = true;
|
2014-03-06 21:14:26 +01:00
|
|
|
static char *arg_root_what = NULL;
|
|
|
|
static char *arg_root_fstype = NULL;
|
|
|
|
static char *arg_root_options = NULL;
|
|
|
|
static int arg_root_rw = -1;
|
2014-10-09 21:37:17 +02:00
|
|
|
static char *arg_usr_what = NULL;
|
|
|
|
static char *arg_usr_fstype = NULL;
|
|
|
|
static char *arg_usr_options = NULL;
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
static int mount_find_pri(struct mntent *me, int *ret) {
|
2014-09-28 04:02:04 +02:00
|
|
|
char *end, *opt;
|
2012-05-22 19:23:33 +02:00
|
|
|
unsigned long r;
|
|
|
|
|
|
|
|
assert(me);
|
|
|
|
assert(ret);
|
|
|
|
|
2014-09-28 04:02:04 +02:00
|
|
|
opt = hasmntopt(me, "pri");
|
|
|
|
if (!opt)
|
2012-05-22 19:23:33 +02:00
|
|
|
return 0;
|
|
|
|
|
2014-09-28 04:02:04 +02:00
|
|
|
opt += strlen("pri");
|
|
|
|
if (*opt != '=')
|
|
|
|
return -EINVAL;
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
errno = 0;
|
2014-09-28 04:02:04 +02:00
|
|
|
r = strtoul(opt + 1, &end, 10);
|
2013-03-28 14:24:15 +01:00
|
|
|
if (errno > 0)
|
2012-05-22 19:23:33 +02:00
|
|
|
return -errno;
|
|
|
|
|
2014-09-28 04:02:04 +02:00
|
|
|
if (end == opt + 1 || (*end != ',' && *end != 0))
|
2012-05-22 19:23:33 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
*ret = (int) r;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_swap(const char *what, struct mntent *me) {
|
2013-09-18 00:30:47 +02:00
|
|
|
_cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
2014-09-24 14:29:05 +02:00
|
|
|
|
2013-09-16 01:08:32 +02:00
|
|
|
bool noauto;
|
2012-05-22 19:23:33 +02:00
|
|
|
int r, pri = -1;
|
|
|
|
|
|
|
|
assert(what);
|
|
|
|
assert(me);
|
|
|
|
|
2014-03-11 17:57:15 +01:00
|
|
|
if (detect_container(NULL) > 0) {
|
|
|
|
log_info("Running in a container, ignoring fstab swap entry for %s.", what);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
r = mount_find_pri(me, &pri);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to parse priority");
|
2014-09-28 04:02:04 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-09-16 01:08:32 +02:00
|
|
|
noauto = !!hasmntopt(me, "noauto");
|
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
name = unit_name_from_path(what, ".swap");
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!name)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2012-07-13 13:41:01 +02:00
|
|
|
unit = strjoin(arg_dest, "/", name, NULL);
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!unit)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
f = fopen(unit, "wxe");
|
|
|
|
if (!f) {
|
2012-11-27 01:09:28 +01:00
|
|
|
if (errno == EEXIST)
|
|
|
|
log_error("Failed to create swap unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
|
|
|
|
else
|
|
|
|
log_error("Failed to create unit file %s: %m", unit);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f,
|
2013-08-21 16:48:56 +02:00
|
|
|
"# Automatically generated by systemd-fstab-generator\n\n"
|
|
|
|
"[Unit]\n"
|
2014-03-06 18:26:06 +01:00
|
|
|
"SourcePath=/etc/fstab\n"
|
|
|
|
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n\n"
|
2012-05-22 19:23:33 +02:00
|
|
|
"[Swap]\n"
|
|
|
|
"What=%s\n",
|
|
|
|
what);
|
|
|
|
|
2014-10-28 14:24:46 +01:00
|
|
|
/* Note that we currently pass the priority field twice, once
|
|
|
|
* in Priority=, and once in Options= */
|
2012-05-22 19:23:33 +02:00
|
|
|
if (pri >= 0)
|
2014-09-28 04:02:04 +02:00
|
|
|
fprintf(f, "Priority=%i\n", pri);
|
|
|
|
|
2014-10-28 14:24:46 +01:00
|
|
|
if (!isempty(me->mnt_opts) && !streq(me->mnt_opts, "defaults"))
|
|
|
|
fprintf(f, "Options=%s\n", me->mnt_opts);
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-10-28 14:24:46 +01:00
|
|
|
r = fflush_and_check(f);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to write unit file %s: %s", unit, strerror(-r));
|
|
|
|
return r;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
2014-06-27 21:03:05 +02:00
|
|
|
/* use what as where, to have a nicer error message */
|
|
|
|
r = generator_write_timeouts(arg_dest, what, what, me->mnt_opts, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-09-16 01:08:32 +02:00
|
|
|
if (!noauto) {
|
|
|
|
lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
|
|
|
|
if (!lnk)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
mkdir_parents_label(lnk, 0755);
|
|
|
|
if (symlink(unit, lnk) < 0) {
|
|
|
|
log_error("Failed to create symlink %s: %m", lnk);
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:15:55 +01:00
|
|
|
return 0;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool mount_is_network(struct mntent *me) {
|
|
|
|
assert(me);
|
|
|
|
|
|
|
|
return
|
|
|
|
hasmntopt(me, "_netdev") ||
|
|
|
|
fstype_is_network(me->mnt_type);
|
|
|
|
}
|
|
|
|
|
2013-02-22 11:52:36 +01:00
|
|
|
static bool mount_in_initrd(struct mntent *me) {
|
|
|
|
assert(me);
|
|
|
|
|
|
|
|
return
|
|
|
|
hasmntopt(me, "x-initrd.mount") ||
|
|
|
|
streq(me->mnt_dir, "/usr");
|
|
|
|
}
|
|
|
|
|
2013-03-25 22:04:40 +01:00
|
|
|
static int add_mount(
|
|
|
|
const char *what,
|
|
|
|
const char *where,
|
2014-03-06 21:14:26 +01:00
|
|
|
const char *fstype,
|
2013-03-25 22:04:40 +01:00
|
|
|
const char *opts,
|
|
|
|
int passno,
|
|
|
|
bool noauto,
|
|
|
|
bool nofail,
|
|
|
|
bool automount,
|
|
|
|
const char *post,
|
|
|
|
const char *source) {
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char
|
2013-09-18 00:30:47 +02:00
|
|
|
*name = NULL, *unit = NULL, *lnk = NULL,
|
2014-06-27 18:12:07 +02:00
|
|
|
*automount_name = NULL, *automount_unit = NULL,
|
|
|
|
*filtered = NULL;
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_fclose_ FILE *f = NULL;
|
fsck,fstab-generator: be lenient about missing fsck.<type>
If fstab contains 1 for passno, treat this as an error, but only warn
briefly. If fstab doesn't contain this information, don't complain at
all.
Patch is complicated a bit by the fact that we might have the fstype specified
in fstab or on /proc/cmdline, in which case we can check if we have the appropriate
fsck tool, or not specified, or specified as auto, in which case we have to look
and check the type of the filesystem ourselves. It cannot be done before the
device appears, so it is too early in the generator phase, and it must be done
directly in fsck service.
2013-11-12 06:53:59 +01:00
|
|
|
int r;
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
assert(what);
|
|
|
|
assert(where);
|
2012-11-23 03:41:13 +01:00
|
|
|
assert(opts);
|
|
|
|
assert(source);
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
if (streq_ptr(fstype, "autofs"))
|
2012-05-22 19:23:33 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!is_path(where)) {
|
|
|
|
log_warning("Mount point %s is not a valid path, ignoring.", where);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mount_point_is_api(where) ||
|
|
|
|
mount_point_ignore(where))
|
|
|
|
return 0;
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
if (path_equal(where, "/")) {
|
2014-03-07 02:39:59 +01:00
|
|
|
/* The root disk is not an option */
|
2014-03-05 22:26:10 +01:00
|
|
|
automount = false;
|
|
|
|
noauto = false;
|
|
|
|
nofail = false;
|
|
|
|
}
|
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
name = unit_name_from_path(where, ".mount");
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!name)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2012-07-13 13:41:01 +02:00
|
|
|
unit = strjoin(arg_dest, "/", name, NULL);
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!unit)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
f = fopen(unit, "wxe");
|
|
|
|
if (!f) {
|
2012-11-27 01:09:28 +01:00
|
|
|
if (errno == EEXIST)
|
|
|
|
log_error("Failed to create mount unit file %s, as it already exists. Duplicate entry in /etc/fstab?", unit);
|
|
|
|
else
|
|
|
|
log_error("Failed to create unit file %s: %m", unit);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
2012-11-23 03:41:13 +01:00
|
|
|
fprintf(f,
|
2014-03-06 18:26:06 +01:00
|
|
|
"# Automatically generated by systemd-fstab-generator\n\n"
|
|
|
|
"[Unit]\n"
|
|
|
|
"SourcePath=%s\n"
|
|
|
|
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
|
|
|
|
source);
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2013-03-04 19:01:05 +01:00
|
|
|
if (post && !noauto && !nofail && !automount)
|
2014-03-07 02:39:59 +01:00
|
|
|
fprintf(f, "Before=%s\n", post);
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
if (passno != 0) {
|
2014-03-06 21:14:26 +01:00
|
|
|
r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
|
2014-03-05 22:26:10 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2013-09-30 01:34:44 +02:00
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
fprintf(f,
|
|
|
|
"\n"
|
|
|
|
"[Mount]\n"
|
|
|
|
"What=%s\n"
|
2014-03-06 21:14:26 +01:00
|
|
|
"Where=%s\n",
|
2012-05-22 19:23:33 +02:00
|
|
|
what,
|
2014-03-06 21:14:26 +01:00
|
|
|
where);
|
|
|
|
|
|
|
|
if (!isempty(fstype) && !streq(fstype, "auto"))
|
|
|
|
fprintf(f, "Type=%s\n", fstype);
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-06-27 18:12:07 +02:00
|
|
|
r = generator_write_timeouts(arg_dest, what, where, opts, &filtered);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (!isempty(filtered) && !streq(filtered, "defaults"))
|
|
|
|
fprintf(f, "Options=%s\n", filtered);
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
fflush(f);
|
|
|
|
if (ferror(f)) {
|
2012-11-09 12:00:46 +01:00
|
|
|
log_error("Failed to write unit file %s: %m", unit);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
2014-03-07 02:39:59 +01:00
|
|
|
if (!noauto && post) {
|
|
|
|
lnk = strjoin(arg_dest, "/", post, nofail || automount ? ".wants/" : ".requires/", name, NULL);
|
|
|
|
if (!lnk)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
mkdir_parents_label(lnk, 0755);
|
|
|
|
if (symlink(unit, lnk) < 0) {
|
|
|
|
log_error("Failed to create symlink %s: %m", lnk);
|
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
if (automount) {
|
2012-05-22 19:23:33 +02:00
|
|
|
automount_name = unit_name_from_path(where, ".automount");
|
2013-08-13 10:38:19 +02:00
|
|
|
if (!automount_name)
|
2013-01-02 23:15:55 +01:00
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2012-07-13 13:41:01 +02:00
|
|
|
automount_unit = strjoin(arg_dest, "/", automount_name, NULL);
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!automount_unit)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
f = fopen(automount_unit, "wxe");
|
|
|
|
if (!f) {
|
2012-11-09 12:00:46 +01:00
|
|
|
log_error("Failed to create unit file %s: %m", automount_unit);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(f,
|
|
|
|
"# Automatically generated by systemd-fstab-generator\n\n"
|
|
|
|
"[Unit]\n"
|
2014-03-06 18:26:06 +01:00
|
|
|
"SourcePath=%s\n"
|
|
|
|
"Documentation=man:fstab(5) man:systemd-fstab-generator(8)\n",
|
2013-03-04 19:01:05 +01:00
|
|
|
source);
|
|
|
|
|
|
|
|
if (post)
|
|
|
|
fprintf(f,
|
2014-03-05 22:26:10 +01:00
|
|
|
"Before=%s\n",
|
2013-03-04 19:01:05 +01:00
|
|
|
post);
|
|
|
|
|
|
|
|
fprintf(f,
|
2012-05-22 19:23:33 +02:00
|
|
|
"[Automount]\n"
|
|
|
|
"Where=%s\n",
|
|
|
|
where);
|
|
|
|
|
|
|
|
fflush(f);
|
|
|
|
if (ferror(f)) {
|
2012-11-09 12:00:46 +01:00
|
|
|
log_error("Failed to write unit file %s: %m", automount_unit);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(lnk);
|
2012-07-13 13:41:01 +02:00
|
|
|
lnk = strjoin(arg_dest, "/", post, nofail ? ".wants/" : ".requires/", automount_name, NULL);
|
2013-01-02 23:15:55 +01:00
|
|
|
if (!lnk)
|
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2012-05-31 12:40:20 +02:00
|
|
|
mkdir_parents_label(lnk, 0755);
|
2012-05-22 19:23:33 +02:00
|
|
|
if (symlink(automount_unit, lnk) < 0) {
|
2012-11-09 12:00:46 +01:00
|
|
|
log_error("Failed to create symlink %s: %m", lnk);
|
2013-01-02 23:15:55 +01:00
|
|
|
return -errno;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-02 23:15:55 +01:00
|
|
|
return 0;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
static int parse_fstab(bool initrd) {
|
2014-03-06 21:14:26 +01:00
|
|
|
_cleanup_endmntent_ FILE *f = NULL;
|
2014-03-05 22:26:10 +01:00
|
|
|
const char *fstab_path;
|
2012-05-22 19:23:33 +02:00
|
|
|
struct mntent *me;
|
2014-03-05 22:26:10 +01:00
|
|
|
int r = 0;
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
fstab_path = initrd ? "/sysroot/etc/fstab" : "/etc/fstab";
|
|
|
|
f = setmntent(fstab_path, "re");
|
2012-05-22 19:23:33 +02:00
|
|
|
if (!f) {
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
log_error("Failed to open %s: %m", fstab_path);
|
2012-05-22 19:23:33 +02:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((me = getmntent(f))) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char *where = NULL, *what = NULL;
|
2012-05-22 19:23:33 +02:00
|
|
|
int k;
|
|
|
|
|
2013-02-22 11:52:36 +01:00
|
|
|
if (initrd && !mount_in_initrd(me))
|
|
|
|
continue;
|
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
what = fstab_node_to_udev_node(me->mnt_fsname);
|
2014-03-05 22:26:10 +01:00
|
|
|
if (!what)
|
|
|
|
return log_oom();
|
|
|
|
|
2014-03-11 17:57:15 +01:00
|
|
|
if (detect_container(NULL) > 0 && is_device_path(what)) {
|
|
|
|
log_info("Running in a container, ignoring fstab device entry for %s.", what);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
where = initrd ? strappend("/sysroot/", me->mnt_dir) : strdup(me->mnt_dir);
|
|
|
|
if (!where)
|
2013-10-04 04:13:55 +02:00
|
|
|
return log_oom();
|
2012-05-22 19:23:33 +02:00
|
|
|
|
fstab-generator: avoid mangling of mount source string
This is a valid source entry in /etc/fstab:
//192.168.6.10/data /data cifs noperm,auto
On Mon, Jun 4, 2012 at 2:04 PM, Dave Reisner <d@falconindy.com> wrote:
> On Mon, Jun 04, 2012 at 12:57:47PM +0200, Kay Sievers wrote:
>>
>> Changed it to use:
>> path_is_absolute()
>> instead of:
>> is_path(),
>> so that we still sanitize the input we might match against.
>>
>> Let me know, if you think that could still cause any problems?
> Yes, this will still break CIFS shares.
2012-06-04 15:21:05 +02:00
|
|
|
if (is_path(where))
|
2012-05-22 19:23:33 +02:00
|
|
|
path_kill_slashes(where);
|
|
|
|
|
|
|
|
log_debug("Found entry what=%s where=%s type=%s", what, where, me->mnt_type);
|
|
|
|
|
|
|
|
if (streq(me->mnt_type, "swap"))
|
|
|
|
k = add_swap(what, me);
|
2012-11-23 03:41:13 +01:00
|
|
|
else {
|
2013-08-19 11:45:24 +02:00
|
|
|
bool noauto, nofail, automount;
|
2013-08-19 03:20:52 +02:00
|
|
|
const char *post;
|
2012-11-23 03:41:13 +01:00
|
|
|
|
|
|
|
noauto = !!hasmntopt(me, "noauto");
|
|
|
|
nofail = !!hasmntopt(me, "nofail");
|
|
|
|
automount =
|
|
|
|
hasmntopt(me, "comment=systemd.automount") ||
|
|
|
|
hasmntopt(me, "x-systemd.automount");
|
2013-03-04 19:01:05 +01:00
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
if (initrd)
|
2013-03-04 19:01:05 +01:00
|
|
|
post = SPECIAL_INITRD_FS_TARGET;
|
2014-03-05 22:26:10 +01:00
|
|
|
else if (mount_in_initrd(me))
|
2013-03-14 13:12:10 +01:00
|
|
|
post = SPECIAL_INITRD_ROOT_FS_TARGET;
|
2014-03-05 22:26:10 +01:00
|
|
|
else if (mount_is_network(me))
|
2013-03-28 21:18:58 +01:00
|
|
|
post = SPECIAL_REMOTE_FS_TARGET;
|
2014-03-05 22:26:10 +01:00
|
|
|
else
|
2013-03-28 21:18:58 +01:00
|
|
|
post = SPECIAL_LOCAL_FS_TARGET;
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
k = add_mount(what,
|
|
|
|
where,
|
|
|
|
me->mnt_type,
|
|
|
|
me->mnt_opts,
|
|
|
|
me->mnt_passno,
|
|
|
|
noauto,
|
|
|
|
nofail,
|
|
|
|
automount,
|
|
|
|
post,
|
|
|
|
fstab_path);
|
2012-11-23 03:41:13 +01:00
|
|
|
}
|
2012-05-22 19:23:33 +02:00
|
|
|
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
static int add_root_mount(void) {
|
2014-06-23 20:11:30 +02:00
|
|
|
_cleanup_free_ char *what = NULL;
|
|
|
|
const char *opts;
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
if (isempty(arg_root_what)) {
|
2014-11-06 15:33:48 +01:00
|
|
|
log_debug("Could not find a root= entry on the kernel command line.");
|
2013-03-04 21:00:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
what = fstab_node_to_udev_node(arg_root_what);
|
|
|
|
if (!path_is_absolute(what)) {
|
|
|
|
log_debug("Skipping entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
|
2013-03-04 21:00:56 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
if (!arg_root_options)
|
2014-06-23 20:11:30 +02:00
|
|
|
opts = arg_root_rw > 0 ? "rw" : "ro";
|
|
|
|
else if (arg_root_rw >= 0 ||
|
|
|
|
(!mount_test_option(arg_root_options, "ro") &&
|
|
|
|
!mount_test_option(arg_root_options, "rw")))
|
2014-03-11 15:41:22 +01:00
|
|
|
opts = strappenda(arg_root_options, ",", arg_root_rw > 0 ? "rw" : "ro");
|
2014-06-23 20:11:30 +02:00
|
|
|
else
|
|
|
|
opts = arg_root_options;
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
log_debug("Found entry what=%s where=/sysroot type=%s", what, strna(arg_root_fstype));
|
|
|
|
return add_mount(what,
|
|
|
|
"/sysroot",
|
|
|
|
arg_root_fstype,
|
2014-06-23 20:11:30 +02:00
|
|
|
opts,
|
2014-03-06 21:14:26 +01:00
|
|
|
1,
|
2014-03-07 02:39:59 +01:00
|
|
|
false,
|
|
|
|
false,
|
2014-03-06 21:14:26 +01:00
|
|
|
false,
|
|
|
|
SPECIAL_INITRD_ROOT_FS_TARGET,
|
|
|
|
"/proc/cmdline");
|
2012-11-23 03:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-10-09 21:37:17 +02:00
|
|
|
static int add_usr_mount(void) {
|
|
|
|
_cleanup_free_ char *what = NULL;
|
|
|
|
const char *opts;
|
|
|
|
|
|
|
|
if (!arg_usr_what && !arg_usr_fstype && !arg_usr_options)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (arg_root_what && !arg_usr_what) {
|
|
|
|
arg_usr_what = strdup(arg_root_what);
|
|
|
|
|
|
|
|
if (!arg_usr_what)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_root_fstype && !arg_usr_fstype) {
|
|
|
|
arg_usr_fstype = strdup(arg_root_fstype);
|
|
|
|
|
|
|
|
if (!arg_usr_fstype)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_root_options && !arg_usr_options) {
|
|
|
|
arg_usr_options = strdup(arg_root_options);
|
|
|
|
|
|
|
|
if (!arg_usr_options)
|
|
|
|
return log_oom();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!arg_usr_what || !arg_usr_options)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
what = fstab_node_to_udev_node(arg_usr_what);
|
|
|
|
if (!path_is_absolute(what)) {
|
|
|
|
log_debug("Skipping entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
opts = arg_usr_options;
|
|
|
|
|
|
|
|
log_debug("Found entry what=%s where=/sysroot/usr type=%s", what, strna(arg_usr_fstype));
|
|
|
|
return add_mount(what,
|
|
|
|
"/sysroot/usr",
|
|
|
|
arg_usr_fstype,
|
|
|
|
opts,
|
|
|
|
1,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
SPECIAL_INITRD_ROOT_FS_TARGET,
|
|
|
|
"/proc/cmdline");
|
|
|
|
}
|
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
static int parse_proc_cmdline_item(const char *key, const char *value) {
|
2013-11-06 03:15:16 +01:00
|
|
|
int r;
|
2012-06-22 10:27:05 +02:00
|
|
|
|
2014-10-09 21:37:17 +02:00
|
|
|
/* 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 */
|
2014-03-06 21:14:26 +01:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
if (STR_IN_SET(key, "fstab", "rd.fstab") && value) {
|
2014-03-05 22:26:10 +01:00
|
|
|
|
2014-03-06 17:05:55 +01:00
|
|
|
r = parse_boolean(value);
|
2014-02-16 00:08:59 +01:00
|
|
|
if (r < 0)
|
2014-03-06 17:05:55 +01:00
|
|
|
log_warning("Failed to parse fstab switch %s. Ignoring.", value);
|
2014-02-16 00:08:59 +01:00
|
|
|
else
|
2014-03-05 22:26:10 +01:00
|
|
|
arg_fstab_enabled = r;
|
2012-06-22 10:27:05 +02:00
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
} else if (streq(key, "root") && value) {
|
|
|
|
|
2014-10-04 02:41:43 +02:00
|
|
|
if (free_and_strdup(&arg_root_what, value) < 0)
|
2014-03-06 21:14:26 +01:00
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
} else if (streq(key, "rootfstype") && value) {
|
|
|
|
|
2014-10-04 02:41:43 +02:00
|
|
|
if (free_and_strdup(&arg_root_fstype, value) < 0)
|
2014-03-06 21:14:26 +01:00
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
} else if (streq(key, "rootflags") && value) {
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
o = arg_root_options ?
|
|
|
|
strjoin(arg_root_options, ",", value, NULL) :
|
|
|
|
strdup(value);
|
|
|
|
if (!o)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
free(arg_root_options);
|
|
|
|
arg_root_options = o;
|
|
|
|
|
2014-10-09 21:37:17 +02:00
|
|
|
} else if (streq(key, "mount.usr") && value) {
|
|
|
|
|
|
|
|
if (free_and_strdup(&arg_usr_what, value) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
} else if (streq(key, "mount.usrfstype") && value) {
|
|
|
|
|
|
|
|
if (free_and_strdup(&arg_usr_fstype, value) < 0)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
} else if (streq(key, "mount.usrflags") && value) {
|
|
|
|
char *o;
|
|
|
|
|
|
|
|
o = arg_usr_options ?
|
|
|
|
strjoin(arg_usr_options, ",", value, NULL) :
|
|
|
|
strdup(value);
|
|
|
|
if (!o)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
free(arg_usr_options);
|
|
|
|
arg_usr_options = o;
|
|
|
|
|
2014-03-06 21:14:26 +01:00
|
|
|
} else if (streq(key, "rw") && !value)
|
|
|
|
arg_root_rw = true;
|
|
|
|
else if (streq(key, "ro") && !value)
|
|
|
|
arg_root_rw = false;
|
2012-06-22 10:27:05 +02:00
|
|
|
|
2013-01-02 23:15:55 +01:00
|
|
|
return 0;
|
2012-06-22 10:27:05 +02:00
|
|
|
}
|
|
|
|
|
2012-05-22 19:23:33 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
2014-03-05 22:26:10 +01:00
|
|
|
int r = 0;
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2012-05-23 03:43:29 +02:00
|
|
|
if (argc > 1 && argc != 4) {
|
|
|
|
log_error("This program takes three or no arguments.");
|
2012-05-22 19:23:33 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
arg_dest = argv[1];
|
|
|
|
|
2012-05-22 22:00:37 +02:00
|
|
|
log_set_target(LOG_TARGET_SAFE);
|
2012-05-22 19:23:33 +02:00
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
umask(0022);
|
|
|
|
|
2014-11-06 21:53:34 +01:00
|
|
|
r = parse_proc_cmdline(parse_proc_cmdline_item);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning("Failed to parse kernel command line, ignoring: %s", strerror(-r));
|
2012-06-22 10:27:05 +02:00
|
|
|
|
2014-10-09 21:37:17 +02:00
|
|
|
/* Always honour root= and usr= in the kernel command line if we are in an initrd */
|
|
|
|
if (in_initrd()) {
|
2014-03-06 21:14:26 +01:00
|
|
|
r = add_root_mount();
|
2014-10-09 21:37:17 +02:00
|
|
|
if (r == 0)
|
|
|
|
r = add_usr_mount();
|
|
|
|
}
|
2012-11-23 03:41:13 +01:00
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
/* Honour /etc/fstab only when that's enabled */
|
|
|
|
if (arg_fstab_enabled) {
|
|
|
|
int k;
|
2013-03-05 06:45:06 +01:00
|
|
|
|
2014-06-23 20:11:30 +02:00
|
|
|
log_debug("Parsing /etc/fstab");
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
/* Parse the local /etc/fstab, possibly from the initrd */
|
|
|
|
k = parse_fstab(false);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
2013-03-05 06:45:06 +01:00
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
/* If running in the initrd also parse the /etc/fstab from the host */
|
|
|
|
if (in_initrd()) {
|
2014-06-23 20:11:30 +02:00
|
|
|
log_debug("Parsing /sysroot/etc/fstab");
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
k = parse_fstab(true);
|
|
|
|
if (k < 0)
|
|
|
|
r = k;
|
|
|
|
}
|
|
|
|
}
|
2012-05-22 19:23:33 +02:00
|
|
|
|
2014-07-08 00:36:03 +02:00
|
|
|
free(arg_root_what);
|
|
|
|
|
2014-03-05 22:26:10 +01:00
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
2012-05-22 19:23:33 +02:00
|
|
|
}
|