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

systemd / systemd / 19619415965

23 Nov 2025 01:36PM UTC coverage: 72.75% (+0.3%) from 72.453%
19619415965

push

github

yuwata
firstboot: drop redundant and spurious errno check

Follow-up for 2319154a6.

309458 of 425369 relevant lines covered (72.75%)

1117248.09 hits per line

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

77.64
/src/firstboot/firstboot.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <getopt.h>
5
#include <unistd.h>
6

7
#include "sd-bus.h"
8
#include "sd-id128.h"
9
#include "sd-varlink.h"
10

11
#include "alloc-util.h"
12
#include "ask-password-api.h"
13
#include "build.h"
14
#include "bus-error.h"
15
#include "bus-locator.h"
16
#include "bus-unit-util.h"
17
#include "bus-util.h"
18
#include "bus-wait-for-jobs.h"
19
#include "chase.h"
20
#include "copy.h"
21
#include "creds-util.h"
22
#include "dissect-image.h"
23
#include "env-file.h"
24
#include "errno-util.h"
25
#include "fd-util.h"
26
#include "fileio.h"
27
#include "fs-util.h"
28
#include "glyph-util.h"
29
#include "hostname-util.h"
30
#include "image-policy.h"
31
#include "kbd-util.h"
32
#include "label-util.h"
33
#include "libcrypt-util.h"
34
#include "locale-setup.h"
35
#include "locale-util.h"
36
#include "lock-util.h"
37
#include "loop-util.h"
38
#include "main-func.h"
39
#include "memory-util.h"
40
#include "mount-util.h"
41
#include "os-util.h"
42
#include "parse-argument.h"
43
#include "parse-util.h"
44
#include "password-quality-util.h"
45
#include "path-util.h"
46
#include "pretty-print.h"
47
#include "proc-cmdline.h"
48
#include "prompt-util.h"
49
#include "runtime-scope.h"
50
#include "smack-util.h"
51
#include "stat-util.h"
52
#include "string-util.h"
53
#include "strv.h"
54
#include "terminal-util.h"
55
#include "time-util.h"
56
#include "tmpfile-util-label.h"
57
#include "user-util.h"
58
#include "vconsole-util.h"
59

60
static char *arg_root = NULL;
61
static char *arg_image = NULL;
62
static char *arg_locale = NULL;  /* $LANG */
63
static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
64
static char *arg_keymap = NULL;
65
static char *arg_timezone = NULL;
66
static char *arg_hostname = NULL;
67
static sd_id128_t arg_machine_id = {};
68
static char *arg_root_password = NULL;
69
static char *arg_root_shell = NULL;
70
static char *arg_kernel_cmdline = NULL;
71
static bool arg_prompt_locale = false;
72
static bool arg_prompt_keymap = false;
73
static bool arg_prompt_keymap_auto = false;
74
static bool arg_prompt_timezone = false;
75
static bool arg_prompt_hostname = false;
76
static bool arg_prompt_root_password = false;
77
static bool arg_prompt_root_shell = false;
78
static bool arg_copy_locale = false;
79
static bool arg_copy_keymap = false;
80
static bool arg_copy_timezone = false;
81
static bool arg_copy_root_password = false;
82
static bool arg_copy_root_shell = false;
83
static bool arg_force = false;
84
static bool arg_delete_root_password = false;
85
static bool arg_root_password_is_hashed = false;
86
static bool arg_welcome = true;
87
static bool arg_reset = false;
88
static ImagePolicy *arg_image_policy = NULL;
89
static bool arg_chrome = true;
90
static bool arg_mute_console = false;
91

92
STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
141✔
93
STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
141✔
94
STATIC_DESTRUCTOR_REGISTER(arg_locale, freep);
141✔
95
STATIC_DESTRUCTOR_REGISTER(arg_locale_messages, freep);
141✔
96
STATIC_DESTRUCTOR_REGISTER(arg_keymap, freep);
141✔
97
STATIC_DESTRUCTOR_REGISTER(arg_timezone, freep);
141✔
98
STATIC_DESTRUCTOR_REGISTER(arg_hostname, freep);
141✔
99
STATIC_DESTRUCTOR_REGISTER(arg_root_password, erase_and_freep);
141✔
100
STATIC_DESTRUCTOR_REGISTER(arg_root_shell, freep);
141✔
101
STATIC_DESTRUCTOR_REGISTER(arg_kernel_cmdline, freep);
141✔
102
STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
141✔
103

104
static void print_welcome(int rfd, sd_varlink **mute_console_link) {
7✔
105
        _cleanup_free_ char *pretty_name = NULL, *os_name = NULL, *ansi_color = NULL;
7✔
106
        static bool done = false;
7✔
107
        const char *pn, *ac;
7✔
108
        int r;
7✔
109

110
        assert(rfd >= 0);
7✔
111
        assert(mute_console_link);
7✔
112

113
        if (!*mute_console_link && arg_mute_console)
7✔
114
                (void) mute_console(mute_console_link);
×
115

116
        if (!arg_welcome)
7✔
117
                return;
118

119
        if (done) {
6✔
120
                putchar('\n'); /* Add some breathing room between multiple prompts */
1✔
121
                return;
122
        }
123

124
        (void) terminal_reset_defensive_locked(STDOUT_FILENO, /* flags= */ 0);
6✔
125

126
        if (arg_chrome)
6✔
127
                chrome_show("Initial Setup", /* bottom= */ NULL);
6✔
128

129
        r = parse_os_release_at(rfd,
6✔
130
                                "PRETTY_NAME", &pretty_name,
131
                                "NAME", &os_name,
132
                                "ANSI_COLOR", &ansi_color);
133
        if (r < 0)
6✔
134
                log_full_errno(r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r,
6✔
135
                               "Failed to read os-release file, ignoring: %m");
136

137
        pn = os_release_pretty_name(pretty_name, os_name);
6✔
138
        ac = isempty(ansi_color) ? "0" : ansi_color;
6✔
139

140
        if (colors_enabled())
6✔
141
                printf(ANSI_HIGHLIGHT "Welcome to " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
×
142
        else
143
                printf("Welcome to %s!\n", pn);
6✔
144

145
        putchar('\n');
6✔
146
        if (emoji_enabled()) {
6✔
147
                fputs(glyph(GLYPH_SPARKLES), stdout);
×
148
                putchar(' ');
×
149
        }
150
        printf("Please configure the system!\n\n");
6✔
151

152
        done = true;
6✔
153
}
154

155
static int should_configure(int dir_fd, const char *filename) {
1,104✔
156
        _cleanup_fclose_ FILE *passwd = NULL, *shadow = NULL;
1,104✔
157
        int r;
1,104✔
158

159
        assert(dir_fd >= 0);
1,104✔
160
        assert(filename);
1,104✔
161

162
        if (streq(filename, "passwd") && !arg_force)
1,104✔
163
                /* We may need to do additional checks, so open the file. */
164
                r = xfopenat(dir_fd, filename, "re", O_NOFOLLOW, &passwd);
135✔
165
        else
166
                r = RET_NERRNO(faccessat(dir_fd, filename, F_OK, AT_SYMLINK_NOFOLLOW));
969✔
167

168
        if (r == -ENOENT)
593✔
169
                return true; /* missing */
170
        if (r < 0)
621✔
171
                return log_error_errno(r, "Failed to access %s: %m", filename);
×
172
        if (arg_force)
621✔
173
                return true; /* exists, but if --force was given we should still configure the file. */
174

175
        if (!passwd)
604✔
176
                return false;
177

178
        /* In case of /etc/passwd, do an additional check for the root password field.
179
         * We first check that passwd redirects to shadow, and then we check shadow.
180
         */
181
        struct passwd *i;
182
        while ((r = fgetpwent_sane(passwd, &i)) > 0) {
110✔
183
                if (!streq(i->pw_name, "root"))
109✔
184
                        continue;
×
185

186
                if (streq_ptr(i->pw_passwd, PASSWORD_SEE_SHADOW))
109✔
187
                        break;
188
                log_debug("passwd: root account with non-shadow password found, treating root as configured");
×
189
                return false;
190
        }
191
        if (r < 0)
110✔
192
                return log_error_errno(r, "Failed to read %s: %m", filename);
×
193
        if (r == 0) {
110✔
194
                log_debug("No root account found in %s, assuming root is not configured.", filename);
1✔
195
                return true;
1✔
196
        }
197

198
        r = xfopenat(dir_fd, "shadow", "re", O_NOFOLLOW, &shadow);
109✔
199
        if (r == -ENOENT) {
109✔
200
                log_debug("No shadow file found, assuming root is not configured.");
×
201
                return true; /* missing */
×
202
        }
203
        if (r < 0)
109✔
204
                return log_error_errno(r, "Failed to access shadow: %m");
×
205

206
        struct spwd *j;
207
        while ((r = fgetspent_sane(shadow, &j)) > 0) {
109✔
208
                if (!streq(j->sp_namp, "root"))
109✔
209
                        continue;
×
210

211
                bool unprovisioned = streq_ptr(j->sp_pwdp, PASSWORD_UNPROVISIONED);
109✔
212
                log_debug("Root account found, %s.",
212✔
213
                          unprovisioned ? "with unprovisioned password, treating root as not configured" :
214
                                          "treating root as configured");
215
                return unprovisioned;
109✔
216
        }
217
        if (r < 0)
×
218
                return log_error_errno(r, "Failed to read shadow: %m");
×
219
        assert(r == 0);
×
220
        log_debug("No root account found in shadow, assuming root is not configured.");
×
221
        return true;
222
}
223

224
static int locale_is_ok(const char *name, void *userdata) {
11✔
225
        int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
11✔
226

227
        r = dir_fd_is_root(rfd);
11✔
228
        if (r < 0)
11✔
229
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
230

231
        return r != 0 ? locale_is_installed(name) > 0 : locale_is_valid(name);
11✔
232
}
233

234
static int prompt_locale(int rfd, sd_varlink **mute_console_link) {
11✔
235
        _cleanup_strv_free_ char **locales = NULL;
×
236
        bool acquired_from_creds = false;
11✔
237
        int r;
11✔
238

239
        assert(rfd >= 0);
11✔
240

241
        if (arg_locale || arg_locale_messages)
11✔
242
                return 0;
243

244
        r = read_credential("firstboot.locale", (void**) &arg_locale, NULL);
6✔
245
        if (r < 0)
6✔
246
                log_debug_errno(r, "Failed to read credential firstboot.locale, ignoring: %m");
5✔
247
        else
248
                acquired_from_creds = true;
249

250
        r = read_credential("firstboot.locale-messages", (void**) &arg_locale_messages, NULL);
6✔
251
        if (r < 0)
6✔
252
                log_debug_errno(r, "Failed to read credential firstboot.locale-messages, ignoring: %m");
5✔
253
        else
254
                acquired_from_creds = true;
255

256
        if (acquired_from_creds) {
5✔
257
                log_debug("Acquired locale from credentials.");
1✔
258
                return 0;
1✔
259
        }
260

261
        if (!arg_prompt_locale) {
5✔
262
                log_debug("Prompting for locale was not requested.");
4✔
263
                return 0;
4✔
264
        }
265

266
        r = get_locales(&locales);
1✔
267
        if (r < 0)
1✔
268
                return log_error_errno(r, "Cannot query locales list: %m");
×
269

270
        if (strv_isempty(locales))
1✔
271
                log_debug("No locales found, skipping locale selection.");
×
272
        else if (strv_length(locales) == 1) {
1✔
273

274
                if (streq(locales[0], SYSTEMD_DEFAULT_LOCALE))
×
275
                        log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
×
276
                else {
277
                        log_debug("Only a single locale available (%s), selecting it as default.", locales[0]);
×
278

279
                        arg_locale = strdup(locales[0]);
×
280
                        if (!arg_locale)
×
281
                                return log_oom();
×
282

283
                        /* Not setting arg_locale_message here, since it defaults to LANG anyway */
284
                }
285
        } else {
286
                print_welcome(rfd, mute_console_link);
1✔
287

288
                r = prompt_loop("Please enter the new system locale name or number",
2✔
289
                                GLYPH_WORLD,
290
                                locales,
291
                                /* accepted= */ NULL,
292
                                /* ellipsize_percentage= */ 60,
293
                                /* n_columns= */ 3,
294
                                /* column_width= */ 20,
295
                                locale_is_ok,
296
                                /* refresh= */ NULL,
297
                                FD_TO_PTR(rfd),
1✔
298
                                PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
299
                                &arg_locale);
300
                if (r < 0)
1✔
301
                        return r;
302
                if (isempty(arg_locale))
12✔
303
                        return 0;
304

305
                r = prompt_loop("Please enter the new system message locale name or number",
1✔
306
                                GLYPH_WORLD,
307
                                locales,
308
                                /* accepted= */ NULL,
309
                                /* ellipsize_percentage= */ 60,
310
                                /* n_columns= */ 3,
311
                                /* column_width= */ 20,
312
                                locale_is_ok,
313
                                /* refresh= */ NULL,
314
                                FD_TO_PTR(rfd),
315
                                PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
316
                                &arg_locale_messages);
317
                if (r < 0)
1✔
318
                        return r;
319

320
                /* Suppress the messages setting if it's the same as the main locale anyway */
321
                if (streq_ptr(arg_locale, arg_locale_messages))
1✔
322
                        arg_locale_messages = mfree(arg_locale_messages);
×
323
        }
324

325
        return 0;
326
}
327

328
static int process_locale(int rfd, sd_varlink **mute_console_link) {
138✔
329
        _cleanup_close_ int pfd = -EBADF;
138✔
330
        _cleanup_free_ char *f = NULL;
138✔
331
        char* locales[3];
138✔
332
        unsigned i = 0;
138✔
333
        int r;
138✔
334

335
        assert(rfd >= 0);
138✔
336

337
        pfd = chase_and_open_parent_at(rfd, etc_locale_conf(),
138✔
338
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
339
                                       &f);
340
        if (pfd < 0)
138✔
341
                return log_error_errno(pfd, "Failed to chase /etc/locale.conf: %m");
×
342

343
        r = should_configure(pfd, f);
138✔
344
        if (r == 0)
138✔
345
                log_debug("Found /etc/locale.conf, assuming locale information has been configured.");
124✔
346
        if (r <= 0)
138✔
347
                return r;
348

349
        r = dir_fd_is_root(rfd);
14✔
350
        if (r < 0)
14✔
351
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
352

353
        if (arg_copy_locale && r == 0) {
14✔
354
                r = copy_file_atomic_at(AT_FDCWD, etc_locale_conf(), pfd, f, 0644, COPY_REFLINK);
3✔
355
                if (r != -ENOENT) {
3✔
356
                        if (r < 0)
3✔
357
                                return log_error_errno(r, "Failed to copy host's /etc/locale.conf: %m");
×
358

359
                        log_info("Copied host's /etc/locale.conf.");
3✔
360
                        return 0;
3✔
361
                }
362
        }
363

364
        r = prompt_locale(rfd, mute_console_link);
11✔
365
        if (r < 0)
11✔
366
                return r;
367

368
        if (!isempty(arg_locale))
11✔
369
                locales[i++] = strjoina("LANG=", arg_locale);
30✔
370
        if (!isempty(arg_locale_messages) && !streq_ptr(arg_locale_messages, arg_locale))
16✔
371
                locales[i++] = strjoina("LC_MESSAGES=", arg_locale_messages);
25✔
372

373
        if (i == 0)
11✔
374
                return 0;
375

376
        locales[i] = NULL;
7✔
377

378
        r = write_env_file(
7✔
379
                        pfd,
380
                        f,
381
                        /* headers= */ NULL,
382
                        locales,
383
                        WRITE_ENV_FILE_LABEL);
384
        if (r < 0)
7✔
385
                return log_error_errno(r, "Failed to write /etc/locale.conf: %m");
×
386

387
        log_info("/etc/locale.conf written.");
7✔
388
        return 1;
389
}
390

391
static int keymap_is_ok(const char* name, void *userdata) {
5✔
392
        int rfd = ASSERT_FD(PTR_TO_FD(userdata)), r;
5✔
393

394
        r = dir_fd_is_root(rfd);
5✔
395
        if (r < 0)
5✔
396
                log_debug_errno(r, "Unable to determine if operating on host root directory, assuming we are: %m");
×
397

398
        return r != 0 ? keymap_exists(name) > 0 : keymap_is_valid(name);
5✔
399
}
400

401
static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
19✔
402
        _cleanup_strv_free_ char **kmaps = NULL;
19✔
403
        int r;
19✔
404

405
        assert(rfd >= 0);
19✔
406

407
        if (arg_keymap)
19✔
408
                return 0;
409

410
        r = read_credential("firstboot.keymap", (void**) &arg_keymap, NULL);
16✔
411
        if (r < 0)
16✔
412
                log_debug_errno(r, "Failed to read credential firstboot.keymap, ignoring: %m");
15✔
413
        else {
414
                log_debug("Acquired keymap from credential.");
1✔
415
                return 0;
1✔
416
        }
417

418
        bool b;
15✔
419
        if (arg_prompt_keymap_auto) {
15✔
420
                _cleanup_free_ char *ttyname = NULL;
1✔
421

422
                r = getttyname_harder(STDOUT_FILENO, &ttyname);
1✔
423
                if (r < 0) {
1✔
424
                        log_debug_errno(r, "Cannot determine TTY we are connected, ignoring: %m");
1✔
425
                        b = false; /* if we can't resolve this, it's probably not a VT */
426
                } else {
427
                        b = tty_is_vc_resolve(ttyname);
×
428
                        log_debug("Detected connection to local console: %s", yes_no(b));
×
429
                }
430
        } else
431
                b = arg_prompt_keymap;
14✔
432
        if (!b) {
15✔
433
                log_debug("Prompting for keymap was not requested.");
14✔
434
                return 0;
14✔
435
        }
436

437
        r = get_keymaps(&kmaps);
1✔
438
        if (r == -ENOENT) /* no keymaps installed */
1✔
439
                return log_debug_errno(r, "No keymaps are installed.");
×
440
        if (r < 0)
1✔
441
                return log_error_errno(r, "Failed to read keymaps: %m");
×
442

443
        print_welcome(rfd, mute_console_link);
1✔
444

445
        return prompt_loop(
1✔
446
                        "Please enter the new keymap name or number",
447
                        GLYPH_KEYBOARD,
448
                        kmaps,
449
                        /* accepted= */ NULL,
450
                        /* ellipsize_percentage= */ 60,
451
                        /* n_columns= */ 3,
452
                        /* column_width= */ 20,
453
                        keymap_is_ok,
454
                        /* refresh= */ NULL,
455
                        FD_TO_PTR(rfd),
1✔
456
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
457
                        &arg_keymap);
458
}
459

