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

systemd / systemd / 19877670170

02 Dec 2025 04:56PM UTC coverage: 72.905% (+0.001%) from 72.904%
19877670170

push

github

web-flow
kernel-install: exit with option --json=help (#39974)

In that case, `parse_json_argument()` returns 0.

Follow-up for bdd36c003 (v255).

0 of 1 new or added line in 1 file covered. (0.0%)

367 existing lines in 34 files now uncovered.

310170 of 425446 relevant lines covered (72.9%)

1132258.26 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 <getopt.h>
4
#include <stdlib.h>
5
#include <sys/utsname.h>
6
#include <unistd.h>
7

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 "dirent-util.h"
15
#include "dissect-image.h"
16
#include "env-file.h"
17
#include "env-util.h"
18
#include "exec-util.h"
19
#include "extract-word.h"
20
#include "fd-util.h"
21
#include "fileio.h"
22
#include "find-esp.h"
23
#include "format-table.h"
24
#include "fs-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 "parse-argument.h"
33
#include "path-util.h"
34
#include "pretty-print.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

55
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
×
56
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
×
57
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
×
58
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
×
59
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
×
60

61
typedef enum Action {
62
        ACTION_ADD,
63
        ACTION_REMOVE,
64
        ACTION_INSPECT,
65
        _ACTION_MAX,
66
        _ACTION_INVALID = -EINVAL,
67
} Action;
68

69
typedef enum Layout {
70
        LAYOUT_AUTO,
71
        LAYOUT_UKI,
72
        LAYOUT_BLS,
73
        LAYOUT_OTHER,
74
        _LAYOUT_MAX,
75
        _LAYOUT_INVALID = -EINVAL,
76
} Layout;
77

78
static const char * const layout_table[_LAYOUT_MAX] = {
79
        [LAYOUT_AUTO]  = "auto",
80
        [LAYOUT_UKI]   = "uki",
81
        [LAYOUT_BLS]   = "bls",
82
        [LAYOUT_OTHER] = "other",
83
};
84

85
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
86

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

112
#define CONTEXT_NULL (Context) { .rfd = -EBADF }
113

114
static void context_done(Context *c) {
×
115
        assert(c);
×
116

117
        free(c->layout_other);
×
118
        free(c->conf_root);
×
119
        free(c->boot_root);
×
120
        free(c->entry_token);
×
121
        free(c->entry_dir);
×
122
        free(c->version);
×
123
        free(c->kernel);
×
124
        strv_free(c->initrds);
×
125
        free(c->initrd_generator);
×
126
        free(c->uki_generator);
×
127
        if (c->action == ACTION_INSPECT)
×
128
                free(c->staging_area);
×
129
        else
130
                rm_rf_physical_and_free(c->staging_area);
×
131
        strv_free(c->plugins);
×
132
        strv_free(c->argv);
×
133
        strv_free(c->envp);
×
134

135
        safe_close(c->rfd);
×
136
}
×
137

138
static int context_copy(const Context *source, Context *ret) {
×
139
        int r;
×
140

141
        assert(source);
×
142
        assert(ret);
×
143
        assert(source->rfd >= 0 || source->rfd == AT_FDCWD);
×
144

145
        _cleanup_(context_done) Context copy = (Context) {
×
146
                .rfd = AT_FDCWD,
147
                .action = source->action,
×
148
                .machine_id = source->machine_id,
149
                .machine_id_is_random = source->machine_id_is_random,
×
150
                .kernel_image_type = source->kernel_image_type,
×
151
                .layout = source->layout,
×
152
                .entry_token_type = source->entry_token_type,
×
153
        };
154

155
        if (source->rfd >= 0) {
×
156
                copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH);
×
157
                if (copy.rfd < 0)
×
158
                        return copy.rfd;
159
        }
160

161
        r = strdup_to(&copy.layout_other, source->layout_other);
×
162
        if (r < 0)
×
163
                return r;
164
        r = strdup_to(&copy.conf_root, source->conf_root);
×
165
        if (r < 0)
×
166
                return r;
167
        r = strdup_to(&copy.boot_root, source->boot_root);
×
168
        if (r < 0)
×
169
                return r;
170
        r = strdup_to(&copy.entry_token, source->entry_token);
×
171
        if (r < 0)
×
172
                return r;
173
        r = strdup_to(&copy.entry_dir, source->entry_dir);
×
174
        if (r < 0)
×
175
                return r;
176
        r = strdup_to(&copy.version, source->version);
×
177
        if (r < 0)
×
178
                return r;
179
        r = strdup_to(&copy.kernel, source->kernel);
×
180
        if (r < 0)
×
181
                return r;
182
        r = strv_copy_unless_empty(source->initrds, &copy.initrds);
×
183
        if (r < 0)
×
184
                return r;
185
        r = strdup_to(&copy.initrd_generator, source->initrd_generator);
×
186
        if (r < 0)
×
187
                return r;
188
        r = strdup_to(&copy.uki_generator, source->uki_generator);
×
189
        if (r < 0)
×
190
                return r;
191
        r = strdup_to(&copy.staging_area, source->staging_area);
×
192
        if (r < 0)
×
193
                return r;
194
        r = strv_copy_unless_empty(source->plugins, &copy.plugins);
×
195
        if (r < 0)
×
196
                return r;
197
        r = strv_copy_unless_empty(source->argv, &copy.argv);
×
198
        if (r < 0)
×
199
                return r;
200
        r = strv_copy_unless_empty(source->envp, &copy.envp);
×
201
        if (r < 0)
×
202
                return r;
203

204
        *ret = copy;
×
205
        copy = CONTEXT_NULL;
×
206

207
        return 0;
×
208
}
209

210
static int context_open_root(Context *c) {
×
211
        int r;
×
212

213
        assert(c);
×
214
        assert(c->rfd < 0);
×
215

216
        if (isempty(arg_root))
×
217
                return 0;
218

219
        r = path_is_root(arg_root);
×
220
        if (r < 0)
×
221
                return log_error_errno(r, "Failed to determine if '%s' is the root directory: %m", arg_root);
×
222
        if (r > 0)
×
223
                return 0;
224

225
        c->rfd = open(arg_root, O_CLOEXEC | O_DIRECTORY | O_PATH);
×
226
        if (c->rfd < 0)
×
227
                return log_error_errno(errno, "Failed to open root directory '%s': %m", arg_root);
×
228

229
        return 0;
230
}
231

232
static const char* context_get_layout(const Context *c) {
×
233
        assert(c);
×
234
        assert(c->layout >= 0);
×
235

236
        return c->layout_other ?: layout_to_string(c->layout);
×
237
}
238

239
static int context_set_layout(Context *c, const char *s, const char *source) {
×
240
        Layout t;
×
241

242
        assert(c);
×
243
        assert(source);
×
244

245
        if (c->layout >= 0 || !s)
×
246
                return 0;
247

248
        assert(!c->layout_other);
×
249

250
        t = layout_from_string(s);
×
251
        if (t >= 0)
×
252
                c->layout = t;
×
253
        else if (isempty(s))
×
254
                c->layout = LAYOUT_AUTO;
×
255
        else {
256
                c->layout_other = strdup(s);
×
257
                if (!c->layout_other)
×
258
                        return log_oom();
×
259

260
                c->layout = LAYOUT_OTHER;
×
261
        }
262

263
        log_debug("layout=%s set via %s", context_get_layout(c), source);
×
264
        return 1;
265
}
266

267
static int context_set_machine_id(Context *c, const char *s, const char *source) {
×
268
        int r;
×
269

270
        assert(c);
×
271
        assert(source);
×
272

273
        if (!sd_id128_is_null(c->machine_id) || !s)
×
274
                return 0;
×
275

276
        r = sd_id128_from_string(s, &c->machine_id);
×
277
        if (r < 0)
×
278
                return log_warning_errno(r, "Failed to parse machine ID specified via %s, ignoring.", source);
×
279

280
        if (sd_id128_is_null(c->machine_id))
×
281
                return 0;
×
282

283
        log_debug("MACHINE_ID=%s set via %s.", SD_ID128_TO_STRING(c->machine_id), source);
×
284
        return 1;
×
285
}
286

287
static int context_set_string(const char *s, const char *source, const char *name, char **dest) {
×
288
        char *p;
×
289

290
        assert(source);
×
291
        assert(name);
×
292
        assert(dest);
×
293

294
        if (*dest || !s)
×
295
                return 0;
296

297
        p = strdup(s);
×
298
        if (!p)
×
299
                return log_oom();
×
300

301
        log_debug("%s (%s) set via %s.", name, p, source);
×
302

303
        *dest = p;
×
304
        return 1;
×
305
}
306

307
static int context_set_initrd_generator(Context *c, const char *s, const char *source) {
×
308
        assert(c);
×
309
        return context_set_string(s, source, "INITRD_GENERATOR", &c->initrd_generator);
×
310
}
311

312
static int context_set_uki_generator(Context *c, const char *s, const char *source) {
×
313
        assert(c);
×
314
        return context_set_string(s, source, "UKI_GENERATOR", &c->uki_generator);
×
315
}
316

317
static int context_set_version(Context *c, const char *s) {
×
318
        assert(c);
×
319

320
        if (s && !filename_is_valid(s))
×
321
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version specified: %s", s);
×
322

323
        return context_set_string(s, "command line", "kernel version", &c->version);
×
324
}
325

326
static int context_set_path(Context *c, const char *s, const char *source, const char *name, char **dest) {
×
327
        char *p;
×
328
        int r;
×
329

330
        assert(c);
×
331
        assert(source);
×
332
        assert(name);
×
333
        assert(dest);
×
334

335
        if (*dest || !s)
×
336
                return 0;
×
337

338
        if (c->rfd >= 0) {
×
339
                r = chaseat(c->rfd, s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd = */ NULL);
×
340
                if (r < 0)
×
341
                        return log_warning_errno(r, "Failed to chase path %s for %s specified via %s, ignoring: %m",
×
342
                                                 s, name, source);
343
        } else {
344
                r = path_make_absolute_cwd(s, &p);
×
345
                if (r < 0)
×
346
                        return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
347
                                                 s, name, source);
348
        }
349

350
        log_debug("%s (%s) set via %s.", name, p, source);
×
351

352
        *dest = p;
×
353
        return 1;
×
354
}
355

356
static int context_set_boot_root(Context *c, const char *s, const char *source) {
×
357
        assert(c);
×
358
        return context_set_path(c, s, source, "BOOT_ROOT", &c->boot_root);
×
359
}
360

361
static int context_set_conf_root(Context *c, const char *s, const char *source) {
×
362
        assert(c);
×
363
        return context_set_path(c, s, source, "CONF_ROOT", &c->conf_root);
×
364
}
365

366
static int context_set_kernel(Context *c, const char *s) {
×
367
        assert(c);
×
368
        return context_set_path(c, s, "command line", "kernel image file", &c->kernel);
×
369
}
370

371
static int context_set_path_strv(Context *c, char* const* strv, const char *source, const char *name, char ***dest) {
×
372
        _cleanup_strv_free_ char **w = NULL;
×
373
        int r;
×
374

375
        assert(c);
×
376
        assert(source);
×
377
        assert(name);
×
378
        assert(dest);
×
379

380
        if (*dest)
×
381
                return 0;
382

383
        STRV_FOREACH(s, strv) {
×
384
                char *p;
×
385

386
                if (c->rfd >= 0) {
×
387
                        r = chaseat(c->rfd, *s, CHASE_AT_RESOLVE_IN_ROOT, &p, /* ret_fd = */ NULL);
×
388
                        if (r < 0)
×
389
                                return log_warning_errno(r, "Failed to chase path %s for %s specified via %s: %m",
×
390
                                                         *s, name, source);
391
                } else {
392
                        r = path_make_absolute_cwd(*s, &p);
×
393
                        if (r < 0)
×
394
                                return log_warning_errno(r, "Failed to make path '%s' for %s specified via %s absolute, ignoring: %m",
×
395
                                                         *s, name, source);
396
                }
397
                r = strv_consume(&w, p);
×
398
                if (r < 0)
×
399
                        return log_oom();
×
400
        }
401

402
        if (strv_isempty(w))
×
403
                return 0;
404

405
        log_debug("%s set via %s", name, source);
×
406

407
        *dest = TAKE_PTR(w);
×
408
        return 1;
×
409
}
410

411
static int context_set_plugins(Context *c, const char *s, const char *source) {
×
412
        _cleanup_strv_free_ char **v = NULL;
×
413
        int r;
×
414

415
        assert(c);
×
416

417
        if (c->plugins || !s)
×
418
                return 0;
419

420
        r = strv_split_full(&v, s, NULL, EXTRACT_UNQUOTE);
×
421
        if (r < 0)
×
422
                return log_error_errno(r, "Failed to parse plugin paths from %s: %m", source);
×
423

424
        return context_set_path_strv(c, v, source, "plugins", &c->plugins);
×
425
}
426

427
static int context_set_initrds(Context *c, char* const* strv) {
×
428
        assert(c);
×
429
        return context_set_path_strv(c, strv, "command line", "initrds", &c->initrds);
×
430
}
431

432
static int context_set_entry_type(Context *c, const char *s) {
×
433
        assert(c);
×
434
        BootEntryType e;
×
435
        if (isempty(s) || streq(s, "all")) {
×
436
                c->entry_type = _BOOT_ENTRY_TYPE_INVALID;
×
437
                return 0;
×
438
        }
439
        e = boot_entry_type_from_string(s);
×
440
        if (e < 0)
×
441
                return log_error_errno(e, "Invalid entry type: %s", s);
×
442
        c->entry_type = e;
×
443
        return 1;
×
444
}
445

446
static int context_load_environment(Context *c) {
×
447
        assert(c);
×
448

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

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

461
        assert(c);
×
462

463
        r = load_kernel_install_conf(arg_root,
×
464
                                     c->conf_root,
×
465
                                     &machine_id,
466
                                     &boot_root,
467
                                     &layout,
468
                                     &initrd_generator,
469
                                     &uki_generator);
470
        if (r <= 0)
×
471
                return r;
472

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

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

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

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

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. */
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

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

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

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

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

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

528
        assert(c);
×
529

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

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

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

543
        assert(c);
×
544

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

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

553
        /* Otherwise we'll use a freshly generated one. */
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

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

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

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

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_uuid = */ NULL,
575
                        /* ret_devid = */ NULL);
576
        if (r == -ENOKEY) {
×
577
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
578
                return 0;
×
579
        }
580
        if (r == -EACCES && geteuid() != 0)
×
581
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
582
        if (r < 0)
×
583
                return r;
584

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

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

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

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

614
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
615
        return 1; /* found */
616
}
617

618
static int context_ensure_boot_root(Context *c) {
×
619
        int r;
×
620

621
        assert(c);
×
622

623
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
624
        if (c->boot_root)
×
625
                return 0;
626

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

632
        /* Otherwise, use EFI system partition, if mounted. */
633
        r = context_acquire_esp(c);
×
634
        if (r != 0)
×
635
                return r;
636

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

648
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
649
        return 0;
650
}
651

652
static int context_ensure_entry_token(Context *c) {
×
653
        int r;
×
654

655
        assert(c);
×
656

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

662
        r = boot_entry_token_ensure_at(
×
663
                        c->rfd,
664
                        c->conf_root,
×
665
                        c->machine_id,
666
                        c->machine_id_is_random,
×
667
                        &c->entry_token_type,
668
                        &c->entry_token);
669
        if (r < 0)
×
670
                return r;
671

672
        log_debug("Using entry token: %s", c->entry_token);
×
673
        return 0;
674
}
675

676
static int context_load_plugins(Context *c) {
×
677
        int r;
×
678

679
        assert(c);
×
680

681
        if (c->plugins)
×
682
                return 0;
×
683

684
        r = conf_files_list_strv_at(
×
685
                        &c->plugins,
686
                        ".install",
687
                        c->rfd,
688
                        CONF_FILES_EXECUTABLE | CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED,
689
                        STRV_MAKE_CONST("/etc/kernel/install.d", "/usr/lib/kernel/install.d"));
×
690
        if (r < 0)
×
691
                return log_error_errno(r, "Failed to find plugins: %m");
×
692

693
        return 0;
694
}
695

696
static int context_init(Context *c) {
×
697
        int r;
×
698

699
        assert(c);
×
700

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

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

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

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

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

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

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

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

733
        return 0;
734
}
735

736
static int context_inspect_kernel(Context *c) {
×
737
        assert(c);
×
738

739
        if (!c->kernel)
×
740
                return 0;
741

742
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
743
}
744

745
static int context_ensure_layout(Context *c) {
×
746
        int r;
×
747

748
        assert(c);
×
749
        assert(c->boot_root);
×
750
        assert(c->entry_token);
×
751

752
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
753
                return 0;
×
754

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

758
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
759
                c->layout = LAYOUT_UKI;
×
760
                log_debug("Kernel image type is %s, using layout=%s.",
×
761
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
762
                return 0;
×
763
        }
764

765
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
766
        if (!srel_path)
×
767
                return log_oom();
×
768

769
        _cleanup_free_ char *srel = NULL;
×
770
        r = read_one_line_file_at(c->rfd, srel_path, &srel);
×
771
        if (r >= 0) {
×
772
                if (streq(srel, "type1"))
×
773
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
774
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
775
                         * Let's default to that, then. */
776
                        c->layout = LAYOUT_BLS;
×
777
                else
778
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
779
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
780
                         * from it by default. */
781
                        c->layout = LAYOUT_OTHER;
×
782

783
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
784
                return 0;
×
785
        } else if (r != -ENOENT)
×
786
                return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
787

788
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
789
        if (!entry_token_path)
×
790
                return log_oom();
×
791

792
        r = is_dir_at(c->rfd, entry_token_path, /* follow = */ false);
×
793
        if (r < 0 && r != -ENOENT)
×
794
                return log_error_errno(r, "Failed to check if '%s' is a directory: %m", entry_token_path);
×
795
        if (r > 0) {
×
796
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
797
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
798
                c->layout = LAYOUT_BLS;
×
799
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
800
                return 0;
×
801
        }
802

803
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
804
         * really don't know anything. */
805
        c->layout = LAYOUT_OTHER;
×
806
        log_debug("Entry-token directory %s not found, using layout=%s.",
×
807
                  entry_token_path,
808
                  layout_to_string(c->layout));
809
        return 0;
810
}
811

812
static int context_set_up_staging_area(Context *c) {
×
813
        int r;
×
814

815
        assert(c);
×
816

817
        if (c->staging_area)
×
818
                return 0;
×
819

820
        const char *d;
×
821
        r = var_tmp_dir(&d);
×
822
        if (r < 0)
×
823
                return log_error_errno(r, "Failed to determine temporary directory location: %m");
×
824

825
        _cleanup_free_ char *template = path_join(d, "kernel-install.staging.XXXXXX");
×
826
        if (!template)
×
827
                return log_oom();
×
828

829
        if (c->action == ACTION_INSPECT)
×
830
                /* This is only used for display. The directory will not be created. */
831
                c->staging_area = TAKE_PTR(template);
×
832
        else {
833
                r = mkdtemp_malloc(template, &c->staging_area);
×
834
                if (r < 0)
×
835
                        return log_error_errno(r, "Failed to create staging area: %m");
×
836
        }
837

838
        return 0;
839
}
840

841
static int context_build_entry_dir(Context *c) {
×
842
        assert(c);
×
843
        assert(c->boot_root);
×
844
        assert(c->entry_token);
×
845
        assert(c->version || c->action == ACTION_INSPECT);
×
846

847
        if (c->entry_dir)
×
848
                return 0;
849

850
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
851
        if (!c->entry_dir)
×
852
                return log_oom();
×
853

854
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
855
        return 0;
856
}
857

858
static bool context_should_make_entry_dir(Context *c) {
×
859
        assert(c);
×
860

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

864
        if (arg_make_entry_directory < 0)
×
865
                return c->layout == LAYOUT_BLS;
×
866

867
        return arg_make_entry_directory;
×
868
}
869

870
static int context_make_entry_dir(Context *c) {
×
871
        _cleanup_close_ int fd = -EBADF;
×
872

873
        assert(c);
×
874
        assert(c->entry_dir);
×
875

876
        if (c->action != ACTION_ADD)
×
877
                return 0;
878

879
        if (!context_should_make_entry_dir(c))
×
880
                return 0;
881

882
        log_debug("mkdir -p %s", c->entry_dir);
×
883
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
884
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
885
        if (fd < 0)
×
886
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
887

888
        return 0;
889
}
890

891
static int context_remove_entry_dir(Context *c) {
×
892
        _cleanup_free_ char *p = NULL;
×
893
        _cleanup_close_ int fd = -EBADF;
×
894
        struct stat st;
×
895
        int r;
×
896

897
        assert(c);
×
898
        assert(c->entry_dir);
×
899

900
        if (c->action != ACTION_REMOVE)
×
901
                return 0;
902

903
        if (!context_should_make_entry_dir(c))
×
904
                return 0;
905

906
        log_debug("rm -rf %s", c->entry_dir);
×
907
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
908
        if (fd < 0) {
×
909
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
910
                        return 0;
911
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
912
        }
913

914
        if (fstat(fd, &st) < 0)
×
915
                return log_debug_errno(errno, "Failed to stat %s: %m", p);
×
916

917
        r = rm_rf_children(TAKE_FD(fd), REMOVE_PHYSICAL|REMOVE_MISSING_OK|REMOVE_CHMOD, &st);
×
918
        if (r < 0)
×
919
                log_debug_errno(r, "Failed to remove children of %s, ignoring: %m", p);
×
920

921
        if (unlinkat(c->rfd, p, AT_REMOVEDIR) < 0)
×
922
                log_debug_errno(errno, "Failed to remove %s, ignoring: %m", p);
×
923

924
        return 0;
925
}
926

927
static int context_build_arguments(Context *c) {
×
928
        _cleanup_strv_free_ char **a = NULL;
×
929
        const char *verb;
×
930
        int r;
×
931

932
        assert(c);
×
933
        assert(c->entry_dir);
×
934

935
        if (c->argv)
×
936
                return 0;
937

938
        switch (c->action) {
×
939
        case ACTION_ADD:
×
940
                assert(c->version);
×
941
                assert(c->kernel);
×
942
                verb = "add";
943
                break;
944

945
        case ACTION_REMOVE:
×
946
                assert(c->version);
×
947
                assert(!c->kernel);
×
948
                assert(!c->initrds);
×
949
                verb = "remove";
950
                break;
951

952
        case ACTION_INSPECT:
953
                verb = "add|remove";
954
                break;
955

956
        default:
×
957
                assert_not_reached();
×
958
        }
959

960
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
961
                     verb,
962
                     c->version ?: "KERNEL_VERSION",
963
                     c->entry_dir);
964
        if (!a)
×
965
                return log_oom();
×
966

967
        if (c->action == ACTION_ADD) {
×
968
                r = strv_extend(&a, c->kernel);
×
969
                if (r < 0)
×
970
                        return log_oom();
×
971

972
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates = */ false);
×
973
                if (r < 0)
×
974
                        return log_oom();
×
975

976
        } else if (c->action == ACTION_INSPECT) {
×
977
                r = strv_extend_many(
×
978
                                &a,
979
                                c->kernel ?: "[KERNEL_IMAGE]",
980
                                "[INITRD...]");
981
                if (r < 0)
×
982
                        return log_oom();
×
983
        }
984

985
        c->argv = TAKE_PTR(a);
×
986
        return 0;
×
987
}
988

989
static int context_build_environment(Context *c) {
×
990
        _cleanup_strv_free_ char **e = NULL;
×
991
        int r;
×
992

993
        assert(c);
×
994

995
        if (c->envp)
×
996
                return 0;
997

998
        r = strv_env_assign_many(&e,
×
999
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
1000
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
1001
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
1002
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
1003
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
1004
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
1005
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
1006
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
1007
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
1008
                                 "KERNEL_INSTALL_BOOT_ENTRY_TYPE",  boot_entry_type_to_string(c->entry_type),
1009
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
1010
        if (r < 0)
×
1011
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
1012

1013
        c->envp = TAKE_PTR(e);
×
1014
        return 0;
×
1015
}
1016

1017
static int context_prepare_execution(Context *c) {
×
1018
        int r;
×
1019

1020
        assert(c);
×
1021

1022
        r = context_inspect_kernel(c);
×
1023
        if (r < 0)
×
1024
                return r;
1025

1026
        r = context_ensure_layout(c);
×
1027
        if (r < 0)
×
1028
                return r;
1029

1030
        r = context_set_up_staging_area(c);
×
1031
        if (r < 0)
×
1032
                return r;
1033

1034
        r = context_build_entry_dir(c);
×
1035
        if (r < 0)
×
1036
                return r;
1037

1038
        r = context_build_arguments(c);
×
1039
        if (r < 0)
×
1040
                return r;
1041

1042
        r = context_build_environment(c);
×
1043
        if (r < 0)
×
1044
                return r;
×
1045

1046
        return 0;
1047
}
1048

1049
static int context_execute(Context *c) {
×
1050
        int r, ret;
×
1051

1052
        assert(c);
×
1053

1054
        r = context_make_entry_dir(c);
×
1055
        if (r < 0)
×
1056
                return r;
1057

1058
        if (DEBUG_LOGGING) {
×
1059
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator = */ false);
×
1060
                log_debug("Using plugins: %s", strna(x));
×
1061

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

1065
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1066
                log_debug("Plugin arguments: %s", strna(z));
×
1067
        }
1068

1069
        ret = execute_strv(
×
1070
                        "plugins",
1071
                        c->plugins,
×
1072
                        /* root = */ NULL,
1073
                        USEC_INFINITY,
1074
                        /* callbacks = */ NULL,
1075
                        /* callback_args = */ NULL,
1076
                        c->argv,
1077
                        c->envp,
1078
                        EXEC_DIR_SKIP_REMAINING);
1079

1080
        r = context_remove_entry_dir(c);
×
1081
        if (r < 0)
×
1082
                return r;
×
1083

1084
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1085
        return ret;
1086
}
1087

1088
static bool bypass(void) {
×
1089
        return should_bypass("KERNEL_INSTALL");
×
1090
}
1091

1092
static int do_add(
×
1093
                Context *c,
1094
                const char *version,
1095
                const char *kernel,
1096
                char **initrds) {
1097

1098
        int r;
×
1099

1100
        assert(c);
×
1101
        assert(version);
×
1102
        assert(kernel);
×
1103

1104
        r = context_set_version(c, version);
×
1105
        if (r < 0)
×
1106
                return r;
1107

1108
        r = context_set_kernel(c, kernel);
×
1109
        if (r < 0)
×
1110
                return r;
1111

1112
        r = context_set_initrds(c, initrds);
×
1113
        if (r < 0)
×
1114
                return r;
1115

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

1120
        return context_execute(c);
×
1121
}
1122

1123
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1124
        _cleanup_free_ char *vmlinuz = NULL;
×
1125
        int r;
×
1126

1127
        assert(version);
×
1128

1129
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1130
        if (!vmlinuz)
×
1131
                return log_oom();
×
1132

1133
        r = access_nofollow(vmlinuz, F_OK);
×
1134
        if (r == -ENOENT)
×
1135
                return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
×
1136
        if (r < 0)
×
1137
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1138

1139
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1140
        return 0;
×
1141
}
1142

1143
static int verb_add(int argc, char *argv[], void *userdata) {
×
1144
        Context *c = ASSERT_PTR(userdata);
×
1145
        _cleanup_free_ char *vmlinuz = NULL;
×
1146
        const char *version, *kernel;
×
1147
        char **initrds;
×
1148
        struct utsname un;
×
1149
        int r;
×
1150

1151
        assert(argv);
×
1152

1153
        if (arg_root)
×
1154
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1155

1156
        if (bypass())
×
1157
                return 0;
1158

1159
        c->action = ACTION_ADD;
×
1160

1161
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1162
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1163
         * optional, not the 2nd. */
1164
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1165
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1166
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1167
        initrds = strv_skip(argv, 3);
×
1168

1169
        if (!version) {
×
1170
                assert_se(uname(&un) >= 0);
×
1171
                version = un.release;
1172
        }
1173

1174
        if (!kernel) {
×
1175
                r = kernel_from_version(version, &vmlinuz);
×
1176
                if (r < 0)
×
1177
                        return r;
1178

1179
                kernel = vmlinuz;
×
1180
        }
1181

1182
        return do_add(c, version, kernel, initrds);
×
1183
}
1184

