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