460
static int process_keymap(int rfd, sd_varlink **mute_console_link) {
138✔
461
        _cleanup_close_ int pfd = -EBADF;
138✔
462
        _cleanup_free_ char *f = NULL;
138✔
463
        _cleanup_strv_free_ char **keymap = NULL;
×
464
        int r;
138✔
465

466
        assert(rfd >= 0);
138✔
467

468
        pfd = chase_and_open_parent_at(rfd, etc_vconsole_conf(),
138✔
469
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
470
                                       &f);
471
        if (pfd < 0)
138✔
472
                return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
×
473

474
        r = should_configure(pfd, f);
138✔
475
        if (r == 0)
138✔
476
                log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
116✔
477
        if (r <= 0)
138✔
478
                return r;
479

480
        r = dir_fd_is_root(rfd);
22✔
481
        if (r < 0)
22✔
482
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
483

484
        if (arg_copy_keymap && r == 0) {
22✔
485
                r = copy_file_atomic_at(AT_FDCWD, etc_vconsole_conf(), pfd, f, 0644, COPY_REFLINK);
3✔
486
                if (r != -ENOENT) {
3✔
487
                        if (r < 0)
3✔
488
                                return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
×
489

490
                        log_info("Copied host's /etc/vconsole.conf.");
3✔
491
                        return 0;
3✔
492
                }
493
        }
494

495
        r = prompt_keymap(rfd, mute_console_link);
19✔
496
        if (r == -ENOENT)
19✔
497
                return 0; /* don't fail if no keymaps are installed */
498
        if (r < 0)
19✔
499
                return r;
500

501
        if (isempty(arg_keymap))
143✔
502
                return 0;
503

504
        VCContext vc = {
5✔
505
                .keymap = arg_keymap,
506
        };
507
        _cleanup_(x11_context_clear) X11Context xc = {};
5✔
508

509
        r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
5✔
510
        if (r < 0)
5✔
511
                return log_error_errno(r, "Failed to convert keymap data: %m");
×
512

513
        r = vconsole_serialize(&vc, &xc, &keymap);
5✔
514
        if (r < 0)
5✔
515
                return log_error_errno(r, "Failed to serialize keymap data: %m");
×
516

517
        r = write_vconsole_conf(pfd, f, keymap);
5✔
518
        if (r < 0)
5✔
519
                return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
×
520

521
        log_info("/etc/vconsole.conf written.");
5✔
522
        return 1;
523
}
524

525
static int timezone_is_ok(const char *name, void *userdata) {
1✔
526
        return timezone_is_valid(name, LOG_DEBUG);
1✔
527
}
528

529
static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
119✔
530
        _cleanup_strv_free_ char **zones = NULL;
119✔
531
        int r;
119✔
532

533
        assert(rfd >= 0);
119✔
534

535
        if (arg_timezone)
119✔
536
                return 0;
537

538
        r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
116✔
539
        if (r < 0)
116✔
540
                log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
12✔
541
        else {
542
                log_debug("Acquired timezone from credential.");
104✔
543
                return 0;
104✔
544
        }
545

546
        if (!arg_prompt_timezone) {
12✔
547
                log_debug("Prompting for timezone was not requested.");
11✔
548
                return 0;
11✔
549
        }
550

551
        r = get_timezones(&zones);
1✔
552
        if (r < 0)
1✔
553
                return log_error_errno(r, "Cannot query timezone list: %m");
×
554

555
        print_welcome(rfd, mute_console_link);
1✔
556

557
        return prompt_loop(
1✔
558
                        "Please enter the new timezone name or number",
559
                        GLYPH_CLOCK,
560
                        zones,
561
                        /* accepted= */ NULL,
562
                        /* ellipsize_percentage= */ 30,
563
                        /* n_columns= */ 3,
564
                        /* column_width= */ 20,
565
                        timezone_is_ok,
566
                        /* refresh= */ NULL,
567
                        FD_TO_PTR(rfd),
1✔
568
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
569
                        &arg_timezone);
570
}
571

572
static int process_timezone(int rfd, sd_varlink **mute_console_link) {
138✔
573
        _cleanup_close_ int pfd = -EBADF;
138✔
574
        _cleanup_free_ char *f = NULL, *relpath = NULL;
138✔
575
        const char *e;
138✔
576
        int r;
138✔
577

578
        assert(rfd >= 0);
138✔
579

580
        pfd = chase_and_open_parent_at(rfd, etc_localtime(),
138✔
581
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
582
                                       &f);
583
        if (pfd < 0)
138✔
584
                return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
×
585

586
        r = should_configure(pfd, f);
138✔
587
        if (r == 0)
138✔
588
                log_debug("Found /etc/localtime, assuming timezone has been configured.");
16✔
589
        if (r <= 0)
138✔
590
                return r;
591

592
        r = dir_fd_is_root(rfd);
122✔
593
        if (r < 0)
122✔
594
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
595

596
        if (arg_copy_timezone && r == 0) {
122✔
597
                _cleanup_free_ char *s = NULL;
3✔
598

599
                r = readlink_malloc(etc_localtime(), &s);
3✔
600
                if (r != -ENOENT) {
3✔
601
                        if (r < 0)
3✔
602
                                return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
×
603

604
                        r = symlinkat_atomic_full(s, pfd, f, SYMLINK_LABEL);
3✔
605
                        if (r < 0)
3✔
606
                                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
607

608
                        log_info("Copied host's /etc/localtime.");
3✔
609
                        return 0;
3✔
610
                }
611
        }
612

613
        r = prompt_timezone(rfd, mute_console_link);
119✔
614
        if (r < 0)
119✔
615
                return r;
616

617
        if (isempty(arg_timezone))
246✔
618
                return 0;
619

620
        e = strjoina("/usr/share/zoneinfo/", arg_timezone);
540✔
621
        r = path_make_relative_parent(etc_localtime(), e, &relpath);
108✔
622
        if (r < 0)
108✔
623
                return r;
624

625
        r = symlinkat_atomic_full(relpath, pfd, f, SYMLINK_LABEL);
108✔
626
        if (r < 0)
108✔
627
                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
628

629
        log_info("/etc/localtime written");
108✔
630
        return 0;
631
}
632

633
static int hostname_is_ok(const char *name, void *userdata) {
1✔
634
        return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
1✔
635
}
636

637
static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
124✔
638
        int r;
124✔
639

640
        assert(rfd >= 0);
124✔
641

642
        if (arg_hostname)
124✔
643
                return 0;
644

645
        if (!arg_prompt_hostname) {
120✔
646
                log_debug("Prompting for hostname was not requested.");
119✔
647
                return 0;
119✔
648
        }
649

650
        print_welcome(rfd, mute_console_link);
1✔
651

