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

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 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 <stdbool.h>
5
#include <sys/utsname.h>
6

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

40
static bool arg_verbose = false;
41
static char *arg_esp_path = NULL;
42
static char *arg_xbootldr_path = NULL;
43
static int arg_make_entry_directory = -1; /* tristate */
44
static PagerFlags arg_pager_flags = 0;
45
static sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
46
static char *arg_root = NULL;
47
static char *arg_image = NULL;
48
static ImagePolicy *arg_image_policy = NULL;
49
static bool arg_legend = true;
50

51
STATIC_DESTRUCTOR_REGISTER(arg_esp_path, freep);
×
52
STATIC_DESTRUCTOR_REGISTER(arg_xbootldr_path, freep);
×
53
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
×
54
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
×
55
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
×
56

57
typedef enum Action {
58
        ACTION_ADD,
59
        ACTION_REMOVE,
60
        ACTION_INSPECT,
61
        _ACTION_MAX,
62
        _ACTION_INVALID = -EINVAL,
63
} Action;
64

65
typedef enum Layout {
66
        LAYOUT_AUTO,
67
        LAYOUT_UKI,
68
        LAYOUT_BLS,
69
        LAYOUT_OTHER,
70
        _LAYOUT_MAX,
71
        _LAYOUT_INVALID = -EINVAL,
72
} Layout;
73

74
static const char * const layout_table[_LAYOUT_MAX] = {
75
        [LAYOUT_AUTO]  = "auto",
76
        [LAYOUT_UKI]   = "uki",
77
        [LAYOUT_BLS]   = "bls",
78
        [LAYOUT_OTHER] = "other",
79
};
80

81
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
82

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

107
#define CONTEXT_NULL (Context) { .rfd = -EBADF }
108

109
static void context_done(Context *c) {
×
110
        assert(c);
×
111

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

130
        safe_close(c->rfd);
×
131
}
×
132

133
static int context_copy(const Context *source, Context *ret) {
×
134
        int r;
×
135

136
        assert(source);
×
137
        assert(ret);
×
138
        assert(source->rfd >= 0 || source->rfd == AT_FDCWD);
×
139

140
        _cleanup_(context_done) Context copy = (Context) {
×
141
                .rfd = AT_FDCWD,
142
                .action = source->action,
×
143
                .machine_id = source->machine_id,
144
                .machine_id_is_random = source->machine_id_is_random,
×
145
                .kernel_image_type = source->kernel_image_type,
×
146
                .layout = source->layout,
×
147
                .entry_token_type = source->entry_token_type,
×
148
        };
149

150
        if (source->rfd >= 0) {
×
151
                copy.rfd = fd_reopen(source->rfd, O_CLOEXEC|O_DIRECTORY|O_PATH);
×
152
                if (copy.rfd < 0)
×
153
                        return copy.rfd;
154
        }
155

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

199
        *ret = copy;
×
200
        copy = CONTEXT_NULL;
×
201

202
        return 0;
×
203
}
204

205
static int context_open_root(Context *c) {
×
206
        int r;
×
207

208
        assert(c);
×
209
        assert(c->rfd < 0);
×
210

211
        if (isempty(arg_root))
×
212
                return 0;
213

214
        r = path_is_root(arg_root);
×
215
        if (r < 0)
×
216
                return log_error_errno(r, "Failed to determine if '%s' is the root directory: %m", arg_root);
×
217
        if (r > 0)
×
218
                return 0;
219

220
        c->rfd = open(empty_to_root(arg_root), O_CLOEXEC | O_DIRECTORY | O_PATH);
×
221
        if (c->rfd < 0)
×
222
                return log_error_errno(errno, "Failed to open root directory '%s': %m", empty_to_root(arg_root));
×
223

224
        return 0;
225
}
226

227
static const char* context_get_layout(const Context *c) {
×
228
        assert(c);
×
229
        assert(c->layout >= 0);
×
230

231
        return c->layout_other ?: layout_to_string(c->layout);
×
232
}
233

234
static int context_set_layout(Context *c, const char *s, const char *source) {
×
235
        Layout t;
×
236

237
        assert(c);
×
238
        assert(source);
×
239

240
        if (c->layout >= 0 || !s)
×
241
                return 0;
242

243
        assert(!c->layout_other);
×
244

245
        t = layout_from_string(s);
×
246
        if (t >= 0)
×
247
                c->layout = t;
×
248
        else if (isempty(s))
×
249
                c->layout = LAYOUT_AUTO;
×
250
        else {
251
                c->layout_other = strdup(s);
×
252
                if (!c->layout_other)
×
253
                        return log_oom();
×
254

255
                c->layout = LAYOUT_OTHER;
×
256
        }
257

258
        log_debug("layout=%s set via %s", context_get_layout(c), source);
×
259
        return 1;
260
}
261

262
static int context_set_machine_id(Context *c, const char *s, const char *source) {
×
263
        int r;
×
264

265
        assert(c);
×
266
        assert(source);
×
267

268
        if (!sd_id128_is_null(c->machine_id) || !s)
×
269
                return 0;
×
270

271
        r = sd_id128_from_string(s, &c->machine_id);
×
272
        if (r < 0)
×
273
                return log_warning_errno(r, "Failed to parse machine ID specified via %s, ignoring.", source);
×
274

275
        if (sd_id128_is_null(c->machine_id))
×
276
                return 0;
×
277

278
        log_debug("MACHINE_ID=%s set via %s.", SD_ID128_TO_STRING(c->machine_id), source);
×
279
        return 1;
×
280
}
281

282
static int context_set_string(const char *s, const char *source, const char *name, char **dest) {
×
283
        char *p;
×
284

285
        assert(source);
×
286
        assert(name);
×
287
        assert(dest);
×
288

289
        if (*dest || !s)
×
290
                return 0;
291

292
        p = strdup(s);
×
293
        if (!p)
×
294
                return log_oom();
×
295

296
        log_debug("%s (%s) set via %s.", name, p, source);
×
297

298
        *dest = p;
×
299
        return 1;
×
300
}
301

302
static int context_set_initrd_generator(Context *c, const char *s, const char *source) {
×
303
        assert(c);
×
304
        return context_set_string(s, source, "INITRD_GENERATOR", &c->initrd_generator);
×
305
}
306

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

