]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/efi-boot-generator/efi-boot-generator.c
Standarize on one spelling of symlink error message
[thirdparty/systemd.git] / src / efi-boot-generator / efi-boot-generator.c
CommitLineData
f4ce2b3e
LP
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"
1da350f1 28#include "mkdir.h"
f4ce2b3e 29
1da350f1 30static const char *arg_dest = "/tmp";
f4ce2b3e
LP
31
32int 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
9cde64ff 52 if (!is_efi_boot())
f4ce2b3e
LP
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"
4beaf24f
LP
80 "[Unit]\n"
81 "Description=EFI System Partition\n\n"
f4ce2b3e
LP
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
4beaf24f
LP
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);
f4ce2b3e
LP
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
1da350f1
LP
115 mkdir_parents(name, 0755);
116
f4ce2b3e 117 if (symlink("../boot.automount", name) < 0) {
c79bb9e4 118 log_error("Failed to create symlink %s: %m", name);
f4ce2b3e
LP
119 return EXIT_FAILURE;
120 }
121
122 return 0;
123}