]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/efi-boot-generator/efi-boot-generator.c
util: move more intellegince into parse_proc_cmdline()
[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
LP
60 }
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
71 if (dir_is_empty("/boot") <= 0) {
72 log_debug("/boot already populated, exiting.");
73 return EXIT_SUCCESS;
74 }
f4ce2b3e 75
c51d84dc 76 r = efi_loader_get_device_part_uuid(&id);
e48fdd84
LP
77 if (r == -ENOENT) {
78 log_debug("EFI loader partition unknown exiting.");
f4ce2b3e 79 return EXIT_SUCCESS;
e48fdd84 80 } else if (r < 0) {
f4ce2b3e
LP
81 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
82 return EXIT_FAILURE;
83 }
84
97123e53 85 name = strappenda(arg_dest, "/boot.mount");
f4ce2b3e
LP
86 f = fopen(name, "wxe");
87 if (!f) {
88 log_error("Failed to create mount unit file %s: %m", name);
89 return EXIT_FAILURE;
90 }
91
d09ee17d
TG
92 r = asprintf(&what,
93 "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
94 SD_ID128_FORMAT_VAL(id));
95 if (r < 0) {
96 log_oom();
97 return EXIT_FAILURE;
98 }
99
f4ce2b3e
LP
100 fprintf(f,
101 "# Automatially generated by systemd-efi-boot-generator\n\n"
4beaf24f 102 "[Unit]\n"
e48fdd84
LP
103 "Description=EFI System Partition\n");
104
105 r = generator_write_fsck_deps(f, arg_dest, what, "/boot", "vfat");
106 if (r < 0)
107 return EXIT_FAILURE;
108
109 fprintf(f,
d09ee17d 110 "\n"
f4ce2b3e 111 "[Mount]\n"
d09ee17d 112 "What=%s\n"
e48fdd84
LP
113 "Where=/boot\n"
114 "Type=vfat\n"
0b9e3f2c 115 "Options=umask=0077,noauto\n",
e48fdd84
LP
116 what);
117
118 fflush(f);
119 if (ferror(f)) {
120 log_error("Failed to write mount unit file: %m");
121 return EXIT_FAILURE;
122 }
f4ce2b3e 123
97123e53 124 name = strappenda(arg_dest, "/boot.automount");
e48fdd84
LP
125 fclose(f);
126 f = fopen(name, "wxe");
127 if (!f) {
f4ce2b3e
LP
128 log_error("Failed to create automount unit file %s: %m", name);
129 return EXIT_FAILURE;
130 }
131
4beaf24f
LP
132 fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
133 "[Unit]\n"
134 "Description=EFI System Partition Automount\n\n"
135 "[Automount]\n"
e48fdd84
LP
136 "Where=/boot\n", f);
137
138 fflush(f);
139 if (ferror(f)) {
140 log_error("Failed to write automount unit file: %m");
141 return EXIT_FAILURE;
142 }
f4ce2b3e 143
e48fdd84 144 name = strappenda(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
1da350f1
LP
145 mkdir_parents(name, 0755);
146
f4ce2b3e 147 if (symlink("../boot.automount", name) < 0) {
c79bb9e4 148 log_error("Failed to create symlink %s: %m", name);
f4ce2b3e
LP
149 return EXIT_FAILURE;
150 }
151
97123e53 152 return EXIT_SUCCESS;
f4ce2b3e 153}