312
static int context_set_version(Context *c, const char *s) {
×
313
        assert(c);
×
314

315
        if (s && !filename_is_valid(s))
×
316
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid version specified: %s", s);
×
317

318
        return context_set_string(s, "command line", "kernel version", &c->version);
×
319
}
320

321
static int context_set_path(Context *c, const char *s, const char *source, const char *name, char **dest) {
×
322
        char *p;
×
323
        int r;
×
324

325
        assert(c);
×
326
        assert(source);
×
327
        assert(name);
×
328
        assert(dest);
×
329

330
        if (*dest || !s)
×
331
                return 0;
×
332

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

345
        log_debug("%s (%s) set via %s.", name, p, source);
×
346

347
        *dest = p;
×
348
        return 1;
×
349
}
350

351
static int context_set_boot_root(Context *c, const char *s, const char *source) {
×
352
        assert(c);
×
353
        return context_set_path(c, s, source, "BOOT_ROOT", &c->boot_root);
×
354
}
355

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

361
static int context_set_kernel(Context *c, const char *s) {
×
362
        assert(c);
×
363
        return context_set_path(c, s, "command line", "kernel image file", &c->kernel);
×
364
}
365

366
static int context_set_path_strv(Context *c, char* const* strv, const char *source, const char *name, char ***dest) {
×
367
        _cleanup_strv_free_ char **w = NULL;
×
368
        int r;
×
369

370
        assert(c);
×
371
        assert(source);
×
372
        assert(name);
×
373
        assert(dest);
×
374

375
        if (*dest)
×
376
                return 0;
377

378
        STRV_FOREACH(s, strv) {
×
379
                char *p;
×
380

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

397
        if (strv_isempty(w))
×
398
                return 0;
399

400
        log_debug("%s set via %s", name, source);
×
401

402
        *dest = TAKE_PTR(w);
×
403
        return 1;
×
404
}
405

406
static int context_set_plugins(Context *c, const char *s, const char *source) {
×
407
        _cleanup_strv_free_ char **v = NULL;
×
408
        int r;
×
409

410
        assert(c);
×
411

412
        if (c->plugins || !s)
×
413
                return 0;
414

415
        r = strv_split_full(&v, s, NULL, EXTRACT_UNQUOTE);
×
416
        if (r < 0)
×
417
                return log_error_errno(r, "Failed to parse plugin paths from %s: %m", source);
×
418

419
        return context_set_path_strv(c, v, source, "plugins", &c->plugins);
×
420
}
421

422
static int context_set_initrds(Context *c, char* const* strv) {
×
423
        assert(c);
×
424
        return context_set_path_strv(c, strv, "command line", "initrds", &c->initrds);
×
425
}
426

427
static int context_load_environment(Context *c) {
×
428
        assert(c);
×
429

430
        (void) context_set_machine_id(c, getenv("MACHINE_ID"), "environment");
×
431
        (void) context_set_boot_root(c, getenv("BOOT_ROOT"), "environment");
×
432
        (void) context_set_conf_root(c, getenv("KERNEL_INSTALL_CONF_ROOT"), "environment");
×
433
        (void) context_set_plugins(c, getenv("KERNEL_INSTALL_PLUGINS"), "environment");
×
434
        return 0;
×
435
}
436

437
static int context_load_install_conf(Context *c) {
×
438
        _cleanup_free_ char *machine_id = NULL, *boot_root = NULL, *layout = NULL,
×
439
                            *initrd_generator = NULL, *uki_generator = NULL;
×
440
        int r;
×
441

442
        assert(c);
×
443

444
        r = load_kernel_install_conf(arg_root,
×
445
                                     c->conf_root,
×
446
                                     &machine_id,
447
                                     &boot_root,
448
                                     &layout,
449
                                     &initrd_generator,
450
                                     &uki_generator);
451
        if (r <= 0)
×
452
                return r;
453

454
        (void) context_set_machine_id(c, machine_id, "config");
×
455
        (void) context_set_boot_root(c, boot_root, "config");
×
456
        (void) context_set_layout(c, layout, "config");
×
457
        (void) context_set_initrd_generator(c, initrd_generator, "config");
×
458
        (void) context_set_uki_generator(c, uki_generator, "config");
×
459

460
        log_debug("Loaded config.");
×
461
        return 0;
462
}
463

464
static int context_load_machine_info(Context *c) {
×
465
        _cleanup_fclose_ FILE *f = NULL;
×
466
        _cleanup_free_ char *machine_id = NULL, *layout = NULL;
×
467
        static const char *path = "/etc/machine-info";
×
468
        int r;
×
469

470
        assert(c);
×
471

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

475
        if (!sd_id128_is_null(c->machine_id) && c->layout >= 0)
×
476
                return 0;
477

478
        /* For testing. To make not read host's /etc/machine-info. */
479
        r = getenv_bool("KERNEL_INSTALL_READ_MACHINE_INFO");
×
480
        if (r < 0 && r != -ENXIO)
×
481
                log_warning_errno(r, "Failed to read $KERNEL_INSTALL_READ_MACHINE_INFO, assuming yes: %m");
×
482
        if (r == 0) {
×
483
                log_debug("Skipping reading of /etc/machine-info.");
×
484
                return 0;
×
485
        }
486

487
        r = chase_and_fopenat_unlocked(c->rfd, path, CHASE_AT_RESOLVE_IN_ROOT, "re", NULL, &f);
×
488
        if (r == -ENOENT)
×
489
                return 0;
490
        if (r < 0)
×
491
                return log_error_errno(r, "Failed to chase %s: %m", path);
×
492

493
        log_debug("Loading %s…", path);
×
494

495
        r = parse_env_file(f, path,
×
496
                           "KERNEL_INSTALL_MACHINE_ID", &machine_id,
497
                           "KERNEL_INSTALL_LAYOUT", &layout);
498
        if (r < 0)
×
499
                return log_error_errno(r, "Failed to parse '%s': %m", path);
×
500

501
        (void) context_set_machine_id(c, machine_id, path);
×
502
        (void) context_set_layout(c, layout, path);
×
503
        return 0;
504
}
505

506
static int context_load_machine_id(Context *c) {
×
507
        int r;
×
508

509
        assert(c);
×
510

511
        r = id128_get_machine_at(c->rfd, &c->machine_id);
×
512
        if (ERRNO_IS_NEG_MACHINE_ID_UNSET(r))
×
513
                return 0;
514
        if (r < 0)
×
515
                return log_error_errno(r, "Failed to load machine ID from /etc/machine-id: %m");
×
516

517
        log_debug("MACHINE_ID=%s set via /etc/machine-id.", SD_ID128_TO_STRING(c->machine_id));
×
518
        return 1; /* loaded */
×
519
}
520

521
static int context_ensure_machine_id(Context *c) {
×
522
        int r;
×
523

524
        assert(c);
×
525

526
        if (!sd_id128_is_null(c->machine_id))
×
527
                return 0;
×
528

529
        /* If /etc/machine-id is initialized we'll use it. */
530
        r = context_load_machine_id(c);
×
531
        if (r != 0)
×
532
                return r;
533

534
        /* Otherwise we'll use a freshly generated one. */
535
        r = sd_id128_randomize(&c->machine_id);
×
536
        if (r < 0)
×
537
                return log_error_errno(r, "Failed to generate random ID: %m");
×
538

539
        c->machine_id_is_random = true;
×
540
        log_debug("New machine ID '%s' generated.", SD_ID128_TO_STRING(c->machine_id));
×
541
        return 0;
×
542
}
543

544
static int context_acquire_xbootldr(Context *c) {
×
545
        int r;
×
546

547
        assert(c);
×
548
        assert(!c->boot_root);
×
549

550
        r = find_xbootldr_and_warn_at(
×
551
                        /* rfd = */ c->rfd,
552
                        /* path = */ arg_xbootldr_path,
553
                        /* unprivileged_mode= */ -1,
554
                        /* ret_path = */ &c->boot_root,
555
                        /* ret_uuid = */ NULL,
556
                        /* ret_devid = */ NULL);
557
        if (r == -ENOKEY) {
×
558
                log_debug_errno(r, "Couldn't find an XBOOTLDR partition.");
×
559
                return 0;
×
560
        }
561
        if (r == -EACCES && geteuid() != 0)
×
562
                return log_error_errno(r, "Failed to determine XBOOTLDR partition: %m");
×
563
        if (r < 0)
×
564
                return r;
565

566
        log_debug("Using XBOOTLDR partition at %s as $BOOT_ROOT.", c->boot_root);
×
567
        return 1; /* found */
568
}
569

570
static int context_acquire_esp(Context *c) {
×
571
        int r;
×
572

573
        assert(c);
×
574
        assert(!c->boot_root);
×
575

576
        r = find_esp_and_warn_at(
×
577
                        /* rfd = */ c->rfd,
578
                        /* path = */ arg_esp_path,
579
                        /* unprivileged_mode= */ -1,
580
                        /* ret_path = */ &c->boot_root,
581
                        /* ret_part = */ NULL,
582
                        /* ret_pstart = */ NULL,
583
                        /* ret_psize = */ NULL,
584
                        /* ret_uuid = */ NULL,
585
                        /* ret_devid = */ NULL);
586
        if (r == -ENOKEY) {
×
587
                log_debug_errno(r, "Couldn't find EFI system partition, ignoring.");
×
588
                return 0;
×
589
        }
590
        if (r == -EACCES && geteuid() != 0)
×
591
                return log_error_errno(r, "Failed to determine EFI system partition: %m");
×
592
        if (r < 0)
×
593
                return r;
594

595
        log_debug("Using EFI System Partition at %s as $BOOT_ROOT.", c->boot_root);
×
596
        return 1; /* found */
597
}
598

599
static int context_ensure_boot_root(Context *c) {
×
600
        int r;
×
601

602
        assert(c);
×
603

604
        /* If BOOT_ROOT is specified via environment or install.conf, then use it. */
605
        if (c->boot_root)
×
606
                return 0;
607

608
        /* Otherwise, use XBOOTLDR partition, if mounted. */
609
        r = context_acquire_xbootldr(c);
×
610
        if (r != 0)
×
611
                return r;
612

613
        /* Otherwise, use EFI system partition, if mounted. */
614
        r = context_acquire_esp(c);
×
615
        if (r != 0)
×
616
                return r;
617

618
        /* If all else fails, use /boot. */
619
        if (c->rfd >= 0) {
×
620
                r = chaseat(c->rfd, "/boot", CHASE_AT_RESOLVE_IN_ROOT, &c->boot_root, /* ret_fd = */ NULL);
×
621
                if (r < 0)
×
622
                        return log_error_errno(r, "Failed to chase '/boot/': %m");
×
623
        } else {
624
                c->boot_root = strdup("/boot");
×
625
                if (!c->boot_root)
×
626
                        return log_oom();
×
627
        }
628

629
        log_debug("KERNEL_INSTALL_BOOT_ROOT autodetection yielded no candidates, using \"%s\".", c->boot_root);
×
630
        return 0;
631
}
632

633
static int context_ensure_entry_token(Context *c) {
×
634
        int r;
×
635

636
        assert(c);
×
637

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

643
        r = boot_entry_token_ensure_at(
×
644
                        c->rfd,
645
                        c->conf_root,
×
646
                        c->machine_id,
647
                        c->machine_id_is_random,
×
648
                        &c->entry_token_type,
649
                        &c->entry_token);
650
        if (r < 0)
×
651
                return r;
652

653
        log_debug("Using entry token: %s", c->entry_token);
×
654
        return 0;
655
}
656

657
static int context_load_plugins(Context *c) {
×
658
        int r;
×
659

660
        assert(c);
×
661

662
        if (c->plugins)
×
663
                return 0;
×
664

665
        r = conf_files_list_strv_at(
×
666
                        &c->plugins,
667
                        ".install",
668
                        c->rfd,
669
                        CONF_FILES_EXECUTABLE | CONF_FILES_REGULAR | CONF_FILES_FILTER_MASKED,
670
                        STRV_MAKE_CONST("/etc/kernel/install.d", "/usr/lib/kernel/install.d"));
×
671
        if (r < 0)
×
672
                return log_error_errno(r, "Failed to find plugins: %m");
×
673

674
        return 0;
675
}
676

677
static int context_init(Context *c) {
×
678
        int r;
×
679

680
        assert(c);
×
681

682
        r = context_open_root(c);
×
683
        if (r < 0)
×
684
                return r;
685

686
        r = context_load_environment(c);
×
687
        if (r < 0)
×
688
                return r;
689

690
        r = context_load_install_conf(c);
×
691
        if (r < 0)
×
692
                return r;
693

694
        r = context_load_machine_info(c);
×
695
        if (r < 0)
×
696
                return r;
697

698
        r = context_ensure_machine_id(c);
×
699
        if (r < 0)
×
700
                return r;
701

702
        r = context_ensure_boot_root(c);
×
703
        if (r < 0)
×
704
                return r;
705

706
        r = context_ensure_entry_token(c);
×
707
        if (r < 0)
×
708
                return r;
709

710
        r = context_load_plugins(c);
×
711
        if (r < 0)
×
712
                return r;
×
713

714
        return 0;
715
}
716

717
static int context_inspect_kernel(Context *c) {
×
718
        assert(c);
×
719

720
        if (!c->kernel)
×
721
                return 0;
722

723
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
724
}
725

726
static int context_ensure_layout(Context *c) {
×
727
        int r;
×
728

729
        assert(c);
×
730
        assert(c->boot_root);
×
731
        assert(c->entry_token);
×
732

733
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
734
                return 0;
×
735

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

739
        if (c->kernel_image_type == KERNEL_IMAGE_TYPE_UKI) {
×
740
                c->layout = LAYOUT_UKI;
×
741
                log_debug("Kernel image type is %s, using layout=%s.",
×
742
                          kernel_image_type_to_string(c->kernel_image_type), layout_to_string(c->layout));
743
                return 0;
×
744
        }
745

746
        _cleanup_free_ char *srel_path = path_join(c->boot_root, "loader/entries.srel");
×
747
        if (!srel_path)
×
748
                return log_oom();
×
749

750
        _cleanup_free_ char *srel = NULL;
×
751
        r = read_one_line_file_at(c->rfd, srel_path, &srel);
×
752
        if (r >= 0) {
×
753
                if (streq(srel, "type1"))
×
754
                        /* The loader/entries.srel file clearly indicates that the installed boot loader
755
                         * implements the proper standard upstream boot loader spec for Type #1 entries.
756
                         * Let's default to that, then. */
757
                        c->layout = LAYOUT_BLS;
×
758
                else
759
                        /* The loader/entries.srel file indicates some other spec is implemented and owns the
760
                         * /loader/entries/ directory. Since we have no idea what that means, let's stay away
761
                         * from it by default. */
762
                        c->layout = LAYOUT_OTHER;
×
763

764
                log_debug("%s with '%s' found, using layout=%s.", srel_path, srel, layout_to_string(c->layout));
×
765
                return 0;
×
766
        } else if (r != -ENOENT)
×
767
                return log_error_errno(r, "Failed to read %s: %m", srel_path);
×
768

769
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
770
        if (!entry_token_path)
×
771
                return log_oom();
×
772

773
        r = is_dir_at(c->rfd, entry_token_path, /* follow = */ false);
×
774
        if (r < 0 && r != -ENOENT)
×
775
                return log_error_errno(r, "Failed to check if '%s' is a directory: %m", entry_token_path);
×
776
        if (r > 0) {
×
777
                /* If the metadata in $BOOT_ROOT doesn't tell us anything, then check if the entry token
778
                 * directory already exists. If so, let's assume it's the standard boot loader spec, too. */
779
                c->layout = LAYOUT_BLS;
×
780
                log_debug("%s exists, using layout=%s.", entry_token_path, layout_to_string(c->layout));
×
781
                return 0;
×
782
        }
783

784
        /* There's no metadata in $BOOT_ROOT, and apparently no entry token directory installed? Then we
785
         * really don't know anything. */
786
        c->layout = LAYOUT_OTHER;
×
787
        log_debug("Entry-token directory not found, using layout=%s.", layout_to_string(c->layout));
×
788
        return 0;
789
}
790

791
static int context_set_up_staging_area(Context *c) {
×
792
        static const char *template = "/tmp/kernel-install.staging.XXXXXX";
×
793
        int r;
×
794

795
        assert(c);
×
796

797
        if (c->staging_area)
×
798
                return 0;
799

800
        if (c->action == ACTION_INSPECT) {
×
801
                /* This is only used for display. The directory will not be created. */
802
                c->staging_area = strdup(template);
×
803
                if (!c->staging_area)
×
804
                        return log_oom();
×
805
        } else {
806
                r = mkdtemp_malloc(template, &c->staging_area);
×
807
                if (r < 0)
×
808
                        return log_error_errno(r, "Failed to create staging area: %m");
×
809
        }
810

811
        return 0;
812
}
813

814
static int context_build_entry_dir(Context *c) {
×
815
        assert(c);
×
816
        assert(c->boot_root);
×
817
        assert(c->entry_token);
×
818
        assert(c->version || c->action == ACTION_INSPECT);
×
819

820
        if (c->entry_dir)
×
821
                return 0;
822

823
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
824
        if (!c->entry_dir)
×
825
                return log_oom();
×
826

827
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
828
        return 0;
829
}
830

831
static bool context_should_make_entry_dir(Context *c) {
×
832
        assert(c);
×
833

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

837
        if (arg_make_entry_directory < 0)
×
838
                return c->layout == LAYOUT_BLS;
×
839

840
        return arg_make_entry_directory;
×
841
}
842

843
static int context_make_entry_dir(Context *c) {
×
844
        _cleanup_close_ int fd = -EBADF;
×
845

846
        assert(c);
×
847
        assert(c->entry_dir);
×
848

849
        if (c->action != ACTION_ADD)
×
850
                return 0;
851

852
        if (!context_should_make_entry_dir(c))
×
853
                return 0;
854

855
        log_debug("mkdir -p %s", c->entry_dir);
×
856
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
857
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
858
        if (fd < 0)
×
859
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
860

861
        return 0;
862
}
863

864
static int context_remove_entry_dir(Context *c) {
×
865
        _cleanup_free_ char *p = NULL;
×
866
        _cleanup_close_ int fd = -EBADF;
×
867
        struct stat st;
×
868
        int r;
×
869

870
        assert(c);
×
871
        assert(c->entry_dir);
×
872

873
        if (c->action != ACTION_REMOVE)
×
874
                return 0;
875

876
        if (!context_should_make_entry_dir(c))
×
877
                return 0;
878

879
        log_debug("rm -rf %s", c->entry_dir);
×
880
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
881
        if (fd < 0) {
×
882
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
883
                        return 0;
884
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
885
        }
886

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

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

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

897
        return 0;
898
}
899

900
static int context_build_arguments(Context *c) {
×
901
        _cleanup_strv_free_ char **a = NULL;
×
902
        const char *verb;
×
903
        int r;
×
904

905
        assert(c);
×
906
        assert(c->entry_dir);
×
907

908
        if (c->argv)
×
909
                return 0;
910

911
        switch (c->action) {
×
912
        case ACTION_ADD:
×
913
                assert(c->version);
×
914
                assert(c->kernel);
×
915
                verb = "add";
916
                break;
917

918
        case ACTION_REMOVE:
×
919
                assert(c->version);
×
920
                assert(!c->kernel);
×
921
                assert(!c->initrds);
×
922
                verb = "remove";
923
                break;
924

925
        case ACTION_INSPECT:
926
                verb = "add|remove";
927
                break;
928

929
        default:
×
930
                assert_not_reached();
×
931
        }
932

933
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
934
                     verb,
935
                     c->version ?: "KERNEL_VERSION",
936
                     c->entry_dir);
937
        if (!a)
×
938
                return log_oom();
×
939

940
        if (c->action == ACTION_ADD) {
×
941
                r = strv_extend(&a, c->kernel);
×
942
                if (r < 0)
×
943
                        return log_oom();
×
944

945
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates = */ false);
×
946
                if (r < 0)
×
947
                        return log_oom();
×
948

949
        } else if (c->action == ACTION_INSPECT) {
×
950
                r = strv_extend_many(
×
951
                                &a,
952
                                c->kernel ?: "[KERNEL_IMAGE]",
953
                                "[INITRD...]");
954
                if (r < 0)
×
955
                        return log_oom();
×
956
        }
957

958
        c->argv = TAKE_PTR(a);
×
959
        return 0;
×
960
}
961

962
static int context_build_environment(Context *c) {
×
963
        _cleanup_strv_free_ char **e = NULL;
×
964
        int r;
×
965

966
        assert(c);
×
967

968
        if (c->envp)
×
969
                return 0;
970

971
        r = strv_env_assign_many(&e,
×
972
                                 "LC_COLLATE",                      SYSTEMD_DEFAULT_LOCALE,
973
                                 "KERNEL_INSTALL_VERBOSE",          one_zero(arg_verbose),
974
                                 "KERNEL_INSTALL_IMAGE_TYPE",       kernel_image_type_to_string(c->kernel_image_type),
975
                                 "KERNEL_INSTALL_MACHINE_ID",       SD_ID128_TO_STRING(c->machine_id),
976
                                 "KERNEL_INSTALL_ENTRY_TOKEN",      c->entry_token,
977
                                 "KERNEL_INSTALL_BOOT_ROOT",        c->boot_root,
978
                                 "KERNEL_INSTALL_LAYOUT",           context_get_layout(c),
979
                                 "KERNEL_INSTALL_INITRD_GENERATOR", strempty(c->initrd_generator),
980
                                 "KERNEL_INSTALL_UKI_GENERATOR",    strempty(c->uki_generator),
981
                                 "KERNEL_INSTALL_STAGING_AREA",     c->staging_area);
982
        if (r < 0)
×
983
                return log_error_errno(r, "Failed to build environment variables for plugins: %m");
×
984

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

989
static int context_prepare_execution(Context *c) {
×
990
        int r;
×
991

992
        assert(c);
×
993

994
        r = context_inspect_kernel(c);
×
995
        if (r < 0)
×
996
                return r;
997

998
        r = context_ensure_layout(c);
×
999
        if (r < 0)
×
1000
                return r;
1001

1002
        r = context_set_up_staging_area(c);
×
1003
        if (r < 0)
×
1004
                return r;
1005

1006
        r = context_build_entry_dir(c);
×
1007
        if (r < 0)
×
1008
                return r;
1009

1010
        r = context_build_arguments(c);
×
1011
        if (r < 0)
×
1012
                return r;
1013

1014
        r = context_build_environment(c);
×
1015
        if (r < 0)
×
1016
                return r;
×
1017

1018
        return 0;
1019
}
1020

1021
static int context_execute(Context *c) {
×
1022
        int r, ret;
×
1023

1024
        assert(c);
×
1025

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

1030
        if (DEBUG_LOGGING) {
×
1031
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator = */ false);
×
1032
                log_debug("Using plugins: %s", strna(x));
×
1033

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

1037
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1038
                log_debug("Plugin arguments: %s", strna(z));
×
1039
        }
1040

1041
        ret = execute_strv(
×
1042
                        /* name = */ NULL,
1043
                        c->plugins,
×
1044
                        /* root = */ NULL,
1045
                        USEC_INFINITY,
1046
                        /* callbacks = */ NULL,
1047
                        /* callback_args = */ NULL,
1048
                        c->argv,
1049
                        c->envp,
1050
                        EXEC_DIR_SKIP_REMAINING);
1051

1052
        r = context_remove_entry_dir(c);
×
1053
        if (r < 0)
×
1054
                return r;
×
1055

1056
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1057
        return ret;
1058
}
1059

