]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/efi-boot-generator/efi-boot-generator.c
efi: add efi boot generator that automatically mounts the ESP to /boot
[thirdparty/systemd.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
29 static const char *arg_dest = NULL;
30
31 int main(int argc, char *argv[]) {
32 int r = EXIT_SUCCESS;
33 sd_id128_t id;
34 _cleanup_free_ char *name = NULL;
35 _cleanup_fclose_ FILE *f = NULL;
36
37 if (argc > 1 && argc != 4) {
38 log_error("This program takes three or no arguments.");
39 return EXIT_FAILURE;
40 }
41
42 if (argc > 1)
43 arg_dest = argv[3];
44
45 log_set_target(LOG_TARGET_SAFE);
46 log_parse_environment();
47 log_open();
48
49 umask(0022);
50
51 if (!is_efiboot())
52 return EXIT_SUCCESS;
53
54 if (dir_is_empty("/boot") <= 0)
55 return EXIT_SUCCESS;
56
57 r = efi_get_loader_device_part_uuid(&id);
58 if (r == -ENOENT)
59 return EXIT_SUCCESS;
60 if (r < 0) {
61 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
62 return EXIT_FAILURE;
63 }
64
65 name = strjoin(arg_dest, "/boot.mount", NULL);
66 if (!name) {
67 log_oom();
68 return EXIT_FAILURE;
69 }
70
71 f = fopen(name, "wxe");
72 if (!f) {
73 log_error("Failed to create mount unit file %s: %m", name);
74 return EXIT_FAILURE;
75 }
76
77 fprintf(f,
78 "# Automatially generated by systemd-efi-boot-generator\n\n"
79 "[Mount]\n"
80 "Where=/boot\n"
81 "What=/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n"
82 "Options=umask=0077\n",
83 SD_ID128_FORMAT_VAL(id));
84
85 free(name);
86 name = strjoin(arg_dest, "/boot.automount", NULL);
87 if (!name) {
88 log_oom();
89 return EXIT_FAILURE;
90 }
91
92 fclose(f);
93 f = fopen(name, "wxe");
94 if (!f) {
95 log_error("Failed to create automount unit file %s: %m", name);
96 return EXIT_FAILURE;
97 }
98
99 fprintf(f,
100 "# Automatially generated by systemd-efi-boot-generator\n\n"
101 "[Automount]\n"
102 "Where=/boot\n");
103
104 free(name);
105 name = strjoin(arg_dest, "/local-fs.target.wants/boot.automount", NULL);
106 if (!name) {
107 log_oom();
108 return EXIT_FAILURE;
109 }
110
111 if (symlink("../boot.automount", name) < 0) {
112 log_error("Failed to create symlink: %m");
113 return EXIT_FAILURE;
114 }
115
116 return 0;
117 }