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

systemd / systemd / 17844524571

18 Sep 2025 08:58PM UTC coverage: 72.326% (+0.09%) from 72.24%
17844524571

push

github

DaanDeMeyer
userdb: suppress creation of empty userdb dirs

13 of 16 new or added lines in 1 file covered. (81.25%)

3248 existing lines in 52 files now uncovered.

303003 of 418940 relevant lines covered (72.33%)

1051258.58 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(empty_to_root(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", empty_to_root(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
        static const char *template = "/tmp/kernel-install.staging.XXXXXX";
×
814
        int r;
×
815

816
        assert(c);
×
817

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

821
        if (c->action == ACTION_INSPECT) {
×
822
                /* This is only used for display. The directory will not be created. */
823
                c->staging_area = strdup(template);
×
824
                if (!c->staging_area)
×
825
                        return log_oom();
×
826
        } else {
827
                r = mkdtemp_malloc(template, &c->staging_area);
×
828
                if (r < 0)
×
829
                        return log_error_errno(r, "Failed to create staging area: %m");
×
830
        }
831

832
        return 0;
833
}
834

835
static int context_build_entry_dir(Context *c) {
×
836
        assert(c);
×
837
        assert(c->boot_root);
×
838
        assert(c->entry_token);
×
839
        assert(c->version || c->action == ACTION_INSPECT);
×
840

841
        if (c->entry_dir)
×
842
                return 0;
843

844
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
845
        if (!c->entry_dir)
×
846
                return log_oom();
×
847

848
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
849
        return 0;
850
}
851

852
static bool context_should_make_entry_dir(Context *c) {
×
853
        assert(c);
×
854

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

858
        if (arg_make_entry_directory < 0)
×
859
                return c->layout == LAYOUT_BLS;
×
860

861
        return arg_make_entry_directory;
×
862
}
863

864
static int context_make_entry_dir(Context *c) {
×
865
        _cleanup_close_ int fd = -EBADF;
×
866

867
        assert(c);
×
868
        assert(c->entry_dir);
×
869

870
        if (c->action != ACTION_ADD)
×
871
                return 0;
872

873
        if (!context_should_make_entry_dir(c))
×
874
                return 0;
875

876
        log_debug("mkdir -p %s", c->entry_dir);
×
877
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT | CHASE_MKDIR_0755,
×
878
                              O_CLOEXEC | O_CREAT | O_DIRECTORY | O_PATH, NULL);
879
        if (fd < 0)
×
880
                return log_error_errno(fd, "Failed to make directory '%s': %m", c->entry_dir);
×
881

882
        return 0;
883
}
884

885
static int context_remove_entry_dir(Context *c) {
×
886
        _cleanup_free_ char *p = NULL;
×
887
        _cleanup_close_ int fd = -EBADF;
×
888
        struct stat st;
×
889
        int r;
×
890

891
        assert(c);
×
892
        assert(c->entry_dir);
×
893

894
        if (c->action != ACTION_REMOVE)
×
895
                return 0;
896

897
        if (!context_should_make_entry_dir(c))
×
898
                return 0;
899

900
        log_debug("rm -rf %s", c->entry_dir);
×
901
        fd = chase_and_openat(c->rfd, c->entry_dir, CHASE_AT_RESOLVE_IN_ROOT, O_CLOEXEC | O_DIRECTORY, &p);
×
902
        if (fd < 0) {
×
903
                if (IN_SET(fd, -ENOTDIR, -ENOENT))
×
904
                        return 0;
905
                return log_debug_errno(fd, "Failed to chase and open %s, ignoring: %m", c->entry_dir);
×
906
        }
907

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

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

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

918
        return 0;
919
}
920