1060
static bool bypass(void) {
×
1061
        return should_bypass("KERNEL_INSTALL");
×
1062
}
1063

1064
static int do_add(
×
1065
                Context *c,
1066
                const char *version,
1067
                const char *kernel,
1068
                char **initrds) {
1069

1070
        int r;
×
1071

1072
        assert(c);
×
1073
        assert(version);
×
1074
        assert(kernel);
×
1075

1076
        r = context_set_version(c, version);
×
1077
        if (r < 0)
×
1078
                return r;
1079

1080
        r = context_set_kernel(c, kernel);
×
1081
        if (r < 0)
×
1082
                return r;
1083

1084
        r = context_set_initrds(c, initrds);
×
1085
        if (r < 0)
×
1086
                return r;
1087

1088
        r = context_prepare_execution(c);
×
1089
        if (r < 0)
×
1090
                return r;
1091

1092
        return context_execute(c);
×
1093
}
1094

1095
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1096
        _cleanup_free_ char *vmlinuz = NULL;
×
1097
        int r;
×
1098

1099
        assert(version);
×
1100

1101
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1102
        if (!vmlinuz)
×
1103
                return log_oom();
×
1104

1105
        r = access_nofollow(vmlinuz, F_OK);
×
1106
        if (r == -ENOENT)
