chiark / gitweb /
util: move more intellegince into parse_proc_cmdline()
[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 "unit-name.h"
30 #include "virt.h"
31 #include "generator.h"
32 #include "special.h"
33
34 static const char *arg_dest = "/tmp";
35
36 int main(int argc, char *argv[]) {
37         _cleanup_free_ char *what = NULL;
38         _cleanup_fclose_ FILE *f = NULL;
39         int r = EXIT_SUCCESS;
40         sd_id128_t id;
41         char *name;
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
57         if (in_initrd()) {
58                 log_debug("In initrd, exiting.");
59                 return EXIT_SUCCESS;
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 (dir_is_empty("/boot") <= 0) {
72                 log_debug("/boot already populated, exiting.");
73                 return EXIT_SUCCESS;
74         }
75
76         r = efi_loader_get_device_part_uuid(&id);
77         if (r == -ENOENT) {
78                 log_debug("EFI loader partition unknown exiting.");
79                 return EXIT_SUCCESS;
80         } else if (r < 0) {
81                 log_error("Failed to read ESP partition UUID: %s", strerror(-r));
82                 return EXIT_FAILURE;
83         }
84
85         name = strappenda(arg_dest, "/boot.mount");
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
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
100         fprintf(f,
101                 "# Automatially generated by systemd-efi-boot-generator\n\n"
102                 "[Unit]\n"
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,
110                 "\n"
111                 "[Mount]\n"
112                 "What=%s\n"
113                 "Where=/boot\n"
114                 "Type=vfat\n"
115                 "Options=umask=0077,noauto\n",
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         }
123
124         name = strappenda(arg_dest, "/boot.automount");
125         fclose(f);
126         f = fopen(name, "wxe");
127         if (!f) {
128                 log_error("Failed to create automount unit file %s: %m", name);
129                 return EXIT_FAILURE;
130         }
131
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"
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         }
143
144         name = strappenda(arg_dest, "/" SPECIAL_LOCAL_FS_TARGET ".wants/boot.automount");
145         mkdir_parents(name, 0755);
146
147         if (symlink("../boot.automount", name) < 0) {
148                 log_error("Failed to create symlink %s: %m", name);
149                 return EXIT_FAILURE;
150         }
151
152         return EXIT_SUCCESS;
153 }