chiark / gitweb /
test: make it easier to override kernel version
[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_efi_boot())
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                 "[Unit]\n"
81                 "Description=EFI System Partition\n\n"
82                 "[Mount]\n"
83                 "Where=/boot\n"
84                 "What=/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n"
85                 "Options=umask=0077\n",
86                 SD_ID128_FORMAT_VAL(id));
87
88         free(name);
89         name = strjoin(arg_dest, "/boot.automount", NULL);
90         if (!name) {
91                 log_oom();
92                 return EXIT_FAILURE;
93         }
94
95         fclose(f);
96         f = fopen(name, "wxe");
97         if (!f) {
98                 log_error("Failed to create automount unit file %s: %m", name);
99                 return EXIT_FAILURE;
100         }
101
102         fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
103               "[Unit]\n"
104               "Description=EFI System Partition Automount\n\n"
105               "[Automount]\n"
106               "Where=/boot\n", f);
107
108         free(name);
109         name = strjoin(arg_dest, "/local-fs.target.wants/boot.automount", NULL);
110         if (!name) {
111                 log_oom();
112                 return EXIT_FAILURE;
113         }
114
115         mkdir_parents(name, 0755);
116
117         if (symlink("../boot.automount", name) < 0) {
118                 log_error("Failed to create symlink: %m");
119                 return EXIT_FAILURE;
120         }
121
122         return 0;
123 }