]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/efi-boot-generator/efi-boot-generator.c
treewide: use log_*_errno whenever %m is in the format string
[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 #include "mkdir.h"
29 #include "unit-name.h"
30 #include "virt.h"
31 #include "generator.h"
32 #include "special.h"
33
34 static const char *arg_dest = "/tmp";
35
36 int main(int argc, char *argv[]) {
37 _cleanup_free_ char *what = NULL;
38 _cleanup_fclose_ FILE *f = NULL;
39 int r = EXIT_SUCCESS;
40 sd_id128_t id;
41 char *name;
42
43 if (argc > 1 && argc != 4) {
44 log_error("This program takes three or no arguments.");
45 return EXIT_FAILURE;
46 }
47
48 if (argc > 1)
49 arg_dest = argv[3];
50
51 log_set_target(LOG_TARGET_SAFE);
52 log_parse_environment();
53 log_open();
54
55 umask(0022);
56
57 if (in_initrd()) {
58 log_debug("In initrd, exiting.");
59 return EXIT_SUCCESS;
60 }
61
62 if (detect_container(NULL) > 0) {
63 log_debug("In a container, exiting.");
64 return EXIT_SUCCESS;
65 }
66
67 if (!is_efi_boot()) {
68 log_debug("Not an EFI boot, exiting.");
69 return EXIT_SUCCESS;
70 }
71
72 if (path_is_mount_point("/boot", true) <= 0 &&
73 dir_is_empty("/boot") <= 0) {
74 log_debug("/boot already populated, exiting.");
75 return EXIT_SUCCESS;
76 }
77
78 r = efi_loader_get_device_part_uuid(&id);
79 if (r == -ENOENT) {
80 log_debug("EFI loader partition unknown, exiting.");
81 return EXIT_SUCCESS;
82 } else if (r < 0) {
83 log_error_errno(r, "Failed to read ESP partition UUID: %m");
84 return EXIT_FAILURE;
85 }
86
87 name = strappenda(arg_dest, "/boot.mount");
88 f = fopen(name, "wxe");
89 if (!f) {
90 log_error_errno(errno, "Failed to create mount unit file %s: %m", name);
91 return EXIT_FAILURE;
92 }
93
94 r = asprintf(&what,
95 "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
96 SD_ID128_FORMAT_VAL(id));
97 if (r < 0) {
98 log_oom();
99 return EXIT_FAILURE;
100 }
101
102 fprintf(f,
103 "# Automatially generated by systemd-efi-boot-generator\n\n"
104 "[Unit]\n"
105 "Description=EFI System Partition\n"
106 "Documentation=man:systemd-efi-boot-generator(8)\n");
107
108 r = generator_write_fsck_deps(f, arg_dest, what, "/boot", "vfat");
109 if (r < 0)
110 return EXIT_FAILURE;
111
112 fprintf(f,
113 "\n"
114 "[Mount]\n"
115 "What=%s\n"
116 "Where=/boot\n"
117 "Type=vfat\n"
118 "Options=umask=0077,noauto\n",
119 what);
120
121 fflush(f);
122 if (ferror(f)) {
123 log_error_errno(errno, "Failed to write mount unit file: %m");
124 return EXIT_FAILURE;
125 }
126
127 name = strappenda(arg_dest, "/boot.automount");
128 fclose(f);
129 f = fopen(name, "wxe");
130 if (!f) {
131 log_error_errno(errno, "Failed to create automount unit file %s: %m", name);
132 return EXIT_FAILURE;
133 }
134
135 fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
136 "[Unit]\n"
137 "Description=EFI System Partition Automount\n\n"
138 "[Automount]\n"
139 "Where=/boot\n", f);
140
141 fflush(f);
142 if (ferror(f)) {
143 log_error_errno(errno, "Failed to write automount unit file: %m");
144 return EXIT_FAILURE;
145 }
146
147 name = strappenda(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
148 mkdir_parents(name, 0755);
149
150 if (symlink("../boot.automount", name) < 0) {
151 log_error_errno(errno, "Failed to create symlink %s: %m", name);
152 return EXIT_FAILURE;
153 }
154
155 return EXIT_SUCCESS;
156 }