]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/efi-boot-generator/efi-boot-generator.c
man: ipv4 link-local
[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"
d09ee17d 29#include "unit-name.h"
f4ce2b3e 30
1da350f1 31static const char *arg_dest = "/tmp";
f4ce2b3e
LP
32
33int main(int argc, char *argv[]) {
34 int r = EXIT_SUCCESS;
35 sd_id128_t id;
97123e53
ZJS
36 _cleanup_free_ char *what = NULL, *fsck = NULL;
37 char *name;
38 _cleanup_fclose_ FILE *f = NULL, *f2 = NULL;
f4ce2b3e
LP
39
40 if (argc > 1 && argc != 4) {
41 log_error("This program takes three or no arguments.");
42 return EXIT_FAILURE;
43 }
44
45 if (argc > 1)
46 arg_dest = argv[3];
47
48 log_set_target(LOG_TARGET_SAFE);
49 log_parse_environment();
50 log_open();
51
52 umask(0022);
53
9cde64ff 54 if (!is_efi_boot())
f4ce2b3e
LP
55 return EXIT_SUCCESS;
56
57 if (dir_is_empty("/boot") <= 0)
58 return EXIT_SUCCESS;
59
c51d84dc 60 r = efi_loader_get_device_part_uuid(&id);
f4ce2b3e
LP
61 if (r == -ENOENT)
62 return EXIT_SUCCESS;
63 if (r < 0) {
64 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
65 return EXIT_FAILURE;
66 }
67
97123e53 68 name = strappenda(arg_dest, "/boot.mount");
f4ce2b3e
LP
69 f = fopen(name, "wxe");
70 if (!f) {
71 log_error("Failed to create mount unit file %s: %m", name);
72 return EXIT_FAILURE;
73 }
74
d09ee17d
TG
75 r = asprintf(&what,
76 "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
77 SD_ID128_FORMAT_VAL(id));
78 if (r < 0) {
79 log_oom();
80 return EXIT_FAILURE;
81 }
82
83 fsck = unit_name_from_path_instance("systemd-fsck", what, ".service");
84 if (!fsck) {
85 log_oom();
86 return EXIT_FAILURE;
87 }
88
f4ce2b3e
LP
89 fprintf(f,
90 "# Automatially generated by systemd-efi-boot-generator\n\n"
4beaf24f 91 "[Unit]\n"
d09ee17d
TG
92 "Description=EFI System Partition\n"
93 "Requires=%s\n"
94 "After=%s\n"
95 "\n"
f4ce2b3e
LP
96 "[Mount]\n"
97 "Where=/boot\n"
d09ee17d 98 "What=%s\n"
0b9e3f2c 99 "Options=umask=0077,noauto\n",
d09ee17d 100 fsck, fsck, what);
f4ce2b3e 101
97123e53
ZJS
102 name = strappenda(arg_dest, "/boot.automount");
103 f2 = fopen(name, "wxe");
104 if (!f2) {
f4ce2b3e
LP
105 log_error("Failed to create automount unit file %s: %m", name);
106 return EXIT_FAILURE;
107 }
108
4beaf24f
LP
109 fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
110 "[Unit]\n"
111 "Description=EFI System Partition Automount\n\n"
112 "[Automount]\n"
97123e53 113 "Where=/boot\n", f2);
f4ce2b3e 114
97123e53 115 name = strappenda(arg_dest, "/local-fs.target.wants/boot.automount");
1da350f1
LP
116 mkdir_parents(name, 0755);
117
f4ce2b3e 118 if (symlink("../boot.automount", name) < 0) {
c79bb9e4 119 log_error("Failed to create symlink %s: %m", name);
f4ce2b3e
LP
120 return EXIT_FAILURE;
121 }
122
97123e53 123 return EXIT_SUCCESS;
f4ce2b3e 124}