921
static int context_build_arguments(Context *c) {
×
922
        _cleanup_strv_free_ char **a = NULL;
×
923
        const char *verb;
×
924
        int r;
×
925

926
        assert(c);
×
927
        assert(c->entry_dir);
×
928

929
        if (c->argv)
×
930
                return 0;
931

932
        switch (c->action) {
×
933
        case ACTION_ADD:
×
934
                assert(c->version);
×
935
                assert(c->kernel);
×
936
                verb = "add";
937
                break;
938

939
        case ACTION_REMOVE:
×
940
                assert(c->version);
×
941
                assert(!c->kernel);
×
942
                assert(!c->initrds);
×
943
                verb = "remove";
944
                break;
945

946
        case ACTION_INSPECT:
947
                verb = "add|remove";
948
                break;
949

950
        default:
×
951
                assert_not_reached();
×
952
        }
953

954
        a = strv_new("dummy-arg", /* to make strv_free() works for this variable. */
×
955
                     verb,
956
                     c->version ?: "KERNEL_VERSION",
957
                     c->entry_dir);
958
        if (!a)
×
959
                return log_oom();
×
960

961
        if (c->action == ACTION_ADD) {
×
962
                r = strv_extend(&a, c->kernel);
×
963
                if (r < 0)
×
964
                        return log_oom();
×
965

966
                r = strv_extend_strv(&a, c->initrds, /* filter_duplicates = */ false);
×
967
                if (r < 0)
×
968
                        return log_oom();
×
969

970
        } else if (c->action == ACTION_INSPECT) {
×
971
                r = strv_extend_many(
×
972
                                &a,
973
                                c->kernel ?: "[KERNEL_IMAGE]",
974
                                "[INITRD...]");
975
                if (r < 0)
×
976
                        return log_oom();
×
977
        }
978

979
        c->argv = TAKE_PTR(a);
×
980
        return 0;
×
981
}
982

983
static int context_build_environment(Context *c) {
×
984
        _cleanup_strv_free_ char **e = NULL;
×
985
        int r;
×
986

987
        assert(c);
×
988

989
        if (c->envp)
×
990
                return 0;
991

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

1007
        c->envp = TAKE_PTR(e);
×
1008
        return 0;
×
1009
}
1010

1011
static int context_prepare_execution(Context *c) {
×
1012
        int r;
×
1013

1014
        assert(c);
×
1015

1016
        r = context_inspect_kernel(c);
×
1017
        if (r < 0)
×
1018
                return r;
1019

1020
        r = context_ensure_layout(c);
×
1021
        if (r < 0)
×
1022
                return r;
1023

1024
        r = context_set_up_staging_area(c);
×
1025
        if (r < 0)
×
1026
                return r;
1027

1028
        r = context_build_entry_dir(c);
×
1029
        if (r < 0)
×
1030
                return r;
1031

1032
        r = context_build_arguments(c);
×
1033
        if (r < 0)
×
1034
                return r;
1035

1036
        r = context_build_environment(c);
×
1037
        if (r < 0)
×
1038
                return r;
×
1039

1040
        return 0;
1041
}
1042

1043
static int context_execute(Context *c) {
×
1044
        int r, ret;
×
1045

1046
        assert(c);
×
1047

1048
        r = context_make_entry_dir(c);
×
1049
        if (r < 0)
×
1050
                return r;
1051

1052
        if (DEBUG_LOGGING) {
×
1053
                _cleanup_free_ char *x = strv_join_full(c->plugins, "", "\n  ", /* escape_separator = */ false);
×
1054
                log_debug("Using plugins: %s", strna(x));
×
1055

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

1059
                _cleanup_free_ char *z = strv_join(strv_skip(c->argv, 1), " ");
×
1060
                log_debug("Plugin arguments: %s", strna(z));
×
1061
        }
1062

1063
        ret = execute_strv(
×
1064
                        "plugins",
1065
                        c->plugins,
×
1066
                        /* root = */ NULL,
1067
                        USEC_INFINITY,
1068
                        /* callbacks = */ NULL,
1069
                        /* callback_args = */ NULL,
1070
                        c->argv,
1071
                        c->envp,
1072
                        EXEC_DIR_SKIP_REMAINING);
1073

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

1078
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1079
        return ret;
1080
}
1081

1082
static bool bypass(void) {
×
1083
        return should_bypass("KERNEL_INSTALL");
×
1084
}
1085

1086
static int do_add(
×
1087
                Context *c,
1088
                const char *version,
1089
                const char *kernel,
1090
                char **initrds) {
1091

1092
        int r;
×
1093

1094
        assert(c);
×
1095
        assert(version);
×
1096
        assert(kernel);
×
1097

1098
        r = context_set_version(c, version);
×
1099
        if (r < 0)
×
1100
                return r;
1101

1102
        r = context_set_kernel(c, kernel);
×
1103
        if (r < 0)
×
1104
                return r;
1105

1106
        r = context_set_initrds(c, initrds);
×
1107
        if (r < 0)
×
1108
                return r;
1109

1110
        r = context_prepare_execution(c);
×
1111
        if (r < 0)
×
1112
                return r;
1113

1114
        return context_execute(c);
×
1115
}
1116

1117
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1118
        _cleanup_free_ char *vmlinuz = NULL;
×
1119
        int r;
×
1120

1121
        assert(version);
×
1122

1123
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1124
        if (!vmlinuz)
×
1125
                return log_oom();
×
1126

1127
        r = access_nofollow(vmlinuz, F_OK);
×
1128
        if (r == -ENOENT)
×
1129
                return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
×
1130
        if (r < 0)
×
1131
                return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
×
1132

1133
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1134
        return 0;
×
1135
}
1136

1137
static int verb_add(int argc, char *argv[], void *userdata) {
×
1138
        Context *c = ASSERT_PTR(userdata);
×
1139
        _cleanup_free_ char *vmlinuz = NULL;
×
1140
        const char *version, *kernel;
×
1141
        char **initrds;
×
1142
        struct utsname un;
×
1143
        int r;
×
1144

1145
        assert(argv);
×
1146

1147
        if (arg_root)
×
1148
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1149

1150
        if (bypass())
×
1151
                return 0;
1152

1153
        c->action = ACTION_ADD;
×
1154

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

1163
        if (!version) {
×
1164
                assert_se(uname(&un) >= 0);
×
1165
                version = un.release;
1166
        }
1167

1168
        if (!kernel) {
×
1169
                r = kernel_from_version(version, &vmlinuz);
×
1170
                if (r < 0)
×
1171
                        return r;
1172

1173
                kernel = vmlinuz;
×
1174
        }
1175

1176
        return do_add(c, version, kernel, initrds);
×
1177
}
1178

1179
static int verb_add_all(int argc, char *argv[], void *userdata) {
×
1180
        Context *c = ASSERT_PTR(userdata);
×
1181
        _cleanup_close_ int fd = -EBADF;
×
1182
        size_t n = 0;
×
1183
        int ret = 0, r;
×
1184

1185
        assert(argv);
×
1186

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

1190
        if (bypass())
×
1191
                return 0;
1192

1193
        c->action = ACTION_ADD;
×
1194

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

1199
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1200
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1201
        if (r < 0)
×
1202
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1203

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

1212
                if ((*d)->d_type != DT_DIR)
×
1213
                        continue;
×
1214

1215
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1216
                if (!fn)
×
1217
                        return log_oom();
×
1218

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

1223
                        log_notice("Not adding version '%s', because kernel image not found.", (*d)->d_name);
×
1224
                        continue;
×
1225
                }
1226

1227
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1228

1229
                r = context_copy(c, &copy);
×
1230
                if (r < 0)
×
1231
                        return log_error_errno(r, "Failed to copy execution context: %m");
×
1232

1233
                /* do_add() will look up the path in the correct root directory so we don't need to prefix it
1234
                 * with arg_root here. */
1235
                _cleanup_free_ char *full = path_join("/usr/lib/modules/", fn);
×
1236
                if (!full)
×
1237
                        return log_oom();
×
1238

1239
                r = do_add(&copy,
×
1240
                           /* version= */ (*d)->d_name,
×
1241
                           /* kernel= */ full,
1242
                           /* initrds= */ NULL);
1243
                if (r == 0)
×
1244
                        n++;
×
1245
                else if (ret == 0)
×
1246
                        ret = r;
×
1247
        }
1248

1249
        if (n > 0)
×
1250
                log_debug("Installed %zu kernel(s).", n);
×
1251
        else if (ret == 0)
×
1252
                ret = log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No kernels to install found.");
×
1253

1254
        return ret;
1255
}
1256

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

1264
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), c);
×
1265
}
1266

1267
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1268
        Context *c = ASSERT_PTR(userdata);
×
1269
        int r;
×
1270

1271
        assert(argc >= 2);
×
1272
        assert(argv);
×
1273

1274
        if (arg_root)
×
1275
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1276

1277
        if (argc > 2)
×
1278
                log_debug("Too many arguments specified. 'kernel-install remove' takes only kernel version. "
×
1279
                          "Ignoring residual arguments.");
1280

1281
        if (bypass())
×
1282
                return 0;
1283

1284
        c->action = ACTION_REMOVE;
×
1285

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

1290
        r = context_set_version(c, argv[1]);
×
1291
        if (r < 0)
×
1292
                return r;
1293

1294
        r = context_prepare_execution(c);
×
1295
        if (r < 0)
×
1296
                return r;
1297

1298
        return context_execute(c);
×
1299
}
1300