×
1107
                return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
×
1108
        if (r < 0)
×
1109
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1110

1111
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1112
        return 0;
×
1113
}
1114

1115
static int verb_add(int argc, char *argv[], void *userdata) {
×
1116
        Context *c = ASSERT_PTR(userdata);
×
1117
        _cleanup_free_ char *vmlinuz = NULL;
×
1118
        const char *version, *kernel;
×
1119
        char **initrds;
×
1120
        struct utsname un;
×
1121
        int r;
×
1122

1123
        assert(argv);
×
1124

1125
        if (arg_root)
×
1126
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1127

1128
        if (bypass())
×
1129
                return 0;
1130

1131
        c->action = ACTION_ADD;
×
1132

1133
        /* We use the same order of arguments that "inspect" introduced, i.e. if only on argument is
1134
         * specified we take it as the kernel path, not the version, i.e. it's the first argument that is
1135
         * optional, not the 2nd. */
1136
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1137
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1138
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1139
        initrds = strv_skip(argv, 3);
×
1140

1141
        if (!version) {
×
1142
                assert_se(uname(&un) >= 0);
×
1143
                version = un.release;
1144
        }
1145

1146
        if (!kernel) {
×
1147
                r = kernel_from_version(version, &vmlinuz);
×
1148
                if (r < 0)
×
1149
                        return r;
1150

1151
                kernel = vmlinuz;
×
1152
        }
1153

1154
        return do_add(c, version, kernel, initrds);
×
1155
}
1156

1157
static int verb_add_all(int argc, char *argv[], void *userdata) {
×
1158
        Context *c = ASSERT_PTR(userdata);
×
1159
        _cleanup_close_ int fd = -EBADF;
×
1160
        size_t n = 0;
×
1161
        int ret = 0, r;
×
1162

1163
        assert(argv);
×
1164

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

1168
        if (bypass())
×
1169
                return 0;
1170

1171
        c->action = ACTION_ADD;
×
1172

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

1177
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1178
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1179
        if (r < 0)
×
1180
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1181

1182
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1183
                r = dirent_ensure_type(fd, *d);
×
1184
                if (r < 0) {
×
1185
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1186
                                log_debug_errno(r, "Failed to check if '%s/usr/lib/modules/%s' is a directory, ignoring: %m", strempty(arg_root), (*d)->d_name);
×
1187
                        continue;
×
1188
                }
1189

1190
                if ((*d)->d_type != DT_DIR)
×
1191
                        continue;
×
1192

1193
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1194
                if (!fn)
×
1195
                        return log_oom();
×
1196

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

1201
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1202
                        continue;
×
1203
                }
1204

1205
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1206

1207
                r = context_copy(c, &copy);
×
1208
                if (r < 0)
×
1209
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1210

1211
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1212
                 * with arg_root here. */
1213
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1214
                if (!full)
×
1215
                        return log_oom();
×
1216

1217
                r = do_add(&copy,
×
1218
                           /* version= */ (*d)->d_name,
×
1219
                           /* kernel= */ full,
1220
                           /* initrds= */ NULL);
1221
                if (r == 0)
×
1222
                        n++;
×
1223
                else if (ret == 0)
×
1224
                        ret = r;
×
1225
        }
1226

1227
        if (n > 0)
×
1228
                log_debug("Installed %zu kernel(s).", n);
×
1229
        else if (ret == 0)
×
1230
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1231

1232
        return ret;
1233
}
1234

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

1242
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), c);
×
1243
}
1244

1245
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1246
        Context *c = ASSERT_PTR(userdata);
×
1247
        int r;
×
1248

1249
        assert(argc >= 2);
×
1250
        assert(argv);
×
1251

1252
        if (arg_root)
×
1253
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1254

1255
        if (argc > 2)
×
1256
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1257
                          "Ignoring residual arguments.");
1258

1259
        if (bypass())
×
1260
                return 0;
1261

1262
        c->action = ACTION_REMOVE;
×
1263

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

1268
        r = context_set_version(c, argv[1]);
×
1269
        if (r < 0)
×
1270
                return r;
1271

1272
        r = context_prepare_execution(c);
×
1273
        if (r < 0)
×
1274
                return r;
1275

1276
        return context_execute(c);
×
1277
}
1278

1279
static int verb_inspect(int argc, char *argv[], void *userdata) {
×
1280
        Context *c = ASSERT_PTR(userdata);
×
1281
        _cleanup_(table_unrefp) Table *t = NULL;
×
1282
        _cleanup_free_ char *vmlinuz = NULL;
×
1283
        const char *version, *kernel;
×
1284
        char **initrds;
×
1285
        struct utsname un;
×
1286
        int r;
×
1287

1288
        c->action = ACTION_INSPECT;
×
1289

1290
        /* When only a single parameter is specified 'inspect' it's the kernel image path, and not the kernel
1291
         * version. i.e. it's the first argument that is optional, not the 2nd. That's a bit unfortunate, but
1292
         * we keep the behaviour for compatibility. If users want to specify only the version (and have the
1293
         * kernel image path derived automatically), then they may specify an empty string or "dash" as
1294
         * kernel image path. */
1295
        version = argc > 2 ? empty_or_dash_to_null(argv[1]) : NULL;
×
1296
        kernel = argc > 2 ? empty_or_dash_to_null(argv[2]) :
×
1297
                (argc > 1 ? empty_or_dash_to_null(argv[1]) : NULL);
×
1298
        initrds = strv_skip(argv, 3);
×
1299

1300
        if (!version && !arg_root) {
×
1301
                assert_se(uname(&un) >= 0);
×
1302
                version = un.release;
1303
        }
1304

1305
        if (!kernel && version) {
×
1306
                r = kernel_from_version(version, &vmlinuz);
×
1307
                if (r < 0)
×
1308
                        return r;
1309

1310
                kernel = vmlinuz;
×
1311
        }
1312

1313
        r = context_set_version(c, version);
×
1314
        if (r < 0)
×
1315
                return r;
1316

1317
        r = context_set_kernel(c, kernel);
×
1318
        if (r < 0)
×
1319
                return r;
1320

1321
        r = context_set_initrds(c, initrds);
×
1322
        if (r < 0)
×
1323
                return r;
1324

1325
        r = context_prepare_execution(c);
×
1326
        if (r < 0)
×
1327
                return r;
1328

1329
        t = table_new_vertical();
×
1330
        if (!t)
×
1331
                return log_oom();
×
1332

1333
        r = table_add_many(t,
×
1334
                           TABLE_FIELD, "Machine ID",
1335
                           TABLE_ID128, c->machine_id,
1336
                           TABLE_FIELD, "Kernel Image Type",
1337
                           TABLE_STRING, kernel_image_type_to_string(c->kernel_image_type),
1338
                           TABLE_FIELD, "Layout",
1339
                           TABLE_STRING, context_get_layout(c),
1340
                           TABLE_FIELD, "Boot Root",
1341
                           TABLE_STRING, c->boot_root,
1342
                           TABLE_FIELD, "Entry Token Type",
1343
                           TABLE_STRING, boot_entry_token_type_to_string(c->entry_token_type),
1344
                           TABLE_FIELD, "Entry Token",
1345
                           TABLE_STRING, c->entry_token,
1346
                           TABLE_FIELD, "Entry Directory",
1347
                           TABLE_STRING, c->entry_dir,
1348
                           TABLE_FIELD, "Kernel Version",
1349
                           TABLE_STRING, c->version,
1350
                           TABLE_FIELD, "Kernel",
1351
                           TABLE_STRING, c->kernel,
1352
                           TABLE_FIELD, "Initrds",
1353
                           TABLE_STRV, c->initrds,
1354
                           TABLE_FIELD, "Initrd Generator",
1355
                           TABLE_STRING, c->initrd_generator,
1356
                           TABLE_FIELD, "UKI Generator",
1357
                           TABLE_STRING, c->uki_generator,
1358
                           TABLE_FIELD, "Plugins",
1359
                           TABLE_STRV, c->plugins,
1360
                           TABLE_FIELD, "Plugin Environment",
1361
                           TABLE_STRV, c->envp);
1362
        if (r < 0)
×
1363
                return table_log_add_error(r);
×
1364

1365
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1366
                r = table_add_many(t,
×
1367
                                   TABLE_FIELD, "Plugin Arguments",
1368
                                   TABLE_STRV, strv_skip(c->argv, 1));
1369
                if (r < 0)
×
1370
                        return table_log_add_error(r);
×
1371
        }
1372

1373
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1374

1375
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1376
                _cleanup_free_ char *name = NULL;
×
1377

1378
                name = strdup(table_get_at(t, row, 0));
×
1379
                if (!name)
×
1380
                        return log_oom();
×
1381

1382
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1383
                if (r < 0)
×
1384
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1385
        }
1386

1387
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1388
}
1389

1390
static int verb_list(int argc, char *argv[], void *userdata) {
×
1391
        Context *c = ASSERT_PTR(userdata);
×
1392
        _cleanup_close_ int fd = -EBADF;
×
1393
        int r;
×
1394

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

1399
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1400
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1401
        if (r < 0)
×
1402
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1403

1404
        _cleanup_(table_unrefp) Table *table = NULL;
×
1405
        table = table_new("version", "has kernel", "path");
×
1406
        if (!table)
×
1407
                return log_oom();
×
1408

1409
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1410
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1411

1412
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1413
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1414
                if (!j)
×
1415
                        return log_oom();
×
1416

1417
                r = dirent_ensure_type(fd, *d);
×
1418
                if (r < 0) {
×
1419
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1420
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
1421
                        continue;
×
1422
                }
1423

1424
                if ((*d)->d_type != DT_DIR)
×
1425
                        continue;
×
1426

1427
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1428
                if (!fn)
×
1429
                        return log_oom();
×
1430

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

1436
                        exists = false;
1437
                } else
1438
                        exists = true;
1439

1440
                r = table_add_many(table,
×
1441
                                   TABLE_STRING, (*d)->d_name,
1442
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1443
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1444
                                   TABLE_PATH, j);
1445
                if (r < 0)
×
1446
                        return table_log_add_error(r);
×
1447
        }
1448

1449
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1450
}
1451

1452
static int help(void) {
×
1453
        _cleanup_free_ char *link = NULL;
×
1454
        int r;
×
1455

1456
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1457
        if (r < 0)
×
1458
                return log_oom();
×
1459

1460
        printf("%1$s [OPTIONS...] COMMAND ...\n\n"
×
1461
               "%5$sAdd and remove kernel and initrd images to and from the boot partition.%6$s\n"
1462
               "\n%3$sUsage:%4$s\n"
1463
               "  kernel-install [OPTIONS...] add [[[KERNEL-VERSION] KERNEL-IMAGE] [INITRD ...]]\n"
1464
               "  kernel-install [OPTIONS...] add-all\n"
1465
               "  kernel-install [OPTIONS...] remove KERNEL-VERSION\n"
1466
               "  kernel-install [OPTIONS...] inspect [[[KERNEL-VERSION] KERNEL-IMAGE]\n"
1467
               "                                      [INITRD ...]]\n"
1468
               "  kernel-install [OPTIONS...] list\n"
1469
               "\n%3$sOptions:%4$s\n"
1470
               "  -h --help                    Show this help\n"
1471
               "     --version                 Show package version\n"
1472
               "  -v --verbose                 Increase verbosity\n"
1473
               "     --esp-path=PATH           Path to the EFI System Partition (ESP)\n"
1474
               "     --boot-path=PATH          Path to the $BOOT partition\n"
1475
               "     --make-entry-directory=yes|no|auto\n"
1476
               "                               Create $BOOT/ENTRY-TOKEN/ directory\n"
1477
               "     --entry-token=machine-id|os-id|os-image-id|auto|literal:…\n"
1478
               "                               Entry token to use for this installation\n"
1479
               "     --no-pager                Do not pipe inspect output into a pager\n"
1480
               "     --json=pretty|short|off   Generate JSON output\n"
1481
               "     --no-legend               Do not show the headers and footers\n"
1482
               "     --root=PATH               Operate on an alternate filesystem root\n"
1483
               "     --image=PATH              Operate on disk image as filesystem root\n"
1484
               "     --image-policy=POLICY     Specify disk image dissection policy\n"
1485
               "\n"
1486
               "This program may also be invoked as 'installkernel':\n"
1487
               "  installkernel  [OPTIONS...] VERSION VMLINUZ [MAP] [INSTALLATION-DIR]\n"
1488
               "(The optional arguments are passed by kernel build system, but ignored.)\n"
1489
               "\n"
1490
               "See the %2$s for details.\n",
1491
               program_invocation_short_name,
1492
               link,
1493
               ansi_underline(),
1494
               ansi_normal(),
1495
               ansi_highlight(),
1496
               ansi_normal());
1497

1498
        return 0;
1499
}
1500

1501
static int parse_argv(int argc, char *argv[], Context *c) {
×
1502
        enum {
×
1503
                ARG_VERSION = 0x100,
1504
                ARG_NO_LEGEND,
1505
                ARG_ESP_PATH,
1506
                ARG_BOOT_PATH,
1507
                ARG_MAKE_ENTRY_DIRECTORY,
1508
                ARG_ENTRY_TOKEN,
1509
                ARG_NO_PAGER,
1510
                ARG_JSON,
1511
                ARG_ROOT,
1512
                ARG_IMAGE,
1513
                ARG_IMAGE_POLICY,
1514
        };
1515
        static const struct option options[] = {
×
1516
                { "help",                 no_argument,       NULL, 'h'                      },
1517
                { "version",              no_argument,       NULL, ARG_VERSION              },
1518
                { "verbose",              no_argument,       NULL, 'v'                      },
1519
                { "esp-path",             required_argument, NULL, ARG_ESP_PATH             },
1520
                { "boot-path",            required_argument, NULL, ARG_BOOT_PATH            },
1521
                { "make-entry-directory", required_argument, NULL, ARG_MAKE_ENTRY_DIRECTORY },
1522
                { "entry-token",          required_argument, NULL, ARG_ENTRY_TOKEN          },
1523
                { "no-pager",             no_argument,       NULL, ARG_NO_PAGER             },
1524
                { "json",                 required_argument, NULL, ARG_JSON                 },
1525
                { "root",                 required_argument, NULL, ARG_ROOT                 },
1526
                { "image",                required_argument, NULL, ARG_IMAGE                },
1527
                { "image-policy",         required_argument, NULL, ARG_IMAGE_POLICY         },
1528
                { "no-legend",            no_argument,       NULL, ARG_NO_LEGEND            },
1529
                {}
1530
        };
1531
        int t, r;
×
1532

1533
        assert(argc >= 0);
×
1534
        assert(argv);
×
1535
        assert(c);
×
1536

1537
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1538
                switch (t) {
×
1539
                case 'h':
×
1540
                        return help();
×
1541

1542
                case ARG_VERSION:
×
1543
                        return version();
×
1544

1545
                case ARG_NO_LEGEND:
×
1546
                        arg_legend = false;
×
1547
                        break;
×
1548

1549
                case 'v':
×
1550
                        log_set_max_level(LOG_DEBUG);
×
1551
                        arg_verbose = true;
×
1552
                        break;
×
1553

1554
                case ARG_ESP_PATH:
×
1555
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_esp_path);
×
1556
                        if (r < 0)
×
1557
                                return log_oom();
×
1558
                        break;
1559

1560
                case ARG_BOOT_PATH:
×
1561
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_xbootldr_path);
×
1562
                        if (r < 0)
×
1563
                                return log_oom();
×
1564
                        break;
1565

1566
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1567
                        if (streq(optarg, "auto"))
×
1568
                                arg_make_entry_directory = -1;
×
1569
                        else {
1570
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
1571
                                if (r < 0)
×
1572
                                        return r;
1573

1574
                                arg_make_entry_directory = r;
×
1575
                        }
1576
                        break;
1577

1578
                case ARG_ENTRY_TOKEN:
×
1579
                        r = parse_boot_entry_token_type(optarg, &c->entry_token_type, &c->entry_token);
×
1580
                        if (r < 0)
×
1581
                                return r;
1582
                        break;
1583

1584
                case ARG_NO_PAGER:
×
1585
                        arg_pager_flags |= PAGER_DISABLE;
×
1586
                        break;
×
1587

1588
                case ARG_JSON:
×
1589
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1590
                        if (r < 0)
×
1591
                                return r;
1592
                        break;
1593

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

1600
                case ARG_IMAGE:
×
1601
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_image);
×
1602
                        if (r < 0)
×
1603
                                return r;
1604
                        break;
1605

1606
                case ARG_IMAGE_POLICY:
×
1607
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1608
                        if (r < 0)
×
1609
                                return r;
1610
                        break;
1611

1612
                case '?':
1613
                        return -EINVAL;
1614

1615
                default:
×
1616
                        assert_not_reached();
×
1617
                }
1618

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

1622
        return 1;
1623
}
1624

1625
static int run(int argc, char* argv[]) {
×
1626
        static const Verb verbs[] = {
×
1627
                { "add",         1,        VERB_ANY, 0,            verb_add            },
1628
                { "add-all",     1,        1,        0,            verb_add_all        },
1629
                { "remove",      2,        VERB_ANY, 0,            verb_remove         },
1630
                { "inspect",     1,        VERB_ANY, VERB_DEFAULT, verb_inspect        },
1631
                { "list",        1,        1,        0,            verb_list           },
1632
                {}
1633
        };
1634
        _cleanup_(context_done) Context c = {
×
1635
                .rfd = AT_FDCWD,
1636
                .action = _ACTION_INVALID,
1637
                .kernel_image_type = KERNEL_IMAGE_TYPE_UNKNOWN,
1638
                .layout = _LAYOUT_INVALID,
1639
                .entry_token_type = BOOT_ENTRY_TOKEN_AUTO,
1640
        };
1641
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
×
1642
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1643
        int r;
×
1644

1645
        log_setup();
×
1646

1647
        r = parse_argv(argc, argv, &c);
×
1648
        if (r <= 0)
×
1649
                return r;
1650

1651
        if (arg_image) {
×
1652
                assert(!arg_root);
×
1653

1654
                r = mount_image_privately_interactively(
×
1655
                                arg_image,
1656
                                arg_image_policy,
1657
                                DISSECT_IMAGE_GENERIC_ROOT |
1658
                                DISSECT_IMAGE_REQUIRE_ROOT |
1659
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1660
                                DISSECT_IMAGE_VALIDATE_OS |
1661
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1662
                                &mounted_dir,
1663
                                /* ret_dir_fd= */ NULL,
1664
                                &loop_device);
1665
                if (r < 0)
×
1666
                        return r;
1667

1668
                arg_root = strdup(mounted_dir);
×
1669
                if (!arg_root)
×
1670
                        return log_oom();
×
1671
        }
1672

1673
        r = context_init(&c);
×
1674
        if (r < 0)
×
1675
                return r;
1676

1677
        if (invoked_as(argv, "installkernel"))
×
1678
                return run_as_installkernel(argc, argv, &c);
×
1679

1680
        return dispatch_verb(argc, argv, verbs, &c);
×
1681
}
1682

1683
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