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