cryptsetup-generator.c revision 66aaf85e178492c27f699d9c94c84a98466a1435
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen/***
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen This file is part of systemd.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Copyright 2010 Lennart Poettering
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is free software; you can redistribute it and/or modify it
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen under the terms of the GNU Lesser General Public License as published by
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen (at your option) any later version.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen systemd is distributed in the hope that it will be useful, but
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen Lesser General Public License for more details.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen You should have received a copy of the GNU Lesser General Public License
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen***/
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <string.h>
2a73e0d39a9bec82c3800071e375d27164727e71Tom Gundersen#include <errno.h>
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include <unistd.h>
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen#include "log.h"
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen#include "util.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include "unit-name.h"
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen#include "mkdir.h"
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "strv.h"
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen#include "fileio.h"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic const char *arg_dest = "/tmp";
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic bool arg_enabled = true;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic bool arg_read_crypttab = true;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic char **arg_disks = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersenstatic char **arg_options = NULL;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersenstatic char *arg_keyfile = NULL;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
3aeb37bc4f32b5edc334f2ac7c5d3c7b0a121328Tom Gundersenstatic bool has_option(const char *haystack, const char *needle) {
c6f7c917a1b494d4455800823472227463f87438Tom Gundersen const char *f = haystack;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering size_t l;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen assert(needle);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!haystack)
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen return false;
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen l = strlen(needle);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen while ((f = strstr(f, needle))) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen if (f > haystack && f[-1] != ',') {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen f++;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen continue;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek }
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek if (f[l] != 0 && f[l] != ',') {
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek f++;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek continue;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek }
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek return true;
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek }
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen return false;
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen}
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersenstatic int create_disk(
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *name,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *device,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *password,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen const char *options) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_free_ char *p = NULL, *n = NULL, *d = NULL, *u = NULL, *to = NULL, *e = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_fclose_ FILE *f = NULL;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen bool noauto, nofail, tmp, swap;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen char *from;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen int r;
977085794d2996320e345433403de75f662b0622Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen assert(name);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen assert(device);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen noauto = has_option(options, "noauto");
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen nofail = has_option(options, "nofail");
5b9d4dc05560ddda89e48b6b39365824b15e1300Tom Gundersen tmp = has_option(options, "tmp");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen swap = has_option(options, "swap");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (tmp && swap) {
977085794d2996320e345433403de75f662b0622Tom Gundersen log_error("Device '%s' cannot be both 'tmp' and 'swap'. Ignoring.", name);
977085794d2996320e345433403de75f662b0622Tom Gundersen return -EINVAL;
977085794d2996320e345433403de75f662b0622Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen e = unit_name_escape(name);
866ee3682213789f85b877700457fdca05695a0eTom Gundersen if (!e)
866ee3682213789f85b877700457fdca05695a0eTom Gundersen return log_oom();
866ee3682213789f85b877700457fdca05695a0eTom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen n = unit_name_build("systemd-cryptsetup", e, ".service");
866ee3682213789f85b877700457fdca05695a0eTom Gundersen if (!n)
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen return log_oom();
977085794d2996320e345433403de75f662b0622Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen p = strjoin(arg_dest, "/", n, NULL);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen if (!p)
866ee3682213789f85b877700457fdca05695a0eTom Gundersen return log_oom();
866ee3682213789f85b877700457fdca05695a0eTom Gundersen
866ee3682213789f85b877700457fdca05695a0eTom Gundersen u = fstab_node_to_udev_node(device);
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen if (!u)
866ee3682213789f85b877700457fdca05695a0eTom Gundersen return log_oom();
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
977085794d2996320e345433403de75f662b0622Tom Gundersen d = unit_name_from_path(u, ".device");
977085794d2996320e345433403de75f662b0622Tom Gundersen if (!d)
977085794d2996320e345433403de75f662b0622Tom Gundersen return log_oom();
977085794d2996320e345433403de75f662b0622Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen f = fopen(p, "wxe");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!f) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_error("Failed to create unit file %s: %m", p);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -errno;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fputs(
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "# Automatically generated by systemd-cryptsetup-generator\n\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "[Unit]\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "Description=Cryptography Setup for %I\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "Documentation=man:crypttab(5) man:systemd-cryptsetup-generator(8) man:systemd-cryptsetup@.service(8)\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "SourcePath=/etc/crypttab\n"
d2df0d0ed3a88e491405b403e6022e6619750130Tom Gundersen "DefaultDependencies=no\n"
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen "Conflicts=umount.target\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "BindsTo=dev-mapper-%i.device\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "IgnoreOnIsolate=true\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "After=systemd-readahead-collect.service systemd-readahead-replay.service\n",
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen f);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!nofail)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fprintf(f,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "Before=cryptsetup.target\n");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering if (password) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (STR_IN_SET(password, "/dev/urandom", "/dev/random", "/dev/hw_random"))
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen fputs("After=systemd-random-seed.service\n", f);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen else if (!streq(password, "-") && !streq(password, "none")) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_free_ char *uu;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen uu = fstab_node_to_udev_node(password);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!uu)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return log_oom();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (is_device_path(uu)) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen _cleanup_free_ char *dd;
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek
6e37cd2f4af8928d905203108a4331e375d7127cThomas Hindoe Paaboel Andersen dd = unit_name_from_path(uu, ".device");
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!dd)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return log_oom();
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen fprintf(f, "After=%1$s\nRequires=%1$s\n", dd);
187dc6e554f2d5b4b5a3bee72c73ff5df6418aa6Thomas Hindoe Paaboel Andersen } else
6916ec29afd488d91e7e0fcbcc2e006b4e5f28dfTom Gundersen fprintf(f, "RequiresMountsFor=%s\n", password);
6916ec29afd488d91e7e0fcbcc2e006b4e5f28dfTom Gundersen }
6916ec29afd488d91e7e0fcbcc2e006b4e5f28dfTom Gundersen }
6916ec29afd488d91e7e0fcbcc2e006b4e5f28dfTom Gundersen
6916ec29afd488d91e7e0fcbcc2e006b4e5f28dfTom Gundersen if (is_device_path(u))
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fprintf(f,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "BindsTo=%s\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "After=%s\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "Before=umount.target\n",
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen d, d);
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek else
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fprintf(f,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "RequiresMountsFor=%s\n",
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen u);
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek fprintf(f,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "\n[Service]\n"
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen "Type=oneshot\n"
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen "RemainAfterExit=yes\n"
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen "TimeoutSec=0\n" /* the binary handles timeouts anyway */
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen "ExecStart=" SYSTEMD_CRYPTSETUP_PATH " attach '%s' '%s' '%s' '%s'\n"
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek "ExecStop=" SYSTEMD_CRYPTSETUP_PATH " detach '%s'\n",
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek name, u, strempty(password), strempty(options),
e9f3d2d508bfd9fb5b54e82994bda365a71eb864Zbigniew Jędrzejewski-Szmek name);
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek if (tmp)
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek fprintf(f,
36f822c4bd077f9121757e24b6516e5c7ada63b5Zbigniew Jędrzejewski-Szmek "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
98a375f6d5cac24eb80d6d4e00699851324afdecTom Gundersen name);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (swap)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fprintf(f,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
ecb08ec6a5c52f2d940f3b8147e2a480affd46e1Zbigniew Jędrzejewski-Szmek name);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen fflush(f);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (ferror(f)) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_error("Failed to write file %s: %m", p);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return -errno;
f8a0bb5285024b6ce372c3157e761e6543ebdcd2Andreas Henriksson }
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen from = strappenda("../", n);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (!noauto) {
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering to = strjoin(arg_dest, "/", d, ".wants/", n, NULL);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (!to)
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering return log_oom();
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen mkdir_parents_label(to, 0755);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (symlink(from, to) < 0) {
ff83aac3647e21f31ac5e2b575ec1285dc585f6bTom Gundersen log_error("Failed to create symlink %s: %m", to);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return -errno;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen free(to);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (!nofail)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen to = strjoin(arg_dest, "/cryptsetup.target.requires/", n, NULL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen else
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen to = strjoin(arg_dest, "/cryptsetup.target.wants/", n, NULL);
edf029b7fd9a5853a87d3ca99aac2922bb8a277eTom Gundersen if (!to)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return log_oom();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen mkdir_parents_label(to, 0755);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen if (symlink(from, to) < 0) {
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen log_error("Failed to create symlink %s: %m", to);
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen return -errno;
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen }
97f2d76d4f4dfab8b0629c09926a05a1e5621125Tom Gundersen
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek free(to);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen to = strjoin(arg_dest, "/dev-mapper-", e, ".device.requires/", n, NULL);
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek if (!to)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return log_oom();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen mkdir_parents_label(to, 0755);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (symlink(from, to) < 0) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_error("Failed to create symlink %s: %m", to);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return -errno;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!noauto && !nofail) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen free(p);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf", NULL);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (!p)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen return log_oom();
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
2ad8416dd057e7e3185169609ca3006e7649f576Zbigniew Jędrzejewski-Szmek mkdir_parents_label(p, 0755);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen r = write_string_file(p,
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "# Automatically generated by systemd-cryptsetup-generator\n\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "[Unit]\n"
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen "JobTimeoutSec=0\n"); /* the binary handles timeouts anyway */
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (r < 0) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_error("Failed to write device drop-in: %s", strerror(-r));
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen return r;
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen }
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen }
edbb03e95a3c31bf719d5c6c46eec14d0bcb9c8fTom Gundersen
505f8da7325591defe5f751f328bd26915267602Tom Gundersen return 0;
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen}
9b1c2626cef16722603bded9bb52033aba34dd74Tom Gundersen
bf175aafd20c9ef974709ef12c5acf836121af33Tom Gundersenstatic int parse_proc_cmdline_item(const char *key, const char *value) {
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen int r;
b3e013148603aa670bc2c060ac63d48e54d76fc2Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (STR_IN_SET(key, "luks", "rd.luks") && value) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen r = parse_boolean(value);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (r < 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_warning("Failed to parse luks switch %s. Ignoring.", value);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen else
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen arg_enabled = r;
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen } else if (STR_IN_SET(key, "luks.crypttab", "rd.luks.crypttab") && value) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen r = parse_boolean(value);
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (r < 0)
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen log_warning("Failed to parse luks crypttab switch %s. Ignoring.", value);
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen else
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen arg_read_crypttab = r;
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen } else if (STR_IN_SET(key, "luks.uuid", "rd.luks.uuid") && value) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (strv_extend(&arg_disks, value) < 0)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return log_oom();
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen } else if (STR_IN_SET(key, "luks.options", "rd.luks.options") && value) {
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen
f1ac700248f231b7bdac2aafe8c35650efddb89fTom Gundersen if (strv_extend(&arg_options, value) < 0)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return log_oom();
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen } else if (STR_IN_SET(key, "luks.key", "rd.luks.key") && value) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen free(arg_keyfile);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen arg_keyfile = strdup(value);
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering if (!arg_keyfile)
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return log_oom();
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering } else if (startswith(key, "luks.") || startswith(key, "rd.luks."))
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen log_warning("Unknown kernel switch %s. Ignoring.", key);
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering return 0;
b5db00e52ee2e20578839e4e4488f7b9af9abc38Umut Tezduyar Lindskog}
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
55428d84f31b52da1c50b7469f14e15740547f20Tom Gundersenint main(int argc, char *argv[]) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen _cleanup_strv_free_ char **disks_done = NULL;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering _cleanup_fclose_ FILE *f = NULL;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering unsigned n = 0;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen int r = EXIT_FAILURE, r2 = EXIT_FAILURE;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen char **i;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (argc > 1 && argc != 4) {
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen log_error("This program takes three or no arguments.");
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen return EXIT_FAILURE;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen }
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (argc > 1)
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen arg_dest = argv[1];
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen log_set_target(LOG_TARGET_SAFE);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen log_parse_environment();
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen log_open();
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen umask(0022);
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen if (parse_proc_cmdline(parse_proc_cmdline_item) < 0)
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen goto cleanup;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen if (!arg_enabled) {
977085794d2996320e345433403de75f662b0622Tom Gundersen r = r2 = EXIT_SUCCESS;
977085794d2996320e345433403de75f662b0622Tom Gundersen goto cleanup;
977085794d2996320e345433403de75f662b0622Tom Gundersen }
977085794d2996320e345433403de75f662b0622Tom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen strv_uniq(arg_disks);
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (arg_read_crypttab) {
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen struct stat st;
733f7a2c69c794a81978a08a79916c224ba355a6Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen f = fopen("/etc/crypttab", "re");
733f7a2c69c794a81978a08a79916c224ba355a6Tom Gundersen if (!f) {
733f7a2c69c794a81978a08a79916c224ba355a6Tom Gundersen if (errno == ENOENT)
733f7a2c69c794a81978a08a79916c224ba355a6Tom Gundersen r = EXIT_SUCCESS;
a501033335ed402c8f7e86fe41a15531ba69abd7Tom Gundersen else
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen log_error("Failed to open /etc/crypttab: %m");
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen goto next;
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen if (fstat(fileno(f), &st) < 0) {
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen log_error("Failed to stat /etc/crypttab: %m");
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen goto next;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen }
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen /* If we readd support for specifying passphrases
f61942250a43a123580d7bbe5d7873dc5118ed97Tom Gundersen * directly in crypttabe we should upgrade the warning
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen * below, though possibly only if a passphrase is
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen * specified directly. */
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (st.st_mode & 0005)
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen log_debug("/etc/crypttab is world-readable. This is usually not a good idea.");
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen for (;;) {
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen char line[LINE_MAX], *l;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen _cleanup_free_ char *name = NULL, *device = NULL, *password = NULL, *options = NULL;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen int k;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (!fgets(line, sizeof(line), f))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen break;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen n++;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen l = strstrip(line);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (*l == '#' || *l == 0)
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen continue;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen k = sscanf(l, "%ms %ms %ms %ms", &name, &device, &password, &options);
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (k < 2 || k > 4) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen log_error("Failed to parse /etc/crypttab:%u, ignoring.", n);
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen continue;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen }
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen /*
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen If options are specified on the kernel commandline, let them override
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen the ones from crypttab.
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen */
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen STRV_FOREACH(i, arg_options) {
3e137a1b9a0eac2bf43d493d3302c3c959b6ccdbTom Gundersen _cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL;
daeb71a36a98834664e4d95773a3629b746f4db8Tom Gundersen const char *p = *i;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen k = sscanf(p, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options);
92d927f850d4b668b44f3e5f41e266d934d03726Tom Gundersen if (k == 2 && streq(proc_uuid, device + 5)) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen free(options);
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen options = strdup(p);
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen if (!proc_options) {
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen log_oom();
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen goto cleanup;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen }
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen }
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen }
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen if (arg_disks) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen /*
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen If luks UUIDs are specified on the kernel command line, use them as a filter
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen for /etc/crypttab and only generate units for those.
a669ea9860900d5cdebbc4cb9aaea72db7e28a02Tom Gundersen */
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen STRV_FOREACH(i, arg_disks) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen _cleanup_free_ char *proc_device = NULL, *proc_name = NULL;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen const char *p = *i;
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen if (startswith(p, "luks-"))
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen p += 5;
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen
16b9b87aeee9353b5b8dae6089a69752422a5b09Tom Gundersen proc_name = strappend("luks-", p);
d2df0d0ed3a88e491405b403e6022e6619750130Tom Gundersen proc_device = strappend("UUID=", p);
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen
d2df0d0ed3a88e491405b403e6022e6619750130Tom Gundersen if (!proc_name || !proc_device) {
5fde13d748749f0e06e2e6cdd15f0980a79ea82cTom Gundersen log_oom();
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen goto cleanup;
43b3a5ef61859f06cdbaf26765cab8e1adac4296Tom Gundersen }
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen
af6f0d422c521374ee6a2dd92df5935a5a476ae5Tom Gundersen if (streq(proc_device, device) || streq(proc_name, name)) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (create_disk(name, device, password, options) < 0)
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen goto cleanup;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen if (strv_extend(&disks_done, p) < 0) {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen log_oom();
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen goto cleanup;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen } else if (create_disk(name, device, password, options) < 0)
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen goto cleanup;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen }
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen r = EXIT_SUCCESS;
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersennext:
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen STRV_FOREACH(i, arg_disks) {
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen /*
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen Generate units for those UUIDs, which were specified
847a8a5fed4d265dfa659917515c6f9bd1b8d5c4Tom Gundersen on the kernel command line and not yet written.
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack */
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen _cleanup_free_ char *name = NULL, *device = NULL, *options = NULL;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen const char *p = *i;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (startswith(p, "luks-"))
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen p += 5;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack if (strv_contains(disks_done, p))
e51660ae56bb747ece2cab8fe6eec37f4d06a438Tom Gundersen continue;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen name = strappend("luks-", p);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen device = strappend("UUID=", p);
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen if (!name || !device) {
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen log_oom();
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen goto cleanup;
be32eb9b7fbcb22e4b648086d644135e38279633Tom Gundersen }
if (arg_options) {
/*
If options are specified on the kernel commandline, use them.
*/
char **j;
STRV_FOREACH(j, arg_options) {
_cleanup_free_ char *proc_uuid = NULL, *proc_options = NULL;
const char *s = *j;
int k;
k = sscanf(s, "%m[0-9a-fA-F-]=%ms", &proc_uuid, &proc_options);
if (k == 2) {
if (streq(proc_uuid, device + 5)) {
free(options);
options = proc_options;
proc_options = NULL;
}
} else if (!options) {
/*
Fall back to options without a specified UUID
*/
options = strdup(s);
if (!options) {
log_oom();
goto cleanup;
};
}
}
}
if (!options) {
options = strdup("timeout=0");
if (!options) {
log_oom();
goto cleanup;
}
}
if (create_disk(name, device, arg_keyfile, options) < 0)
goto cleanup;
}
r2 = EXIT_SUCCESS;
cleanup:
strv_free(arg_disks);
strv_free(arg_options);
free(arg_keyfile);
return r != EXIT_SUCCESS ? r : r2;
}