chiark / gitweb /
Remove src/detect-virt
[elogind.git] / src / efi-boot-generator / efi-boot-generator.c
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"
28 #include "mkdir.h"
29 #include "virt.h"
30 #include "generator.h"
31 #include "special.h"
32
33 static const char *arg_dest = "/tmp";
34
35 int main(int argc, char *argv[]) {
36         _cleanup_free_ char *what = NULL;
37         _cleanup_fclose_ FILE *f = NULL;
38         int r = EXIT_SUCCESS;
39         sd_id128_t id;
40         char *name;
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
56         if (in_initrd()) {
57                 log_debug("In initrd, exiting.");
58                 return EXIT_SUCCESS;
59         }
60
61         if (detect_container(NULL) > 0) {
62                 log_debug("In a container, exiting.");
63                 return EXIT_SUCCESS;
64         }
65
66         if (!is_efi_boot()) {
67                 log_debug("Not an EFI boot, exiting.");
68                 return EXIT_SUCCESS;
69         }
70
71         if (path_is_mount_point("/boot", true) <= 0 &&
72             dir_is_empty("/boot") <= 0) {
73                 log_debug("/boot already populated, exiting.");
74                 return EXIT_SUCCESS;
75         }
76
77         r = efi_loader_get_device_part_uuid(&id);
78         if (r == -ENOENT) {
79                 log_debug("EFI loader partition unknown, exiting.");
80                 return EXIT_SUCCESS;
81         } else if (r < 0) {
82                 log_error_errno(r, "Failed to read ESP partition UUID: %m");
83                 return EXIT_FAILURE;
84         }
85
86         name = strjoina(arg_dest, "/boot.mount");
87         f = fopen(name, "wxe");
88         if (!f) {
89                 log_error_errno(errno, "Failed to create mount unit file %s: %m", name);
90                 return EXIT_FAILURE;
91         }
92
93         r = asprintf(&what,
94                      "/dev/disk/by-partuuid/%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
95                      SD_ID128_FORMAT_VAL(id));
96         if (r < 0) {
97                 log_oom();
98                 return EXIT_FAILURE;
99         }
100
101         fprintf(f,
102                 "# Automatially generated by systemd-efi-boot-generator\n\n"
103                 "[Unit]\n"
104                 "Description=EFI System Partition\n"
105                 "Documentation=man:systemd-efi-boot-generator(8)\n");
106
107         r = generator_write_fsck_deps(f, arg_dest, what, "/boot", "vfat");
108         if (r < 0)
109                 return EXIT_FAILURE;
110
111         fprintf(f,
112                 "\n"
113                 "[Mount]\n"
114                 "What=%s\n"
115                 "Where=/boot\n"
116                 "Type=vfat\n"
117                 "Options=umask=0077,noauto\n",
118                 what);
119
120         fflush(f);
121         if (ferror(f)) {
122                 log_error_errno(errno, "Failed to write mount unit file: %m");
123                 return EXIT_FAILURE;
124         }
125
126         name = strjoina(arg_dest, "/boot.automount");
127         fclose(f);
128         f = fopen(name, "wxe");
129         if (!f) {
130                 log_error_errno(errno, "Failed to create automount unit file %s: %m", name);
131                 return EXIT_FAILURE;
132         }
133
134         fputs("# Automatially generated by systemd-efi-boot-generator\n\n"
135               "[Unit]\n"
136               "Description=EFI System Partition Automount\n\n"
137               "[Automount]\n"
138               "Where=/boot\n", f);
139
140         fflush(f);
141         if (ferror(f)) {
142                 log_error_errno(errno, "Failed to write automount unit file: %m");
143                 return EXIT_FAILURE;
144         }
145
146         name = strjoina(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
147         mkdir_parents(name, 0755);
148
149         if (symlink("../boot.automount", name) < 0) {
150                 log_error_errno(errno, "Failed to create symlink %s: %m", name);
151                 return EXIT_FAILURE;
152         }
153
154         return EXIT_SUCCESS;
155 }