gpt-auto-generator.c revision 821b2e792159e237a1e5a1ea4bb6ae2e55d64be5
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>
fc8eec10f6a95a7ebb0f88954b49f2ed731c3a15Zbigniew Jędrzejewski-Szmek
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#ifdef HAVE_LINUX_BTRFS_H
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <linux/btrfs.h>
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#endif
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "sd-id128.h"
d7671a3efdaef690b4316a03011038f731f2eea9Zbigniew Jędrzejewski-Szmek#include "libudev.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "path-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "mkdir.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "missing.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "udev-util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "special.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart 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
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;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen _cleanup_fclose_ FILE *f = NULL;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering assert(path);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen log_debug("Adding swap: %s", path);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering 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 log_error("Failed to create unit file %s: %m", unit);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return -errno;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering }
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);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen fflush(f);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (ferror(f)) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Failed to write unit file %s: %m", unit);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -errno;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering lnk = strjoin(arg_dest, "/" SPECIAL_SWAP_TARGET ".wants/", name, NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!lnk)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poettering mkdir_parents_label(lnk, 0755);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (symlink(unit, lnk) < 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Failed to create symlink %s: %m", lnk);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -errno;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart 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
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering d = unit_name_from_path(what, ".device");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!d)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering e = unit_name_escape(id);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!e)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart 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 log_error("Failed to create unit file %s: %m", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering fprintf(f,
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering "[Unit]\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart 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"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "After=%s\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "IgnoreOnIsolate=true\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "After=systemd-readahead-collect.service systemd-readahead-replay.service\n\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "[Service]\n"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "Type=oneshot\n"
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart 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)) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Failed to write file %s: %m", p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering }
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering from = strappenda("../", n);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (!to)
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering return log_oom();
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering mkdir_parents_label(to, 0755);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (symlink(from, to) < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Failed to create symlink %s: %m", to);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return -errno;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering }
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering to = strjoin(arg_dest, "/cryptsetup.target.requires/", 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 log_error("Failed to create symlink %s: %m", to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering free(to);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n, NULL);
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering if (!to)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering mkdir_parents_label(to, 0755);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (symlink(from, to) < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_error("Failed to create symlink %s: %m", to);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering return -errno;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering }
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering free(p);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!p)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart 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"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_error("Failed to write device drop-in: %s", strerror(-r));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering ret = strappend("/dev/mapper/", id);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!ret)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering *device = ret;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int add_mount(
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering const char *id,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *what,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *where,
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen const char *fstype,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering bool rw,
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering const char *description,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *post) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *unit = NULL, *lnk = NULL, *crypto_what = NULL, *p = NULL;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen _cleanup_fclose_ FILE *f = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen assert(id);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen assert(what);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen assert(where);
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen assert(description);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_debug("Adding %s: %s %s", where, what, strna(fstype));
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (streq_ptr(fstype, "crypto_LUKS")) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = add_cryptsetup(id, what, rw, &crypto_what);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering what = crypto_what;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering fstype = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen unit = unit_name_from_path(where, ".mount");
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (!unit)
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return log_oom();
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering p = strjoin(arg_dest, "/", unit, NULL);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (!p)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return log_oom();
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering f = fopen(p, "wxe");
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (!f) {
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_error("Failed to create unit file %s: %m", unit);
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return -errno;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering fprintf(f,
65b200e70d01cb7c513114b602c9b5cf41348f77Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "[Unit]\n"
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering "Description=%s\n"
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering "Documentation=man:systemd-gpt-auto-generator(8)\n",
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering description);
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
0936416a1cba187dc76903dae73246f4cbddf312Lennart Poettering if (post)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fprintf(f, "Before=%s\n", post);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen r = generator_write_fsck_deps(f, arg_dest, what, where, fstype);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (r < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return r;
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fprintf(f,
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "\n"
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "[Mount]\n"
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen "What=%s\n"
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering "Where=%s\n",
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering what, where);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (fstype)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fprintf(f, "Type=%s\n", fstype);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fprintf(f, "Options=%s\n", rw ? "rw" : "ro");
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering fflush(f);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (ferror(f)) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_error("Failed to write unit file %s: %m", p);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (post) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering lnk = strjoin(arg_dest, "/", post, ".requires/", unit, NULL);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!lnk)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return log_oom();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering mkdir_parents_label(lnk, 0755);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (symlink(p, lnk) < 0) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_error("Failed to create symlink %s: %m", lnk);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt }
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt return 0;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
d23a27a964748967e1ad20e86de869a753af555bTom Gundersenstatic int probe_and_add_mount(
f2af5ea3cbf54998ed41d2d21a3448abb92ca7ffLennart Poettering const char *id,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *what,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *where,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering bool rw,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering const char *description,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering const char *post) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering _cleanup_blkid_free_probe_ blkid_probe b = NULL;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen const char *fstype;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(id);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(what);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(where);
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen assert(description);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (path_is_mount_point(where, true) <= 0 &&
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering dir_is_empty(where) <= 0) {
f2af5ea3cbf54998ed41d2d21a3448abb92ca7ffLennart Poettering log_debug("%s already populated, ignoring.", where);
f2af5ea3cbf54998ed41d2d21a3448abb92ca7ffLennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* Let's check the partition type here, so that we know
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * whether to do LUKS magic. */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering errno = 0;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering b = blkid_new_probe_from_filename(what);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering if (!b) {
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering if (errno == 0)
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering return log_oom();
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering log_error("Failed to allocate prober: %m");
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering return -errno;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering }
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering blkid_probe_enable_superblocks(b, 1);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering blkid_probe_set_superblocks_flags(b, BLKID_SUBLKS_TYPE);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering errno = 0;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering r = blkid_do_safeprobe(b);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering if (r == -2 || r == 1) /* no result or uncertain */
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering return 0;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering else if (r != 0) {
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering if (errno == 0)
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering errno = EIO;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering log_error("Failed to probe %s: %m", what);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering return -errno;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering }
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering blkid_probe_lookup_value(b, "TYPE", &fstype, NULL);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering return add_mount(
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering id,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering what,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering where,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering fstype,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering rw,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering description,
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering post);
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering}
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poetteringstatic int enumerate_partitions(dev_t devnum) {
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering _cleanup_udev_device_unref_ struct udev_device *d = NULL;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering _cleanup_blkid_free_probe_ blkid_probe b = NULL;
f57e3cd5fa709ec0f52531eccba909ac0851927cLennart Poettering _cleanup_udev_unref_ struct udev *udev = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *home = NULL, *srv = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_list_entry *first, *item;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct udev_device *parent = NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *node, *pttype, *devtype;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int home_nr = -1, srv_nr = -1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bool home_rw = true, srv_rw = true;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering blkid_partlist pl;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int r, k;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering dev_t pn;
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering
97c67192eadaffe67b803ec5b991a92bb1137d0bLennart Poettering udev = udev_new();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (!udev)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_oom();
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering d = udev_device_new_from_devnum(udev, 'b', devnum);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!d)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return log_oom();
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering parent = udev_device_get_parent(d);
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (!parent) {
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_debug("Not a partitioned device, ignoring.");
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return 0;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart 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 }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Is this a disk or a partition? We only care for disks... */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!streq(devtype, "disk")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Parent isn't a raw disk, ignoring.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Does it have a device node? */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering node = udev_device_get_devnode(parent);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!node) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Parent device does not have device node, ignoring.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Root device %s.", node);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering pn = udev_device_get_devnum(parent);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (major(pn) == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering errno = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering b = blkid_new_probe_from_filename(node);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (!b) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (errno == 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return log_oom();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering log_error("Failed allocate prober: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek blkid_probe_enable_partitions(b, 1);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering blkid_probe_set_partitions_flags(b, BLKID_PARTS_ENTRY_DETAILS);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek errno = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = blkid_do_safeprobe(b);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (r == -2 || r == 1) /* no result or uncertain */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering else if (r != 0) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (errno == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering errno = EIO;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_error("Failed to probe %s: %m", node);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -errno;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering errno = 0;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = blkid_probe_lookup_value(b, "PTTYPE", &pttype, NULL);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (r != 0) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (errno == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering errno = EIO;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_error("Failed to determine partition table type of %s: %m", node);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return -errno;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering /* We only do this all for GPT... */
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen if (!streq_ptr(pttype, "gpt")) {
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_debug("Not a GPT partition table, ignoring.");
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek errno = 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek pl = blkid_probe_get_partitions(b);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (!pl) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (errno == 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return log_oom();
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_error("Failed to list partitions of %s: %m", node);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return -errno;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen e = udev_enumerate_new(udev);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!e)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_oom();
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen r = udev_enumerate_add_match_parent(e, parent);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (r < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_oom();
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = udev_enumerate_add_match_subsystem(e, "block");
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (r < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek return log_oom();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = udev_enumerate_scan_devices(e);
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek if (r < 0) {
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek log_error("Failed to enumerate partitions on %s: %s", node, strerror(-r));
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek return r;
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek }
d93a16b81f8baa0e6a16310b210f225129347322Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek first = udev_enumerate_get_list_entry(e);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering udev_list_entry_foreach(item, first) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek _cleanup_udev_device_unref_ struct udev_device *q;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *stype, *subnode;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering sd_id128_t type_id;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering blkid_partition pp;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering dev_t qn;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int nr;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering unsigned long long flags;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!q)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart 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);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!pp)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering flags = blkid_partition_get_flags(pp);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* Ignore partitions that are not marked for automatic
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering * mounting on discovery */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (flags & GPT_FLAG_NO_AUTO)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering nr = blkid_partition_get_partno(pp);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (nr < 0)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering stype = blkid_partition_get_type_string(pp);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!stype)
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering continue;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart 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)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = k;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering } else if (sd_id128_equal(type_id, GPT_HOME)) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* We only care for the first /home partition */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (home && nr >= home_nr)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering continue;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering home_nr = nr;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering home_rw = !(flags & GPT_FLAG_READ_ONLY),
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering free(home);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering home = strdup(subnode);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (!home)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return log_oom();
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering } else if (sd_id128_equal(type_id, GPT_SRV)) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* We only care for the first /srv partition */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (srv && nr >= srv_nr)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering continue;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering srv_nr = nr;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering srv_rw = !(flags & GPT_FLAG_READ_ONLY),
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering free(srv);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering srv = strdup(node);
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering if (!srv)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_oom();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (home) {
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering k = probe_and_add_mount("home", home, "/home", home_rw, "Home Partition", SPECIAL_LOCAL_FS_TARGET);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if (k < 0)
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering r = k;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (srv) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering k = probe_and_add_mount("srv", srv, "/srv", srv_rw, "Server Data Partition", SPECIAL_LOCAL_FS_TARGET);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (k < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = k;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return r;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int get_btrfs_block_device(const char *path, dev_t *dev) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering struct btrfs_ioctl_fs_info_args fsi = {};
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering _cleanup_close_ int fd = -1;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering uint64_t id;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(path);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering assert(dev);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering fd = open(path, O_DIRECTORY|O_CLOEXEC);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (fd < 0)
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann return -errno;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek if (ioctl(fd, BTRFS_IOC_FS_INFO, &fsi) < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return -errno;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* We won't do this for btrfs RAID */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (fsi.num_devices != 1)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering for (id = 1; id <= fsi.max_id; id++) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering struct btrfs_ioctl_dev_info_args di = {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering .devid = id,
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering };
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering struct stat st;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (ioctl(fd, BTRFS_IOC_DEV_INFO, &di) < 0) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (errno == ENODEV)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering continue;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering return -errno;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (stat((char*) di.path, &st) < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -errno;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (!S_ISBLK(st.st_mode))
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -ENODEV;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (major(st.st_rdev) == 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENODEV;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek *dev = st.st_rdev;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 1;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return -ENODEV;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekstatic int get_block_device(const char *path, dev_t *dev) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen struct stat st;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen struct statfs sfs;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen assert(path);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen assert(dev);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (lstat(path, &st))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering return -errno;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (major(st.st_dev) != 0) {
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen *dev = st.st_dev;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen return 1;
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering if (statfs(path, &sfs) < 0)
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering return -errno;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC))
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return get_btrfs_block_device(path, dev);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykrynstatic int parse_proc_cmdline_item(const char *key, const char *value) {
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int r;
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn assert(key);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn if (STR_IN_SET(key, "systemd.gpt_auto", "rd.systemd.gpt_auto") && value) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek r = parse_boolean(value);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (r < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_warning("Failed to parse gpt-auto switch %s. Ignoring.", value);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek arg_enabled = r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen } else if (streq(key, "root") && value) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen /* Disable root disk logic if there's a root= value
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen * specified (unless it happens to be "gpt-auto") */
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen arg_root_enabled = streq(value, "gpt-auto");
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen } else if (streq(key, "rw") && !value)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen arg_root_rw = true;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen else if (streq(key, "ro") && !value)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen arg_root_rw = false;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek return 0;
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek}
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmekstatic int add_root_mount(void) {
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek#ifdef ENABLE_EFI
48d45d2b49d2adb870cd5f1bc7cb389b33655f1cZbigniew Jędrzejewski-Szmek int r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (!is_efi_boot()) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_debug("Not a EFI boot, not creating root mount.");
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = efi_loader_get_device_part_uuid(NULL);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r == -ENOENT) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_debug("EFI loader partition unknown, exiting.");
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek } else if (r < 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek log_error("Failed to read ESP partition UUID: %s", strerror(-r));
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek /* OK, we have an ESP partition, this is fantastic, so let's
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * wait for a root device to show up. A udev rule will create
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek * the link for us under the right name. */
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return add_mount(
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "root",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "/dev/gpt-auto-root",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek in_initrd() ? "/sysroot" : "/",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek NULL,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek arg_root_rw,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek "Root Partition",
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek in_initrd() ? SPECIAL_INITRD_ROOT_FS_TARGET : SPECIAL_LOCAL_FS_TARGET);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#else
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#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);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (r < 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_error("Failed to determine block device of root file system: %s", strerror(-r));
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return r;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen } else if (r == 0) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_debug("Root file system not on a (single) block device.");
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return 0;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen }
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return enumerate_partitions(devno);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenint main(int argc, char *argv[]) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen int r = 0;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (argc > 1 && argc != 4) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_error("This program takes three or no arguments.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return EXIT_FAILURE;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (argc > 1)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen arg_dest = argv[3];
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen log_set_target(LOG_TARGET_SAFE);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_parse_environment();
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering log_open();
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen umask(0022);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (detect_container(NULL) > 0) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_debug("In a container, exiting.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return EXIT_SUCCESS;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering return EXIT_FAILURE;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!arg_enabled) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_debug("Disabled, exiting.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return EXIT_SUCCESS;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen }
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (arg_root_enabled)
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen r = add_root_mount();
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (!in_initrd()) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen int k;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering k = add_mounts();
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (k < 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = k;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering }
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering