gpt-auto-generator.c revision 42225ff62fe1c2577bbb344d21f1cc5a0afcd2cc
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Lennart 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 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 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/>.
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic bool arg_enabled = true;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poetteringstatic bool arg_root_enabled = true;
24710c48ed16be5fa461fbb303a744a907541dafLennart Poetteringstatic bool arg_root_rw = false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int add_cryptsetup(const char *id, const char *what, bool rw, char **device) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *to = NULL;
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen return log_error_errno(r, "Failed to generate unit name: %m");
f9ebb22ab4758bc5bbaaf8eeead74b5b4f81d5c3Lennart Poettering r = unit_name_build("systemd-cryptsetup", e, ".service", &n);
f9ebb22ab4758bc5bbaaf8eeead74b5b4f81d5c3Lennart Poettering return log_error_errno(r, "Failed to generate unit name: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_error_errno(errno, "Failed to create unit file %s: %m", p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "# Automatically generated by systemd-gpt-auto-generator\n\n"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen "Description=Cryptography Setup for %%I\n"
9df3ba6c6cb65eecec06f39dfe85a3596cedac4eTom Gundersen "Documentation=man:systemd-gpt-auto-generator(8) man:systemd-cryptsetup@.service(8)\n"
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering "DefaultDependencies=no\n"
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering "Conflicts=umount.target\n"
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering "BindsTo=dev-mapper-%%i.device %s\n"
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering "Before=umount.target cryptsetup.target\n"
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "IgnoreOnIsolate=true\n"
2c27fbca2d88214bd305272308a370a962818f1eLennart Poettering "Type=oneshot\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "RemainAfterExit=yes\n"
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "TimeoutSec=0\n" /* the binary handles timeouts anyway */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '' '%s'\n"
0db4c90afd7d9c7c8884bf8b3ec459edc74a03daDaniel Mack "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering return log_error_errno(r, "Failed to write file %s: %m", p);
a51c10485af349eb15faa4d1a63b9818bcf3e589Lennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
if (!to)
return log_oom();
if (!to)
return log_oom();
free(p);
return log_oom();
r = write_string_file(p,
if (!ret)
return log_oom();
static int add_mount(
const char *id,
const char *what,
const char *where,
const char *fstype,
bool rw,
const char *options,
const char *description,
const char *post) {
return log_oom();
fprintf(f,
if (post)
fprintf(f,
if (fstype)
if (options)
r = fflush_and_check(f);
if (post) {
if (!lnk)
return log_oom();
if (r == -ENOENT)
static int probe_and_add_mount(
const char *id,
const char *what,
const char *where,
bool rw,
const char *description,
const char *post) {
errno = 0;
if (errno == 0)
return log_oom();
return -errno;
errno = 0;
r = blkid_do_safeprobe(b);
return add_mount(
id,
what,
rw,
NULL,
post);
if (!unit)
return log_oom();
fprintf(f,
path);
r = fflush_and_check(f);
if (!lnk)
return log_oom();
#ifdef ENABLE_EFI
static int add_automount(
const char *id,
const char *what,
const char *where,
const char *fstype,
bool rw,
const char *options,
const char *description,
if (options)
if (!opt)
return log_oom();
what,
rw,
opt,
NULL);
return log_oom();
fprintf(f,
r = fflush_and_check(f);
if (!lnk)
return log_oom();
if (!is_efi_boot()) {
if (in_initrd()) {
if (detect_container() > 0) {
if (r == -ENOENT) {
errno = 0;
if (errno == 0)
return log_oom();
return -errno;
errno = 0;
r = blkid_do_safeprobe(b);
what,
if (!udev)
return log_oom();
return log_oom();
if (!name)
if (!name) {
if (!parent) {
if (!devtype) {
if (!node) {
errno = 0;
if (errno == 0)
return log_oom();
errno = 0;
r = blkid_do_safeprobe(b);
errno = 0;
errno = 0;
if (!pl) {
if (errno == 0)
return log_oom();
return log_oom();
return log_oom();
return log_oom();
r = udev_enumerate_scan_devices(e);
unsigned long long flags;
int nr;
if (!subnode)
if (!pp)
if (nr < 0)
if (!stype)
return log_oom();
return log_oom();
return log_oom();
if (boot) {
if (home) {
if (srv) {
k = probe_and_add_mount("srv", srv, "/srv", srv_rw, "Server Data Partition", SPECIAL_LOCAL_FS_TARGET);
return -errno;
return -errno;
return -ENOMEM;
d = opendir(p);
goto fallback;
return -errno;
goto fallback;
if (!found)
goto fallback;
r = read_one_line_file(q, &t);
if (r == -ENOENT)
goto fallback;
return -EINVAL;
if (maj == 0)
goto fallback;
arg_enabled = r;
arg_root_rw = true;
arg_root_rw = false;
static int add_root_mount(void) {
#ifdef ENABLE_EFI
if (!is_efi_boot()) {
if (r == -ENOENT) {
return add_mount(
"/dev/gpt-auto-root",
NULL,
NULL,
static int add_mounts(void) {
return EXIT_FAILURE;
log_open();
if (detect_container() > 0) {
return EXIT_SUCCESS;
if (!arg_enabled) {
return EXIT_SUCCESS;
if (arg_root_enabled)
r = add_root_mount();
if (!in_initrd()) {
k = add_mounts();