1301
static int verb_inspect(int argc, char *argv[], void *userdata) {
×
1302
        Context *c = ASSERT_PTR(userdata);
×
1303
        _cleanup_(table_unrefp) Table *t = NULL;
×
1304
        _cleanup_free_ char *vmlinuz = NULL;
×
1305
        const char *version, *kernel;
×
1306
        char **initrds;
×
1307
        struct utsname un;
×
1308
        int r;
×
1309

1310
        c->action = ACTION_INSPECT;
×
1311

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

1322
        if (!version && !arg_root) {
×
1323
                assert_se(uname(&un) >= 0);
×
1324
                version = un.release;
1325
        }
1326

1327
        if (!kernel && version) {
×
1328
                r = kernel_from_version(version, &vmlinuz);
×
1329
                if (r < 0)
×
1330
                        return r;
1331

1332
                kernel = vmlinuz;
×
1333
        }
1334

1335
        r = context_set_version(c, version);
×
1336
        if (r < 0)
×
1337
                return r;
1338

1339
        r = context_set_kernel(c, kernel);
×
1340
        if (r < 0)
×
1341
                return r;
1342

1343
        r = context_set_initrds(c, initrds);
×
1344
        if (r < 0)
×
1345
                return r;
1346

1347
        r = context_prepare_execution(c);
×
1348
        if (r < 0)
×
1349
                return r;
1350

1351
        t = table_new_vertical();
×
1352
        if (!t)
×
1353
                return log_oom();
×
1354

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

1387
        if (!sd_json_format_enabled(arg_json_format_flags)) {
×
1388
                r = table_add_many(t,
×
1389
                                   TABLE_FIELD, "Plugin Arguments",
1390
                                   TABLE_STRV, strv_skip(c->argv, 1));
1391
                if (r < 0)
×
1392
                        return table_log_add_error(r);
×
1393
        }
1394

1395
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1396

1397
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1398
                _cleanup_free_ char *name = NULL;
×
1399

1400
                name = strdup(table_get_at(t, row, 0));
×
1401
                if (!name)
×
1402
                        return log_oom();
×
1403

1404
                r = table_set_json_field_name(t, row - 1, delete_chars(name, " "));
×
1405
                if (r < 0)
×
1406
                        return log_error_errno(r, "Failed to set JSON field name: %m");
×
1407
        }
1408

1409
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1410
}
1411

1412
static int verb_list(int argc, char *argv[], void *userdata) {
×
1413
        Context *c = ASSERT_PTR(userdata);
×
1414
        _cleanup_close_ int fd = -EBADF;
×
1415
        int r;
×
1416

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

1421
        _cleanup_free_ DirectoryEntries *de = NULL;
×
1422
        r = readdir_all(fd, RECURSE_DIR_SORT|RECURSE_DIR_IGNORE_DOT, &de);
×
1423
        if (r < 0)
×
1424
                return log_error_errno(r, "Failed to numerate /usr/lib/modules/ contents: %m");
×
1425

1426
        _cleanup_(table_unrefp) Table *table = NULL;
×
1427
        table = table_new("version", "has kernel", "path");
×
1428
        if (!table)
×
1429
                return log_oom();
×
1430

1431
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1432
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
UNCOV
1433
        (void) table_set_sort(table, (size_t) 0);
×
1434

1435
        FOREACH_ARRAY(d, de->entries, de->n_entries) {
×
1436
                _cleanup_free_ char *j = path_join("/usr/lib/modules/", (*d)->d_name);
×
1437
                if (!j)
×
UNCOV
1438
                        return log_oom();
×
1439

1440
                r = dirent_ensure_type(fd, *d);
×
1441
                if (r < 0) {
×
1442
                        if (r != -ENOENT) /* don't log if just gone by now */
×
1443
                                log_debug_errno(r, "Failed to check if '%s/%s' is a directory, ignoring: %m", strempty(arg_root), j);
×
UNCOV
1444
                        continue;
×
1445
                }
1446

1447
                if ((*d)->d_type != DT_DIR)
×
UNCOV
1448
                        continue;
×
1449

1450
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1451
                if (!fn)
×
UNCOV
1452
                        return log_oom();
×
1453

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

1459
                        exists = false;
1460
                } else
1461
                        exists = true;
1462

UNCOV
1463
                r = table_add_many(table,
×
1464
                                   TABLE_VERSION, (*d)->d_name,
1465
                                   TABLE_BOOLEAN_CHECKMARK, exists,
1466
                                   TABLE_SET_COLOR, ansi_highlight_green_red(exists),
1467
                                   TABLE_PATH, j);
1468
                if (r < 0)
×
UNCOV
1469
                        return table_log_add_error(r);
×
1470
        }
1471

UNCOV
1472
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1473
}
1474

