gpt-auto-generator.c revision f9ac15442e4132f00eca5495d53c17062aae13e0
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <unistd.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdlib.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <fcntl.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <sys/ioctl.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include <sys/statfs.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <blkid/blkid.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include "sd-id128.h"
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include "libudev.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "path-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "mkdir.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "missing.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "udev-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "special.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "unit-name.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "virt.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "generator.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "gpt.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "fileio.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "efivars.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "blkid-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "btrfs-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic const char *arg_dest = "/tmp";
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_enabled = true;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_root_enabled = true;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool arg_root_rw = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int add_swap(const char *path) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *name = NULL, *unit = NULL, *lnk = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_fclose_ FILE *f = NULL;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(path);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen log_debug("Adding swap: %s", path);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen name = unit_name_from_path(path, ".swap");
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!name)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return log_oom();
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering unit = strjoin(arg_dest, "/", name, NULL);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!unit)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return log_oom();
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering f = fopen(unit, "wxe");
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (!f)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering fprintf(f,
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "[Unit]\n"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "Description=Swap Partition\n"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "Documentation=man:systemd-gpt-auto-generator(8)\n\n"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "[Swap]\n"
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering "What=%s\n",
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering path);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering fflush(f);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (ferror(f))
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen return log_error_errno(errno, "Failed to write unit file %s: %m", unit);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!lnk)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering mkdir_parents_label(lnk, 0755);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (symlink(unit, lnk) < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int add_cryptsetup(const char *id, const char *what, bool rw, char **device) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *to = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_fclose_ FILE *f = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering char *from, *ret;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(id);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(what);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(device);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering d = unit_name_from_path(what, ".device");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!d)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering e = unit_name_escape(id);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!e)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering n = unit_name_build("systemd-cryptsetup", e, ".service");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!n)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering p = strjoin(arg_dest, "/", n, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!p)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering f = fopen(p, "wxe");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!f)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_error_errno(errno, "Failed to create unit file %s: %m", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "[Unit]\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "Description=Cryptography Setup for %%I\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "DefaultDependencies=no\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "Conflicts=umount.target\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "BindsTo=dev-mapper-%%i.device %s\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "Before=umount.target cryptsetup.target\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "After=%s\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "IgnoreOnIsolate=true\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "[Service]\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "Type=oneshot\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "RemainAfterExit=yes\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "TimeoutSec=0\n" /* the binary handles timeouts anyway */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '' '%s'\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering d, d,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering id, what, rw ? "" : "read-only",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering id);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fflush(f);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (ferror(f))
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return log_error_errno(errno, "Failed to write file %s: %m", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering from = strappenda("../", n);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!to)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering mkdir_parents_label(to, 0755);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (symlink(from, to) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_error_errno(errno, "Failed to create symlink %s: %m", to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering free(to);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (!to)
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering return log_oom();
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering mkdir_parents_label(to, 0755);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (symlink(from, to) < 0)
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering return log_error_errno(errno, "Failed to create symlink %s: %m", to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n, NULL);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering if (!to)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return log_oom();
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering mkdir_parents_label(to, 0755);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (symlink(from, to) < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_error_errno(errno, "Failed to create symlink %s: %m", to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!p)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering mkdir_parents_label(p, 0755);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = write_string_file(p,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "[Unit]\n"
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_error_errno(r, "Failed to write device drop-in: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ret = strappend("/dev/mapper/", id);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!ret)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return log_oom();
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering *device = ret;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int add_mount(
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *id,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *what,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *where,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *fstype,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering bool rw,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *description,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering const char *post) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *crypto_what = NULL, *p = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_fclose_ FILE *f = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(id);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(what);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(where);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering assert(description);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_debug("Adding %s: %s %s", where, what, strna(fstype));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (streq_ptr(fstype, "crypto_LUKS")) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = add_cryptsetup(id, what, rw, &crypto_what);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (r < 0)
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering what = crypto_what;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fstype = NULL;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen unit = unit_name_from_path(where, ".mount");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!unit)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen p = strjoin(arg_dest, "/", unit, NULL);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (!p)
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return log_oom();
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering f = fopen(p, "wxe");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!f)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering fprintf(f,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "[Unit]\n"
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "Description=%s\n"
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "Documentation=man:systemd-gpt-auto-generator(8)\n",
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering description);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (post)
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen fprintf(f, "Before=%s\n", post);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering fprintf(f,
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "\n"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "[Mount]\n"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "What=%s\n"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "Where=%s\n",
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering what, where);
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (fstype)
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering fprintf(f, "Type=%s\n", fstype);
65b200e70d01cb7c513114b602c9b5cf41348f77Lennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering fprintf(f, "Options=%s\n", rw ? "rw" : "ro");
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering fflush(f);
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (ferror(f))
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return log_error_errno(errno, "Failed to write unit file %s: %m", p);
0936416a1cba187dc76903dae73246f4cbddf312Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (post) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering lnk = strjoin(arg_dest, "/", post, ".requires/", unit, NULL);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen if (!lnk)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return log_oom();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen mkdir_parents_label(lnk, 0755);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (symlink(p, lnk) < 0)
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return log_error_errno(errno, "Failed to create symlink %s: %m", lnk);
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen }
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int probe_and_add_mount(
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *id,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *what,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *where,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering bool rw,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *description,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *post) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering _cleanup_blkid_free_probe_ blkid_probe b = NULL;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *fstype;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(id);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(what);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(where);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(description);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (path_is_mount_point(where, true) <= 0 &&
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering dir_is_empty(where) <= 0) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_debug("%s already populated, ignoring.", where);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return 0;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt /* Let's check the partition type here, so that we know
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt * whether to do LUKS magic. */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen errno = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering b = blkid_new_probe_from_filename(what);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!b) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (errno == 0)
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering return log_oom();
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering log_error_errno(errno, "Failed to allocate prober: %m");
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering return -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen blkid_probe_enable_superblocks(b, 1);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering errno = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = blkid_do_safeprobe(b);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (r == -2 || r == 1) /* no result or uncertain */
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else if (r != 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (errno == 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering errno = EIO;
c52a97b896c914e17ba5be73c0e806455fd9ad4dLennart Poettering log_error_errno(errno, "Failed to probe %s: %m", what);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -errno;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return add_mount(
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering id,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering what,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering where,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fstype,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering rw,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering description,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering post);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poetteringstatic int enumerate_partitions(dev_t devnum) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_blkid_free_probe_ blkid_probe b = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering _cleanup_udev_unref_ struct udev *udev = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering _cleanup_free_ char *home = NULL, *srv = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering struct udev_list_entry *first, *item;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering struct udev_device *parent = NULL;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering const char *node, *pttype, *devtype;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering int home_nr = -1, srv_nr = -1;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering bool home_rw = true, srv_rw = true;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering blkid_partlist pl;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering int r, k;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering dev_t pn;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering udev = udev_new();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_oom();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering d = udev_device_new_from_devnum(udev, 'b', devnum);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!d)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_oom();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering parent = udev_device_get_parent(d);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!parent) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Not a partitioned device, ignoring.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Does it have a devtype? */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering devtype = udev_device_get_devtype(parent);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!devtype) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Parent doesn't have a device type, ignoring.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek /* Is this a disk or a partition? We only care for disks... */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!streq(devtype, "disk")) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_debug("Parent isn't a raw disk, ignoring.");
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek /* Does it have a device node? */
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek node = udev_device_get_devnode(parent);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!node) {
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering log_debug("Parent device does not have device node, ignoring.");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_debug("Root device %s.", node);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pn = udev_device_get_devnum(parent);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (major(pn) == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering errno = 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering b = blkid_new_probe_from_filename(node);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!b) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (errno == 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return log_oom();
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_error_errno(errno, "Failed allocate prober: %m");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -errno;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek blkid_probe_enable_partitions(b, 1);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen errno = 0;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen r = blkid_do_safeprobe(b);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r == -2 || r == 1) /* no result or uncertain */
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return 0;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering else if (r != 0) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (errno == 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering errno = EIO;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_error_errno(errno, "Failed to probe %s: %m", node);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return -errno;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek errno = 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r != 0) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (errno == 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek errno = EIO;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error_errno(errno, "Failed to determine partition table type of %s: %m", node);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -errno;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* We only do this all for GPT... */
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!streq_ptr(pttype, "gpt")) {
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen log_debug("Not a GPT partition table, ignoring.");
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen }
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen errno = 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen pl = blkid_probe_get_partitions(b);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (!pl) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (errno == 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return log_oom();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_error_errno(errno, "Failed to list partitions of %s: %m", node);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering e = udev_enumerate_new(udev);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering if (!e)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = udev_enumerate_add_match_parent(e, parent);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (r < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = udev_enumerate_add_match_subsystem(e, "block");
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return log_oom();
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = udev_enumerate_scan_devices(e);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (r < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return log_error_errno(r, "Failed to enumerate partitions on %s: %m", node);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering first = udev_enumerate_get_list_entry(e);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering udev_list_entry_foreach(item, first) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering _cleanup_udev_device_unref_ struct udev_device *q;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *stype, *subnode;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering sd_id128_t type_id;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering blkid_partition pp;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering dev_t qn;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int nr;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering unsigned long long flags;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!q)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering qn = udev_device_get_devnum(q);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (major(qn) == 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (qn == devnum)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (qn == pn)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering subnode = udev_device_get_devnode(q);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!subnode)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering pp = blkid_partlist_devno_to_partition(pl, qn);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!pp)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering flags = blkid_partition_get_flags(pp);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* Ignore partitions that are not marked for automatic
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering * mounting on discovery */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (flags & GPT_FLAG_NO_AUTO)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering nr = blkid_partition_get_partno(pp);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (nr < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering stype = blkid_partition_get_type_string(pp);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!stype)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (sd_id128_from_string(stype, &type_id) < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (sd_id128_equal(type_id, GPT_SWAP)) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (flags & GPT_FLAG_READ_ONLY) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_debug("%s marked as read-only swap partition, which is bogus, ignoring.", subnode);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering }
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering k = add_swap(subnode);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (k < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering r = k;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering } else if (sd_id128_equal(type_id, GPT_HOME)) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* We only care for the first /home partition */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (home && nr >= home_nr)
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering continue;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering home_nr = nr;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering home_rw = !(flags & GPT_FLAG_READ_ONLY),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering free(home);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering home = strdup(subnode);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (!home)
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering return log_oom();
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering } else if (sd_id128_equal(type_id, GPT_SRV)) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering /* We only care for the first /srv partition */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (srv && nr >= srv_nr)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering continue;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering srv_nr = nr;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering srv_rw = !(flags & GPT_FLAG_READ_ONLY),
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering free(srv);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering srv = strdup(node);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (!srv)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return log_oom();
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (home) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering k = probe_and_add_mount("home", home, "/home", home_rw, "Home Partition", SPECIAL_LOCAL_FS_TARGET);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (k < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = k;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann if (srv) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering k = probe_and_add_mount("srv", srv, "/srv", srv_rw, "Server Data Partition", SPECIAL_LOCAL_FS_TARGET);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (k < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = k;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int get_block_device(const char *path, dev_t *dev) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering struct stat st;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering struct statfs sfs;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering assert(path);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering assert(dev);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (lstat(path, &st))
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -errno;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (major(st.st_dev) != 0) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering *dev = st.st_dev;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return 1;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (statfs(path, &sfs) < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -errno;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC))
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return btrfs_get_block_device(path, dev);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int parse_proc_cmdline_item(const char *key, const char *value) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek assert(key);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (STR_IN_SET(key, "systemd.gpt_auto", "rd.systemd.gpt_auto") && value) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen r = parse_boolean(value);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to parse gpt-auto switch %s. Ignoring.", value);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen else
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen arg_enabled = r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering } else if (streq(key, "root") && value) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering /* Disable root disk logic if there's a root= value
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen * specified (unless it happens to be "gpt-auto") */
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering arg_root_enabled = streq(value, "gpt-auto");
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering } else if (streq(key, "rw") && !value)
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering arg_root_rw = true;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek else if (streq(key, "ro") && !value)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek arg_root_rw = false;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykrynstatic int add_root_mount(void) {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn#ifdef ENABLE_EFI
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int r;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (!is_efi_boot()) {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn log_debug("Not a EFI boot, not creating root mount.");
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek }
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = efi_loader_get_device_part_uuid(NULL);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r == -ENOENT) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_debug("EFI loader partition unknown, exiting.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } else if (r < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return log_error_errno(r, "Failed to read ESP partition UUID: %m");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen /* OK, we have an ESP partition, this is fantastic, so let's
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * wait for a root device to show up. A udev rule will create
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * the link for us under the right name. */
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return add_mount(
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen "root",
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen "/dev/gpt-auto-root",
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen in_initrd() ? "/sysroot" : "/",
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen NULL,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering arg_root_rw,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "Root Partition",
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering#else
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering#endif
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int add_mounts(void) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek dev_t devno;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = get_block_device("/", &devno);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return log_error_errno(r, "Failed to determine block device of root file system: %m");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek else if (r == 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_debug("Root file system not on a (single) block device.");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return enumerate_partitions(devno);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r = 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (argc > 1 && argc != 4) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error("This program takes three or no arguments.");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return EXIT_FAILURE;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (argc > 1)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek arg_dest = argv[3];
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_set_target(LOG_TARGET_SAFE);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_parse_environment();
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_open();
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen umask(0022);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (detect_container(NULL) > 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_debug("In a container, exiting.");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return EXIT_SUCCESS;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = parse_proc_cmdline(parse_proc_cmdline_item);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (r < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (!arg_enabled) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_debug("Disabled, exiting.");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return EXIT_SUCCESS;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (arg_root_enabled)
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen r = add_root_mount();
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!in_initrd()) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int k;
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen k = add_mounts();
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering if (k < 0)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen