• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 27993746414

22 Jun 2026 09:22PM UTC coverage: 72.966% (+0.2%) from 72.75%
27993746414

push

github

web-flow
imds: expose imds info fields also as metrics (#42409)

32 of 110 new or added lines in 4 files covered. (29.09%)

7826 existing lines in 84 files now uncovered.

340224 of 466275 relevant lines covered (72.97%)

1291684.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/kernel-install/kernel-install.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/utsname.h>
5
#include <unistd.h>
6

7
#include "ansi-color.h"
8
#include "argv-util.h"
9
#include "boot-entry.h"
10
#include "bootspec.h"
11
#include "build.h"
12
#include "chase.h"
13
#include "conf-files.h"
14
#include "dissect-image.h"
15
#include "env-file.h"
16
#include "env-util.h"
17
#include "exec-util.h"
18
#include "extract-word.h"
19
#include "fd-util.h"
20
#include "fileio.h"
21
#include "find-esp.h"
22
#include "format-table.h"
23
#include "fs-util.h"
24
#include "help-util.h"
25
#include "id128-util.h"
26
#include "image-policy.h"
27
#include "kernel-config.h"
28
#include "kernel-image.h"
29
#include "loop-util.h"
30
#include "main-func.h"
31
#include "mount-util.h"
32
#include "options.h"
33
#include "parse-argument.h"
34
#include "path-util.h"
35
#include "recurse-dir.h"
36
#include "rm-rf.h"
37
#include "stat-util.h"
38
#include "string-table.h"
39
#include "string-util.h"
40
#include "strv.h"
41
#include "tmpfile-util.h"
42
#include "verbs.h"
43

44
static bool arg_verbose = false;
45
static char *arg_esp_path = NULL;
46
static char *arg_xbootldr_path = NULL;
47
static int arg_make_entry_directory = -1; /* tristate */
48
static PagerFlags arg_pager_flags = 0;
49
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
50
static char *arg_root = NULL;
51
static char *arg_image = NULL;
52
static ImagePolicy *arg_image_policy = NULL;
53
static bool arg_legend = true;
54
static BootEntryTokenType arg_entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
55
static char *arg_entry_token = NULL;
56
static BootEntryType arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
57

UNCOV
58
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
×
59
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
×
60
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
×
61
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
×
62
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
×
63
STATIC_DESTRUCTOR_REGISTER(arg_entry_token, freep);
×
64

65
typedef enum Action {
66
        ACTION_ADD,
67
        ACTION_REMOVE,
68
        ACTION_INSPECT,
69
        _ACTION_MAX,
70
        _ACTION_INVALID = -EINVAL,
71
} Action;
72

73
typedef enum Layout {
74
        LAYOUT_AUTO,
75
        LAYOUT_UKI,
76
        LAYOUT_BLS,
77
        LAYOUT_OTHER,
78
        _LAYOUT_MAX,
79
        _LAYOUT_INVALID = -EINVAL,
80
} Layout;
81

82
static const char * const layout_table[_LAYOUT_MAX] = {
83
        [LAYOUT_AUTO]  = "auto",
84
        [LAYOUT_UKI]   = "uki",
85
        [LAYOUT_BLS]   = "bls",
86
        [LAYOUT_OTHER] = "other",
87
};
88

UNCOV
89
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
90

91
typedef struct Context {
92
        int rfd;
93
        Action action;
94
        sd_id128_t machine_id;
95
        bool machine_id_is_random;
96
        BootEntryType entry_type;
97
        KernelImageType kernel_image_type;
98
        Layout layout;
99
        char *layout_other;
100
        char *conf_root;
101
        char *boot_root;
102
        BootEntryTokenType entry_token_type;
103
        char *entry_token;
104
        char *entry_dir;
105
        char *version;
106
        char *kernel;
107
        char **initrds;
108
        char *initrd_generator;
109
        char *uki_generator;
110
        char *staging_area;
111
        char **plugins;
112
        char **argv;
113
        char **envp;
114
} Context;
115

116
#define CONTEXT_NULL                                                    \
117
        (Context) {                                                     \
118
                .rfd = XAT_FDROOT,                                      \
119
                .action = _ACTION_INVALID,                              \
120
                .kernel_image_type = _KERNEL_IMAGE_TYPE_INVALID,        \
121
                .layout = _LAYOUT_INVALID,                              \
122
                .entry_type = _BOOT_ENTRY_TYPE_INVALID,                 \
123
                .entry_token_type = _BOOT_ENTRY_TOKEN_TYPE_INVALID,     \
124
        }
125

UNCOV
126
static void context_done(Context *c) {
×
127
        assert(c);
×
128

UNCOV
129
        free(c->layout_other);
×
130
        free(c->conf_root);
×
131
        free(c->boot_root);
×
132
        free(c->entry_token);
×
133
        free(c->entry_dir);
×
134
        free(c->version);
×
135
        free(c->kernel);
×
136
        strv_free(c->initrds);
×
137
        free(c->initrd_generator);
×
138
        free(c->uki_generator);
×
139
        if (c->action == ACTION_INSPECT)
×
140
                free(c->staging_area);
×
141
        else
UNCOV
142
                rm_rf_physical_and_free(c->staging_area);
×
143
        strv_free(c->plugins);
×
144
        strv_free(c->argv);
×
145
        strv_free(c->envp);
×
146

UNCOV
147
        safe_close(c->rfd);
×
148
}
×
149

UNCOV
150
static int context_copy(const Context *source, Context *ret) {
×
151
        int r;
×
152

UNCOV
153
        assert(source);
×
154
        assert(ret);
×
155
        assert(wildcard_fd_is_valid(source->rfd));
×
156

UNCOV
157
        _cleanup_(context_done) Context copy = (Context) {
×
158
                .rfd = source->rfd,
×
159
                .action = source->action,
×
160
                .machine_id = source->machine_id,
UNCOV
161
                .machine_id_is_random = source->machine_id_is_random,
×
162
                .kernel_image_type = source->kernel_image_type,
×
163
                .layout = source->layout,
×
164
                .entry_token_type = source->entry_token_type,
×
165
        };
166

UNCOV
167
        if (source->rfd >= 0) {
×
168
                copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH);
×
169
                if (copy.rfd < 0)
×
170
                        return copy.rfd;
171
        }
172

UNCOV
173
        r = strdup_to(&copy.layout_other, source->layout_other);
×
174
        if (r < 0)
×
175
                return r;
UNCOV
176
        r = strdup_to(&copy.conf_root, source->conf_root);
×
177
        if (r < 0)
×
178
                return r;
UNCOV
179
        r = strdup_to(&copy.boot_root, source->boot_root);
×
180
        if (r < 0)
×
181
                return r;
UNCOV
182
        r = strdup_to(&copy.entry_token, source->entry_token);
×
183
        if (r < 0)
×
184
                return r;
UNCOV
185
        r = strdup_to(&copy.entry_dir, source->entry_dir);
×
186
        if (r < 0)
×
187
                return r;
UNCOV
188
        r = strdup_to(&copy.version, source->version);
×
189
        if (r < 0)
×
190
                return r;
UNCOV
191
        r = strdup_to(&copy.kernel, source->kernel);
×
192
        if (r < 0)
×
193
                return r;
UNCOV
194
        r = strv_copy_unless_empty(source->initrds, &copy.initrds);
×
195
        if (r < 0)
×
196
                return r;
UNCOV
197
        r = strdup_to(&copy.initrd_generator, source->initrd_generator);
×
198
        if (r < 0)
×
199
                return r;
UNCOV
200
        r = strdup_to(&copy.uki_generator, source->uki_generator);
×
201
        if (r < 0)
×
202
                return r;
UNCOV
203
        r = strdup_to(&copy.staging_area, source->staging_area);
×
204
        if (r < 0)
×
205
                return r;
UNCOV
206
        r = strv_copy_unless_empty(source->plugins, &copy.plugins);
×
207
        if (r < 0)
×
208
                return r;
UNCOV
209
        r = strv_copy_unless_empty(source->argv, &copy.argv);
×
210
        if (r < 0)
×
211
                return r;
UNCOV
212
        r = strv_copy_unless_empty(source->envp, &copy.envp);
×
213
        if (r < 0)
×
214
                return r;
215

UNCOV
216
        *ret = copy;
×
217
        copy = CONTEXT_NULL;
×
218

UNCOV
219
        return 0;
×
220
}
221

UNCOV
222
static int context_open_root(Context *c) {
×
223
        int r;
×
224

UNCOV
225
        assert(c);
×
226
        assert(c->rfd < 0);
×
227

UNCOV
228
        if (isempty(arg_root))
×
229
                return 0;
230

UNCOV
231
        r = path_is_root(arg_root);
×
232
        if (r < 0)
×
233
                return log_error_errno(r, "Failed to determine if '%s' is the root directory: %m", arg_root);
×
234
        if (r > 0)
×
235
                return 0;
236

UNCOV
237
        c->rfd = open(arg_root, O_CLOEXEC | O_DIRECTORY | O_PATH);
×
238
        if (c->rfd < 0)
×
239
                return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
×
240

241
        return 0;
242
}
243

UNCOV
244
static const char* context_get_layout(const Context *c) {
×
245
        assert(c);
×
246
        assert(c->layout >= 0);
×
247

UNCOV
248
        return c->layout_other ?: layout_to_string(c->layout);
×
249
}
250

UNCOV
251
static int context_set_layout(Context *c, const char *s, const char *source) {
×
252
        Layout t;
×
253

UNCOV
254
        assert(c);
×
255
        assert(source);
×
256

UNCOV
257
        if (c->layout >= 0 || !s)
×
258
                return 0;
259

UNCOV
260
        assert(!c->layout_other);
×
261

UNCOV
262
        t = layout_from_string(s);
×
263
        if (t >= 0)
×
264
                c->layout = t;
×
265
        else if (isempty(s))
×
266
                c->layout = LAYOUT_AUTO;
×
267
        else {
UNCOV
268
                c->layout_other = strdup(s);
×
269
                if (!c->layout_other)
×
270
                        return log_oom();
×
271

UNCOV
272
                c->layout = LAYOUT_OTHER;
×
273
        }
274

UNCOV
275
        log_debug("layout=%s set via %s", context_get_layout(c), source);
×
276
        return 1;
277
}
278

UNCOV
279
static int context_set_machine_id(Context *c, const char *s, const char *source) {
×
280
        int r;
×
281

UNCOV
282
        assert(c);
×
283
        assert(source);
×
284

UNCOV
285
        if (!sd_id128_is_null(c->machine_id) || !s)
×
286
                return 0;
287

UNCOV
288
        r = sd_id128_from_string(s, &c->machine_id);
×
289
        if (r < 0)
×
290
                return log_warning_errno(r, "Failed to parse machine ID specified via %s, ignoring.", source);
×
291

UNCOV
292
        if (sd_id128_is_null(c->machine_id))
×
293
                return 0;
×
294

UNCOV
295
        log_debug("MACHINE_ID=%s set via %s.", SD_ID128_TO_STRING(c->machine_id), source);
×
296
        return 1;
×
297
}
298

UNCOV
299
static int context_set_string(const char *s, const char *source, const char *name, char **dest) {
×
300
        char *p;
×
301

UNCOV
302
        assert(source);
×
303
        assert(name);
×
304
        assert(dest);
×
305

UNCOV
306
        if (*dest || !s)
×
307
                return 0;
308

UNCOV
309
        p = strdup(s);
×
310
        if (!p)
×
311
                return log_oom();
×
312

UNCOV
313
        log_debug("%s (%s) set via %s.", name, p, source);
×
314

UNCOV
315
        *dest = p;
×
316
        return 1;
×
317
}
318

UNCOV
319
static int context_set_initrd_generator(Context *c, const char *s, const char *source) {
×
320
        assert(c);
×
321
        return context_set_string(s, source, "INITRD_GENERATOR", &c->initrd_generator);
×
322
}
323

UNCOV
324
static int context_set_uki_generator(Context *c, const char *s, const char *source) {
×
325
        assert(c);
×
326
        return context_set_string(s, source, "UKI_GENERATOR", &c->uki_generator);
×
327
}
328

UNCOV
329
static int context_set_version(Context *c, const char *s) {
×
330
        assert(c);
×
331

UNCOV
332
        if (s && !filename_is_valid(s))
×
333
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version specified: %s", s);
×
334

UNCOV
335
        return context_set_string(s, "command line", "kernel version", &c->version);
×
336
}
337

UNCOV
338
static int context_set_path(Context *c, const char *s, const char *source, const char *name, char **dest) {
×
339
        char *p;
×
340
        int r;
×
341

UNCOV
342
        assert(c);
×
343
        assert(source);
×
344
        assert(name);
×
345
        assert(dest);
×
346

UNCOV
347
        if (*dest || !s)
×
348
                return 0;
×
349

UNCOV
350
        if (c->rfd >= 0) {
×
351
                r = chaseat(c->rfd, c->rfd, s, /* flags= */ 0, &p, /* ret_fd= */ NULL);
×
352
                if (r < 0)
×
353
                        return log_warning_errno(r, "Failed to chase path %s for %s specified via %s, ignoring: %m",
×
354
                                                 s, name, source);
355
        } else {
UNCOV
356
                r = path_make_absolute_cwd(s, &p);
×
357
                if (r < 0)
×
358
                        return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
359
                                                 s, name, source);
360
        }
361

UNCOV
362
        log_debug("%s (%s) set via %s.", name, p, source);
×
363

UNCOV
364
        *dest = p;
×
365
        return 1;
×
366
}
367

UNCOV
368
static int context_set_boot_root(Context *c, const char *s, const char *source) {
×
369
        assert(c);
×
370
        return context_set_path(c, s, source, "BOOT_ROOT", &c->boot_root);
×
371
}
372

UNCOV
373
static int context_set_conf_root(Context *c, const char *s, const char *source) {
×
374
        assert(c);
×
375
        return context_set_path(c, s, source, "CONF_ROOT", &c->conf_root);
×
376
}
377

UNCOV
378
static int context_set_kernel(Context *c, const char *s) {
×
379
        assert(c);
×
380
        return context_set_path(c, s, "command line", "kernel image file", &c->kernel);
×
381
}
382

UNCOV
383
static int context_set_path_strv(Context *c, char* const* strv, const char *source, const char *name, char ***dest) {
×
384
        _cleanup_strv_free_ char **w = NULL;
×
385
        int r;
×
386

UNCOV
387
        assert(c);
×
388
        assert(source);
×
389
        assert(name);
×
390
        assert(dest);
×
391

UNCOV
392
        if (*dest)
×
393
                return 0;
394

UNCOV
395
        STRV_FOREACH(s, strv) {
×
396
                char *p;
×
397

UNCOV
398
                if (c->rfd >= 0) {
×
399
                        r = chaseat(c->rfd, c->rfd, *s, /* flags= */ 0, &p, /* ret_fd= */ NULL);
×
400
                        if (r < 0)
×
401
                                return log_warning_errno(r, "Failed to chase path %s for %s specified via %s: %m",
×
402
                                                         *s, name, source);
403
                } else {
UNCOV
404
                        r = path_make_absolute_cwd(*s, &p);
×
405
                        if (r < 0)
×
406
                                return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
407
                                                         *s, name, source);
408
                }
UNCOV
409
                r = strv_consume(&w, p);
×
410
                if (r < 0)
×
411
                        return log_oom();
×
412
        }
413

UNCOV
414
        if (strv_isempty(w))
×
415
                return 0;
416

UNCOV
417
        log_debug("%s set via %s", name, source);
×
418

UNCOV
419
        *dest = TAKE_PTR(w);
×
420
        return 1;
×
421
}
422

UNCOV
423
static int context_set_plugins(Context *c, const char *s, const char *source) {
×
424
        _cleanup_strv_free_ char **v = NULL;
×
425
        int r;
×
426

UNCOV
427
        assert(c);
×
428

UNCOV
429
        if (c->plugins || !s)
×
430
                return 0;
431

UNCOV
432
        r = strv_split_full(&v, s, NULL, EXTRACT_UNQUOTE);
×
433
        if (r < 0)
×
434
                return log_error_errno(r, "Failed to parse plugin paths from %s: %m", source);
×
435

UNCOV
436
        return context_set_path_strv(c, v, source, "plugins", &c->plugins);
×
437
}
438

UNCOV
439
static int context_set_initrds(Context *c, char* const* strv) {
×
440
        assert(c);
×
441
        return context_set_path_strv(c, strv, "command line", "initrds", &c->initrds);
×
442
}
443

UNCOV
444
static int context_load_environment(Context *c) {
×
445
        assert(c);
×
446

UNCOV
447
        (void) context_set_machine_id(c, getenv("MACHINE_ID"), "environment");
×
448
        (void) context_set_boot_root(c, getenv("BOOT_ROOT"), "environment");
×
449
        (void) context_set_conf_root(c, getenv("KERNEL_INSTALL_CONF_ROOT"), "environment");
×
450
        (void) context_set_plugins(c, getenv("KERNEL_INSTALL_PLUGINS"), "environment");
×
451
        return 0;
×
452
}
453

UNCOV
454
static int context_load_install_conf(Context *c) {
×
455
        _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
×
456
                            *initrd_generator = NULL, *uki_generator = NULL;
×
457
        int r;
×
458

UNCOV
459
        assert(c);
×
460

UNCOV
461
        r = load_kernel_install_conf_at(
×
462
                        c->conf_root ? NULL : arg_root,
463
                        c->conf_root ? XAT_FDROOT : c->rfd,
UNCOV
464
                        c->conf_root,
×
465
                        &machine_id,
466
                        &boot_root,
467
                        &layout,
468
                        &initrd_generator,
469
                        &uki_generator);
UNCOV
470
        if (r <= 0)
×
471
                return r;
472

UNCOV
473
        (void) context_set_machine_id(c, machine_id, "config");
×
474
        (void) context_set_boot_root(c, boot_root, "config");
×
475
        (void) context_set_layout(c, layout, "config");
×
476
        (void) context_set_initrd_generator(c, initrd_generator, "config");
×
477
        (void) context_set_uki_generator(c, uki_generator, "config");
×
478

UNCOV
479
        log_debug("Loaded config.");
×
480
        return 0;
481
}
482

UNCOV
483
static int context_load_machine_info(Context *c) {
×
484
        _cleanup_fclose_ FILE *f = NULL;
×
485
        _cleanup_free_ char *machine_id = NULL, *layout = NULL;
×
486
        static const char *path = "/etc/machine-info";
×
487
        int r;
×
488

UNCOV
489
        assert(c);
×
490

491
        /* If the user configured an explicit machine ID in /etc/machine-info to use for our purpose, we'll
492
         * use that instead (for compatibility). */
493

UNCOV
494
        if (!sd_id128_is_null(c->machine_id) && c->layout >= 0)
×
495
                return 0;
496

497
        /* For testing. To make not read host's /etc/machine-info. */
UNCOV
498
        r = getenv_bool("KERNEL_INSTALL_READ_MACHINE_INFO");
×
499
        if (r < 0 && r != -ENXIO)
×
500
                log_warning_errno(r, "Failed to read $KERNEL_INSTALL_READ_MACHINE_INFO, assuming yes: %m");
×
501
        if (r == 0) {
×
502
                log_debug("Skipping reading of /etc/machine-info.");
×
503
                return 0;
504
        }
505

UNCOV
506
        r = chase_and_fopenat_unlocked(c->rfd, c->rfd, path, /* chase_flags= */ 0, "re", NULL, &f);
×
507
        if (r == -ENOENT)
×
508
                return 0;
UNCOV
509
        if (r < 0)
×
510
                return log_error_errno(r, "Failed to chase %s: %m", path);
×
511

UNCOV
512
        log_debug("Loading %s…", path);
×
513

UNCOV
514
        r = parse_env_file(f, path,
×
515
                           "KERNEL_INSTALL_MACHINE_ID", &machine_id,
516
                           "KERNEL_INSTALL_LAYOUT", &layout);
UNCOV
517
        if (r < 0)
×
518
                return log_error_errno(r, "Failed to parse '%s': %m", path);
×
519

UNCOV
520
        (void) context_set_machine_id(c, machine_id, path);
×
521
        (void) context_set_layout(c, layout, path);
×
522
        return 0;
523
}
524

UNCOV
525
static int context_load_machine_id(Context *c) {
×
526
        int r;
×
527

UNCOV
528
        assert(c);
×
529

UNCOV
530
        r = id128_get_machine_at(c->rfd, &c->machine_id);
×
531
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
×
532
                return 0;
UNCOV
533
        if (r < 0)
×
534
                return log_error_errno(r, "Failed to load machine ID from /etc/machine-id: %m");
×
535

UNCOV
536
        log_debug("MACHINE_ID=%s set via /etc/machine-id.", SD_ID128_TO_STRING(c->machine_id));
×
537
        return 1; /* loaded */
×
538
}
539

UNCOV
540
static int context_ensure_machine_id(Context *c) {
×
541
        int r;
×
542

UNCOV
543
        assert(c);
×
544

UNCOV
545
        if (!sd_id128_is_null(c->machine_id))
×
546
                return 0;
×
547

548
        /* If /etc/machine-id is initialized we'll use it. */
UNCOV
549
        r = context_load_machine_id(c);
×
550
        if (r != 0)
×
551
                return r;
552

553
        /* Otherwise we'll use a freshly generated one. */
UNCOV
554
        r = sd_id128_randomize(&c->machine_id);
×
555
        if (r < 0)
×
556
                return log_error_errno(r, "Failed to generate random ID: %m");
×
557

UNCOV
558
        c->machine_id_is_random = true;
×
559
        log_debug("New machine ID '%s' generated.", SD_ID128_TO_STRING(c->machine_id));
×
560
        return 0;
×
561
}
562

UNCOV
563
static int context_acquire_xbootldr(Context *c) {
×
564
        int r;
×
565

UNCOV
566
        assert(c);
×
567
        assert(!c->boot_root);
×
568

UNCOV
569
        r = find_xbootldr_and_warn_at(
×
570
                        /* rfd= */ c->rfd,
571
                        /* path= */ arg_xbootldr_path,
572
                        /* unprivileged_mode= */ -1,
573
                        /* ret_path= */ &c->boot_root,
574
                        /* ret_fd= */ NULL);
UNCOV
575
        if (r == -ENOKEY) {
×
576
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
577
                return 0;
578
        }
UNCOV
579
        if (r == -EACCES && geteuid() != 0)
×
580
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
581
        if (r < 0)
×
582
                return r;
583

UNCOV
584
        log_debug("Using XBOOTLDR partition at %s as $BOOT_ROOT.", c->boot_root);
×
585
        return 1; /* found */
586
}
587

UNCOV
588
static int context_acquire_esp(Context *c) {
×
589
        int r;
×
590

UNCOV
591
        assert(c);
×
592
        assert(!c->boot_root);
×
593

UNCOV
594
        r = find_esp_and_warn_at(
×
595
                        /* rfd= */ c->rfd,
596
                        /* path= */ arg_esp_path,
597
                        /* unprivileged_mode= */ -1,
598
                        /* ret_path= */ &c->boot_root,
599
                        /* ret_fd= */ NULL);
UNCOV
600
        if (r == -ENOKEY) {
×
601
                log_debug_errno(r, "Couldn't find EFI system partition, ignoring.");
×
602
                return 0;
603
        }
UNCOV
604
        if (r == -EACCES && geteuid() != 0)
×
605
                return log_error_errno(r, "Failed to determine EFI system partition: %m");
×
606
        if (r < 0)
×
607
                return r;
608

UNCOV
609
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
610
        return 1; /* found */
611
}
612

UNCOV
613
static int context_ensure_boot_root(Context *c) {
×
614
        int r;
×
615

UNCOV
616
        assert(c);
×
617

618
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
UNCOV
619
        if (c->boot_root)
×
620
                return 0;
621

622
        /* Otherwise, use XBOOTLDR partition, if mounted. */
UNCOV
623
        r = context_acquire_xbootldr(c);
×
624
        if (r != 0)
×
625
                return r;
626

627
        /* Otherwise, use EFI system partition, if mounted. */
UNCOV
628
        r = context_acquire_esp(c);
×
629
        if (r != 0)
×
630
                return r;
631

632
        /* If all else fails, use /boot. */
UNCOV
633
        if (c->rfd >= 0) {
×
634
                r = chaseat(c->rfd, c->rfd, "/boot", 0, &c->boot_root, /* ret_fd= */ NULL);
×
635
                if (r < 0)
×
636
                        return log_error_errno(r, "Failed to chase '/boot/': %m");
×
637
        } else {
UNCOV
638
                c->boot_root = strdup("/boot");
×
639
                if (!c->boot_root)
×
640
                        return log_oom();
×
641
        }
642

UNCOV
643
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
644
        return 0;
645
}
646

UNCOV
647
static int context_ensure_entry_token(Context *c) {
×
648
        int r;
×
649

UNCOV
650
        assert(c);
×
651

652
        /* Now that we determined the machine ID to use, let's determine the "token" for the boot loader
653
         * entry to generate. We use that for naming the directory below $BOOT where we want to place the
654
         * kernel/initrd and related resources, as well for naming the .conf boot loader spec entry.
655
         * Typically this is just the machine ID, but it can be anything else, too, if we are told so. */
656

UNCOV
657
        r = boot_entry_token_ensure_at(
×
658
                        c->rfd,
UNCOV
659
                        c->conf_root,
×
660
                        c->machine_id,
661
                        c->machine_id_is_random,
662
                        &c->entry_token_type,
663
                        &c->entry_token);
UNCOV
664
        if (r < 0)
×
665
                return r;
666

UNCOV
667
        log_debug("Using entry token: %s", c->entry_token);
×
668
        return 0;
669
}
670

UNCOV
671
static int context_load_plugins(Context *c) {
×
672
        int r;
×
673

UNCOV
674
        assert(c);
×
675

UNCOV
676
        if (c->plugins)
×
677
                return 0;
×
678

UNCOV
679
        r = conf_files_list_strv_at(
×
680
                        &c->plugins,
681
                        ".install",
682
                        c->rfd,
683
                        CONF_FILES_EXECUTABLE | CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED | CONF_FILES_WARN,
UNCOV
684
                        STRV_MAKE_CONST("/etc/kernel/install.d", "/usr/lib/kernel/install.d"));
×
685
        if (r < 0)
×
686
                return log_error_errno(r, "Failed to find plugins: %m");
×
687

688
        return 0;
689
}
690

UNCOV
691
static int context_setup(Context *c) {
×
692
        int r;
×
693

UNCOV
694
        assert(c);
×
695

UNCOV
696
        if (c->kernel_image_type < 0)
×
697
                c->kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN;
×
698
        if (c->entry_token_type < 0)
×
699
                c->entry_token_type = BOOT_ENTRY_TOKEN_AUTO;
×
700

UNCOV
701
        r = context_open_root(c);
×
702
        if (r < 0)
×
703
                return r;
704

UNCOV
705
        r = context_load_environment(c);
×
706
        if (r < 0)
×
707
                return r;
708

UNCOV
709
        r = context_load_install_conf(c);
×
710
        if (r < 0)
×
711
                return r;
712

UNCOV
713
        r = context_load_machine_info(c);
×
714
        if (r < 0)
×
715
                return r;
716

UNCOV
717
        r = context_ensure_machine_id(c);
×
718
        if (r < 0)
×
719
                return r;
720

UNCOV
721
        r = context_ensure_boot_root(c);
×
722
        if (r < 0)
×
723
                return r;
724

UNCOV
725
        r = context_ensure_entry_token(c);
×
726
        if (r < 0)
×
727
                return r;
728

UNCOV
729
        r = context_load_plugins(c);
×
730
        if (r < 0)
×
731
                return r;
×
732

733
        return 0;
734
}
735

UNCOV
736
static int context_from_cmdline(Context *c, Action action) {
×
737
        int r;
×
738

UNCOV
739
        assert(c);
×
740

UNCOV
741
        c->action = action;
×
742

UNCOV
743
        c->entry_type = arg_boot_entry_type;
×
744

UNCOV
745
        r = free_and_strdup_warn(&c->entry_token, arg_entry_token);
×
746
        if (r < 0)
×
747
                return r;
748

UNCOV
749
        c->entry_token_type = arg_entry_token_type;
×
750

UNCOV
751
        return context_setup(c);
×
752
}
753

UNCOV
754
static int context_inspect_kernel(Context *c) {
×
755
        int r;
×
756

UNCOV
757
        assert(c);
×
758

UNCOV
759
        if (!c->kernel)
×
760
                return 0;
761

UNCOV
762
        r = inspect_kernel(
×
763
                        c->rfd,
764
                        c->kernel,
765
                        &c->kernel_image_type);
UNCOV
766
        if (r < 0)
×
767
                return log_error_errno(r, "Failed to inspect kernel image '%s': %m", c->kernel);
×
768

769
        return 0;
770
}
771

UNCOV
772
static int context_ensure_layout(Context *c) {
×
773
        int r;
×
774

UNCOV
775
        assert(c);
×
776
        assert(c->boot_root);
×
777
        assert(c->entry_token);
×
778

UNCOV
779
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
780
                return 0;
×
781

782
        /* No layout configured by the administrator. Let's try to figure it out automatically from metadata
783
         * already contained in $BOOT_ROOT. */
784

UNCOV
785
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
786
                c->layout = LAYOUT_UKI;
×
787
                log_debug("Kernel image type is %s, using layout=%s.",
×
788
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
789
                return 0;
790
        }
791

UNCOV
792
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
793
        if (!srel_path)
×
794
                return log_oom();
×
795

UNCOV
796
        _cleanup_fclose_ FILE *f = NULL;
×
797
        r = chase_and_fopenat_unlocked(c->rfd, c->rfd, srel_path, CHASE_MUST_BE_REGULAR, "re", /* ret_path= */ NULL, &f);
×
798
        if (r < 0) {
×
799
                if (r != -ENOENT)
×
800
                        return log_error_errno(r, "Failed to open '%s': %m", srel_path);
×
801
        } else {
UNCOV
802
                _cleanup_free_ char *srel = NULL;
×
803

UNCOV
804
                r = read_line(f, LONG_LINE_MAX, &srel);
×
805
                if (r < 0)
×
806
                        return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
807

UNCOV
808
                if (streq(srel, "type1"))
×
809
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
810
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
811
                         * Let's default to that, then. */
UNCOV
812
                        c->layout = LAYOUT_BLS;
×
813
                else
814
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
815
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
816
                         * from it by default. */
UNCOV
817
                        c->layout = LAYOUT_OTHER;
×
818

UNCOV
819
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
820
                return 0;
821
        }
822

UNCOV
823
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
824
        if (!entry_token_path)
×
825
                return log_oom();
×
826

UNCOV
827
        r = chaseat(c->rfd, c->rfd, entry_token_path, CHASE_MUST_BE_DIRECTORY, /* ret_path= */ NULL, /* ret_fd= */ NULL);
×
828
        if (r < 0) {
×
829
                if (!IN_SET(r, -ENOENT, -ENOTDIR))
×
830
                        return log_error_errno(r, "Failed to check if '%s' exists and is a directory: %m", entry_token_path);
×
831
        } else {
832
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
833
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
UNCOV
834
                c->layout = LAYOUT_BLS;
×
835
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
836
                return 0;
837
        }
838

839
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
840
         * really don't know anything. */
UNCOV
841
        c->layout = LAYOUT_OTHER;
×
842
        log_debug("Entry-token directory %s not found, using layout=%s.",
×
843
                  entry_token_path,
844
                  layout_to_string(c->layout));
845
        return 0;
846
}
847

UNCOV
848
static int context_set_up_staging_area(Context *c) {
×
849
        int r;
×
850

UNCOV
851
        assert(c);
×
852

UNCOV
853
        if (c->staging_area)
×
854
                return 0;
×
855

UNCOV
856
        const char *d;
×
857
        r = var_tmp_dir(&d);
×
858
        if (r < 0)
×
859
                return log_error_errno(r, "Failed to determine temporary directory location: %m");
×
860

UNCOV
861
        _cleanup_free_ char *template = path_join(d, "kernel-install.staging.XXXXXX");
×
862
        if (!template)
×
863
                return log_oom();
×
864

UNCOV
865
        if (c->action == ACTION_INSPECT)
×
866
                /* This is only used for display. The directory will not be created. */
UNCOV
867
                c->staging_area = TAKE_PTR(template);
×
868
        else {
UNCOV
869
                r = mkdtemp_malloc(template, &c->staging_area);
×
870
                if (r < 0)
×
871
                        return log_error_errno(r, "Failed to create staging area: %m");
×
872
        }
873

874
        return 0;
875
}
876

UNCOV
877
static int context_build_entry_dir(Context *c) {
×
878
        assert(c);
×
879
        assert(c->boot_root);
×
880
        assert(c->entry_token);
×
881
        assert(c->version || c->action == ACTION_INSPECT);
×
882

UNCOV
883
        if (c->entry_dir)
×
884
                return 0;
885

UNCOV
886
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
887
        if (!c->entry_dir)
×
888
                return log_oom();
×
889

UNCOV
890
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
891
        return 0;
892
}
893

UNCOV
894
static bool context_should_make_entry_dir(Context *c) {
×
895
        assert(c);
×
896

897
        /* Compatibility with earlier versions that used the presence of $BOOT_ROOT/$ENTRY_TOKEN to signal to
898
         * 00-entry-directory to create $ENTRY_DIR to serve as the indication to use or to not use the BLS */
899

UNCOV
900
        if (arg_make_entry_directory < 0)
×
901
                return c->layout == LAYOUT_BLS;
×
902

UNCOV
903
        return arg_make_entry_directory;
×
904
}
905

UNCOV
906
static int context_make_entry_dir(Context *c) {
×
907
        _cleanup_close_ int fd = -EBADF;
×
908

UNCOV
909
        assert(c);
×
910
        assert(c->entry_dir);
×
911

UNCOV
912
        if (c->action != ACTION_ADD)
×
913
                return 0;
914

UNCOV
915
        if (!context_should_make_entry_dir(c))
×
916
                return 0;
917

UNCOV
918
        log_debug("mkdir -p %s", c->entry_dir);
×
919
        fd = chase_and_openat(c->rfd, c->rfd, c->entry_dir, CHASE_MKDIR_0755,
×
920
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
UNCOV
921
        if (fd < 0)
×
922
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
923

924
        return 0;
925
}
926

UNCOV
927
static int context_remove_entry_dir(Context *c) {
×
928
        _cleanup_free_ char *p = NULL;
×
929
        _cleanup_close_ int fd = -EBADF;
×
930
        struct stat st;
×
931
        int r;
×
932

UNCOV
933
        assert(c);
×
934
        assert(c->entry_dir);
×
935

UNCOV
936
        if (c->action != ACTION_REMOVE)
×
937
                return 0;
938

UNCOV
939
        if (!context_should_make_entry_dir(c))
×
940
                return 0;
941

UNCOV
942
        log_debug("rm -rf %s", c->entry_dir);
×
943
        fd = chase_and_openat(c->rfd, c->rfd, c->entry_dir, /* chase_flags= */ 0, O_CLOEXEC | O_DIRECTORY, &p);
×
944
        if (fd < 0) {
×
945
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
946
                        return 0;
UNCOV
947
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
948
        }
949

UNCOV
950
        if (fstat(fd, &st) < 0)
×
951
                return log_debug_errno(errno, "Failed to stat %s: %m", p);
×
952

UNCOV
953
        r = rm_rf_children(TAKE_FD(fd), REMOVE_PHYSICAL|REMOVE_MISSING_OK|REMOVE_CHMOD, &st);
×
954
        if (r < 0)
×
955
                log_debug_errno(r, "Failed to remove children of %s, ignoring: %m", p);
×
956

UNCOV
957
        if (unlinkat(c->rfd, p, AT_REMOVEDIR) < 0)
×
958
                log_debug_errno(errno, "Failed to remove %s, ignoring: %m", p);
×
959

960
        return 0;
961
}
962

UNCOV
963
static int context_build_arguments(Context *c) {
×
964
        _cleanup_strv_free_ char **a = NULL;
×
965
        const char *verb;
×
966
        int r;
×
967

UNCOV
968
        assert(c);
×
969
        assert(c->entry_dir);
×
970

UNCOV
971
        if (c->argv)
×
972
                return 0;
973

UNCOV
974
        switch (c->action) {
×
975
        case ACTION_ADD:
×
976
                assert(c->version);
×
977
                assert(c->kernel);
×
978
                verb = "add";
979
                break;
980

UNCOV
981
        case ACTION_REMOVE:
×
982
                assert(c->version);
×
983
                assert(!c->kernel);
×
984
                assert(!c->initrds);
×
985
                verb = "remove";
986
                break;
987

988
        case ACTION_INSPECT:
989
                verb = "add|remove";
990
                break;
991

UNCOV
992
        default:
×
993
                assert_not_reached();
×
994
        }
995

UNCOV
996
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
997
                     verb,
998
                     c->version ?: "KERNEL_VERSION",
999
                     c->entry_dir);
UNCOV
1000
        if (!a)
×
1001
                return log_oom();
×
1002

UNCOV
1003
        if (c->action == ACTION_ADD) {
×
1004
                r = strv_extend(&a, c->kernel);
×
1005
                if (r < 0)
×
1006
                        return log_oom();
×
1007

UNCOV
1008
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates= */ false);
×
1009
                if (r < 0)
×
1010
                        return log_oom();
×
1011

UNCOV
1012
        } else if (c->action == ACTION_INSPECT) {
×
1013
                r = strv_extend_many(
×
1014
                                &a,
1015
                                c->kernel ?: "[KERNEL_IMAGE]",
1016
                                "[INITRD...]");
UNCOV
1017
                if (r < 0)
×
1018
                        return log_oom();
×
1019
        }
1020

UNCOV
1021
        c->argv = TAKE_PTR(a);
×
1022
        return 0;
×
1023
}
1024

UNCOV
1025
static int context_build_environment(Context *c) {
×
1026
        _cleanup_strv_free_ char **e = NULL;
×
1027
        int r;
×
1028

UNCOV
1029
        assert(c);
×
1030

UNCOV
1031
        if (c->envp)
×
1032
                return 0;
1033

UNCOV
1034
        r = strv_env_assign_many(&e,
×
1035
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
1036
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
1037
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
1038
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
1039
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
1040
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
1041
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
1042
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
1043
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
1044
                                 "KERNEL_INSTALL_BOOT_ENTRY_TYPE",  boot_entry_type_to_string(c->entry_type),
1045
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
UNCOV
1046
        if (r < 0)
×
1047
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
1048

UNCOV
1049
        c->envp = TAKE_PTR(e);
×
1050
        return 0;
×
1051
}
1052

UNCOV
1053
static int context_prepare_execution(Context *c) {
×
1054
        int r;
×
1055

UNCOV
1056
        assert(c);
×
1057

UNCOV
1058
        r = context_inspect_kernel(c);
×
1059
        if (r < 0)
×
1060
                return r;
1061

UNCOV
1062
        r = context_ensure_layout(c);
×
1063
        if (r < 0)
×
1064
                return r;
1065

UNCOV
1066
        r = context_set_up_staging_area(c);
×
1067
        if (r < 0)
×
1068
                return r;
1069

UNCOV
1070
        r = context_build_entry_dir(c);
×
1071
        if (r < 0)
×
1072
                return r;
1073

UNCOV
1074
        r = context_build_arguments(c);
×
1075
        if (r < 0)
×
1076
                return r;
1077

UNCOV
1078
        r = context_build_environment(c);
×
1079
        if (r < 0)
×
1080
                return r;
×
1081

1082
        return 0;
1083
}
1084

UNCOV
1085
static int context_execute(Context *c) {
×
1086
        int r, ret;
×
1087

UNCOV
1088
        assert(c);
×
1089

UNCOV
1090
        r = context_make_entry_dir(c);
×
1091
        if (r < 0)
×
1092
                return r;
1093

UNCOV
1094
        if (DEBUG_LOGGING) {
×
1095
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator= */ false);
×
1096
                log_debug("Using plugins: %s", strna(x));
×
1097

UNCOV
1098
                _cleanup_free_ char *y = strv_join_full(c->envp, "", "\n  ", /* escape_separator= */ false);
×
1099
                log_debug("Plugin environment: %s", strna(y));
×
1100

UNCOV
1101
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1102
                log_debug("Plugin arguments: %s", strna(z));
×
1103
        }
1104

UNCOV
1105
        ret = execute_strv(
×
1106
                        "plugins",
UNCOV
1107
                        c->plugins,
×
1108
                        /* root= */ NULL,
1109
                        USEC_INFINITY,
1110
                        /* callbacks= */ NULL,
1111
                        /* callback_args= */ NULL,
1112
                        c->argv,
1113
                        c->envp,
1114
                        EXEC_DIR_SKIP_REMAINING);
1115

UNCOV
1116
        r = context_remove_entry_dir(c);
×
1117
        if (r < 0)
×
1118
                return r;
×
1119

1120
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1121
        return ret;
1122
}
1123

UNCOV
1124
static bool bypass(void) {
×
1125
        return should_bypass("KERNEL_INSTALL");
×
1126
}
1127

UNCOV
1128
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1129
        _cleanup_free_ char *vmlinuz = NULL;
×
1130
        int r;
×
1131

UNCOV
1132
        assert(version);
×
1133
        assert(ret_kernel);
×
1134

UNCOV
1135
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1136
        if (!vmlinuz)
×
1137
                return log_oom();
×
1138

UNCOV
1139
        r = access_nofollow(vmlinuz, F_OK);
×
1140
        if (r == -ENOENT)
×
1141
                return log_error_errno(r, "Kernel image not installed to '%s', specify kernel kernel image path explicitly.", vmlinuz);
×
1142
        if (r < 0)
×
1143
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1144

UNCOV
1145
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1146
        return 0;
×
1147
}
1148

UNCOV
1149
static int do_add(
×
1150
                Context *c,
1151
                const char *version,
1152
                const char *kernel,
1153
                char **initrds) {
1154

UNCOV
1155
        int r;
×
1156

UNCOV
1157
        assert(c);
×
1158

UNCOV
1159
        struct utsname un;
×
1160
        if (!version) {
×
1161
                assert_se(uname(&un) >= 0);
×
1162
                version = un.release;
1163
        }
1164

UNCOV
1165
        _cleanup_free_ char *vmlinuz = NULL;
×
1166
        if (!kernel) {
×
1167
                r = kernel_from_version(version, &vmlinuz);
×
1168
                if (r < 0)
×
1169
                        return r;
1170

UNCOV
1171
                kernel = vmlinuz;
×
1172
        }
1173

UNCOV
1174
        r = context_set_version(c, version);
×
1175
        if (r < 0)
×
1176
                return r;
1177

UNCOV
1178
        r = context_set_kernel(c, kernel);
×
1179
        if (r < 0)
×
1180
                return r;
1181

UNCOV
1182
        r = context_set_initrds(c, initrds);
×
1183
        if (r < 0)
×
1184
                return r;
1185

UNCOV
1186
        r = context_prepare_execution(c);
×
1187
        if (r < 0)
×
1188
                return r;
1189

UNCOV
1190
        return context_execute(c);
×
1191
}
1192

1193
VERB(verb_add, "add", "[[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]", 1, VERB_ANY, 0,
1194
     "Add a kernel and initrd images to the boot partition");
UNCOV
1195
static int verb_add(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1196
        const char *version, *kernel;
×
1197
        char **initrds;
×
1198
        int r;
×
1199

UNCOV
1200
        assert(argv);
×
1201

UNCOV
1202
        if (arg_root)
×
1203
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1204

UNCOV
1205
        if (bypass())
×
1206
                return 0;
1207

UNCOV
1208
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1209
        r = context_from_cmdline(&c, ACTION_ADD);
×
1210
        if (r < 0)
×
1211
                return r;
1212

1213
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1214
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1215
         * optional, not the 2nd. */
UNCOV
1216
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1217
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1218
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1219
        initrds = strv_skip(argv, 3);
×
1220

UNCOV
1221
        return do_add(&c, version, kernel, initrds);
×
1222
}
1223

1224
VERB_NOARG(verb_add_all, "add-all",
1225
           "Add all kernels found in /usr/lib/modules/");
UNCOV
1226
static int verb_add_all(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1227
        _cleanup_close_ int fd = -EBADF;
×
1228
        size_t n = 0;
×
1229
        int ret = 0, r;
×
1230

UNCOV
1231
        assert(argv);
×
1232

UNCOV
1233
        if (arg_root)
×
1234
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add-all' does not support --root= or --image=.");
×
1235

UNCOV
1236
        if (bypass())
×
1237
                return 0;
1238

UNCOV
1239
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1240
        r = context_from_cmdline(&c, ACTION_ADD);
×
1241
        if (r < 0)
×
1242
                return r;
1243

UNCOV
1244
        fd = chase_and_openat(c.rfd, c.rfd, "/usr/lib/modules", /* chase_flags= */ 0, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1245
        if (fd < 0)
×
1246
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1247

UNCOV
1248
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1249
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_MUST_BE_DIRECTORY, &de);
×
1250
        if (r < 0)
×
1251
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1252

UNCOV
1253
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1254
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1255
                if (!fn)
×
1256
                        return log_oom();
×
1257

1258
                if (faccessat(fd, fn, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
×
1259
                        if (errno != ENOENT)
×
UNCOV
1260
                                log_debug_errno(errno, "Failed to check if '%s/usr/lib/modules/%s/vmlinuz' exists, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1261

1262
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1263
                        continue;
×
1264
                }
1265

1266
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1267

UNCOV
1268
                r = context_copy(&c, &copy);
×
1269
                if (r < 0)
×
1270
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1271

1272
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1273
                 * with arg_root here. */
1274
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
UNCOV
1275
                if (!full)
×
UNCOV
1276
                        return log_oom();
×
1277

UNCOV
1278
                r = do_add(&copy,
×
1279
                           /* version= */ (*d)->d_name,
×
1280
                           /* kernel= */ full,
1281
                           /* initrds= */ NULL);
UNCOV
1282
                if (r == 0)
×
UNCOV
1283
                        n++;
×
UNCOV
1284
                else if (ret == 0)
×
1285
                        ret = r;
×
1286
        }
1287

UNCOV
1288
        if (n > 0)
×
1289
                log_debug("Installed %zu kernel(s).", n);
×
1290
        else if (ret == 0)
×
UNCOV
1291
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1292

1293
        return ret;
1294
}
1295

1296
static int run_as_installkernel(char **args) {
×
1297
        /* kernel's install.sh invokes us as
1298
         *   /sbin/installkernel <version> <vmlinuz> <map> <installation-dir>
1299
         * We ignore the last two arguments. */
1300
        if (strv_length(args) < 2)
×
1301
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
×
1302

UNCOV
1303
        return verb_add(3, STRV_MAKE("add", args[0], args[1]), /* data= */ 0, /* userdata= */ NULL);
×
1304
}
1305

1306
VERB(verb_remove, "remove", "KERNEL-VERSION", 2, VERB_ANY, 0,
1307
     "Remove a kernel from the boot partition");
UNCOV
1308
static int verb_remove(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
UNCOV
1309
        int r;
×
1310

1311
        assert(argc >= 2);
×
1312
        assert(argv);
×
1313

1314
        if (arg_root)
×
UNCOV
1315
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1316

UNCOV
1317
        if (argc > 2)
×
UNCOV
1318
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1319
                          "Ignoring residual arguments.");
1320

UNCOV
1321
        if (bypass())
×
1322
                return 0;
1323

UNCOV
1324
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
1325
        r = context_from_cmdline(&c, ACTION_REMOVE);
×
1326
        if (r < 0)
×
1327
                return r;
1328

1329
        /* Note, we do not automatically derive the kernel version to remove from uname() here (unlike we do
1330
         * it for the "add" verb), since we don't want to make it too easy to uninstall your running
1331
         * kernel, as a safety precaution */
1332

UNCOV
1333
        r = context_set_version(&c, argv[1]);
×
UNCOV
1334
        if (r < 0)
×
1335
                return r;
1336

1337
        r = context_prepare_execution(&c);
×
UNCOV
1338
        if (r < 0)
×
1339
                return r;
1340

UNCOV
1341
        return context_execute(&c);
×
1342
}
1343

1344
VERB(verb_inspect, "inspect", "[[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]", 1, VERB_ANY, VERB_DEFAULT,
1345
     "Print details about the installation");
UNCOV
1346
static int verb_inspect(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
UNCOV
1347
        _cleanup_(table_unrefp) Table *t = NULL;
×
1348
        _cleanup_free_ char *vmlinuz = NULL;
×
1349
        const char *version, *kernel;
×
UNCOV
1350
        char **initrds;
×
UNCOV
1351
        struct utsname un;
×
1352
        int r;
×
1353

UNCOV
1354
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
UNCOV
1355
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
UNCOV
1356
        if (r < 0)
×
1357
                return r;
1358

1359
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1360
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1361
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1362
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1363
         * kernel image path. */
UNCOV
1364
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1365
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1366
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1367
        initrds = strv_skip(argv, 3);
×
1368

UNCOV
1369
        if (!version && !arg_root) {
×
UNCOV
1370
                assert_se(uname(&un) >= 0);
×
1371
                version = un.release;
1372
        }
1373

UNCOV
1374
        if (!kernel && version) {
×
1375
                r = kernel_from_version(version, &vmlinuz);
×
1376
                if (r < 0)
×
1377
                        return r;
1378

UNCOV
1379
                kernel = vmlinuz;
×
1380
        }
1381

UNCOV
1382
        r = context_set_version(&c, version);
×
UNCOV
1383
        if (r < 0)
×
1384
                return r;
1385

1386
        r = context_set_kernel(&c, kernel);
×
1387
        if (r < 0)
×
1388
                return r;
1389

1390
        r = context_set_initrds(&c, initrds);
×
UNCOV
1391
        if (r < 0)
×
1392
                return r;
1393

1394
        r = context_prepare_execution(&c);
×
UNCOV
1395
        if (r < 0)
×
1396
                return r;
1397

1398
        t = table_new_vertical();
×
UNCOV
1399
        if (!t)
×
UNCOV
1400
                return log_oom();
×
1401

1402
        r = table_add_many(t,
×
1403
                           TABLE_FIELD, "Machine ID",
1404
                           TABLE_ID128, c.machine_id,
1405
                           TABLE_FIELD, "Kernel Image Type",
1406
                           TABLE_STRING, kernel_image_type_to_string(c.kernel_image_type),
1407
                           TABLE_FIELD, "Layout",
1408
                           TABLE_STRING, context_get_layout(&c),
1409
                           TABLE_FIELD, "Boot Root",
1410
                           TABLE_STRING, c.boot_root,
1411
                           TABLE_FIELD, "Entry Token Type",
1412
                           TABLE_STRING, boot_entry_token_type_to_string(c.entry_token_type),
1413
                           TABLE_FIELD, "Entry Token",
1414
                           TABLE_STRING, c.entry_token,
1415
                           TABLE_FIELD, "Entry Directory",
1416
                           TABLE_STRING, c.entry_dir,
1417
                           TABLE_FIELD, "Kernel Version",
1418
                           TABLE_VERSION, c.version,
1419
                           TABLE_FIELD, "Kernel",
1420
                           TABLE_STRING, c.kernel,
1421
                           TABLE_FIELD, "Initrds",
1422
                           TABLE_STRV, c.initrds,
1423
                           TABLE_FIELD, "Initrd Generator",
1424
                           TABLE_STRING, c.initrd_generator,
1425
                           TABLE_FIELD, "UKI Generator",
1426
                           TABLE_STRING, c.uki_generator,
1427
                           TABLE_FIELD, "Plugins",
1428
                           TABLE_STRV, c.plugins,
1429
                           TABLE_FIELD, "Plugin Environment",
1430
                           TABLE_STRV, c.envp);
UNCOV
1431
        if (r < 0)
×
UNCOV
1432
                return table_log_add_error(r);
×
1433

UNCOV
1434
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
UNCOV
1435
                r = table_add_many(t,
×
1436
                                   TABLE_FIELD, "Plugin Arguments",
1437
                                   TABLE_STRV, strv_skip(c.argv, 1));
UNCOV
1438
                if (r < 0)
×
UNCOV
1439
                        return table_log_add_error(r);
×
1440
        }
1441

1442
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1443

UNCOV
1444
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1445
                _cleanup_free_ char *name = NULL;
×
1446

UNCOV
1447
                name = strdup(table_get_at(t, row, 0));
×
UNCOV
1448
                if (!name)
×
1449
                        return log_oom();
×
1450

UNCOV
1451
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
UNCOV
1452
                if (r < 0)
×
1453
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1454
        }
1455

1456
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1457
}
1458

1459
VERB_NOARG(verb_list, "list",
1460
           "List installed kernels");
UNCOV
1461
static int verb_list(int argc, char *argv[], uintptr_t _data, void *userdata) {
×
1462
        _cleanup_close_ int fd = -EBADF;
×
1463
        int r;
×
1464

UNCOV
1465
        _cleanup_(context_done) Context c = CONTEXT_NULL;
×
UNCOV
1466
        r = context_from_cmdline(&c, ACTION_INSPECT);
×
1467
        if (r < 0)
×
1468
                return r;
1469

UNCOV
1470
        fd = chase_and_openat(c.rfd, c.rfd, "/usr/lib/modules", /* chase_flags= */ 0, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
UNCOV
1471
        if (fd < 0)
×
1472
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1473

1474
        _cleanup_free_ DirectoryEntries *de = NULL;
×
UNCOV
1475
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT|RECURSE_DIR_MUST_BE_DIRECTORY, &de);
×
1476
        if (r < 0)
×
1477
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1478

UNCOV
1479
        _cleanup_(table_unrefp) Table *table = NULL;
×
UNCOV
1480
        table = table_new("version", "has kernel", "path");
×
1481
        if (!table)
×
1482
                return log_oom();
×
1483

UNCOV
1484
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1485
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1486
        (void) table_set_sort(table, (size_t) 0);
×
1487

1488
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
UNCOV
1489
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1490
                if (!j)
×
1491
                        return log_oom();
×
1492

1493
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
UNCOV
1494
                if (!fn)
×
1495
                        return log_oom();
×
1496

1497
                bool exists;
×
UNCOV
1498
                if (faccessat(fd, fn, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
×
1499
                        if (errno != ENOENT)
×
1500
                                log_debug_errno(errno, "Failed to check if '%s/usr/lib/modules/%s/vmlinuz' exists, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1501

1502
                        exists = false;
1503
                } else
1504
                        exists = true;
1505

1506
                r = table_add_many(table,
×
1507
                                   TABLE_VERSION, (*d)->d_name,
1508
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1509
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1510
                                   TABLE_PATH, j);
1511
                if (r < 0)
×
1512
                        return table_log_add_error(r);
×
1513
        }
1514

1515
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1516
}
1517

1518
static int help(void) {
×
1519
        _cleanup_(table_unrefp) Table *options = NULL, *verbs = NULL;
×
1520
        int r;
×
1521

UNCOV
1522
        r = verbs_get_help_table(&verbs);
×
UNCOV
1523
        if (r < 0)
×
1524
                return r;
1525

UNCOV
1526
        r = option_parser_get_help_table(&options);
×
1527
        if (r < 0)
×
1528
                return r;
1529

1530
        /* Note: column widths are not synced, because the verbs table is very wide. */
1531

1532
        help_cmdline("[OPTIONS…] COMMAND …");
×
1533
        help_abstract("Add and remove kernel and initrd images to and from the boot partition.");
×
1534

UNCOV
1535
        help_section("Commands");
×
1536
        r = table_print_or_warn(verbs);
×
UNCOV
1537
        if (r < 0)
×
1538
                return r;
1539

1540
        help_section("Options");
×
1541
        r = table_print_or_warn(options);
×
UNCOV
1542
        if (r < 0)
×
1543
                return r;
1544

UNCOV
1545
        printf("\n"
×
1546
               "This program may also be invoked as 'installkernel':\n"
1547
               "  installkernel [OPTIONS...] VERSION VMLINUZ [MAP] [INSTALLATION-DIR]\n"
1548
               "(The optional arguments are passed by kernel build system, but ignored.)\n");
1549

UNCOV
1550
        help_man_page_reference("kernel-install", "8");
×
1551

1552
        return 0;
1553
}
1554

UNCOV
1555
VERB_COMMON_HELP(help);
×
1556

1557
static int parse_argv(int argc, char *argv[], char ***remaining_args) {
×
1558
        assert(argc >= 0);
×
UNCOV
1559
        assert(argv);
×
UNCOV
1560
        assert(remaining_args);
×
1561

1562
        OptionParser opts = { argc, argv };
×
1563
        int r;
×
1564

UNCOV
1565
        FOREACH_OPTION_OR_RETURN(c, &opts)
×
1566
                switch (c) {
×
1567

UNCOV
1568
                OPTION_COMMON_HELP:
×
UNCOV
1569
                        return help();
×
1570

1571
                OPTION_COMMON_VERSION:
×
UNCOV
1572
                        return version();
×
1573

UNCOV
1574
                OPTION_COMMON_NO_LEGEND:
×
UNCOV
1575
                        arg_legend = false;
×
1576
                        break;
×
1577

1578
                OPTION('v', "verbose", NULL, "Increase verbosity"):
×
1579
                        log_set_max_level(LOG_DEBUG);
×
1580
                        arg_verbose = true;
×
1581
                        break;
×
1582

1583
                OPTION_LONG("esp-path", "PATH", "Path to the EFI System Partition (ESP)"):
×
1584
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_esp_path);
×
UNCOV
1585
                        if (r < 0)
×
1586
                                return r;
1587
                        break;
1588

1589
                OPTION_LONG("boot-path", "PATH", "Path to the $BOOT partition"):
×
1590
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_xbootldr_path);
×
UNCOV
1591
                        if (r < 0)
×
1592
                                return r;
1593
                        break;
1594

1595
                OPTION_COMMON_MAKE_ENTRY_DIRECTORY:
×
1596
                        if (streq(opts.arg, "auto"))
×
1597
                                arg_make_entry_directory = -1;
×
1598
                        else {
1599
                                r = parse_boolean_argument("--make-entry-directory=", opts.arg, NULL);
×
1600
                                if (r < 0)
×
1601
                                        return r;
1602

UNCOV
1603
                                arg_make_entry_directory = r;
×
1604
                        }
1605
                        break;
1606

UNCOV
1607
                OPTION_COMMON_ENTRY_TOKEN:
×
UNCOV
1608
                        r = parse_boot_entry_token_type(opts.arg, &arg_entry_token_type, &arg_entry_token);
×
UNCOV
1609
                        if (r < 0)
×
1610
                                return r;
1611
                        break;
1612

UNCOV
1613
                OPTION_LONG("entry-type", "TYPE",
×
1614
                            "Operate only on the specified bootloader entry type (type1, type2, all)"): {
UNCOV
1615
                        if (isempty(opts.arg) || streq(opts.arg, "all")) {
×
1616
                                arg_boot_entry_type = _BOOT_ENTRY_TYPE_INVALID;
×
1617
                                break;
×
1618
                        }
1619

1620
                        BootEntryType e = boot_entry_type_from_string(opts.arg);
×
1621
                        if (e < 0)
×
UNCOV
1622
                                return log_error_errno(e, "Invalid entry type: %s", opts.arg);
×
UNCOV
1623
                        arg_boot_entry_type = e;
×
1624
                        break;
×
1625
                }
1626

UNCOV
1627
                OPTION_COMMON_NO_PAGER:
×
1628
                        arg_pager_flags |= PAGER_DISABLE;
×
1629
                        break;
×
1630

UNCOV
1631
                OPTION_COMMON_JSON:
×
UNCOV
1632
                        r = parse_json_argument(opts.arg, &arg_json_format_flags);
×
UNCOV
1633
                        if (r <= 0)
×
1634
                                return r;
1635
                        break;
1636

1637
                OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
×
1638
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_root);
×
UNCOV
1639
                        if (r < 0)
×
1640
                                return r;
1641
                        break;
1642

1643
                OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
×
1644
                        r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
×
1645
                        if (r < 0)
×
1646
                                return r;
1647
                        break;
1648

1649
                OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
×
1650
                        r = parse_image_policy_argument(opts.arg, &arg_image_policy);
×
UNCOV
1651
                        if (r < 0)
×
1652
                                return r;
1653
                        break;
1654
                }
1655

UNCOV
1656
        if (arg_image && arg_root)
×
UNCOV
1657
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1658
                                       "Please specify either --root= or --image=, the combination of both is not supported.");
1659

1660
        *remaining_args = option_parser_get_args(&opts);
×
UNCOV
1661
        return 1;
×
1662
}
1663

1664
static int run(int argc, char* argv[]) {
×
1665
        int r;
×
1666

UNCOV
1667
        log_setup();
×
1668

UNCOV
1669
        char **args = NULL;
×
1670
        r = parse_argv(argc, argv, &args);
×
1671
        if (r <= 0)
×
1672
                return r;
×
1673

UNCOV
1674
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
UNCOV
1675
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
UNCOV
1676
        if (arg_image) {
×
1677
                assert(!arg_root);
×
1678

UNCOV
1679
                r = mount_image_privately_interactively(
×
1680
                                arg_image,
1681
                                arg_image_policy,
1682
                                DISSECT_IMAGE_GENERIC_ROOT |
1683
                                DISSECT_IMAGE_REQUIRE_ROOT |
1684
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1685
                                DISSECT_IMAGE_VALIDATE_OS |
1686
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1687
                                &mounted_dir,
1688
                                /* ret_dir_fd= */ NULL,
1689
                                &loop_device);
1690
                if (r < 0)
×
1691
                        return r;
1692

1693
                arg_root = strdup(mounted_dir);
×
UNCOV
1694
                if (!arg_root)
×
1695
                        return log_oom();
×
1696
        }
1697

1698
        if (invoked_as(argv, "installkernel"))
×
UNCOV
1699
                return run_as_installkernel(args);
×
1700

UNCOV
1701
        return dispatch_verb(args, /* userdata= */ NULL);
×
1702
}
1703

UNCOV
1704
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run);
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc