chiark / gitweb /
generators: add Documentation= fields that point to the generator man pages
[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 #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         if (detect_container(NULL) > 0) {
62                 log_debug("In a container, exiting.");
63                 return EXIT_SUCCESS;
64         }
65
66         if (!is_efi_boot()) {
67                 log_debug("Not an EFI boot, exiting.");
68                 return EXIT_SUCCESS;
69         }
70
71         if (dir_is_empty("/boot") <= 0) {
72                 log_debug("/boot already populated, exiting.");
73                 return EXIT_SUCCESS;
74         }
75
76         r = efi_loader_get_device_part_uuid(&id);
77         if (r == -ENOENT) {
78                 log_debug("EFI loader partition unknown exiting.");
79                 return EXIT_SUCCESS;
80         } else if (r < 0) {
81                 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
82                 return EXIT_FAILURE;
83         }
84
85         name = strappenda(arg_dest, "/boot.mount");
86         f = fopen(name, "wxe");
87         if (!f) {
88                 log_error("Failed to create mount unit file %s: %m", name);
89                 return EXIT_FAILURE;
90         }
91
92         r = asprintf(&what,
93                      "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
94                      SD_ID128_FORMAT_VAL(id));
95         if (r < 0) {
96                 log_oom();
97                 return EXIT_FAILURE;
98         }
99
100         fprintf(f,
101                 "# Automatially generated by systemd-efi-boot-generator\n\n"
102                 "[Unit]\n"
103                 "Description=EFI System Partition\n"
104                 "Documentation=man:systemd-efi-boot-generator(8)\n");
105
106         r = generator_write_fsck_deps(f, arg_dest, what, "/boot", "vfat");
107         if (r < 0)
108                 return EXIT_FAILURE;
109
110         fprintf(f,
111                 "\n"
112                 "[Mount]\n"
113                 "What=%s\n"
114                 "Where=/boot\n"
115                 "Type=vfat\n"
116                 "Options=umask=0077,noauto\n",
117                 what);
118
119         fflush(f);
120         if (ferror(f)) {
121                 log_error("Failed to write mount unit file: %m");
122                 return EXIT_FAILURE;
123         }
124
125         name = strappenda(arg_dest, "/boot.automount");
126         fclose(f);
127         f = fopen(name, "wxe");
128         if (!f) {
129                 log_error("Failed to create automount unit file %s: %m", name);
130                 return EXIT_FAILURE;
131         }
132
133         fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
134               "[Unit]\n"
135               "Description=EFI System Partition Automount\n\n"
136               "[Automount]\n"
137               "Where=/boot\n", f);
138
139         fflush(f);
140         if (ferror(f)) {
141                 log_error("Failed to write automount unit file: %m");
142                 return EXIT_FAILURE;
143         }
144
145         name = strappenda(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
146         mkdir_parents(name, 0755);
147
148         if (symlink("../boot.automount", name) < 0) {
149                 log_error("Failed to create symlink %s: %m", name);
150                 return EXIT_FAILURE;
151         }
152
153         return EXIT_SUCCESS;
154 }