1185
static int verb_add_all(int argc, char *argv[], void *userdata) {
×
1186
        Context *c = ASSERT_PTR(userdata);
×
1187
        _cleanup_close_ int fd = -EBADF;
×
1188
        size_t n = 0;
×
1189
        int ret = 0, r;
×
1190

1191
        assert(argv);
×
1192

1193
        if (arg_root)
×
1194
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add-all' does not support --root= or --image=.");
×
1195

1196
        if (bypass())
×
1197
                return 0;
1198

1199
        c->action = ACTION_ADD;
×
1200

1201
        fd = chase_and_openat(c->rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1202
        if (fd < 0)
×
1203
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1204

1205
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1206
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1207
        if (r < 0)
×
1208
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1209

1210
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1211
                r = dirent_ensure_type(fd, *d);
×
1212
                if (r < 0) {
×
1213
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1214
                                log_debug_errno(r, "Failed to check if '%s/usr/lib/modules/%s' is a directory, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1215
                        continue;
×
1216
                }
1217

1218
                if ((*d)->d_type != DT_DIR)
×
1219
                        continue;
×
1220

1221
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1222
                if (!fn)
×
1223
                        return log_oom();
×
1224

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

1229
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1230
                        continue;
×
1231
                }
1232

1233
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1234

1235
                r = context_copy(c, &copy);
×
1236
                if (r < 0)
×
1237
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1238

1239
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1240
                 * with arg_root here. */
1241
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1242
                if (!full)
×
1243
                        return log_oom();
×
1244

1245
                r = do_add(&copy,
×
1246
                           /* version= */ (*d)->d_name,
×
1247
                           /* kernel= */ full,
1248
                           /* initrds= */ NULL);
1249
                if (r == 0)
×
1250
                        n++;
×
1251
                else if (ret == 0)
×
1252
                        ret = r;
×
1253
        }
1254

1255
        if (n > 0)
×
1256
                log_debug("Installed %zu kernel(s).", n);
×
1257
        else if (ret == 0)
×
1258
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1259

1260
        return ret;
1261
}
1262

1263
static int run_as_installkernel(int argc, char *argv[], Context *c) {
×
1264
        /* kernel's install.sh invokes us as
1265
         *   /sbin/installkernel <version> <vmlinuz> <map> <installation-dir>
1266
         * We ignore the last two arguments. */
1267
        if (optind + 2 > argc)
×
1268
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "'installkernel' command requires at least two arguments.");
×
1269

1270
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), c);
×
1271
}
1272

1273
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1274
        Context *c = ASSERT_PTR(userdata);
×
1275
        int r;
×
1276

1277
        assert(argc >= 2);
×
1278
        assert(argv);
×
1279

1280
        if (arg_root)
×
1281
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1282

1283
        if (argc > 2)
×
1284
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1285
                          "Ignoring residual arguments.");
1286

1287
        if (bypass())
×
1288
                return 0;
1289

1290
        c->action = ACTION_REMOVE;
×
1291

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

1296
        r = context_set_version(c, argv[1]);
×
1297
        if (r < 0)
×
1298
                return r;
1299

1300
        r = context_prepare_execution(c);
×
1301
        if (r < 0)
×
1302
                return r;
1303

1304
        return context_execute(c);
×
1305
}
1306

1307
static int verb_inspect(int argc, char *argv[], void *userdata) {
×
1308
        Context *c = ASSERT_PTR(userdata);
×
1309
        _cleanup_(table_unrefp) Table *t = NULL;
×
1310
        _cleanup_free_ char *vmlinuz = NULL;
×
1311
        const char *version, *kernel;
×
1312
        char **initrds;
×
1313
        struct utsname un;
×
1314
        int r;
×
1315

1316
        c->action = ACTION_INSPECT;
×
1317

1318
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1319
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1320
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1321
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1322
         * kernel image path. */
1323
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1324
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1325
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1326
        initrds = strv_skip(argv, 3);
×
1327

1328
        if (!version && !arg_root) {
×
1329
                assert_se(uname(&un) >= 0);
×
1330
                version = un.release;
1331
        }
1332

1333
        if (!kernel && version) {
×
1334
                r = kernel_from_version(version, &vmlinuz);
×
1335
                if (r < 0)
×
1336
                        return r;
1337

1338
                kernel = vmlinuz;
×
1339
        }
1340

1341
        r = context_set_version(c, version);
×
1342
        if (r < 0)
×
1343
                return r;
1344

1345
        r = context_set_kernel(c, kernel);
×
1346
        if (r < 0)
×
1347
                return r;
1348

1349
        r = context_set_initrds(c, initrds);
×
1350
        if (r < 0)
×
1351
                return r;
1352

1353
        r = context_prepare_execution(c);
×
1354
        if (r < 0)
×
1355
                return r;
1356

1357
        t = table_new_vertical();
×
1358
        if (!t)
×
1359
                return log_oom();
×
1360

1361
        r = table_add_many(t,
×
1362
                           TABLE_FIELD, "Machine ID",
1363
                           TABLE_ID128, c->machine_id,
1364
                           TABLE_FIELD, "Kernel Image Type",
1365
                           TABLE_STRING, kernel_image_type_to_string(c->kernel_image_type),
1366
                           TABLE_FIELD, "Layout",
1367
                           TABLE_STRING, context_get_layout(c),
1368
                           TABLE_FIELD, "Boot Root",
1369
                           TABLE_STRING, c->boot_root,
1370
                           TABLE_FIELD, "Entry Token Type",
1371
                           TABLE_STRING, boot_entry_token_type_to_string(c->entry_token_type),
1372
                           TABLE_FIELD, "Entry Token",
1373
                           TABLE_STRING, c->entry_token,
1374
                           TABLE_FIELD, "Entry Directory",
1375
                           TABLE_STRING, c->entry_dir,
1376
                           TABLE_FIELD, "Kernel Version",
1377
                           TABLE_VERSION, c->version,
1378
                           TABLE_FIELD, "Kernel",
1379
                           TABLE_STRING, c->kernel,
1380
                           TABLE_FIELD, "Initrds",
1381
                           TABLE_STRV, c->initrds,
1382
                           TABLE_FIELD, "Initrd Generator",
1383
                           TABLE_STRING, c->initrd_generator,
1384
                           TABLE_FIELD, "UKI Generator",
1385
                           TABLE_STRING, c->uki_generator,
1386
                           TABLE_FIELD, "Plugins",
1387
                           TABLE_STRV, c->plugins,
1388
                           TABLE_FIELD, "Plugin Environment",
1389
                           TABLE_STRV, c->envp);
1390
        if (r < 0)
×
1391
                return table_log_add_error(r);
×
1392

1393
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1394
                r = table_add_many(t,
×
1395
                                   TABLE_FIELD, "Plugin Arguments",
1396
                                   TABLE_STRV, strv_skip(c->argv, 1));
1397
                if (r < 0)
×
1398
                        return table_log_add_error(r);
×
1399
        }
1400

1401
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1402

1403
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1404
                _cleanup_free_ char *name = NULL;
×
1405

1406
                name = strdup(table_get_at(t, row, 0));
×
1407
                if (!name)
×
1408
                        return log_oom();
×
1409

1410
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1411
                if (r < 0)
×
1412
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1413
        }
1414

1415
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1416
}
1417

1418
static int verb_list(int argc, char *argv[], void *userdata) {
×
1419
        Context *c = ASSERT_PTR(userdata);
×
1420
        _cleanup_close_ int fd = -EBADF;
×
1421
        int r;
×
1422

1423
        fd = chase_and_openat(c->rfd, "/usr/lib/modules", CHASE_AT_RESOLVE_IN_ROOT, O_DIRECTORY|O_RDONLY|O_CLOEXEC, NULL);
×
1424
        if (fd < 0)
×
1425
                return log_error_errno(fd, "Failed to open %s/usr/lib/modules/: %m", strempty(arg_root));
×
1426

1427
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1428
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1429
        if (r < 0)
×
1430
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1431

1432
        _cleanup_(table_unrefp) Table *table = NULL;
×
1433
        table = table_new("version", "has kernel", "path");
×
1434
        if (!table)
×
1435
                return log_oom();
×
1436

1437
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1438
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1439
        (void) table_set_sort(table, (size_t) 0);
×
1440

1441
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1442
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1443
                if (!j)
×
1444
                        return log_oom();
×
1445

1446
                r = dirent_ensure_type(fd, *d);
×
1447
                if (r < 0) {
×
1448
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1449
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
1450
                        continue;
×
1451
                }
1452

1453
                if ((*d)->d_type != DT_DIR)
×
1454
                        continue;
×
1455

1456
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1457
                if (!fn)
×
1458
                        return log_oom();
×
1459

1460
                bool exists;
×
1461
                if (faccessat(fd, fn, F_OK, AT_SYMLINK_NOFOLLOW) < 0) {
×
1462
                        if (errno != ENOENT)
×
1463
                                log_debug_errno(errno, "Failed to check if '%s/usr/lib/modules/%s/vmlinuz' exists, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1464

1465
                        exists = false;
1466
                } else
1467
                        exists = true;
1468

1469
                r = table_add_many(table,
×
1470
                                   TABLE_VERSION, (*d)->d_name,
1471
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1472
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1473
                                   TABLE_PATH, j);
1474
                if (r < 0)
×
1475
                        return table_log_add_error(r);
×
1476
        }
1477

1478
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1479
}
1480

1481
static int help(void) {
×
1482
        _cleanup_free_ char *link = NULL;
×
1483
        int r;
×
1484

1485
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1486
        if (r < 0)
×
1487
                return log_oom();
×
1488

1489
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
×
1490
               "%5$sAdd and remove kernel and initrd images to and from the boot partition.%6$s\n"
1491
               "\n%3$sUsage:%4$s\n"
1492
               "  kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n"
1493
               "  kernel-install [OPTIONS...] add-all\n"
1494
               "  kernel-install [OPTIONS...] remove KERNEL-VERSION\n"
1495
               "  kernel-install [OPTIONS...] inspect [[[KERNEL-VERSION] KERNEL-IMAGE]\n"
1496
               "                                      [INITRD ...]]\n"
1497
               "  kernel-install [OPTIONS...] list\n"
1498
               "\n%3$sOptions:%4$s\n"
1499
               "  -h --help                    Show this help\n"
1500
               "     --version                 Show package version\n"
1501
               "  -v --verbose                 Increase verbosity\n"
1502
               "     --esp-path=PATH           Path to the EFI System Partition (ESP)\n"
1503
               "     --boot-path=PATH          Path to the $BOOT partition\n"
1504
               "     --make-entry-directory=yes|no|auto\n"
1505
               "                               Create $BOOT/ENTRY-TOKEN/ directory\n"
1506
               "     --entry-type=type1|type2|all\n"
1507
               "                               Operate only on the specified bootloader\n"
1508
               "                               entry type\n"
1509
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
1510
               "                               Entry token to be used for this installation\n"
1511
               "     --no-pager                Do not pipe inspect output into a pager\n"
1512
               "     --json=pretty|short|off   Generate JSON output\n"
1513
               "     --no-legend               Do not show the headers and footers\n"
1514
               "     --root=PATH               Operate on an alternate filesystem root\n"
1515
               "     --image=PATH              Operate on disk image as filesystem root\n"
1516
               "     --image-policy=POLICY     Specify disk image dissection policy\n"
1517
               "\n"
1518
               "This program may also be invoked as 'installkernel':\n"
1519
               "  installkernel  [OPTIONS...] VERSION VMLINUZ [MAP] [INSTALLATION-DIR]\n"
1520
               "(The optional arguments are passed by kernel build system, but ignored.)\n"
1521
               "\n"
1522
               "See the %2$s for details.\n",
1523
               program_invocation_short_name,
1524
               link,
1525
               ansi_underline(),
1526
               ansi_normal(),
1527
               ansi_highlight(),
1528
               ansi_normal());
1529

1530
        return 0;
1531
}
1532

