chiark / gitweb /
e20d3250b836b8bc5fdf52bda3908033757bc787
[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 *name = NULL, *what = NULL, *fsck = NULL;
37         _cleanup_fclose_ FILE *f = NULL;
38
39         if (argc > 1 && argc != 4) {
40                 log_error("This program takes three or no arguments.");
41                 return EXIT_FAILURE;
42         }
43
44         if (argc > 1)
45                 arg_dest = argv[3];
46
47         log_set_target(LOG_TARGET_SAFE);
48         log_parse_environment();
49         log_open();
50
51         umask(0022);
52
53         if (!is_efi_boot())
54                 return EXIT_SUCCESS;
55
56         if (dir_is_empty("/boot") <= 0)
57                 return EXIT_SUCCESS;
58
59         r = efi_loader_get_device_part_uuid(&id);
60         if (r == -ENOENT)
61                 return EXIT_SUCCESS;
62         if (r < 0) {
63                 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
64                 return EXIT_FAILURE;
65         }
66
67         name = strjoin(arg_dest, "/boot.mount", NULL);
68         if (!name) {
69                 log_oom();
70                 return EXIT_FAILURE;
71         }
72
73         f = fopen(name, "wxe");
74         if (!f) {
75                 log_error("Failed to create mount unit file %s: %m", name);
76                 return EXIT_FAILURE;
77         }
78
79         r = asprintf(&what,
80                      "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
81                      SD_ID128_FORMAT_VAL(id));
82         if (r < 0) {
83                 log_oom();
84                 return EXIT_FAILURE;
85         }
86
87         fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
88         if (!fsck) {
89                 log_oom();
90                 return EXIT_FAILURE;
91         }
92
93         fprintf(f,
94                 "# Automatially generated by systemd-efi-boot-generator\n\n"
95                 "[Unit]\n"
96                 "Description=EFI System Partition\n"
97                 "Requires=%s\n"
98                 "After=%s\n"
99                 "\n"
100                 "[Mount]\n"
101                 "Where=/boot\n"
102                 "What=%s\n"
103                 "Options=umask=0077\n",
104                 fsck, fsck, what);
105
106         free(name);
107         name = strjoin(arg_dest, "/boot.automount", NULL);
108         if (!name) {
109                 log_oom();
110                 return EXIT_FAILURE;
111         }
112
113         fclose(f);
114         f = fopen(name, "wxe");
115         if (!f) {
116                 log_error("Failed to create automount unit file %s: %m", name);
117                 return EXIT_FAILURE;
118         }
119
120         fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
121               "[Unit]\n"
122               "Description=EFI System Partition Automount\n\n"
123               "[Automount]\n"
124               "Where=/boot\n", f);
125
126         free(name);
127         name = strjoin(arg_dest, "/local-fs.target.wants/boot.automount", NULL);
128         if (!name) {
129                 log_oom();
130                 return EXIT_FAILURE;
131         }
132
133         mkdir_parents(name, 0755);
134
135         if (symlink("../boot.automount", name) < 0) {
136                 log_error("Failed to create symlink %s: %m", name);
137                 return EXIT_FAILURE;
138         }
139
140         return 0;
141 }