652
        r = prompt_loop("Please enter the new hostname",
2✔
653
                        GLYPH_LABEL,
654
                        /* menu= */ NULL,
655
                        /* accepted= */ NULL,
656
                        /* ellipsize_percentage= */ 100,
657
                        /* n_columns= */ 3,
658
                        /* column_width= */ 20,
659
                        hostname_is_ok,
660
                        /* refresh= */ NULL,
661
                        FD_TO_PTR(rfd),
1✔
662
                        PROMPT_MAY_SKIP,
663
                        &arg_hostname);
664
        if (r < 0)
1✔
665
                return r;
666

667
        if (arg_hostname)
1✔
668
                hostname_cleanup(arg_hostname);
1✔
669

670
        return 0;
671
}
672

673
static int process_hostname(int rfd, sd_varlink **mute_console_link) {
138✔
674
        _cleanup_close_ int pfd = -EBADF;
138✔
675
        _cleanup_free_ char *f = NULL;
138✔
676
        int r;
138✔
677

678
        assert(rfd >= 0);
138✔
679

680
        pfd = chase_and_open_parent_at(rfd, etc_hostname(),
138✔
681
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
682
                                       &f);
683
        if (pfd < 0)
138✔
684
                return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
×
685

686
        r = should_configure(pfd, f);
138✔
687
        if (r == 0)
138✔
688
                log_debug("Found /etc/hostname, assuming hostname has been configured.");
14✔
689
        if (r <= 0)
138✔
690
                return r;
691

692
        r = prompt_hostname(rfd, mute_console_link);
124✔
693
        if (r < 0)
124✔
694
                return r;
695

696
        if (isempty(arg_hostname))
143✔
697
                return 0;
698

699
        r = write_string_file_at(pfd, f, arg_hostname,
5✔
700
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
701
        if (r < 0)
5✔
702
                return log_error_errno(r, "Failed to write /etc/hostname: %m");
×
703

704
        log_info("/etc/hostname written.");
5✔
705
        return 0;
706
}
707

708
static int process_machine_id(int rfd) {
138✔
709
        _cleanup_close_ int pfd = -EBADF;
138✔
710
        _cleanup_free_ char *f = NULL;
138✔
711
        int r;
138✔
712

713
        assert(rfd >= 0);
138✔
714

715
        pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
138✔
716
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
717
                                       &f);
718
        if (pfd < 0)
138✔
719
                return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
×
720

721
        r = should_configure(pfd, f);
138✔
722
        if (r == 0)
138✔
723
                log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
113✔
724
        if (r <= 0)
138✔
725
                return r;
726

727
        if (sd_id128_is_null(arg_machine_id)) {
25✔
728
                log_debug("Initialization of machine-id was not requested, skipping.");
22✔
729
                return 0;
22✔
730
        }
731

732
        r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
3✔
733
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
734
        if (r < 0)
3✔
735
                return log_error_errno(r, "Failed to write /etc/machine-id: %m");
×
736

737
        log_info("/etc/machine-id written.");
3✔
738
        return 0;
739
}
740

741
static int prompt_root_password(int rfd, sd_varlink **mute_console_link) {
30✔
742
        const char *msg1, *msg2;
30✔
743
        int r;
30✔
744

745
        assert(rfd >= 0);
30✔
746

747
        if (arg_root_password)
30✔
748
                return 0;
749

750
        if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
21✔
751
                return 0;
752

753
        if (!arg_prompt_root_password) {
20✔
754
                log_debug("Prompting for root password was not requested.");
19✔
755
                return 0;
19✔
756
        }
757

758
        print_welcome(rfd, mute_console_link);
1✔
759

760
        msg1 = "Please enter the new root password (empty to skip):";
1✔
761
        msg2 = "Please enter the new root password again:";
1✔
762

763
        suggest_passwords();
1✔
764

765
        for (;;) {
×
766
                _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
1✔
767
                _cleanup_free_ char *error = NULL;
1✔
768

769
                AskPasswordRequest req = {
1✔
770
                        .tty_fd = -EBADF,
771
                        .message = msg1,
772
                        .until = USEC_INFINITY,
773
                        .hup_fd = -EBADF,
774
                };
775

776
                r = ask_password_tty(&req, /* flags= */ 0, &a);
1✔
777
                if (r < 0)
1✔
778
                        return log_error_errno(r, "Failed to query root password: %m");
×
779
                if (strv_length(a) != 1)
1✔
780
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
781
                                               "Received multiple passwords, where we expected one.");
782

783
                if (isempty(*a)) {
1✔
784
                        log_info("No password entered, skipping.");
1✔
785
                        break;
786
                }
787

788
                r = check_password_quality(*a, /* old = */ NULL, "root", &error);
×
789
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
790
                        log_warning("Password quality check is not supported, proceeding anyway.");
×
791
                else if (r < 0)
×
792
                        return log_error_errno(r, "Failed to check password quality: %m");
×
793
                else if (r == 0)
×
794
                        log_warning("Password is weak, accepting anyway: %s", error);
×
795

796
                req.message = msg2;
×
797

798
                r = ask_password_tty(&req, /* flags= */ 0, &b);
×
799
                if (r < 0)
×
800
                        return log_error_errno(r, "Failed to query root password: %m");
×
801
                if (strv_length(b) != 1)
×
802
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
803
                                               "Received multiple passwords, where we expected one.");
804

805
                if (!streq(*a, *b)) {
×
806
                        log_error("Entered passwords did not match, please try again.");
×
807
                        continue;
×
808
                }
809

810
                arg_root_password = TAKE_PTR(*a);
×
811
                break;
×
812
        }
813

814
        return 0;
1✔
815
}
816

817
static int find_shell(int rfd, const char *path) {
7✔
818
        int r;
7✔
819

820
        assert(path);
7✔
821

822
        if (!valid_shell(path))
7✔
823
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
×
824

825
        r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
7✔
826
        if (r < 0)
7✔
827
                return log_error_errno(r, "Failed to resolve shell %s: %m", path);
1✔
828

829
        return 0;
830
}
831

832
static int shell_is_ok(const char *path, void *userdata) {
2✔
833
        int rfd = ASSERT_FD(PTR_TO_FD(userdata));
2✔
834

835
        return find_shell(rfd, path) >= 0;
2✔
836
}
837

838
static int prompt_root_shell(int rfd, sd_varlink **mute_console_link) {
30✔
839
        int r;
30✔
840

841
        assert(rfd >= 0);
30✔
842

843
        if (arg_root_shell)
30✔
844
                return 0;
845

846
        r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
25✔
847
        if (r < 0)
25✔
848
                log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
24✔
849
        else {
850
                log_debug("Acquired root shell from credential.");
1✔
851
                return 0;
1✔
852
        }
853

854
        if (!arg_prompt_root_shell) {
24✔
855
                log_debug("Prompting for root shell was not requested.");
22✔
856
                return 0;
22✔
857
        }
858

859
        print_welcome(rfd, mute_console_link);
2✔
860

861
        return prompt_loop(
2✔
862
                        "Please enter the new root shell",
863
                        GLYPH_SHELL,
864
                        /* menu= */ NULL,
865
                        /* accepted= */ NULL,
866
                        /* ellipsize_percentage= */ 0,
867
                        /* n_columns= */ 3,
868
                        /* column_width= */ 20,
869
                        shell_is_ok,
870
                        /* refresh= */ NULL,
871
                        FD_TO_PTR(rfd),
2✔
872
                        PROMPT_MAY_SKIP,
873
                        &arg_root_shell);
874
}
875

876
static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
15✔
877
        _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
15✔
878
        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
15✔
879
        int r;
15✔
880
        bool found = false;
15✔
881

882
        r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
15✔
883
        if (r < 0)
15✔
884
                return r;
885

886
        r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
15✔
887
        if (r < 0 && r != -ENOENT)
15✔
888
                return r;
889

890
        if (original) {
15✔
891
                struct passwd *i;
4✔
892

893
                r = copy_rights(fileno(original), fileno(passwd));
4✔
894
                if (r < 0)
4✔
895
                        return r;
×
896

897
                while ((r = fgetpwent_sane(original, &i)) > 0) {
7✔
898

899
                        if (streq(i->pw_name, "root")) {
3✔
900
                                if (password)
3✔
901
                                        i->pw_passwd = (char *) password;
2✔
902
                                if (shell)
3✔
903
                                        i->pw_shell = (char *) shell;
2✔
904
                                found = true;
905
                        }
906

907
                        r = putpwent_sane(i, passwd);
3✔
908
                        if (r < 0)
3✔
909
                                return r;
910
                }
911
                if (r < 0)
4✔
912
                        return r;
913

914
        } else {
915
                r = fchmod(fileno(passwd), 0644);
11✔
916
                if (r < 0)
11✔
917
                        return -errno;
×
918
        }
919

920
        if (!found) {
4✔
921
                struct passwd root = {
12✔
922
                        .pw_name = (char *) "root",
923
                        .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
12✔
924
                        .pw_uid = 0,
925
                        .pw_gid = 0,
926
                        .pw_gecos = (char *) "Super User",
927
                        .pw_dir = (char *) "/root",
928
                        .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
6✔
929
                };
930

931
                r = putpwent_sane(&root, passwd);
12✔
932
                if (r < 0)
12✔
933
                        return r;
×
934
        }
935

936
        r = fflush_sync_and_check(passwd);
15✔
937
        if (r < 0)
15✔
938
                return r;
939

940
        r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
15✔
941
        if (r < 0)
15✔
942
                return r;
×
943

944
        return 0;
945
}
946

947
static int write_root_shadow(int etc_fd, const char *hashed_password) {
15✔
948
        _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
15✔
949
        _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
15✔
950
        int r;
15✔
951
        bool found = false;
15✔
952

953
        r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
15✔
954
        if (r < 0)
15✔
955
                return r;
956

957
        r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
15✔
958
        if (r < 0 && r != -ENOENT)
15✔
959
                return r;
960

961
        if (original) {
15✔
962
                struct spwd *i;
4✔
963

964
                r = copy_rights(fileno(original), fileno(shadow));
4✔
965
                if (r < 0)
4✔
966
                        return r;
×
967

968
                while ((r = fgetspent_sane(original, &i)) > 0) {
7✔
969

970
                        if (streq(i->sp_namp, "root")) {
3✔
971
                                if (hashed_password) {
3✔
972
                                        i->sp_pwdp = (char *) hashed_password;
2✔
973
                                        i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
2✔
974
                                }
975
                                found = true;
976
                        }
977

978
                        r = putspent_sane(i, shadow);
3✔
979
                        if (r < 0)
3✔
980
                                return r;
981
                }
982
                if (r < 0)
4✔
983
                        return r;
984

985
        } else {
986
                r = fchmod(fileno(shadow), 0000);
11✔
987
                if (r < 0)
11✔
988
                        return -errno;
×
989
        }
990

991
        if (!found) {
4✔
992
                struct spwd root = {
36✔
993
                        .sp_namp = (char*) "root",
994
                        .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
12✔
995
                        .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
12✔
996
                        .sp_min = -1,
997
                        .sp_max = -1,
998
                        .sp_warn = -1,
999
                        .sp_inact = -1,
1000
                        .sp_expire = -1,
1001
                        .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
1002
                };
1003

1004
                r = putspent_sane(&root, shadow);
12✔
1005
                if (r < 0)
12✔
1006
                        return r;
×
1007
        }
1008

1009
        r = fflush_sync_and_check(shadow);
15✔
1010
        if (r < 0)
15✔
1011
                return r;
1012

1013
        r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
15✔
1014
        if (r < 0)
15✔
1015
                return r;
×
1016

1017
        return 0;
1018
}
1019

1020
static int process_root_account(int rfd, sd_varlink **mute_console_link) {
138✔
1021
        _cleanup_close_ int pfd = -EBADF;
138✔
1022
        _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
×
1023
        _cleanup_(erase_and_freep) char *_hashed_password = NULL;
138✔
1024
        const char *password, *hashed_password;
138✔
1025
        int k = 0, r;
138✔
1026

1027
        assert(rfd >= 0);
138✔
1028

1029
        pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
138✔
1030
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1031
                                       NULL);
1032
        if (pfd < 0)
138✔
1033
                return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
×
1034

1035
        /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1036

1037
        FOREACH_STRING(s, "passwd", "shadow") {
414✔
1038
                r = verify_regular_at(pfd, s, /* follow = */ false);
276✔
1039
                if (r < 0 && r != -ENOENT)
276✔
1040
                        return log_error_errno(r, "Verification of /etc/%s being regular file failed: %m", s);
×
1041

1042
                r = should_configure(pfd, s);
276✔
1043
                if (r < 0)
276✔
1044
                        return r;
1045

1046
                k += r;
276✔
1047
        }
1048

1049
        if (k == 0) {
138✔
1050
                log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
108✔
1051
                return 0;
108✔
1052
        }
1053

1054
        r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
30✔
1055
        if (r < 0)
30✔
1056
                return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
×
1057

1058
        k = dir_fd_is_root(rfd);
30✔
1059
        if (k < 0)
30✔
1060
                return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
×
1061

1062
        if (arg_copy_root_shell && k == 0) {
30✔
1063
                _cleanup_free_ struct passwd *p = NULL;
2✔
1064

1065
                r = getpwnam_malloc("root", &p);
2✔
1066
                if (r < 0)
2✔
1067
                        return log_error_errno(r, "Failed to find passwd entry for root: %m");
×
1068

1069
                r = free_and_strdup(&arg_root_shell, p->pw_shell);
2✔
1070
                if (r < 0)
2✔
1071
                        return log_oom();
×
1072
        }
1073

1074
        r = prompt_root_shell(rfd, mute_console_link);
30✔
1075
        if (r < 0)
30✔
1076
                return r;
1077

1078
        if (arg_copy_root_password && k == 0) {
30✔
1079
                struct spwd *p;
2✔
1080

1081
                errno = 0;
2✔
1082
                p = getspnam("root");
2✔
1083
                if (!p)
2✔
1084
                        return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
×
1085

1086
                r = free_and_strdup(&arg_root_password, p->sp_pwdp);
2✔
1087
                if (r < 0)
2✔
1088
                        return log_oom();
×
1089

1090
                arg_root_password_is_hashed = true;
2✔
1091
        }
1092

1093
        r = prompt_root_password(rfd, mute_console_link);
30✔
1094
        if (r < 0)
30✔
1095
                return r;
1096

1097
        if (arg_root_password && arg_root_password_is_hashed) {
30✔
1098
                password = PASSWORD_SEE_SHADOW;
1099
                hashed_password = arg_root_password;
1100
        } else if (arg_root_password) {
24✔
1101
                r = hash_password(arg_root_password, &_hashed_password);
4✔
1102
                if (r < 0)
4✔
1103
                        return log_error_errno(r, "Failed to hash password: %m");
×
1104

1105
                password = PASSWORD_SEE_SHADOW;
4✔
1106
                hashed_password = _hashed_password;
4✔
1107

1108
        } else if (arg_delete_root_password) {
20✔
1109
                password = PASSWORD_SEE_SHADOW;
1110
                hashed_password = PASSWORD_NONE;
1111
        } else if (!arg_root_password && arg_prompt_root_password) {
19✔
1112
                /* If the user was prompted, but no password was supplied, lock the account. */
1113
                password = PASSWORD_SEE_SHADOW;
1114
                hashed_password = PASSWORD_LOCKED_AND_INVALID;
1115
        } else
1116
                /* Leave the password as is. */
1117
                password = hashed_password = NULL;
18✔
1118

1119
        /* Don't create/modify passwd and shadow if there's nothing to do. */
1120
        if (!(password || hashed_password || arg_root_shell)) {
30✔
1121
                log_debug("Initialization of root account was not requested, skipping.");
15✔
1122
                return 0;
15✔
1123
        }
1124

1125
        r = write_root_passwd(rfd, pfd, password, arg_root_shell);
15✔
1126
        if (r < 0)
15✔
1127
                return log_error_errno(r, "Failed to write /etc/passwd: %m");
×
1128

1129
        log_info("/etc/passwd written.");
15✔
1130

1131
        r = write_root_shadow(pfd, hashed_password);
15✔
1132
        if (r < 0)
15✔
1133
                return log_error_errno(r, "Failed to write /etc/shadow: %m");
×
1134

1135
        log_info("/etc/shadow written.");
15✔
1136
        return 0;
1137
}
1138

1139
static int process_kernel_cmdline(int rfd) {
138✔
1140
        _cleanup_close_ int pfd = -EBADF;
138✔
1141
        _cleanup_free_ char *f = NULL;
138✔
1142
        int r;
138✔
1143

1144
        assert(rfd >= 0);
138✔
1145

1146
        pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
138✔
1147
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1148
                                       &f);
1149
        if (pfd < 0)
138✔
1150
                return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
×
1151

1152
        r = should_configure(pfd, f);
138✔
1153
        if (r == 0)
138✔
1154
                log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1✔
1155
        if (r <= 0)
138✔
1156
                return r;
1157

1158
        if (!arg_kernel_cmdline) {
137✔
1159
                log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
135✔
1160
                return 0;
135✔
1161
        }
1162

1163
        r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
2✔
1164
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
1165
        if (r < 0)
2✔
1166
                return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
×
1167

1168
        log_info("/etc/kernel/cmdline written.");
2✔
1169
        return 0;
1170
}
1171

1172
static int reset_one(int rfd, const char *path) {
6✔
1173
        _cleanup_close_ int pfd = -EBADF;
6✔
1174
        _cleanup_free_ char *f = NULL;
6✔
1175

1176
        assert(rfd >= 0);
6✔
1177
        assert(path);
6✔
1178

1179
        pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
6✔
1180
        if (pfd == -ENOENT)
6✔
1181
                return 0;
1182
        if (pfd < 0)
6✔
1183
                return log_error_errno(pfd, "Failed to resolve %s: %m", path);
×
1184

1185
        if (unlinkat(pfd, f, 0) < 0)
6✔
1186
                return errno == ENOENT ? 0 : log_error_errno(errno, "Failed to remove %s: %m", path);
×
1187

1188
        log_info("Removed %s", path);
6✔
1189
        return 0;
1190
}
1191

1192
static int process_reset(int rfd) {
138✔
1193
        int r;
138✔
1194

1195
        assert(rfd >= 0);
138✔
1196

1197
        if (!arg_reset)
138✔
1198
                return 0;
1199

1200
        FOREACH_STRING(p,
7✔
1201
                       etc_locale_conf(),
1202
                       etc_vconsole_conf(),
1203
                       etc_hostname(),
1204
                       "/etc/machine-id",
1205
                       "/etc/kernel/cmdline",
1206
                       etc_localtime()) {
1207
                r = reset_one(rfd, p);
6✔
1208
                if (r < 0)
6✔
1209
                        return r;
×
1210
        }
1211

1212
        return 0;
1✔
1213
}
1214

1215
static int help(void) {
×
1216
        _cleanup_free_ char *link = NULL;
×
1217
        int r;
×
1218

1219
        r = terminal_urlify_man("systemd-firstboot", "1", &link);
×
1220
        if (r < 0)
×
1221
                return log_oom();
×
1222

1223
        printf("%1$s [OPTIONS...]\n"
×
1224
               "\n%3$sConfigures basic settings of the system.%4$s\n\n"
1225
               "  -h --help                       Show this help\n"
1226
               "     --version                    Show package version\n"
1227
               "     --root=PATH                  Operate on an alternate filesystem root\n"
1228
               "     --image=PATH                 Operate on disk image as filesystem root\n"
1229
               "     --image-policy=POLICY        Specify disk image dissection policy\n"
1230
               "     --locale=LOCALE              Set primary locale (LANG=)\n"
1231
               "     --locale-messages=LOCALE     Set message locale (LC_MESSAGES=)\n"
1232
               "     --keymap=KEYMAP              Set keymap\n"
1233
               "     --timezone=TIMEZONE          Set timezone\n"
1234
               "     --hostname=NAME              Set hostname\n"
1235
               "     --setup-machine-id           Set a random machine ID\n"
1236
               "     --machine-id=ID              Set specified machine ID\n"
1237
               "     --root-password=PASSWORD     Set root password from plaintext password\n"
1238
               "     --root-password-file=FILE    Set root password from file\n"
1239
               "     --root-password-hashed=HASH  Set root password from hashed password\n"
1240
               "     --root-shell=SHELL           Set root shell\n"
1241
               "     --kernel-command-line=CMDLINE\n"
1242
               "                                  Set kernel command line\n"
1243
               "     --prompt-locale              Prompt the user for locale settings\n"
1244
               "     --prompt-keymap              Prompt the user for keymap settings\n"
1245
               "     --prompt-keymap-auto         Prompt the user for keymap settings if invoked\n"
1246
               "                                  on local console\n"
1247
               "     --prompt-timezone            Prompt the user for timezone\n"
1248
               "     --prompt-hostname            Prompt the user for hostname\n"
1249
               "     --prompt-root-password       Prompt the user for root password\n"
1250
               "     --prompt-root-shell          Prompt the user for root shell\n"
1251
               "     --prompt                     Prompt for all of the above\n"
1252
               "     --copy-locale                Copy locale from host\n"
1253
               "     --copy-keymap                Copy keymap from host\n"
1254
               "     --copy-timezone              Copy timezone from host\n"
1255
               "     --copy-root-password         Copy root password from host\n"
1256
               "     --copy-root-shell            Copy root shell from host\n"
1257
               "     --copy                       Copy locale, keymap, timezone, root password\n"
1258
               "     --force                      Overwrite existing files\n"
1259
               "     --delete-root-password       Delete root password\n"
1260
               "     --welcome=no                 Disable the welcome text\n"
1261
               "     --chrome=no                  Don't show color bar at top and bottom of\n"
1262
               "                                  terminal\n"
1263
               "     --mute-console=yes           Tell kernel/PID 1 to not write to the console\n"
1264
               "                                  while running\n"
1265
               "     --reset                      Remove existing files\n"
1266
               "\nSee the %2$s for details.\n",
1267
               program_invocation_short_name,
1268
               link,
1269
               ansi_highlight(),
1270
               ansi_normal());
1271

1272
        return 0;
1273
}
1274

1275
static int parse_argv(int argc, char *argv[]) {
141✔
1276

1277
        enum {
141✔
1278
                ARG_VERSION = 0x100,
1279
                ARG_ROOT,
1280
                ARG_IMAGE,
1281
                ARG_IMAGE_POLICY,
1282
                ARG_LOCALE,
1283
                ARG_LOCALE_MESSAGES,
1284
                ARG_KEYMAP,
1285
                ARG_TIMEZONE,
1286
                ARG_HOSTNAME,
1287
                ARG_SETUP_MACHINE_ID,
1288
                ARG_MACHINE_ID,
1289
                ARG_ROOT_PASSWORD,
1290
                ARG_ROOT_PASSWORD_FILE,
1291
                ARG_ROOT_PASSWORD_HASHED,
1292
                ARG_ROOT_SHELL,
1293
                ARG_KERNEL_COMMAND_LINE,
1294
                ARG_PROMPT,
1295
                ARG_PROMPT_LOCALE,
1296
                ARG_PROMPT_KEYMAP,
1297
                ARG_PROMPT_KEYMAP_AUTO,
1298
                ARG_PROMPT_TIMEZONE,
1299
                ARG_PROMPT_HOSTNAME,
1300
                ARG_PROMPT_ROOT_PASSWORD,
1301
                ARG_PROMPT_ROOT_SHELL,
1302
                ARG_COPY,
1303
                ARG_COPY_LOCALE,
1304
                ARG_COPY_KEYMAP,
1305
                ARG_COPY_TIMEZONE,
1306
                ARG_COPY_ROOT_PASSWORD,
1307
                ARG_COPY_ROOT_SHELL,
1308
                ARG_FORCE,
1309
                ARG_DELETE_ROOT_PASSWORD,
1310
                ARG_WELCOME,
1311
                ARG_CHROME,
1312
                ARG_RESET,
1313
                ARG_MUTE_CONSOLE,
1314
        };
1315

1316
        static const struct option options[] = {
141✔
1317
                { "help",                    no_argument,       NULL, 'h'                         },
1318
                { "version",                 no_argument,       NULL, ARG_VERSION                 },
1319
                { "root",                    required_argument, NULL, ARG_ROOT                    },
1320
                { "image",                   required_argument, NULL, ARG_IMAGE                   },
1321
                { "image-policy",            required_argument, NULL, ARG_IMAGE_POLICY            },
1322
                { "locale",                  required_argument, NULL, ARG_LOCALE                  },
1323
                { "locale-messages",         required_argument, NULL, ARG_LOCALE_MESSAGES         },
1324
                { "keymap",                  required_argument, NULL, ARG_KEYMAP                  },
1325
                { "timezone",                required_argument, NULL, ARG_TIMEZONE                },
1326
                { "hostname",                required_argument, NULL, ARG_HOSTNAME                },
1327
                { "setup-machine-id",        no_argument,       NULL, ARG_SETUP_MACHINE_ID        },
1328
                { "machine-id",              required_argument, NULL, ARG_MACHINE_ID              },
1329
                { "root-password",           required_argument, NULL, ARG_ROOT_PASSWORD           },
1330
                { "root-password-file",      required_argument, NULL, ARG_ROOT_PASSWORD_FILE      },
1331
                { "root-password-hashed",    required_argument, NULL, ARG_ROOT_PASSWORD_HASHED    },
1332
                { "root-shell",              required_argument, NULL, ARG_ROOT_SHELL              },
1333
                { "kernel-command-line",     required_argument, NULL, ARG_KERNEL_COMMAND_LINE     },
1334
                { "prompt",                  no_argument,       NULL, ARG_PROMPT                  },
1335
                { "prompt-locale",           no_argument,       NULL, ARG_PROMPT_LOCALE           },
1336
                { "prompt-keymap",           no_argument,       NULL, ARG_PROMPT_KEYMAP           },
1337
                { "prompt-keymap-auto",      no_argument,       NULL, ARG_PROMPT_KEYMAP_AUTO      },
1338
                { "prompt-timezone",         no_argument,       NULL, ARG_PROMPT_TIMEZONE         },
1339
                { "prompt-hostname",         no_argument,       NULL, ARG_PROMPT_HOSTNAME         },
1340
                { "prompt-root-password",    no_argument,       NULL, ARG_PROMPT_ROOT_PASSWORD    },
1341
                { "prompt-root-shell",       no_argument,       NULL, ARG_PROMPT_ROOT_SHELL       },
1342
                { "copy",                    no_argument,       NULL, ARG_COPY                    },
1343
                { "copy-locale",             no_argument,       NULL, ARG_COPY_LOCALE             },
1344
                { "copy-keymap",             no_argument,       NULL, ARG_COPY_KEYMAP             },
1345
                { "copy-timezone",           no_argument,       NULL, ARG_COPY_TIMEZONE           },
1346
                { "copy-root-password",      no_argument,       NULL, ARG_COPY_ROOT_PASSWORD      },
1347
                { "copy-root-shell",         no_argument,       NULL, ARG_COPY_ROOT_SHELL         },
1348
                { "force",                   no_argument,       NULL, ARG_FORCE                   },
1349
                { "delete-root-password",    no_argument,       NULL, ARG_DELETE_ROOT_PASSWORD    },
1350
                { "welcome",                 required_argument, NULL, ARG_WELCOME                 },
1351
                { "chrome",                  required_argument, NULL, ARG_CHROME                  },
1352
                { "reset",                   no_argument,       NULL, ARG_RESET                   },
1353
                { "mute-console",            required_argument, NULL, ARG_MUTE_CONSOLE            },
1354
                {}
1355
        };
1356

1357
        int r, c;
141✔
1358

1359
        assert(argc >= 0);
141✔
1360
        assert(argv);
141✔
1361

1362
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
766✔
1363

1364
                switch (c) {
627✔
1365

1366
                case 'h':
×
1367
                        return help();
×
1368

1369
                case ARG_VERSION:
×
1370
                        return version();
×
1371

1372
                case ARG_ROOT:
38✔
1373
                        r = parse_path_argument(optarg, true, &arg_root);
38✔
1374
                        if (r < 0)
38✔
1375
                                return r;
1376
                        break;
1377

1378
                case ARG_IMAGE:
×
1379
                        r = parse_path_argument(optarg, false, &arg_image);
×
1380
                        if (r < 0)
×
1381
                                return r;
1382
                        break;
1383

1384
                case ARG_IMAGE_POLICY:
×
1385
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1386
                        if (r < 0)
×
1387
                                return r;
1388
                        break;
1389

1390
                case ARG_LOCALE:
5✔
1391
                        r = free_and_strdup(&arg_locale, optarg);
5✔
1392
                        if (r < 0)
5✔
1393
                                return log_oom();
×
1394

1395
                        break;
1396

1397
                case ARG_LOCALE_MESSAGES:
4✔
1398
                        r = free_and_strdup(&arg_locale_messages, optarg);
4✔
1399
                        if (r < 0)
4✔
1400
                                return log_oom();
×
1401

1402
                        break;
1403

1404
                case ARG_KEYMAP:
4✔
1405
                        if (!keymap_is_valid(optarg))
4✔
1406
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1407
                                                       "Keymap %s is not valid.", optarg);
1408

1409
                        r = free_and_strdup(&arg_keymap, optarg);
4✔
1410
                        if (r < 0)
4✔
1411
                                return log_oom();
×
1412

1413
                        break;
1414

1415
                case ARG_TIMEZONE:
5✔
1416
                        if (!timezone_is_valid(optarg, LOG_ERR))
5✔
1417
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1418
                                                       "Timezone %s is not valid.", optarg);
1419

1420
                        r = free_and_strdup(&arg_timezone, optarg);
4✔
1421
                        if (r < 0)
4✔
1422
                                return log_oom();
×
1423

1424
                        break;
1425

1426
                case ARG_ROOT_PASSWORD:
4✔
1427
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1428
                        if (r < 0)
4✔
1429
                                return log_oom();
×
1430

1431
                        arg_root_password_is_hashed = false;
4✔
1432
                        break;
4✔
1433

1434
                case ARG_ROOT_PASSWORD_FILE:
1✔
1435
                        arg_root_password = mfree(arg_root_password);
1✔
1436

1437
                        r = read_one_line_file(optarg, &arg_root_password);
1✔
1438
                        if (r < 0)
1✔
1439
                                return log_error_errno(r, "Failed to read %s: %m", optarg);
×
1440

1441
                        arg_root_password_is_hashed = false;
1✔
1442
                        break;
1✔
1443

1444
                case ARG_ROOT_PASSWORD_HASHED:
4✔
1445
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1446
                        if (r < 0)
4✔
1447
                                return log_oom();
×
1448

1449
                        arg_root_password_is_hashed = true;
4✔
1450
                        break;
4✔
1451

1452
                case ARG_ROOT_SHELL:
5✔
1453
                        r = free_and_strdup(&arg_root_shell, optarg);
5✔
1454
                        if (r < 0)
5✔
1455
                                return log_oom();
×
1456

1457
                        break;
1458

1459
                case ARG_HOSTNAME:
5✔
1460
                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
5✔
1461
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1462
                                                       "Host name %s is not valid.", optarg);
1463

1464
                        r = free_and_strdup(&arg_hostname, optarg);
5✔
1465
                        if (r < 0)
5✔
1466
                                return log_oom();
×
1467

1468
                        hostname_cleanup(arg_hostname);
5✔
1469
                        break;
5✔
1470

1471
                case ARG_SETUP_MACHINE_ID:
1✔
1472
                        r = sd_id128_randomize(&arg_machine_id);
1✔
1473
                        if (r < 0)
1✔
1474
                                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
1475

1476
                        break;
1477

1478
                case ARG_MACHINE_ID:
4✔
1479
                        r = sd_id128_from_string(optarg, &arg_machine_id);
4✔
1480
                        if (r < 0)
4✔
1481
                                return log_error_errno(r, "Failed to parse machine id %s.", optarg);
1✔
1482

1483
                        break;
1484

1485
                case ARG_KERNEL_COMMAND_LINE:
3✔
1486
                        r = free_and_strdup(&arg_kernel_cmdline, optarg);
3✔
1487
                        if (r < 0)
3✔
1488
                                return log_oom();
×
1489

1490
                        break;
1491

1492
                case ARG_PROMPT:
×
1493
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
×
1494
                                arg_prompt_root_password = arg_prompt_root_shell = true;
×
1495
                        arg_prompt_keymap_auto = false;
×
1496
                        break;
×
1497

1498
                case ARG_PROMPT_LOCALE:
105✔
1499
                        arg_prompt_locale = true;
105✔
1500
                        break;
105✔
1501

1502
                case ARG_PROMPT_KEYMAP:
2✔
1503
                        arg_prompt_keymap = true;
2✔
1504
                        arg_prompt_keymap_auto = false;
2✔
1505
                        break;
2✔
1506

1507
                case ARG_PROMPT_KEYMAP_AUTO:
104✔
1508
                        arg_prompt_keymap_auto = true;
104✔
1509
                        break;
104✔
1510

1511
                case ARG_PROMPT_TIMEZONE:
105✔
1512
                        arg_prompt_timezone = true;
105✔
1513
                        break;
105✔
1514

1515
                case ARG_PROMPT_HOSTNAME:
1✔
1516
                        arg_prompt_hostname = true;
1✔
1517
                        break;
1✔
1518

1519
                case ARG_PROMPT_ROOT_PASSWORD:
105✔
1520
                        arg_prompt_root_password = true;
105✔
1521
                        break;
105✔
1522

1523
                case ARG_PROMPT_ROOT_SHELL:
4✔
1524
                        arg_prompt_root_shell = true;
4✔
1525
                        break;
4✔
1526

1527
                case ARG_COPY:
1✔
1528
                        arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1✔
