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

systemd / systemd / 15057632786

15 May 2025 09:01PM UTC coverage: 72.267% (+0.02%) from 72.244%
15057632786

push

github

bluca
man: document how to hook stuff into system wakeup

Fixes: #6364

298523 of 413084 relevant lines covered (72.27%)

738132.88 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 "build.h"
11
#include "chase.h"
12
#include "conf-files.h"
13
#include "dirent-util.h"
14
#include "dissect-image.h"
15
#include "env-file.h"
16
#include "env-util.h"
17
#include "exec-util.h"
18
#include "extract-word.h"
19
#include "fd-util.h"
20
#include "fileio.h"
21
#include "find-esp.h"
22
#include "format-table.h"
23
#include "fs-util.h"
24
#include "id128-util.h"
25
#include "image-policy.h"
26
#include "kernel-config.h"
27
#include "kernel-image.h"
28
#include "loop-util.h"
29
#include "main-func.h"
30
#include "mount-util.h"
31
#include "parse-argument.h"
32
#include "path-util.h"
33
#include "pretty-print.h"
34
#include "recurse-dir.h"
35
#include "rm-rf.h"
36
#include "stat-util.h"
37
#include "string-table.h"
38
#include "string-util.h"
39
#include "strv.h"
40
#include "tmpfile-util.h"
41
#include "verbs.h"
42

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

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

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

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

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

84
DEFINE_PRIVATE_STRING_TABLE_LOOKUP(layout, Layout);
×
85

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

110
#define CONTEXT_NULL (Context) { .rfd = -EBADF }
111

112
static void context_done(Context *c) {
×
113
        assert(c);
×
114

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

133
        safe_close(c->rfd);
×
134
}
×
135

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

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

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

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

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

202
        *ret = copy;
×
203
        copy = CONTEXT_NULL;
×
204

205
        return 0;
×
206
}
207

208
static int context_open_root(Context *c) {
×
209
        int r;
×
210

211
        assert(c);
×
212
        assert(c->rfd < 0);
×
213

214
        if (isempty(arg_root))
×
215
                return 0;
216

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

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

227
        return 0;
228
}
229

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

234
        return c->layout_other ?: layout_to_string(c->layout);
×
235
}
236

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

240
        assert(c);
×
241
        assert(source);
×
242

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

246
        assert(!c->layout_other);
×
247

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

258
                c->layout = LAYOUT_OTHER;
×
259
        }
260

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

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

268
        assert(c);
×
269
        assert(source);
×
270

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

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

278
        if (sd_id128_is_null(c->machine_id))
×
279
                return 0;
×
280

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

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

288
        assert(source);
×
289
        assert(name);
×
290
        assert(dest);
×
291

292
        if (*dest || !s)
×
293
                return 0;
294

295
        p = strdup(s);
×
296
        if (!p)
×
297
                return log_oom();
×
298

299
        log_debug("%s (%s) set via %s.", name, p, source);
×
300

301
        *dest = p;
×
302
        return 1;
×
303
}
304

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

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

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

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

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

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

328
        assert(c);
×
329
        assert(source);
×
330
        assert(name);
×
331
        assert(dest);
×
332

333
        if (*dest || !s)
×
334
                return 0;
×
335

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

348
        log_debug("%s (%s) set via %s.", name, p, source);
×
349

350
        *dest = p;
×
351
        return 1;
×
352
}
353

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

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

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

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

373
        assert(c);
×
374
        assert(source);
×
375
        assert(name);
×
376
        assert(dest);
×
377

378
        if (*dest)
×
379
                return 0;
380

381
        STRV_FOREACH(s, strv) {
×
382
                char *p;
×
383

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

400
        if (strv_isempty(w))
×
401
                return 0;
402

403
        log_debug("%s set via %s", name, source);
×
404

405
        *dest = TAKE_PTR(w);
×
406
        return 1;
×
407
}
408

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

413
        assert(c);
×
414

415
        if (c->plugins || !s)
×
416
                return 0;
417

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

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

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

430
static int context_load_environment(Context *c) {
×
431
        assert(c);
×
432

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

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

445
        assert(c);
×
446

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

457
        (void) context_set_machine_id(c, machine_id, "config");
×
458
        (void) context_set_boot_root(c, boot_root, "config");
×
459
        (void) context_set_layout(c, layout, "config");
×
460
        (void) context_set_initrd_generator(c, initrd_generator, "config");
×
461
        (void) context_set_uki_generator(c, uki_generator, "config");
×
462

463
        log_debug("Loaded config.");
×
464
        return 0;
465
}
466

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

473
        assert(c);
×
474

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

478
        if (!sd_id128_is_null(c->machine_id) && c->layout >= 0)
×
479
                return 0;
480

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

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

496
        log_debug("Loading %s…", path);
×
497

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

504
        (void) context_set_machine_id(c, machine_id, path);
×
505
        (void) context_set_layout(c, layout, path);
×
506
        return 0;
507
}
508

509
static int context_load_machine_id(Context *c) {
×
510
        int r;
×
511

512
        assert(c);
×
513

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

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

524
static int context_ensure_machine_id(Context *c) {
×
525
        int r;
×
526

527
        assert(c);
×
528

529
        if (!sd_id128_is_null(c->machine_id))
×
530
                return 0;
×
531

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

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

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

547
static int context_acquire_xbootldr(Context *c) {
×
548
        int r;
×
549

550
        assert(c);
×
551
        assert(!c->boot_root);
×
552

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

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

573
static int context_acquire_esp(Context *c) {
×
574
        int r;
×
575

576
        assert(c);
×
577
        assert(!c->boot_root);
×
578

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

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

602
static int context_ensure_boot_root(Context *c) {
×
603
        int r;
×
604

605
        assert(c);
×
606

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

611
        /* Otherwise, use XBOOTLDR partition, if mounted. */
612
        r = context_acquire_xbootldr(c);
×
613
        if (r != 0)
×
614
                return r;
615

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

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

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

636
static int context_ensure_entry_token(Context *c) {
×
637
        int r;
×
638

639
        assert(c);
×
640

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

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

656
        log_debug("Using entry token: %s", c->entry_token);
×
657
        return 0;
658
}
659

660
static int context_load_plugins(Context *c) {
×
661
        int r;
×
662

663
        assert(c);
×
664

665
        if (c->plugins)
×
666
                return 0;
×
667

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

677
        return 0;
678
}
679

680
static int context_init(Context *c) {
×
681
        int r;
×
682

683
        assert(c);
×
684

685
        r = context_open_root(c);
×
686
        if (r < 0)
×
687
                return r;
688

689
        r = context_load_environment(c);
×
690
        if (r < 0)
×
691
                return r;
692

693
        r = context_load_install_conf(c);
×
694
        if (r < 0)
×
695
                return r;
696

697
        r = context_load_machine_info(c);
×
698
        if (r < 0)
×
699
                return r;
700

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

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

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

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

717
        return 0;
718
}
719

720
static int context_inspect_kernel(Context *c) {
×
721
        assert(c);
×
722

723
        if (!c->kernel)
×
724
                return 0;
725

726
        return inspect_kernel(c->rfd, c->kernel, &c->kernel_image_type, NULL, NULL, NULL);
×
727
}
728

729
static int context_ensure_layout(Context *c) {
×
730
        int r;
×
731

732
        assert(c);
×
733
        assert(c->boot_root);
×
734
        assert(c->entry_token);
×
735

736
        if (c->layout >= 0 && c->layout != LAYOUT_AUTO)
×
737
                return 0;
×
738

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

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

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

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

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

772
        _cleanup_free_ char *entry_token_path = path_join(c->boot_root, c->entry_token);
×
773
        if (!entry_token_path)
×
774
                return log_oom();
×
775

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

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

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

798
        assert(c);
×
799

800
        if (c->staging_area)
×
801
                return 0;
802

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

814
        return 0;
815
}
816

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

823
        if (c->entry_dir)
×
824
                return 0;
825

826
        c->entry_dir = path_join(c->boot_root, c->entry_token, c->version ?: "KERNEL_VERSION");
×
827
        if (!c->entry_dir)
×
828
                return log_oom();
×
829

830
        log_debug("Using ENTRY_DIR=%s", c->entry_dir);
×
831
        return 0;
832
}
833

834
static bool context_should_make_entry_dir(Context *c) {
×
835
        assert(c);
×
836

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

840
        if (arg_make_entry_directory < 0)
×
841
                return c->layout == LAYOUT_BLS;
×
842

843
        return arg_make_entry_directory;
×
844
}
845

846
static int context_make_entry_dir(Context *c) {
×
847
        _cleanup_close_ int fd = -EBADF;
×
848

849
        assert(c);
×
850
        assert(c->entry_dir);
×
851

852
        if (c->action != ACTION_ADD)
×
853
                return 0;
854

855
        if (!context_should_make_entry_dir(c))
×
856
                return 0;
857

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

864
        return 0;
865
}
866

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

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

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

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

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

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

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

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

900
        return 0;
901
}
902

903
static int context_build_arguments(Context *c) {
×
904
        _cleanup_strv_free_ char **a = NULL;
×
905
        const char *verb;
×
906
        int r;
×
907

908
        assert(c);
×
909
        assert(c->entry_dir);
×
910

911
        if (c->argv)
×
912
                return 0;
913

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

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

928
        case ACTION_INSPECT:
929
                verb = "add|remove";
930
                break;
931

932
        default:
×
933
                assert_not_reached();
×
934
        }
935

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

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

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

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

961
        c->argv = TAKE_PTR(a);
×
962
        return 0;
×
963
}
964

965
static int context_build_environment(Context *c) {
×
966
        _cleanup_strv_free_ char **e = NULL;
×
967
        int r;
×
968

969
        assert(c);
×
970

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

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

988
        c->envp = TAKE_PTR(e);
×
989
        return 0;
×
990
}
991

992
static int context_prepare_execution(Context *c) {
×
993
        int r;
×
994

995
        assert(c);
×
996

997
        r = context_inspect_kernel(c);
×
998
        if (r < 0)
×
999
                return r;
1000

1001
        r = context_ensure_layout(c);
×
1002
        if (r < 0)
×
1003
                return r;
1004

1005
        r = context_set_up_staging_area(c);
×
1006
        if (r < 0)
×
1007
                return r;
1008

1009
        r = context_build_entry_dir(c);
×
1010
        if (r < 0)
×
1011
                return r;
1012

1013
        r = context_build_arguments(c);
×
1014
        if (r < 0)
×
1015
                return r;
1016

1017
        r = context_build_environment(c);
×
1018
        if (r < 0)
×
1019
                return r;
×
1020

1021
        return 0;
1022
}
1023

1024
static int context_execute(Context *c) {
×
1025
        int r, ret;
×
1026

1027
        assert(c);
×
1028

1029
        r = context_make_entry_dir(c);
×
1030
        if (r < 0)
×
1031
                return r;
1032

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

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

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

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

1055
        r = context_remove_entry_dir(c);
×
1056
        if (r < 0)
×
1057
                return r;
×
1058

1059
        /* This returns 0 on success, positive exit code on plugin failure, negative errno on other failures. */
1060
        return ret;
1061
}
1062

1063
static bool bypass(void) {
×
1064
        return should_bypass("KERNEL_INSTALL");
×
1065
}
1066

1067
static int do_add(
×
1068
                Context *c,
1069
                const char *version,
1070
                const char *kernel,
1071
                char **initrds) {
1072

1073
        int r;
×
1074

1075
        assert(c);
×
1076
        assert(version);
×
1077
        assert(kernel);
×
1078

1079
        r = context_set_version(c, version);
×
1080
        if (r < 0)
×
1081
                return r;
1082

1083
        r = context_set_kernel(c, kernel);
×
1084
        if (r < 0)
×
1085
                return r;
1086

1087
        r = context_set_initrds(c, initrds);
×
1088
        if (r < 0)
×
1089
                return r;
1090

1091
        r = context_prepare_execution(c);
×
1092
        if (r < 0)
×
1093
                return r;
1094

1095
        return context_execute(c);
×
1096
}
1097

1098
static int kernel_from_version(const char *version, char **ret_kernel) {
×
1099
        _cleanup_free_ char *vmlinuz = NULL;
×
1100
        int r;
×
1101

1102
        assert(version);
×
1103

1104
        vmlinuz = path_join("/usr/lib/modules/", version, "/vmlinuz");
×
1105
        if (!vmlinuz)
×
1106
                return log_oom();
×
1107

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

1114
        *ret_kernel = TAKE_PTR(vmlinuz);
×
1115
        return 0;
×
1116
}
1117

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

1126
        assert(argv);
×
1127

1128
        if (arg_root)
×
1129
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'add' does not support --root= or --image=.");
×
1130

1131
        if (bypass())
×
1132
                return 0;
1133

1134
        c->action = ACTION_ADD;
×
1135

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

1144
        if (!version) {
×
1145
                assert_se(uname(&un) >= 0);
×
1146
                version = un.release;
1147
        }
1148

1149
        if (!kernel) {
×
1150
                r = kernel_from_version(version, &vmlinuz);
×
1151
                if (r < 0)
×
1152
                        return r;
1153

1154
                kernel = vmlinuz;
×
1155
        }
1156

1157
        return do_add(c, version, kernel, initrds);
×
1158
}
1159

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

1166
        assert(argv);
×
1167

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

1171
        if (bypass())
×
1172
                return 0;
1173

1174
        c->action = ACTION_ADD;
×
1175

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

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

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

1193
                if ((*d)->d_type != DT_DIR)
×
1194
                        continue;
×
1195

1196
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1197
                if (!fn)
×
1198
                        return log_oom();
×
1199

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

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

1208
                _cleanup_(context_done) Context copy = CONTEXT_NULL;
×
1209

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

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

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

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

1235
        return ret;
1236
}
1237

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

1245
        return verb_add(3, STRV_MAKE("add", argv[optind], argv[optind+1]), c);
×
1246
}
1247

1248
static int verb_remove(int argc, char *argv[], void *userdata) {
×
1249
        Context *c = ASSERT_PTR(userdata);
×
1250
        int r;
×
1251

1252
        assert(argc >= 2);
×
1253
        assert(argv);
×
1254

1255
        if (arg_root)
×
1256
                return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "'remove' does not support --root= or --image=.");
×
1257

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

1262
        if (bypass())
×
1263
                return 0;
1264

1265
        c->action = ACTION_REMOVE;
×
1266

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

1271
        r = context_set_version(c, argv[1]);
×
1272
        if (r < 0)
×
1273
                return r;
1274

1275
        r = context_prepare_execution(c);
×
1276
        if (r < 0)
×
1277
                return r;
1278

1279
        return context_execute(c);
×
1280
}
1281

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

1291
        c->action = ACTION_INSPECT;
×
1292

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

1303
        if (!version && !arg_root) {
×
1304
                assert_se(uname(&un) >= 0);
×
1305
                version = un.release;
1306
        }
1307

1308
        if (!kernel && version) {
×
1309
                r = kernel_from_version(version, &vmlinuz);
×
1310
                if (r < 0)
×
1311
                        return r;
1312

1313
                kernel = vmlinuz;
×
1314
        }
1315

1316
        r = context_set_version(c, version);
×
1317
        if (r < 0)
×
1318
                return r;
1319

1320
        r = context_set_kernel(c, kernel);
×
1321
        if (r < 0)
×
1322
                return r;
1323

1324
        r = context_set_initrds(c, initrds);
×
1325
        if (r < 0)
×
1326
                return r;
1327

1328
        r = context_prepare_execution(c);
×
1329
        if (r < 0)
×
1330
                return r;
1331

1332
        t = table_new_vertical();
×
1333
        if (!t)
×
1334
                return log_oom();
×
1335

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

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

1376
        table_set_ersatz_string(t, TABLE_ERSATZ_UNSET);
×
1377

1378
        for (size_t row = 1; row < table_get_rows(t); row++) {
×
1379
                _cleanup_free_ char *name = NULL;
×
1380

1381
                name = strdup(table_get_at(t, row, 0));
×
1382
                if (!name)
×
1383
                        return log_oom();
×
1384

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

1390
        return table_print_with_pager(t, arg_json_format_flags, arg_pager_flags, /* show_header= */ false);
×
1391
}
1392

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

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

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

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

1412
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
×
1413
        table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
×
1414

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

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

1427
                if ((*d)->d_type != DT_DIR)
×
1428
                        continue;
×
1429

1430
                _cleanup_free_ char *fn = path_join((*d)->d_name, "vmlinuz");
×
1431
                if (!fn)
×
1432
                        return log_oom();
×
1433

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

1439
                        exists = false;
1440
                } else
1441
                        exists = true;
1442

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

1452
        return table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
×
1453
}
1454

1455
static int help(void) {
×
1456
        _cleanup_free_ char *link = NULL;
×
1457
        int r;
×
1458

1459
        r = terminal_urlify_man("kernel-install", "8", &link);
×
1460
        if (r < 0)
×
1461
                return log_oom();
×
1462

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

1501
        return 0;
1502
}
1503

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

1536
        assert(argc >= 0);
×
1537
        assert(argv);
×
1538
        assert(c);
×
1539

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

1545
                case ARG_VERSION:
×
1546
                        return version();
×
1547

1548
                case ARG_NO_LEGEND:
×
1549
                        arg_legend = false;
×
1550
                        break;
×
1551

1552
                case 'v':
×
1553
                        log_set_max_level(LOG_DEBUG);
×
1554
                        arg_verbose = true;
×
1555
                        break;
×
1556

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

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

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

1577
                                arg_make_entry_directory = r;
×
1578
                        }
1579
                        break;
1580

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

1587
                case ARG_NO_PAGER:
×
1588
                        arg_pager_flags |= PAGER_DISABLE;
×
1589
                        break;
×
1590

1591
                case ARG_JSON:
×
1592
                        r = parse_json_argument(optarg, &arg_json_format_flags);
×
1593
                        if (r < 0)
×
1594
                                return r;
1595
                        break;
1596

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

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

1609
                case ARG_IMAGE_POLICY:
×
1610
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1611
                        if (r < 0)
×
1612
                                return r;
1613
                        break;
1614

1615
                case '?':
1616
                        return -EINVAL;
1617

1618
                default:
×
1619
                        assert_not_reached();
×
1620
                }
1621

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

1625
        return 1;
1626
}
1627

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

1648
        log_setup();
×
1649

1650
        r = parse_argv(argc, argv, &c);
×
1651
        if (r <= 0)
×
1652
                return r;
1653

1654
        if (arg_image) {
×
1655
                assert(!arg_root);
×
1656

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

1671
                arg_root = strdup(mounted_dir);
×
1672
                if (!arg_root)
×
1673
                        return log_oom();
×
1674
        }
1675

1676
        r = context_init(&c);
×
1677
        if (r < 0)
×
1678
                return r;
1679

1680
        if (invoked_as(argv, "installkernel"))
×
1681
                return run_as_installkernel(argc, argv, &c);
×
1682

1683
        return dispatch_verb(argc, argv, verbs, &c);
×
1684
}
1685

1686
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