1533
static int parse_argv(int argc, char *argv[], Context *c) {
×
1534
        enum {
×
1535
                ARG_VERSION = 0x100,
1536
                ARG_NO_LEGEND,
1537
                ARG_ESP_PATH,
1538
                ARG_BOOT_PATH,
1539
                ARG_MAKE_ENTRY_DIRECTORY,
1540
                ARG_ENTRY_TOKEN,
1541
                ARG_NO_PAGER,
1542
                ARG_JSON,
1543
                ARG_ROOT,
1544
                ARG_IMAGE,
1545
                ARG_IMAGE_POLICY,
1546
                ARG_BOOT_ENTRY_TYPE,
1547
        };
1548
        static const struct option options[] = {
×
1549
                { "help",                 no_argument,       NULL, 'h'                      },
1550
                { "version",              no_argument,       NULL, ARG_VERSION              },
1551
                { "verbose",              no_argument,       NULL, 'v'                      },
1552
                { "esp-path",             required_argument, NULL, ARG_ESP_PATH             },
1553
                { "boot-path",            required_argument, NULL, ARG_BOOT_PATH            },
1554
                { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
1555
                { "entry-token",          required_argument, NULL, ARG_ENTRY_TOKEN          },
1556
                { "no-pager",             no_argument,       NULL, ARG_NO_PAGER             },
1557
                { "json",                 required_argument, NULL, ARG_JSON                 },
1558
                { "root",                 required_argument, NULL, ARG_ROOT                 },
1559
                { "image",                required_argument, NULL, ARG_IMAGE                },
1560
                { "image-policy",         required_argument, NULL, ARG_IMAGE_POLICY         },
1561
                { "no-legend",            no_argument,       NULL, ARG_NO_LEGEND            },
1562
                { "entry-type",           required_argument, NULL, ARG_BOOT_ENTRY_TYPE      },
1563
                {}
1564
        };
1565
        int t, r;
×
1566

1567
        assert(argc >= 0);
×
1568
        assert(argv);
×
1569
        assert(c);
×
1570

1571
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1572
                switch (t) {
×
1573
                case 'h':
×
1574
                        return help();
×
1575

1576
                case ARG_VERSION:
×
1577
                        return version();
×
1578

1579
                case ARG_NO_LEGEND:
×
1580
                        arg_legend = false;
×
1581
                        break;
×
1582

1583
                case 'v':
×
1584
                        log_set_max_level(LOG_DEBUG);
×
1585
                        arg_verbose = true;
×
1586
                        break;
×
1587

1588
                case ARG_ESP_PATH:
×
1589
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_esp_path);
×
1590
                        if (r < 0)
×
1591
                                return log_oom();
×
1592
                        break;
1593

1594
                case ARG_BOOT_PATH:
×
1595
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_xbootldr_path);
×
1596
                        if (r < 0)
×
1597
                                return log_oom();
×
1598
                        break;
1599

1600
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1601
                        if (streq(optarg, "auto"))
×
1602
                                arg_make_entry_directory = -1;
×
1603
                        else {
1604
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1605
                                if (r < 0)
×
1606
                                        return r;
1607

1608
                                arg_make_entry_directory = r;
×
1609
                        }
1610
                        break;
1611

1612
                case ARG_ENTRY_TOKEN:
×
1613
                        r = parse_boot_entry_token_type(optarg, &c->entry_token_type, &c->entry_token);
×
1614
                        if (r < 0)
×
1615
                                return r;
1616
                        break;
1617

1618
                case ARG_NO_PAGER:
×
1619
                        arg_pager_flags |= PAGER_DISABLE;
×
1620
                        break;
×
1621

1622
                case ARG_JSON:
×
1623
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
NEW
1624
                        if (r <= 0)
×
1625
                                return r;
1626
                        break;
1627

1628
                case ARG_ROOT:
×
1629
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
1630
                        if (r < 0)
×
1631
                                return r;
1632
                        break;
1633

1634
                case ARG_IMAGE:
×
1635
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1636
                        if (r < 0)
×
1637
                                return r;
1638
                        break;
1639

1640
                case ARG_IMAGE_POLICY:
×
1641
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1642
                        if (r < 0)
×
1643
                                return r;
1644
                        break;
1645

1646
                case ARG_BOOT_ENTRY_TYPE:
×
1647
                        r = context_set_entry_type(c, optarg);
×
1648
                        if (r < 0)
×
1649
                                return r;
1650
                        break;
1651

1652
                case '?':
1653
                        return -EINVAL;
1654

1655
                default:
×
1656
                        assert_not_reached();
×
1657
                }
1658

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

1662
        return 1;
1663
}
1664

1665
static int run(int argc, char* argv[]) {
×
1666
        static const Verb verbs[] = {
×
1667
                { "add",         1,        VERB_ANY, 0,            verb_add            },
1668
                { "add-all",     1,        1,        0,            verb_add_all        },
1669
                { "remove",      2,        VERB_ANY, 0,            verb_remove         },
1670
                { "inspect",     1,        VERB_ANY, VERB_DEFAULT, verb_inspect        },
1671
                { "list",        1,        1,        0,            verb_list           },
1672
                {}
1673
        };
1674
        _cleanup_(context_done) Context c = {
×
1675
                .rfd = AT_FDCWD,
1676
                .action = _ACTION_INVALID,
1677
                .kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN,
1678
                .layout = _LAYOUT_INVALID,
1679
                .entry_type = _BOOT_ENTRY_TYPE_INVALID,
1680
                .entry_token_type = BOOT_ENTRY_TOKEN_AUTO,
1681
        };
1682
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1683
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1684
        int r;
×
1685

1686
        log_setup();
×
1687

1688
        r = parse_argv(argc, argv, &c);
×
1689
        if (r <= 0)
×
1690
                return r;
1691

1692
        if (arg_image) {
×
1693
                assert(!arg_root);
×
1694

1695
                r = mount_image_privately_interactively(
×
1696
                                arg_image,
1697
                                arg_image_policy,
1698
                                DISSECT_IMAGE_GENERIC_ROOT |
1699
                                DISSECT_IMAGE_REQUIRE_ROOT |
1700
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1701
                                DISSECT_IMAGE_VALIDATE_OS |
1702
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1703
                                &mounted_dir,
1704
                                /* ret_dir_fd= */ NULL,
1705
                                &loop_device);
1706
                if (r < 0)
×
1707
                        return r;
1708

1709
                arg_root = strdup(mounted_dir);
×
1710
                if (!arg_root)
×
1711
                        return log_oom();
×
1712
        }
1713

1714
        r = context_init(&c);
×
1715
        if (r < 0)
×
1716
                return r;
1717

1718
        if (invoked_as(argv, "installkernel"))
×
1719
                return run_as_installkernel(argc, argv, &c);
×
1720

1721
        return dispatch_verb(argc, argv, verbs, &c);
×
1722
}
1723

1724
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