chiark / gitweb /
efi-boot-generator: simplify
[elogind.git] / src / efi-boot-generator / efi-boot-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <unistd.h>
23 #include <stdlib.h>
24
25 #include "efivars.h"
26 #include "path-util.h"
27 #include "util.h"
28 #include "mkdir.h"
29 #include "unit-name.h"
30
31 static const char *arg_dest = "/tmp";
32
33 int main(int argc, char *argv[]) {
34         int r = EXIT_SUCCESS;
35         sd_id128_t id;
36         _cleanup_free_ char *what = NULL, *fsck = NULL;
37         char *name;
38         _cleanup_fclose_ FILE *f = NULL, *f2 = NULL;
39
40         if (argc > 1 && argc != 4) {
41                 log_error("This program takes three or no arguments.");
42                 return EXIT_FAILURE;
43         }
44
45         if (argc > 1)
46                 arg_dest = argv[3];
47
48         log_set_target(LOG_TARGET_SAFE);
49         log_parse_environment();
50         log_open();
51
52         umask(0022);
53
54         if (!is_efi_boot())
55                 return EXIT_SUCCESS;
56
57         if (dir_is_empty("/boot") <= 0)
58                 return EXIT_SUCCESS;
59
60         r = efi_loader_get_device_part_uuid(&id);
61         if (r == -ENOENT)
62                 return EXIT_SUCCESS;
63         if (r < 0) {
64                 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
65                 return EXIT_FAILURE;
66         }
67
68         name = strappenda(arg_dest, "/boot.mount");
69         f = fopen(name, "wxe");
70         if (!f) {
71                 log_error("Failed to create mount unit file %s: %m", name);
72                 return EXIT_FAILURE;
73         }
74
75         r = asprintf(&what,
76                      "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
77                      SD_ID128_FORMAT_VAL(id));
78         if (r < 0) {
79                 log_oom();
80                 return EXIT_FAILURE;
81         }
82
83         fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
84         if (!fsck) {
85                 log_oom();
86                 return EXIT_FAILURE;
87         }
88
89         fprintf(f,
90                 "# Automatially generated by systemd-efi-boot-generator\n\n"
91                 "[Unit]\n"
92                 "Description=EFI System Partition\n"
93                 "Requires=%s\n"
94                 "After=%s\n"
95                 "\n"
96                 "[Mount]\n"
97                 "Where=/boot\n"
98                 "What=%s\n"
99                 "Options=umask=0077\n",
100                 fsck, fsck, what);
101
102         name = strappenda(arg_dest, "/boot.automount");
103         f2 = fopen(name, "wxe");
104         if (!f2) {
105                 log_error("Failed to create automount unit file %s: %m", name);
106                 return EXIT_FAILURE;
107         }
108
109         fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
110               "[Unit]\n"
111               "Description=EFI System Partition Automount\n\n"
112               "[Automount]\n"
113               "Where=/boot\n", f2);
114
115         name = strappenda(arg_dest, "/local-fs.target.wants/boot.automount");
116         mkdir_parents(name, 0755);
117
118         if (symlink("../boot.automount", name) < 0) {
119                 log_error("Failed to create symlink %s: %m", name);
120                 return EXIT_FAILURE;
121         }
122
123         return EXIT_SUCCESS;
124 }