]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/efi-boot-generator/efi-boot-generator.c
hostname-util: ignore case when checking if hostname is localhost
[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"
e48fdd84
LP
29#include "virt.h"
30#include "generator.h"
31#include "special.h"
f4ce2b3e 32
1da350f1 33static const char *arg_dest = "/tmp";
f4ce2b3e
LP
34
35int main(int argc, char *argv[]) {
e48fdd84
LP
36 _cleanup_free_ char *what = NULL;
37 _cleanup_fclose_ FILE *f = NULL;
f4ce2b3e
LP
38 int r = EXIT_SUCCESS;
39 sd_id128_t id;
97123e53 40 char *name;
f4ce2b3e
LP
41
42 if (argc > 1 && argc != 4) {
43 log_error("This program takes three or no arguments.");
44 return EXIT_FAILURE;
45 }
46
47 if (argc > 1)
48 arg_dest = argv[3];
49
50 log_set_target(LOG_TARGET_SAFE);
51 log_parse_environment();
52 log_open();
53
54 umask(0022);
55
e48fdd84
LP
56 if (in_initrd()) {
57 log_debug("In initrd, exiting.");
f4ce2b3e 58 return EXIT_SUCCESS;
e48fdd84 59 }
6d26dfe1 60
e48fdd84
LP
61 if (detect_container(NULL) > 0) {
62 log_debug("In a container, exiting.");
63 return EXIT_SUCCESS;
64 }
f4ce2b3e 65
e48fdd84
LP
66 if (!is_efi_boot()) {
67 log_debug("Not an EFI boot, exiting.");
f4ce2b3e 68 return EXIT_SUCCESS;
e48fdd84
LP
69 }
70
e26d6ce5 71 r = path_is_mount_point("/boot", AT_SYMLINK_FOLLOW);
3d8dcaf1
LP
72 if (r > 0) {
73 log_debug("/boot is already a mount point, exiting.");
74 return EXIT_SUCCESS;
75 }
776c4419
TH
76 if (r == -ENOENT)
77 log_debug("/boot does not exist, continuing.");
3d8dcaf1 78 else if (dir_is_empty("/boot") <= 0) {
e48fdd84
LP
79 log_debug("/boot already populated, exiting.");
80 return EXIT_SUCCESS;
81 }
f4ce2b3e 82
c51d84dc 83 r = efi_loader_get_device_part_uuid(&id);
e48fdd84 84 if (r == -ENOENT) {
a873c5bd 85 log_debug("EFI loader partition unknown, exiting.");
f4ce2b3e 86 return EXIT_SUCCESS;
e48fdd84 87 } else if (r < 0) {
da927ba9 88 log_error_errno(r, "Failed to read ESP partition UUID: %m");
f4ce2b3e
LP
89 return EXIT_FAILURE;
90 }
91
63c372cb 92 name = strjoina(arg_dest, "/boot.mount");
f4ce2b3e
LP
93 f = fopen(name, "wxe");
94 if (!f) {
56f64d95 95 log_error_errno(errno, "Failed to create mount unit file %s: %m", name);
f4ce2b3e
LP
96 return EXIT_FAILURE;
97 }
98
d09ee17d
TG
99 r = asprintf(&what,
100 "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
101 SD_ID128_FORMAT_VAL(id));
102 if (r < 0) {
103 log_oom();
104 return EXIT_FAILURE;
105 }
106
f4ce2b3e
LP
107 fprintf(f,
108 "# Automatially generated by systemd-efi-boot-generator\n\n"
4beaf24f 109 "[Unit]\n"
c3834f9b
LP
110 "Description=EFI System Partition\n"
111 "Documentation=man:systemd-efi-boot-generator(8)\n");
e48fdd84
LP
112
113 r = generator_write_fsck_deps(f, arg_dest, what, "/boot", "vfat");
114 if (r < 0)
115 return EXIT_FAILURE;
116
117 fprintf(f,
d09ee17d 118 "\n"
f4ce2b3e 119 "[Mount]\n"
d09ee17d 120 "What=%s\n"
e48fdd84
LP
121 "Where=/boot\n"
122 "Type=vfat\n"
0b9e3f2c 123 "Options=umask=0077,noauto\n",
e48fdd84
LP
124 what);
125
4652c56c
ZJS
126 r = fflush_and_check(f);
127 if (r < 0) {
128 log_error_errno(r, "Failed to write mount unit file: %m");
e48fdd84
LP
129 return EXIT_FAILURE;
130 }
f4ce2b3e 131
63c372cb 132 name = strjoina(arg_dest, "/boot.automount");
e48fdd84
LP
133 fclose(f);
134 f = fopen(name, "wxe");
135 if (!f) {
56f64d95 136 log_error_errno(errno, "Failed to create automount unit file %s: %m", name);
f4ce2b3e
LP
137 return EXIT_FAILURE;
138 }
139
4beaf24f
LP
140 fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
141 "[Unit]\n"
142 "Description=EFI System Partition Automount\n\n"
143 "[Automount]\n"
163ab296
KS
144 "Where=/boot\n"
145 "TimeoutIdleSec=120\n", f);
e48fdd84 146
4652c56c
ZJS
147 r = fflush_and_check(f);
148 if (r < 0) {
149 log_error_errno(r, "Failed to write automount unit file: %m");
e48fdd84
LP
150 return EXIT_FAILURE;
151 }
f4ce2b3e 152
63c372cb 153 name = strjoina(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
1da350f1
LP
154 mkdir_parents(name, 0755);
155
f4ce2b3e 156 if (symlink("../boot.automount", name) < 0) {
56f64d95 157 log_error_errno(errno, "Failed to create symlink %s: %m", name);
f4ce2b3e
LP
158 return EXIT_FAILURE;
159 }
160
97123e53 161 return EXIT_SUCCESS;
f4ce2b3e 162}