]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "unit-name.h"
33 static const char *arg_dest
= "/tmp";
34 static bool arg_enabled
= true;
35 static bool arg_read_crypttab
= true;
36 static char **arg_proc_cmdline_disks
= NULL
;
38 static bool has_option(const char *haystack
, const char *needle
) {
39 const char *f
= haystack
;
49 while ((f
= strstr(f
, needle
))) {
51 if (f
> haystack
&& f
[-1] != ',') {
56 if (f
[l
] != 0 && f
[l
] != ',') {
67 static int create_disk(
71 const char *options
) {
73 char *p
= NULL
, *n
= NULL
, *d
= NULL
, *u
= NULL
, *from
= NULL
, *to
= NULL
, *e
= NULL
;
81 noauto
= has_option(options
, "noauto");
82 nofail
= has_option(options
, "nofail");
84 n
= unit_name_from_path_instance("systemd-cryptsetup", name
, ".service");
87 log_error("Failed to allocate unit name.");
91 p
= join(arg_dest
, "/", n
, NULL
);
94 log_error("Failed to allocate unit file name.");
98 u
= fstab_node_to_udev_node(device
);
101 log_error("Failed to allocate device node.");
105 d
= unit_name_from_path(u
, ".device");
108 log_error("Failed to allocate device name.");
115 log_error("Failed to create unit file: %m");
120 "# Automatically generated by systemd-cryptsetup-generator\n\n"
122 "Description=Cryptography Setup for %%I\n"
123 "Documentation=man:systemd-cryptsetup@.service(8)\n"
124 "SourcePath=/etc/crypttab\n"
125 "Conflicts=umount.target\n"
126 "DefaultDependencies=no\n"
127 "BindTo=%s dev-mapper-%%i.device\n"
128 "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n"
129 "Before=umount.target\n",
134 "Before=cryptsetup.target\n");
136 if (password
&& (streq(password
, "/dev/urandom") ||
137 streq(password
, "/dev/random") ||
138 streq(password
, "/dev/hw_random")))
139 fputs("After=systemd-random-seed-load.service\n", f
);
141 fputs("Before=local-fs.target\n", f
);
146 "RemainAfterExit=yes\n"
147 "TimeoutSec=0\n" /* the binary handles timeouts anyway */
148 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" attach '%s' '%s' '%s' '%s'\n"
149 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
150 name
, u
, strempty(password
), strempty(options
),
153 if (has_option(options
, "tmp"))
155 "ExecStartPost=/sbin/mke2fs '/dev/mapper/%s'\n",
158 if (has_option(options
, "swap"))
160 "ExecStartPost=/sbin/mkswap '/dev/mapper/%s'\n",
167 log_error("Failed to write file: %m");
171 if (asprintf(&from
, "../%s", n
) < 0) {
178 to
= join(arg_dest
, "/", d
, ".wants/", n
, NULL
);
184 mkdir_parents_label(to
, 0755);
185 if (symlink(from
, to
) < 0) {
186 log_error("Failed to create symlink '%s' to '%s': %m", from
, to
);
194 to
= join(arg_dest
, "/cryptsetup.target.requires/", n
, NULL
);
196 to
= join(arg_dest
, "/cryptsetup.target.wants/", n
, NULL
);
202 mkdir_parents_label(to
, 0755);
203 if (symlink(from
, to
) < 0) {
204 log_error("Failed to create symlink '%s' to '%s': %m", from
, to
);
213 e
= unit_name_escape(name
);
214 to
= join(arg_dest
, "/dev-mapper-", e
, ".device.requires/", n
, NULL
);
220 mkdir_parents_label(to
, 0755);
221 if (symlink(from
, to
) < 0) {
222 log_error("Failed to create symlink '%s' to '%s': %m", from
, to
);
244 static int parse_proc_cmdline(void) {
245 char *line
, *w
, *state
;
249 if (detect_container(NULL
) > 0)
252 r
= read_one_line_file("/proc/cmdline", &line
);
254 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r
));
258 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
261 word
= strndup(w
, l
);
267 if (startswith(word
, "luks=")) {
268 r
= parse_boolean(word
+ 5);
270 log_warning("Failed to parse luks switch %s. Ignoring.", word
+ 5);
274 } else if (startswith(word
, "rd.luks=")) {
277 r
= parse_boolean(word
+ 8);
279 log_warning("Failed to parse luks switch %s. Ignoring.", word
+ 8);
284 } else if (startswith(word
, "luks.crypttab=")) {
285 r
= parse_boolean(word
+ 14);
287 log_warning("Failed to parse luks crypttab switch %s. Ignoring.", word
+ 14);
289 arg_read_crypttab
= r
;
291 } else if (startswith(word
, "rd.luks.crypttab=")) {
294 r
= parse_boolean(word
+ 17);
296 log_warning("Failed to parse luks crypttab switch %s. Ignoring.", word
+ 17);
298 arg_read_crypttab
= r
;
301 } else if (startswith(word
, "luks.uuid=")) {
304 t
= strv_append(arg_proc_cmdline_disks
, word
+ 10);
306 log_error("Out of memory");
310 strv_free(arg_proc_cmdline_disks
);
311 arg_proc_cmdline_disks
= t
;
313 } else if (startswith(word
, "rd.luks.uuid=")) {
318 t
= strv_append(arg_proc_cmdline_disks
, word
+ 13);
320 log_error("Out of memory");
324 strv_free(arg_proc_cmdline_disks
);
325 arg_proc_cmdline_disks
= t
;
328 } else if (startswith(word
, "luks.") ||
329 (in_initrd() && startswith(word
, "rd.luks."))) {
331 log_warning("Unknown kernel switch %s. Ignoring.", word
);
344 int main(int argc
, char *argv
[]) {
346 int r
= EXIT_SUCCESS
;
350 if (argc
> 1 && argc
!= 4) {
351 log_error("This program takes three or no arguments.");
358 log_set_target(LOG_TARGET_SAFE
);
359 log_parse_environment();
364 if (parse_proc_cmdline() < 0)
372 STRV_FOREACH(i
, arg_proc_cmdline_disks
) {
376 if (startswith(p
, "luks-"))
379 name
= strappend("luks-", *i
);
380 device
= strappend("UUID=", *i
);
382 if (!name
|| !device
) {
383 log_error("Out of memory");
390 if (create_disk(name
, device
, NULL
, NULL
) < 0)
397 if (!arg_read_crypttab
)
400 f
= fopen("/etc/crypttab", "re");
407 log_error("Failed to open /etc/crypttab: %m");
414 char line
[LINE_MAX
], *l
;
415 char *name
= NULL
, *device
= NULL
, *password
= NULL
, *options
= NULL
;
418 if (!fgets(line
, sizeof(line
), f
))
424 if (*l
== '#' || *l
== 0)
427 k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &password
, &options
);
428 if (k
< 2 || k
> 4) {
429 log_error("Failed to parse /etc/crypttab:%u, ignoring.", n
);
434 if (create_disk(name
, device
, password
, options
) < 0)
448 strv_free(arg_proc_cmdline_disks
);