1529
                                arg_copy_root_shell = true;
1✔
1530
                        break;
1✔
1531

1532
                case ARG_COPY_LOCALE:
2✔
1533
                        arg_copy_locale = true;
2✔
1534
                        break;
2✔
1535

1536
                case ARG_COPY_KEYMAP:
2✔
1537
                        arg_copy_keymap = true;
2✔
1538
                        break;
2✔
1539

1540
                case ARG_COPY_TIMEZONE:
2✔
1541
                        arg_copy_timezone = true;
2✔
1542
                        break;
2✔
1543

1544
                case ARG_COPY_ROOT_PASSWORD:
1✔
1545
                        arg_copy_root_password = true;
1✔
1546
                        break;
1✔
1547

1548
                case ARG_COPY_ROOT_SHELL:
1✔
1549
                        arg_copy_root_shell = true;
1✔
1550
                        break;
1✔
1551

1552
                case ARG_FORCE:
3✔
1553
                        arg_force = true;
3✔
1554
                        break;
3✔
1555

1556
                case ARG_DELETE_ROOT_PASSWORD:
1✔
1557
                        arg_delete_root_password = true;
1✔
1558
                        break;
1✔
1559

1560
                case ARG_WELCOME:
1✔
1561
                        r = parse_boolean(optarg);
1✔
1562
                        if (r < 0)
1✔
1563
                                return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
×
1564

1565
                        arg_welcome = r;
1✔
1566
                        break;
1✔
1567

1568
                case ARG_CHROME:
×
1569
                        r = parse_boolean_argument("--chrome=", optarg, &arg_chrome);
×
1570
                        if (r < 0)
×
1571
                                return r;
1572

1573
                        break;
1574

1575
                case ARG_RESET:
1✔
1576
                        arg_reset = true;
1✔
1577
                        break;
1✔
1578

1579
                case ARG_MUTE_CONSOLE:
103✔
1580
                        r = parse_boolean_argument("--mute-console=", optarg, &arg_mute_console);
103✔
1581
                        if (r < 0)
103✔
1582
                                return r;
1583

1584
                        break;
1585

1586
                case '?':
1587
                        return -EINVAL;
1588

1589
                default:
×
1590
                        assert_not_reached();
×
1591
                }
1592

1593
        if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
139✔
1594
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1595
                                       "--delete-root-password cannot be combined with other root password options.");
1596

1597
        if (arg_image && arg_root)
139✔
1598
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1599
                                       "--root= and --image= cannot be used together.");
1600

1601
        if (!sd_id128_is_null(arg_machine_id) && !(arg_image || arg_root) && !arg_force)
274✔
1602
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1603
                                       "--machine-id=/--setup-machine-id only works with --root= or --image=.");
1604

1605
        return 1;
1606
}
1607

1608
static int reload_system_manager(sd_bus **bus) {
×
1609
        int r;
×
1610

1611
        assert(bus);
×
1612

1613
        if (!*bus) {
×
1614
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1615
                if (r < 0)
×
1616
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1617
        }
1618

1619
        r = bus_service_manager_reload(*bus);
×
1620
        if (r < 0)
×
1621
                return r;
1622

1623
        log_info("Requested manager reload to apply locale configuration.");
×
1624
        return 0;
1625
}
1626

1627
static int reload_vconsole(sd_bus **bus) {
×
1628
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1629
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1630
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
1631
        const char *object;
×
1632
        int r;
×
1633

1634
        assert(bus);
×
1635

1636
        if (!*bus) {
×
1637
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1638
                if (r < 0)
×
1639
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1640
        }
1641

1642
        r = bus_wait_for_jobs_new(*bus, &w);
×
1643
        if (r < 0)
×
1644
                return log_error_errno(r, "Could not watch jobs: %m");
×
1645

1646
        r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
×
1647
                            "ss", "systemd-vconsole-setup.service", "replace");
1648
        if (r < 0)
×
1649
                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
×
1650

1651
        r = sd_bus_message_read(reply, "o", &object);
×
1652
        if (r < 0)
×
1653
                return bus_log_parse_error(r);
×
1654

1655
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
×
1656
        if (r < 0)
×
1657
                return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
×
1658
        return 0;
1659
}
1660

1661
static int run(int argc, char *argv[]) {
141✔
1662
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
141✔
1663
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
141✔
1664
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1665
        _cleanup_close_ int rfd = -EBADF;
141✔
1666
        int r;
141✔
1667

1668
        r = parse_argv(argc, argv);
141✔
1669
        if (r <= 0)
141✔
1670
                return r;
1671

1672
        log_setup();
139✔
1673

1674
        umask(0022);
139✔
1675

1676
        bool offline = arg_root || arg_image;
139✔
1677

1678
        if (!offline) {
103✔
1679
                /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1680
                 * command line option, because we are called to provision the host with basic settings (as
1681
                 * opposed to some other file system tree/image) */
1682

1683
                bool enabled;
103✔
1684
                r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled);
103✔
1685
                if (r < 0)
103✔
1686
                        return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
×
1687
                if (r > 0 && !enabled) {
103✔
1688
                        log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
103✔
1689
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_keymap_auto = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
103✔
1690
                }
1691
        }
1692

1693
        r = mac_init();
139✔
1694
        if (r < 0)
139✔
1695
                return r;
1696

1697
        if (arg_image) {
139✔
1698
                assert(!arg_root);
×
1699

1700
                r = mount_image_privately_interactively(
×
1701
                                arg_image,
1702
                                arg_image_policy,
1703
                                DISSECT_IMAGE_GENERIC_ROOT |
1704
                                DISSECT_IMAGE_REQUIRE_ROOT |
1705
                                DISSECT_IMAGE_VALIDATE_OS |
1706
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1707
                                DISSECT_IMAGE_FSCK |
1708
                                DISSECT_IMAGE_GROWFS |
1709
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1710
                                &mounted_dir,
1711
                                &rfd,
1712
                                &loop_device);
1713
                if (r < 0)
×
1714
                        return r;
1715

1716
                arg_root = strdup(mounted_dir);
×
1717
                if (!arg_root)
×
1718
                        return log_oom();
×
1719
        } else {
1720
                rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
139✔
1721
                if (rfd < 0)
139✔
1722
                        return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
×
1723
        }
1724

1725
        LOG_SET_PREFIX(arg_image ?: arg_root);
280✔
1726
        DEFER_VOID_CALL(chrome_hide);
×
1727

1728
        /* We check these conditions here instead of in parse_argv() so that we can take the root directory
1729
         * into account. */
1730

1731
        if (arg_keymap && !keymap_is_ok(arg_keymap, FD_TO_PTR(rfd)))
139✔
1732
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
×
1733
        if (arg_locale && !locale_is_ok(arg_locale, FD_TO_PTR(rfd)))
139✔
1734
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
×
1735
        if (arg_locale_messages && !locale_is_ok(arg_locale_messages, FD_TO_PTR(rfd)))
139✔
1736
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
×
1737

1738
        if (arg_root_shell) {
139✔
1739
                r = find_shell(rfd, arg_root_shell);
5✔
1740
                if (r < 0)
5✔
1741
                        return r;
1742
        }
1743

1744
        r = process_reset(rfd);
138✔
1745
        if (r < 0)
138✔
1746
                return r;
1747

1748
        _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
139✔
1749
        r = process_locale(rfd, &mute_console_link);
138✔
1750
        if (r < 0)
138✔
1751
                return r;
1752
        if (r > 0 && !offline)
138✔
1753
                (void) reload_system_manager(&bus);
×
1754

1755
        r = process_keymap(rfd, &mute_console_link);
138✔
1756
        if (r < 0)
138✔
1757
                return r;
1758
        if (r > 0 && !offline)
138✔
1759
                (void) reload_vconsole(&bus);
×
1760

1761
        r = process_timezone(rfd, &mute_console_link);
138✔
1762
        if (r < 0)
138✔
1763
                return r;
1764

1765
        r = process_hostname(rfd, &mute_console_link);
138✔
1766
        if (r < 0)
138✔
1767
                return r;
1768

1769
        r = process_root_account(rfd, &mute_console_link);
138✔
1770
        if (r < 0)
138✔
1771
                return r;
1772

1773
        r = process_kernel_cmdline(rfd);
138✔
1774
        if (r < 0)
138✔
1775
                return r;
1776

1777
        r = process_machine_id(rfd);
138✔
1778
        if (r < 0)
138✔
1779
                return r;
×
1780

1781
        return 0;
1782
}
1783

1784
DEFINE_MAIN_FUNCTION(run);
141✔
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