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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "unit-name.h"
30 const char *arg_dest
= "/tmp";
32 static bool has_option(const char *haystack
, const char *needle
) {
33 const char *f
= haystack
;
38 while ((f
= strstr(f
, needle
))) {
40 if (f
> haystack
&& f
[-1] != ',') {
45 if (f
[l
] != 0 && f
[l
] == ',') {
56 static int create_disk(
60 const char *options
) {
62 char *p
= NULL
, *n
= NULL
, *d
= NULL
, *u
= NULL
, *from
= NULL
, *to
= NULL
;
69 if (!(n
= unit_name_build_escape("cryptsetup", name
, ".service"))) {
71 log_error("Failed to allocate unit name.");
75 if (asprintf(&p
, "%s/%s", arg_dest
, n
) < 0) {
77 log_error("Failed to allocate unit file name.");
81 if (!(u
= fstab_node_to_udev_node(device
))) {
83 log_error("Failed to allocate device node.");
87 if (!(d
= unit_name_from_path(u
, ".device"))) {
89 log_error("Failed to allocate device name.");
93 if (!(f
= fopen(p
, "wxe"))) {
95 log_error("Failed to create unit file: %m");
101 "Description=Cryptography Setup for %%f\n"
102 "DefaultDependencies=no\n"
104 "After=systemd-readahead-collect.service systemd-readahead-replay.service %s\n"
105 "Before=dev-mapper-%%i.device shutdown.target\n",
108 if (password
&& (streq(password
, "/dev/urandom") ||
109 streq(password
, "/dev/random") ||
110 streq(password
, "/dev/hw_random")))
112 "After=systemd-random-seed-load.service\n");
117 "RemainAfterExit=yes\n"
118 "ExecStart=" SYSTEMD_CRYPTSETUP_PATH
" %s '%s' '%s' '%s' '%s'\n"
119 "ExecStop=" SYSTEMD_CRYPTSETUP_PATH
" detach '%s'\n",
120 options
&& has_option(options
, "swap") ? "format-and-attach" : "attach",
121 name
, u
, strempty(password
), strempty(options
),
124 if (options
&& has_option(options
, "tmp"))
126 "ExecStartPost=/sbin/mke2fs '%s'",
129 if (options
&& has_option(options
, "swap"))
131 "ExecStartPost=/sbin/mkswap '%s'",
138 log_error("Failed to write file: %m");
142 if (!options
|| !has_option(options
, "noauto")) {
144 if (asprintf(&to
, "%s/%s.wants/%s", arg_dest
, d
, n
) < 0) {
149 if (asprintf(&from
, "../%s", n
) < 0) {
154 mkdir_parents(to
, 0755);
156 if (symlink(from
, to
) < 0) {
157 log_error("Failed to create symlink '%s' to '%s': %m", from
, to
);
179 int main(int argc
, char *argv
[]) {
181 int r
= EXIT_SUCCESS
;
185 log_error("This program takes one or no arguments.");
191 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG
);
192 log_parse_environment();
195 if (!(f
= fopen("/etc/crypttab", "re"))) {
201 log_error("Failed to open /etc/crypttab: %m");
208 char line
[LINE_MAX
], *l
;
209 char *name
= NULL
, *device
= NULL
, *password
= NULL
, *options
= NULL
;
212 if (!(fgets(line
, sizeof(line
), f
)))
218 if (*l
== '#' || *l
== 0)
221 if ((k
= sscanf(l
, "%ms %ms %ms %ms", &name
, &device
, &password
, &options
)) < 2 || k
> 4) {
222 log_error("Failed to parse /etc/crypttab:%u, ignoring.", n
);
227 if (create_disk(name
, device
, password
, options
) < 0)