1475
static int help(void) {
×
1476
        _cleanup_free_ char *link = NULL;
×
UNCOV
1477
        int r;
×
1478

1479
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1480
        if (r < 0)
×
UNCOV
1481
                return log_oom();
×
1482

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

1524
        return 0;
1525
}
1526

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

1561
        assert(argc >= 0);
×
1562
        assert(argv);
×
UNCOV
1563
        assert(c);
×
1564

1565
        while ((t = getopt_long(argc, argv, "hv", options, NULL)) >= 0)
×
1566
                switch (t) {
×
1567
                case 'h':
×
UNCOV
1568
                        return help();
×
1569

1570
                case ARG_VERSION:
×
UNCOV
1571
                        return version();
×
1572

1573
                case ARG_NO_LEGEND:
×
1574
                        arg_legend = false;
×
UNCOV
1575
                        break;
×
1576

1577
                case 'v':
×
1578
                        log_set_max_level(LOG_DEBUG);
×
1579
                        arg_verbose = true;
×
UNCOV
1580
                        break;
×
1581

1582
                case ARG_ESP_PATH:
×
1583
                        r = parse_path_argument(optarg, /* suppress_root = */ false, &arg_esp_path);
×
1584
                        if (r < 0)
×
UNCOV
1585
                                return log_oom();
×
1586
                        break;
1587

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

1594
                case ARG_MAKE_ENTRY_DIRECTORY:
×
1595
                        if (streq(optarg, "auto"))
×
UNCOV
1596
                                arg_make_entry_directory = -1;
×
1597
                        else {
1598
                                r = parse_boolean_argument("--make-entry-directory=", optarg, NULL);
×
UNCOV
1599
                                if (r < 0)
×
1600
                                        return r;
1601

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

1606
                case ARG_ENTRY_TOKEN:
×
1607
                        r = parse_boot_entry_token_type(optarg, &c->entry_token_type, &c->entry_token);
×
UNCOV
1608
                        if (r < 0)
×
1609
                                return r;
1610
                        break;
1611

1612
                case ARG_NO_PAGER:
×
1613
                        arg_pager_flags |= PAGER_DISABLE;
×
UNCOV
1614
                        break;
×
1615

1616
                case ARG_JSON:
×
1617
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
UNCOV
1618
                        if (r < 0)
×
1619
                                return r;
1620
                        break;
1621

1622
                case ARG_ROOT:
×
1623
                        r = parse_path_argument(optarg, /* suppress_root= */ false, &arg_root);
×
UNCOV
1624
                        if (r < 0)
×
1625
                                return r;
1626
                        break;
1627

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

1634
                case ARG_IMAGE_POLICY:
×
1635
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
UNCOV
1636
                        if (r < 0)
×
1637
                                return r;
1638
                        break;
1639

1640
                case ARG_BOOT_ENTRY_TYPE:
×
1641
                        r = context_set_entry_type(c, optarg);
×
UNCOV
1642
                        if (r < 0)
×
1643
                                return r;
1644
                        break;
1645

1646
                case '?':
1647
                        return -EINVAL;
1648

1649
                default:
×
UNCOV
1650
                        assert_not_reached();
×
1651
                }
1652

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

1656
        return 1;
1657
}
1658

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

UNCOV
1680
        log_setup();
×
1681

1682
        r = parse_argv(argc, argv, &c);
×
UNCOV
1683
        if (r <= 0)
×
1684
                return r;
1685

1686
        if (arg_image) {
×
UNCOV
1687
                assert(!arg_root);
×
1688

UNCOV
1689
                r = mount_image_privately_interactively(
×
1690
                                arg_image,
1691
                                arg_image_policy,
1692
                                DISSECT_IMAGE_GENERIC_ROOT |
1693
                                DISSECT_IMAGE_REQUIRE_ROOT |
1694
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1695
                                DISSECT_IMAGE_VALIDATE_OS |
1696
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1697
                                &mounted_dir,
1698
                                /* ret_dir_fd= */ NULL,
1699
                                &loop_device);
UNCOV
1700
                if (r < 0)
×
1701
                        return r;
1702

1703
                arg_root = strdup(mounted_dir);
×
1704
                if (!arg_root)
×
UNCOV
1705
                        return log_oom();
×
1706
        }
1707

1708
        r = context_init(&c);
×
UNCOV
1709
        if (r < 0)
×
1710
                return r;
1711

1712
        if (invoked_as(argv, "installkernel"))
×
UNCOV
1713
                return run_as_installkernel(argc, argv, &c);
×
1714

UNCOV
1715
        return dispatch_verb(argc, argv, verbs, &c);
×
1716
}
1717

UNCOV
1718
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