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

systemd / systemd / 18052125394

26 Sep 2025 11:00PM UTC coverage: 72.224% (+0.02%) from 72.205%
18052125394

push

github

YHNdnzj
pam_systemd: correct alignment

Follow-up for cf2630aca

303350 of 420010 relevant lines covered (72.22%)

1058085.05 hits per line

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

77.7
/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.h"
33
#include "label-util.h"
34
#include "libcrypt-util.h"
35
#include "locale-setup.h"
36
#include "locale-util.h"
37
#include "lock-util.h"
38
#include "loop-util.h"
39
#include "main-func.h"
40
#include "memory-util.h"
41
#include "mount-util.h"
42
#include "os-util.h"
43
#include "parse-argument.h"
44
#include "parse-util.h"
45
#include "password-quality-util.h"
46
#include "path-util.h"
47
#include "pretty-print.h"
48
#include "proc-cmdline.h"
49
#include "prompt-util.h"
50
#include "runtime-scope.h"
51
#include "smack-util.h"
52
#include "stat-util.h"
53
#include "string-util.h"
54
#include "strv.h"
55
#include "terminal-util.h"
56
#include "time-util.h"
57
#include "tmpfile-util-label.h"
58
#include "user-util.h"
59
#include "vconsole-util.h"
60

61
static char *arg_root = NULL;
62
static char *arg_image = NULL;
63
static char *arg_locale = NULL;  /* $LANG */
64
static char *arg_locale_messages = NULL; /* $LC_MESSAGES */
65
static char *arg_keymap = NULL;
66
static char *arg_timezone = NULL;
67
static char *arg_hostname = NULL;
68
static sd_id128_t arg_machine_id = {};
69
static char *arg_root_password = NULL;
70
static char *arg_root_shell = NULL;
71
static char *arg_kernel_cmdline = NULL;
72
static bool arg_prompt_locale = false;
73
static bool arg_prompt_keymap = 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 your new installation of " ANSI_NORMAL "\x1B[%sm%s" ANSI_HIGHLIGHT "!" ANSI_NORMAL "\n", ac, pn);
×
142
        else
143
                printf("Welcome to your new installation of %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 your new system!\n");
6✔
151

152
        any_key_to_proceed();
6✔
153

154
        done = true;
6✔
155
}
156

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

161
        assert(dir_fd >= 0);
1,104✔
162
        assert(filename);
1,104✔
163

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

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

177
        if (!passwd)
613✔
178
                return false;
179

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

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

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

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

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

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

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

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

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

241
        assert(rfd >= 0);
11✔
242

243
        if (arg_locale || arg_locale_messages)
11✔
244
                return 0;
245

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

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

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

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

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

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

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

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

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

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

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

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

327
        return 0;
328
}
329

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

337
        assert(rfd >= 0);
138✔
338

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

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

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

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

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

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

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

375
        if (i == 0)
11✔
376
                return 0;
377

378
        locales[i] = NULL;
7✔
379

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

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

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

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

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

403
static int prompt_keymap(int rfd, sd_varlink **mute_console_link) {
13✔
404
        _cleanup_strv_free_ char **kmaps = NULL;
13✔
405
        int r;
13✔
406

407
        assert(rfd >= 0);
13✔
408

409
        if (arg_keymap)
13✔
410
                return 0;
411

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

420
        if (!arg_prompt_keymap) {
9✔
421
                log_debug("Prompting for keymap was not requested.");
8✔
422
                return 0;
8✔
423
        }
424

425
        r = get_keymaps(&kmaps);
1✔
426
        if (r == -ENOENT) /* no keymaps installed */
1✔
427
                return log_debug_errno(r, "No keymaps are installed.");
×
428
        if (r < 0)
1✔
429
                return log_error_errno(r, "Failed to read keymaps: %m");
×
430

431
        print_welcome(rfd, mute_console_link);
1✔
432

433
        return prompt_loop(
1✔
434
                        "Please enter the new keymap name or number",
435
                        GLYPH_KEYBOARD,
436
                        kmaps,
437
                        /* accepted= */ NULL,
438
                        /* ellipsize_percentage= */ 60,
439
                        /* n_columns= */ 3,
440
                        /* column_width= */ 20,
441
                        keymap_is_ok,
442
                        /* refresh= */ NULL,
443
                        FD_TO_PTR(rfd),
1✔
444
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
445
                        &arg_keymap);
446
}
447

448
static int process_keymap(int rfd, sd_varlink **mute_console_link) {
138✔
449
        _cleanup_close_ int pfd = -EBADF;
138✔
450
        _cleanup_free_ char *f = NULL;
138✔
451
        _cleanup_strv_free_ char **keymap = NULL;
×
452
        int r;
138✔
453

454
        assert(rfd >= 0);
138✔
455

456
        pfd = chase_and_open_parent_at(rfd, etc_vconsole_conf(),
138✔
457
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
458
                                       &f);
459
        if (pfd < 0)
138✔
460
                return log_error_errno(pfd, "Failed to chase /etc/vconsole.conf: %m");
×
461

462
        r = should_configure(pfd, f);
138✔
463
        if (r == 0)
138✔
464
                log_debug("Found /etc/vconsole.conf, assuming console has been configured.");
122✔
465
        if (r <= 0)
138✔
466
                return r;
467

468
        r = dir_fd_is_root(rfd);
16✔
469
        if (r < 0)
16✔
470
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
471

472
        if (arg_copy_keymap && r == 0) {
16✔
473
                r = copy_file_atomic_at(AT_FDCWD, etc_vconsole_conf(), pfd, f, 0644, COPY_REFLINK);
3✔
474
                if (r != -ENOENT) {
3✔
475
                        if (r < 0)
3✔
476
                                return log_error_errno(r, "Failed to copy host's /etc/vconsole.conf: %m");
×
477

478
                        log_info("Copied host's /etc/vconsole.conf.");
3✔
479
                        return 0;
3✔
480
                }
481
        }
482

483
        r = prompt_keymap(rfd, mute_console_link);
13✔
484
        if (r == -ENOENT)
13✔
485
                return 0; /* don't fail if no keymaps are installed */
486
        if (r < 0)
13✔
487
                return r;
488

489
        if (isempty(arg_keymap))
143✔
490
                return 0;
491

492
        VCContext vc = {
5✔
493
                .keymap = arg_keymap,
494
        };
495
        _cleanup_(x11_context_clear) X11Context xc = {};
5✔
496

497
        r = vconsole_convert_to_x11(&vc, /* verify= */ NULL, &xc);
5✔
498
        if (r < 0)
5✔
499
                return log_error_errno(r, "Failed to convert keymap data: %m");
×
500

501
        r = vconsole_serialize(&vc, &xc, &keymap);
5✔
502
        if (r < 0)
5✔
503
                return log_error_errno(r, "Failed to serialize keymap data: %m");
×
504

505
        r = write_vconsole_conf(pfd, f, keymap);
5✔
506
        if (r < 0)
5✔
507
                return log_error_errno(r, "Failed to write /etc/vconsole.conf: %m");
×
508

509
        log_info("/etc/vconsole.conf written.");
5✔
510
        return 1;
511
}
512

513
static int timezone_is_ok(const char *name, void *userdata) {
1✔
514
        return timezone_is_valid(name, LOG_DEBUG);
1✔
515
}
516

517
static int prompt_timezone(int rfd, sd_varlink **mute_console_link) {
119✔
518
        _cleanup_strv_free_ char **zones = NULL;
119✔
519
        int r;
119✔
520

521
        assert(rfd >= 0);
119✔
522

523
        if (arg_timezone)
119✔
524
                return 0;
525

526
        r = read_credential("firstboot.timezone", (void**) &arg_timezone, NULL);
116✔
527
        if (r < 0)
116✔
528
                log_debug_errno(r, "Failed to read credential firstboot.timezone, ignoring: %m");
11✔
529
        else {
530
                log_debug("Acquired timezone from credential.");
105✔
531
                return 0;
105✔
532
        }
533

534
        if (!arg_prompt_timezone) {
11✔
535
                log_debug("Prompting for timezone was not requested.");
10✔
536
                return 0;
10✔
537
        }
538

539
        r = get_timezones(&zones);
1✔
540
        if (r < 0)
1✔
541
                return log_error_errno(r, "Cannot query timezone list: %m");
×
542

543
        print_welcome(rfd, mute_console_link);
1✔
544

545
        return prompt_loop(
1✔
546
                        "Please enter the new timezone name or number",
547
                        GLYPH_CLOCK,
548
                        zones,
549
                        /* accepted= */ NULL,
550
                        /* ellipsize_percentage= */ 30,
551
                        /* n_columns= */ 3,
552
                        /* column_width= */ 20,
553
                        timezone_is_ok,
554
                        /* refresh= */ NULL,
555
                        FD_TO_PTR(rfd),
1✔
556
                        PROMPT_MAY_SKIP|PROMPT_SHOW_MENU,
557
                        &arg_timezone);
558
}
559

560
static int process_timezone(int rfd, sd_varlink **mute_console_link) {
138✔
561
        _cleanup_close_ int pfd = -EBADF;
138✔
562
        _cleanup_free_ char *f = NULL, *relpath = NULL;
138✔
563
        const char *e;
138✔
564
        int r;
138✔
565

566
        assert(rfd >= 0);
138✔
567

568
        pfd = chase_and_open_parent_at(rfd, etc_localtime(),
138✔
569
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
570
                                       &f);
571
        if (pfd < 0)
138✔
572
                return log_error_errno(pfd, "Failed to chase /etc/localtime: %m");
×
573

574
        r = should_configure(pfd, f);
138✔
575
        if (r == 0)
138✔
576
                log_debug("Found /etc/localtime, assuming timezone has been configured.");
16✔
577
        if (r <= 0)
138✔
578
                return r;
579

580
        r = dir_fd_is_root(rfd);
122✔
581
        if (r < 0)
122✔
582
                return log_error_errno(r, "Failed to check if directory file descriptor is root: %m");
×
583

584
        if (arg_copy_timezone && r == 0) {
122✔
585
                _cleanup_free_ char *s = NULL;
3✔
586

587
                r = readlink_malloc(etc_localtime(), &s);
3✔
588
                if (r != -ENOENT) {
3✔
589
                        if (r < 0)
3✔
590
                                return log_error_errno(r, "Failed to read host's /etc/localtime: %m");
×
591

592
                        r = symlinkat_atomic_full(s, pfd, f, SYMLINK_LABEL);
3✔
593
                        if (r < 0)
3✔
594
                                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
595

596
                        log_info("Copied host's /etc/localtime.");
3✔
597
                        return 0;
3✔
598
                }
599
        }
600

601
        r = prompt_timezone(rfd, mute_console_link);
119✔
602
        if (r < 0)
119✔
603
                return r;
604

605
        if (isempty(arg_timezone))
247✔
606
                return 0;
607

608
        e = strjoina("/usr/share/zoneinfo/", arg_timezone);
545✔
609
        r = path_make_relative_parent(etc_localtime(), e, &relpath);
109✔
610
        if (r < 0)
109✔
611
                return r;
612

613
        r = symlinkat_atomic_full(relpath, pfd, f, SYMLINK_LABEL);
109✔
614
        if (r < 0)
109✔
615
                return log_error_errno(r, "Failed to create /etc/localtime symlink: %m");
×
616

617
        log_info("/etc/localtime written");
109✔
618
        return 0;
619
}
620

621
static int hostname_is_ok(const char *name, void *userdata) {
1✔
622
        return hostname_is_valid(name, VALID_HOSTNAME_TRAILING_DOT);
1✔
623
}
624

625
static int prompt_hostname(int rfd, sd_varlink **mute_console_link) {
124✔
626
        int r;
124✔
627

628
        assert(rfd >= 0);
124✔
629

630
        if (arg_hostname)
124✔
631
                return 0;
632

633
        if (!arg_prompt_hostname) {
120✔
634
                log_debug("Prompting for hostname was not requested.");
119✔
635
                return 0;
119✔
636
        }
637

638
        print_welcome(rfd, mute_console_link);
1✔
639

640
        r = prompt_loop("Please enter the new hostname",
2✔
641
                        GLYPH_LABEL,
642
                        /* menu= */ NULL,
643
                        /* accepted= */ NULL,
644
                        /* ellipsize_percentage= */ 100,
645
                        /* n_columns= */ 3,
646
                        /* column_width= */ 20,
647
                        hostname_is_ok,
648
                        /* refresh= */ NULL,
649
                        FD_TO_PTR(rfd),
1✔
650
                        PROMPT_MAY_SKIP,
651
                        &arg_hostname);
652
        if (r < 0)
1✔
653
                return r;
654

655
        if (arg_hostname)
1✔
656
                hostname_cleanup(arg_hostname);
1✔
657

658
        return 0;
659
}
660

661
static int process_hostname(int rfd, sd_varlink **mute_console_link) {
138✔
662
        _cleanup_close_ int pfd = -EBADF;
138✔
663
        _cleanup_free_ char *f = NULL;
138✔
664
        int r;
138✔
665

666
        assert(rfd >= 0);
138✔
667

668
        pfd = chase_and_open_parent_at(rfd, etc_hostname(),
138✔
669
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN,
670
                                       &f);
671
        if (pfd < 0)
138✔
672
                return log_error_errno(pfd, "Failed to chase /etc/hostname: %m");
×
673

674
        r = should_configure(pfd, f);
138✔
675
        if (r == 0)
138✔
676
                log_debug("Found /etc/hostname, assuming hostname has been configured.");
14✔
677
        if (r <= 0)
138✔
678
                return r;
679

680
        r = prompt_hostname(rfd, mute_console_link);
124✔
681
        if (r < 0)
124✔
682
                return r;
683

684
        if (isempty(arg_hostname))
143✔
685
                return 0;
686

687
        r = write_string_file_at(pfd, f, arg_hostname,
5✔
688
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
689
        if (r < 0)
5✔
690
                return log_error_errno(r, "Failed to write /etc/hostname: %m");
×
691

692
        log_info("/etc/hostname written.");
5✔
693
        return 0;
694
}
695

696
static int process_machine_id(int rfd) {
138✔
697
        _cleanup_close_ int pfd = -EBADF;
138✔
698
        _cleanup_free_ char *f = NULL;
138✔
699
        int r;
138✔
700

701
        assert(rfd >= 0);
138✔
702

703
        pfd = chase_and_open_parent_at(rfd, "/etc/machine-id",
138✔
704
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
705
                                       &f);
706
        if (pfd < 0)
138✔
707
                return log_error_errno(pfd, "Failed to chase /etc/machine-id: %m");
×
708

709
        r = should_configure(pfd, f);
138✔
710
        if (r == 0)
138✔
711
                log_debug("Found /etc/machine-id, assuming machine-id has been configured.");
114✔
712
        if (r <= 0)
138✔
713
                return r;
714

715
        if (sd_id128_is_null(arg_machine_id)) {
24✔
716
                log_debug("Initialization of machine-id was not requested, skipping.");
21✔
717
                return 0;
21✔
718
        }
719

720
        r = write_string_file_at(pfd, "machine-id", SD_ID128_TO_STRING(arg_machine_id),
3✔
721
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
722
        if (r < 0)
3✔
723
                return log_error_errno(r, "Failed to write /etc/machine-id: %m");
×
724

725
        log_info("/etc/machine-id written.");
3✔
726
        return 0;
727
}
728

729
static int prompt_root_password(int rfd, sd_varlink **mute_console_link) {
29✔
730
        const char *msg1, *msg2;
29✔
731
        int r;
29✔
732

733
        assert(rfd >= 0);
29✔
734

735
        if (arg_root_password)
29✔
736
                return 0;
737

738
        if (get_credential_user_password("root", &arg_root_password, &arg_root_password_is_hashed) >= 0)
20✔
739
                return 0;
740

741
        if (!arg_prompt_root_password) {
19✔
742
                log_debug("Prompting for root password was not requested.");
18✔
743
                return 0;
18✔
744
        }
745

746
        print_welcome(rfd, mute_console_link);
1✔
747

748
        msg1 = "Please enter the new root password (empty to skip):";
1✔
749
        msg2 = "Please enter the new root password again:";
1✔
750

751
        suggest_passwords();
1✔
752

753
        for (;;) {
×
754
                _cleanup_strv_free_erase_ char **a = NULL, **b = NULL;
1✔
755
                _cleanup_free_ char *error = NULL;
1✔
756

757
                AskPasswordRequest req = {
1✔
758
                        .tty_fd = -EBADF,
759
                        .message = msg1,
760
                        .until = USEC_INFINITY,
761
                        .hup_fd = -EBADF,
762
                };
763

764
                r = ask_password_tty(&req, /* flags= */ 0, &a);
1✔
765
                if (r < 0)
1✔
766
                        return log_error_errno(r, "Failed to query root password: %m");
×
767
                if (strv_length(a) != 1)
1✔
768
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
769
                                               "Received multiple passwords, where we expected one.");
770

771
                if (isempty(*a)) {
1✔
772
                        log_info("No password entered, skipping.");
1✔
773
                        break;
774
                }
775

776
                r = check_password_quality(*a, /* old = */ NULL, "root", &error);
×
777
                if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
×
778
                        log_warning("Password quality check is not supported, proceeding anyway.");
×
779
                else if (r < 0)
×
780
                        return log_error_errno(r, "Failed to check password quality: %m");
×
781
                else if (r == 0)
×
782
                        log_warning("Password is weak, accepting anyway: %s", error);
×
783

784
                req.message = msg2;
×
785

786
                r = ask_password_tty(&req, /* flags= */ 0, &b);
×
787
                if (r < 0)
×
788
                        return log_error_errno(r, "Failed to query root password: %m");
×
789
                if (strv_length(b) != 1)
×
790
                        return log_error_errno(SYNTHETIC_ERRNO(EIO),
×
791
                                               "Received multiple passwords, where we expected one.");
792

793
                if (!streq(*a, *b)) {
×
794
                        log_error("Entered passwords did not match, please try again.");
×
795
                        continue;
×
796
                }
797

798
                arg_root_password = TAKE_PTR(*a);
×
799
                break;
×
800
        }
801

802
        return 0;
1✔
803
}
804

805
static int find_shell(int rfd, const char *path) {
7✔
806
        int r;
7✔
807

808
        assert(path);
7✔
809

810
        if (!valid_shell(path))
7✔
811
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "%s is not a valid shell", path);
×
812

813
        r = chaseat(rfd, path, CHASE_AT_RESOLVE_IN_ROOT, NULL, NULL);
7✔
814
        if (r < 0)
7✔
815
                return log_error_errno(r, "Failed to resolve shell %s: %m", path);
1✔
816

817
        return 0;
818
}
819

820
static int shell_is_ok(const char *path, void *userdata) {
2✔
821
        int rfd = ASSERT_FD(PTR_TO_FD(userdata));
2✔
822

823
        return find_shell(rfd, path) >= 0;
2✔
824
}
825

826
static int prompt_root_shell(int rfd, sd_varlink **mute_console_link) {
29✔
827
        int r;
29✔
828

829
        assert(rfd >= 0);
29✔
830

831
        if (arg_root_shell)
29✔
832
                return 0;
833

834
        r = read_credential("passwd.shell.root", (void**) &arg_root_shell, NULL);
24✔
835
        if (r < 0)
24✔
836
                log_debug_errno(r, "Failed to read credential passwd.shell.root, ignoring: %m");
23✔
837
        else {
838
                log_debug("Acquired root shell from credential.");
1✔
839
                return 0;
1✔
840
        }
841

842
        if (!arg_prompt_root_shell) {
23✔
843
                log_debug("Prompting for root shell was not requested.");
21✔
844
                return 0;
21✔
845
        }
846

847
        print_welcome(rfd, mute_console_link);
2✔
848

849
        return prompt_loop(
2✔
850
                        "Please enter the new root shell",
851
                        GLYPH_SHELL,
852
                        /* menu= */ NULL,
853
                        /* accepted= */ NULL,
854
                        /* ellipsize_percentage= */ 0,
855
                        /* n_columns= */ 3,
856
                        /* column_width= */ 20,
857
                        shell_is_ok,
858
                        /* refresh= */ NULL,
859
                        FD_TO_PTR(rfd),
2✔
860
                        PROMPT_MAY_SKIP,
861
                        &arg_root_shell);
862
}
863

864
static int write_root_passwd(int rfd, int etc_fd, const char *password, const char *shell) {
15✔
865
        _cleanup_fclose_ FILE *original = NULL, *passwd = NULL;
15✔
866
        _cleanup_(unlink_and_freep) char *passwd_tmp = NULL;
15✔
867
        int r;
15✔
868
        bool found = false;
15✔
869

870
        r = fopen_temporary_at_label(etc_fd, "passwd", "passwd", &passwd, &passwd_tmp);
15✔
871
        if (r < 0)
15✔
872
                return r;
873

874
        r = xfopenat(etc_fd, "passwd", "re", O_NOFOLLOW, &original);
15✔
875
        if (r < 0 && r != -ENOENT)
15✔
876
                return r;
877

878
        if (original) {
15✔
879
                struct passwd *i;
4✔
880

881
                r = copy_rights(fileno(original), fileno(passwd));
4✔
882
                if (r < 0)
4✔
883
                        return r;
×
884

885
                while ((r = fgetpwent_sane(original, &i)) > 0) {
7✔
886

887
                        if (streq(i->pw_name, "root")) {
3✔
888
                                if (password)
3✔
889
                                        i->pw_passwd = (char *) password;
2✔
890
                                if (shell)
3✔
891
                                        i->pw_shell = (char *) shell;
2✔
892
                                found = true;
893
                        }
894

895
                        r = putpwent_sane(i, passwd);
3✔
896
                        if (r < 0)
3✔
897
                                return r;
898
                }
899
                if (r < 0)
4✔
900
                        return r;
901

902
        } else {
903
                r = fchmod(fileno(passwd), 0644);
11✔
904
                if (r < 0)
11✔
905
                        return -errno;
×
906
        }
907

908
        if (!found) {
4✔
909
                struct passwd root = {
12✔
910
                        .pw_name = (char *) "root",
911
                        .pw_passwd = (char *) (password ?: PASSWORD_SEE_SHADOW),
12✔
912
                        .pw_uid = 0,
913
                        .pw_gid = 0,
914
                        .pw_gecos = (char *) "Super User",
915
                        .pw_dir = (char *) "/root",
916
                        .pw_shell = (char *) (shell ?: default_root_shell_at(rfd)),
6✔
917
                };
918

919
                if (errno != ENOENT)
12✔
920
                        return -errno;
×
921

922
                r = putpwent_sane(&root, passwd);
12✔
923
                if (r < 0)
12✔
924
                        return r;
925
        }
926

927
        r = fflush_sync_and_check(passwd);
15✔
928
        if (r < 0)
15✔
929
                return r;
930

931
        r = renameat_and_apply_smack_floor_label(etc_fd, passwd_tmp, etc_fd, "passwd");
15✔
932
        if (r < 0)
15✔
933
                return r;
×
934

935
        return 0;
936
}
937

938
static int write_root_shadow(int etc_fd, const char *hashed_password) {
15✔
939
        _cleanup_fclose_ FILE *original = NULL, *shadow = NULL;
15✔
940
        _cleanup_(unlink_and_freep) char *shadow_tmp = NULL;
15✔
941
        int r;
15✔
942
        bool found = false;
15✔
943

944
        r = fopen_temporary_at_label(etc_fd, "shadow", "shadow", &shadow, &shadow_tmp);
15✔
945
        if (r < 0)
15✔
946
                return r;
947

948
        r = xfopenat(etc_fd, "shadow", "re", O_NOFOLLOW, &original);
15✔
949
        if (r < 0 && r != -ENOENT)
15✔
950
                return r;
951

952
        if (original) {
15✔
953
                struct spwd *i;
4✔
954

955
                r = copy_rights(fileno(original), fileno(shadow));
4✔
956
                if (r < 0)
4✔
957
                        return r;
×
958

959
                while ((r = fgetspent_sane(original, &i)) > 0) {
7✔
960

961
                        if (streq(i->sp_namp, "root")) {
3✔
962
                                if (hashed_password) {
3✔
963
                                        i->sp_pwdp = (char *) hashed_password;
2✔
964
                                        i->sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY);
2✔
965
                                }
966
                                found = true;
967
                        }
968

969
                        r = putspent_sane(i, shadow);
3✔
970
                        if (r < 0)
3✔
971
                                return r;
972
                }
973
                if (r < 0)
4✔
974
                        return r;
975

976
        } else {
977
                r = fchmod(fileno(shadow), 0000);
11✔
978
                if (r < 0)
11✔
979
                        return -errno;
×
980
        }
981

982
        if (!found) {
4✔
983
                struct spwd root = {
36✔
984
                        .sp_namp = (char*) "root",
985
                        .sp_pwdp = (char *) (hashed_password ?: PASSWORD_LOCKED_AND_INVALID),
12✔
986
                        .sp_lstchg = (long) (now(CLOCK_REALTIME) / USEC_PER_DAY),
12✔
987
                        .sp_min = -1,
988
                        .sp_max = -1,
989
                        .sp_warn = -1,
990
                        .sp_inact = -1,
991
                        .sp_expire = -1,
992
                        .sp_flag = ULONG_MAX, /* this appears to be what everybody does ... */
993
                };
994

995
                if (errno != ENOENT)
12✔
996
                        return -errno;
×
997

998
                r = putspent_sane(&root, shadow);
12✔
999
                if (r < 0)
12✔
1000
                        return r;
1001
        }
1002

1003
        r = fflush_sync_and_check(shadow);
15✔
1004
        if (r < 0)
15✔
1005
                return r;
1006

1007
        r = renameat_and_apply_smack_floor_label(etc_fd, shadow_tmp, etc_fd, "shadow");
15✔
1008
        if (r < 0)
15✔
1009
                return r;
×
1010

1011
        return 0;
1012
}
1013

1014
static int process_root_account(int rfd, sd_varlink **mute_console_link) {
138✔
1015
        _cleanup_close_ int pfd = -EBADF;
138✔
1016
        _cleanup_(release_lock_file) LockFile lock = LOCK_FILE_INIT;
×
1017
        _cleanup_(erase_and_freep) char *_hashed_password = NULL;
138✔
1018
        const char *password, *hashed_password;
138✔
1019
        int k = 0, r;
138✔
1020

1021
        assert(rfd >= 0);
138✔
1022

1023
        pfd = chase_and_open_parent_at(rfd, "/etc/passwd",
138✔
1024
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1025
                                       NULL);
1026
        if (pfd < 0)
138✔
1027
                return log_error_errno(pfd, "Failed to chase /etc/passwd: %m");
×
1028

1029
        /* Ensure that passwd and shadow are in the same directory and are not symlinks. */
1030

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

1036
                r = should_configure(pfd, s);
276✔
1037
                if (r < 0)
276✔
1038
                        return r;
1039

1040
                k += r;
276✔
1041
        }
1042

1043
        if (k == 0) {
138✔
1044
                log_debug("Found /etc/passwd and /etc/shadow, assuming root account has been initialized.");
109✔
1045
                return 0;
109✔
1046
        }
1047

1048
        r = make_lock_file_at(pfd, ETC_PASSWD_LOCK_FILENAME, LOCK_EX, &lock);
29✔
1049
        if (r < 0)
29✔
1050
                return log_error_errno(r, "Failed to take a lock on /etc/passwd: %m");
×
1051

1052
        k = dir_fd_is_root(rfd);
29✔
1053
        if (k < 0)
29✔
1054
                return log_error_errno(k, "Failed to check if directory file descriptor is root: %m");
×
1055

1056
        if (arg_copy_root_shell && k == 0) {
29✔
1057
                _cleanup_free_ struct passwd *p = NULL;
2✔
1058

1059
                r = getpwnam_malloc("root", &p);
2✔
1060
                if (r < 0)
2✔
1061
                        return log_error_errno(r, "Failed to find passwd entry for root: %m");
×
1062

1063
                r = free_and_strdup(&arg_root_shell, p->pw_shell);
2✔
1064
                if (r < 0)
2✔
1065
                        return log_oom();
×
1066
        }
1067

1068
        r = prompt_root_shell(rfd, mute_console_link);
29✔
1069
        if (r < 0)
29✔
1070
                return r;
1071

1072
        if (arg_copy_root_password && k == 0) {
29✔
1073
                struct spwd *p;
2✔
1074

1075
                errno = 0;
2✔
1076
                p = getspnam("root");
2✔
1077
                if (!p)
2✔
1078
                        return log_error_errno(errno_or_else(EIO), "Failed to find shadow entry for root: %m");
×
1079

1080
                r = free_and_strdup(&arg_root_password, p->sp_pwdp);
2✔
1081
                if (r < 0)
2✔
1082
                        return log_oom();
×
1083

1084
                arg_root_password_is_hashed = true;
2✔
1085
        }
1086

1087
        r = prompt_root_password(rfd, mute_console_link);
29✔
1088
        if (r < 0)
29✔
1089
                return r;
1090

1091
        if (arg_root_password && arg_root_password_is_hashed) {
29✔
1092
                password = PASSWORD_SEE_SHADOW;
1093
                hashed_password = arg_root_password;
1094
        } else if (arg_root_password) {
23✔
1095
                r = hash_password(arg_root_password, &_hashed_password);
4✔
1096
                if (r < 0)
4✔
1097
                        return log_error_errno(r, "Failed to hash password: %m");
×
1098

1099
                password = PASSWORD_SEE_SHADOW;
4✔
1100
                hashed_password = _hashed_password;
4✔
1101

1102
        } else if (arg_delete_root_password) {
19✔
1103
                password = PASSWORD_SEE_SHADOW;
1104
                hashed_password = PASSWORD_NONE;
1105
        } else if (!arg_root_password && arg_prompt_root_password) {
18✔
1106
                /* If the user was prompted, but no password was supplied, lock the account. */
1107
                password = PASSWORD_SEE_SHADOW;
1108
                hashed_password = PASSWORD_LOCKED_AND_INVALID;
1109
        } else
1110
                /* Leave the password as is. */
1111
                password = hashed_password = NULL;
17✔
1112

1113
        /* Don't create/modify passwd and shadow if there's nothing to do. */
1114
        if (!(password || hashed_password || arg_root_shell)) {
29✔
1115
                log_debug("Initialization of root account was not requested, skipping.");
14✔
1116
                return 0;
14✔
1117
        }
1118

1119
        r = write_root_passwd(rfd, pfd, password, arg_root_shell);
15✔
1120
        if (r < 0)
15✔
1121
                return log_error_errno(r, "Failed to write /etc/passwd: %m");
×
1122

1123
        log_info("/etc/passwd written.");
15✔
1124

1125
        r = write_root_shadow(pfd, hashed_password);
15✔
1126
        if (r < 0)
15✔
1127
                return log_error_errno(r, "Failed to write /etc/shadow: %m");
×
1128

1129
        log_info("/etc/shadow written.");
15✔
1130
        return 0;
1131
}
1132

1133
static int process_kernel_cmdline(int rfd) {
138✔
1134
        _cleanup_close_ int pfd = -EBADF;
138✔
1135
        _cleanup_free_ char *f = NULL;
138✔
1136
        int r;
138✔
1137

1138
        assert(rfd >= 0);
138✔
1139

1140
        pfd = chase_and_open_parent_at(rfd, "/etc/kernel/cmdline",
138✔
1141
                                       CHASE_AT_RESOLVE_IN_ROOT|CHASE_MKDIR_0755|CHASE_WARN|CHASE_NOFOLLOW,
1142
                                       &f);
1143
        if (pfd < 0)
138✔
1144
                return log_error_errno(pfd, "Failed to chase /etc/kernel/cmdline: %m");
×
1145

1146
        r = should_configure(pfd, f);
138✔
1147
        if (r == 0)
138✔
1148
                log_debug("Found /etc/kernel/cmdline, assuming kernel command line has been configured.");
1✔
1149
        if (r <= 0)
138✔
1150
                return r;
1151

1152
        if (!arg_kernel_cmdline) {
137✔
1153
                log_debug("Creation of /etc/kernel/cmdline was not requested, skipping.");
135✔
1154
                return 0;
135✔
1155
        }
1156

1157
        r = write_string_file_at(pfd, "cmdline", arg_kernel_cmdline,
2✔
1158
                                 WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_SYNC|WRITE_STRING_FILE_ATOMIC|WRITE_STRING_FILE_LABEL);
1159
        if (r < 0)
2✔
1160
                return log_error_errno(r, "Failed to write /etc/kernel/cmdline: %m");
×
1161

1162
        log_info("/etc/kernel/cmdline written.");
2✔
1163
        return 0;
1164
}
1165

1166
static int reset_one(int rfd, const char *path) {
6✔
1167
        _cleanup_close_ int pfd = -EBADF;
6✔
1168
        _cleanup_free_ char *f = NULL;
6✔
1169

1170
        assert(rfd >= 0);
6✔
1171
        assert(path);
6✔
1172

1173
        pfd = chase_and_open_parent_at(rfd, path, CHASE_AT_RESOLVE_IN_ROOT|CHASE_WARN|CHASE_NOFOLLOW, &f);
6✔
1174
        if (pfd == -ENOENT)
6✔
1175
                return 0;
1176
        if (pfd < 0)
6✔
1177
                return log_error_errno(pfd, "Failed to resolve %s: %m", path);
×
1178

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

1182
        log_info("Removed %s", path);
6✔
1183
        return 0;
1184
}
1185

1186
static int process_reset(int rfd) {
138✔
1187
        int r;
138✔
1188

1189
        assert(rfd >= 0);
138✔
1190

1191
        if (!arg_reset)
138✔
1192
                return 0;
1193

1194
        FOREACH_STRING(p,
7✔
1195
                       etc_locale_conf(),
1196
                       etc_vconsole_conf(),
1197
                       etc_hostname(),
1198
                       "/etc/machine-id",
1199
                       "/etc/kernel/cmdline",
1200
                       etc_localtime()) {
1201
                r = reset_one(rfd, p);
6✔
1202
                if (r < 0)
6✔
1203
                        return r;
×
1204
        }
1205

1206
        return 0;
1✔
1207
}
1208

1209
static int help(void) {
×
1210
        _cleanup_free_ char *link = NULL;
×
1211
        int r;
×
1212

1213
        r = terminal_urlify_man("systemd-firstboot", "1", &link);
×
1214
        if (r < 0)
×
1215
                return log_oom();
×
1216

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

1264
        return 0;
1265
}
1266

1267
static int parse_argv(int argc, char *argv[]) {
141✔
1268

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

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

1347
        int r, c;
141✔
1348

1349
        assert(argc >= 0);
141✔
1350
        assert(argv);
141✔
1351

1352
        while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
769✔
1353

1354
                switch (c) {
630✔
1355

1356
                case 'h':
×
1357
                        return help();
×
1358

1359
                case ARG_VERSION:
×
1360
                        return version();
×
1361

1362
                case ARG_ROOT:
37✔
1363
                        r = parse_path_argument(optarg, true, &arg_root);
37✔
1364
                        if (r < 0)
37✔
1365
                                return r;
1366
                        break;
1367

1368
                case ARG_IMAGE:
×
1369
                        r = parse_path_argument(optarg, false, &arg_image);
×
1370
                        if (r < 0)
×
1371
                                return r;
1372
                        break;
1373

1374
                case ARG_IMAGE_POLICY:
×
1375
                        r = parse_image_policy_argument(optarg, &arg_image_policy);
×
1376
                        if (r < 0)
×
1377
                                return r;
1378
                        break;
1379

1380
                case ARG_LOCALE:
5✔
1381
                        r = free_and_strdup(&arg_locale, optarg);
5✔
1382
                        if (r < 0)
5✔
1383
                                return log_oom();
×
1384

1385
                        break;
1386

1387
                case ARG_LOCALE_MESSAGES:
4✔
1388
                        r = free_and_strdup(&arg_locale_messages, optarg);
4✔
1389
                        if (r < 0)
4✔
1390
                                return log_oom();
×
1391

1392
                        break;
1393

1394
                case ARG_KEYMAP:
4✔
1395
                        if (!keymap_is_valid(optarg))
4✔
1396
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1397
                                                       "Keymap %s is not valid.", optarg);
1398

1399
                        r = free_and_strdup(&arg_keymap, optarg);
4✔
1400
                        if (r < 0)
4✔
1401
                                return log_oom();
×
1402

1403
                        break;
1404

1405
                case ARG_TIMEZONE:
5✔
1406
                        if (!timezone_is_valid(optarg, LOG_ERR))
5✔
1407
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
1✔
1408
                                                       "Timezone %s is not valid.", optarg);
1409

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

1414
                        break;
1415

1416
                case ARG_ROOT_PASSWORD:
4✔
1417
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1418
                        if (r < 0)
4✔
1419
                                return log_oom();
×
1420

1421
                        arg_root_password_is_hashed = false;
4✔
1422
                        break;
4✔
1423

1424
                case ARG_ROOT_PASSWORD_FILE:
1✔
1425
                        arg_root_password = mfree(arg_root_password);
1✔
1426

1427
                        r = read_one_line_file(optarg, &arg_root_password);
1✔
1428
                        if (r < 0)
1✔
1429
                                return log_error_errno(r, "Failed to read %s: %m", optarg);
×
1430

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

1434
                case ARG_ROOT_PASSWORD_HASHED:
4✔
1435
                        r = free_and_strdup(&arg_root_password, optarg);
4✔
1436
                        if (r < 0)
4✔
1437
                                return log_oom();
×
1438

1439
                        arg_root_password_is_hashed = true;
4✔
1440
                        break;
4✔
1441

1442
                case ARG_ROOT_SHELL:
5✔
1443
                        r = free_and_strdup(&arg_root_shell, optarg);
5✔
1444
                        if (r < 0)
5✔
1445
                                return log_oom();
×
1446

1447
                        break;
1448

1449
                case ARG_HOSTNAME:
5✔
1450
                        if (!hostname_is_valid(optarg, VALID_HOSTNAME_TRAILING_DOT|VALID_HOSTNAME_QUESTION_MARK))
5✔
1451
                                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1452
                                                       "Host name %s is not valid.", optarg);
1453

1454
                        r = free_and_strdup(&arg_hostname, optarg);
5✔
1455
                        if (r < 0)
5✔
1456
                                return log_oom();
×
1457

1458
                        hostname_cleanup(arg_hostname);
5✔
1459
                        break;
5✔
1460

1461
                case ARG_SETUP_MACHINE_ID:
1✔
1462
                        r = sd_id128_randomize(&arg_machine_id);
1✔
1463
                        if (r < 0)
1✔
1464
                                return log_error_errno(r, "Failed to generate randomized machine ID: %m");
×
1465

1466
                        break;
1467

1468
                case ARG_MACHINE_ID:
4✔
1469
                        r = sd_id128_from_string(optarg, &arg_machine_id);
4✔
1470
                        if (r < 0)
4✔
1471
                                return log_error_errno(r, "Failed to parse machine id %s.", optarg);
1✔
1472

1473
                        break;
1474

1475
                case ARG_KERNEL_COMMAND_LINE:
3✔
1476
                        r = free_and_strdup(&arg_kernel_cmdline, optarg);
3✔
1477
                        if (r < 0)
3✔
1478
                                return log_oom();
×
1479

1480
                        break;
1481

1482
                case ARG_PROMPT:
×
1483
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname =
×
1484
                                arg_prompt_root_password = arg_prompt_root_shell = true;
×
1485
                        break;
×
1486

1487
                case ARG_PROMPT_LOCALE:
106✔
1488
                        arg_prompt_locale = true;
106✔
1489
                        break;
106✔
1490

1491
                case ARG_PROMPT_KEYMAP:
106✔
1492
                        arg_prompt_keymap = true;
106✔
1493
                        break;
106✔
1494

1495
                case ARG_PROMPT_TIMEZONE:
106✔
1496
                        arg_prompt_timezone = true;
106✔
1497
                        break;
106✔
1498

1499
                case ARG_PROMPT_HOSTNAME:
1✔
1500
                        arg_prompt_hostname = true;
1✔
1501
                        break;
1✔
1502

1503
                case ARG_PROMPT_ROOT_PASSWORD:
106✔
1504
                        arg_prompt_root_password = true;
106✔
1505
                        break;
106✔
1506

1507
                case ARG_PROMPT_ROOT_SHELL:
4✔
1508
                        arg_prompt_root_shell = true;
4✔
1509
                        break;
4✔
1510

1511
                case ARG_COPY:
1✔
1512
                        arg_copy_locale = arg_copy_keymap = arg_copy_timezone = arg_copy_root_password =
1✔
1513
                                arg_copy_root_shell = true;
1✔
1514
                        break;
1✔
1515

1516
                case ARG_COPY_LOCALE:
2✔
1517
                        arg_copy_locale = true;
2✔
1518
                        break;
2✔
1519

1520
                case ARG_COPY_KEYMAP:
2✔
1521
                        arg_copy_keymap = true;
2✔
1522
                        break;
2✔
1523

1524
                case ARG_COPY_TIMEZONE:
2✔
1525
                        arg_copy_timezone = true;
2✔
1526
                        break;
2✔
1527

1528
                case ARG_COPY_ROOT_PASSWORD:
1✔
1529
                        arg_copy_root_password = true;
1✔
1530
                        break;
1✔
1531

1532
                case ARG_COPY_ROOT_SHELL:
1✔
1533
                        arg_copy_root_shell = true;
1✔
1534
                        break;
1✔
1535

1536
                case ARG_FORCE:
3✔
1537
                        arg_force = true;
3✔
1538
                        break;
3✔
1539

1540
                case ARG_DELETE_ROOT_PASSWORD:
1✔
1541
                        arg_delete_root_password = true;
1✔
1542
                        break;
1✔
1543

1544
                case ARG_WELCOME:
1✔
1545
                        r = parse_boolean(optarg);
1✔
1546
                        if (r < 0)
1✔
1547
                                return log_error_errno(r, "Failed to parse --welcome= argument: %s", optarg);
×
1548

1549
                        arg_welcome = r;
1✔
1550
                        break;
1✔
1551

1552
                case ARG_CHROME:
×
1553
                        r = parse_boolean_argument("--chrome=", optarg, &arg_chrome);
×
1554
                        if (r < 0)
×
1555
                                return r;
1556

1557
                        break;
1558

1559
                case ARG_RESET:
1✔
1560
                        arg_reset = true;
1✔
1561
                        break;
1✔
1562

1563
                case ARG_MUTE_CONSOLE:
104✔
1564
                        r = parse_boolean_argument("--mute-console=", optarg, &arg_mute_console);
104✔
1565
                        if (r < 0)
104✔
1566
                                return r;
1567

1568
                        break;
1569

1570
                case '?':
1571
                        return -EINVAL;
1572

1573
                default:
×
1574
                        assert_not_reached();
×
1575
                }
1576

1577
        if (arg_delete_root_password && (arg_copy_root_password || arg_root_password || arg_prompt_root_password))
139✔
1578
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1579
                                       "--delete-root-password cannot be combined with other root password options.");
1580

1581
        if (arg_image && arg_root)
139✔
1582
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
×
1583
                                       "--root= and --image= cannot be used together.");
1584

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

1589
        return 1;
1590
}
1591

1592
static int reload_system_manager(sd_bus **bus) {
×
1593
        int r;
×
1594

1595
        assert(bus);
×
1596

1597
        if (!*bus) {
×
1598
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1599
                if (r < 0)
×
1600
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1601
        }
1602

1603
        r = bus_service_manager_reload(*bus);
×
1604
        if (r < 0)
×
1605
                return r;
1606

1607
        log_info("Requested manager reload to apply locale configuration.");
×
1608
        return 0;
1609
}
1610

1611
static int reload_vconsole(sd_bus **bus) {
×
1612
        _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
×
1613
        _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
×
1614
        _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
×
1615
        const char *object;
×
1616
        int r;
×
1617

1618
        assert(bus);
×
1619

1620
        if (!*bus) {
×
1621
                r = bus_connect_transport_systemd(BUS_TRANSPORT_LOCAL, NULL, RUNTIME_SCOPE_SYSTEM, bus);
×
1622
                if (r < 0)
×
1623
                        return bus_log_connect_error(r, BUS_TRANSPORT_LOCAL, RUNTIME_SCOPE_SYSTEM);
×
1624
        }
1625

1626
        r = bus_wait_for_jobs_new(*bus, &w);
×
1627
        if (r < 0)
×
1628
                return log_error_errno(r, "Could not watch jobs: %m");
×
1629

1630
        r = bus_call_method(*bus, bus_systemd_mgr, "RestartUnit", &error, &reply,
×
1631
                            "ss", "systemd-vconsole-setup.service", "replace");
1632
        if (r < 0)
×
1633
                return log_error_errno(r, "Failed to issue method call: %s", bus_error_message(&error, r));
×
1634

1635
        r = sd_bus_message_read(reply, "o", &object);
×
1636
        if (r < 0)
×
1637
                return bus_log_parse_error(r);
×
1638

1639
        r = bus_wait_for_jobs_one(w, object, BUS_WAIT_JOBS_LOG_ERROR, NULL);
×
1640
        if (r < 0)
×
1641
                return log_error_errno(r, "Failed to wait for systemd-vconsole-setup.service/restart: %m");
×
1642
        return 0;
1643
}
1644

1645
static int run(int argc, char *argv[]) {
141✔
1646
        _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
141✔
1647
        _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
141✔
1648
        _cleanup_(umount_and_freep) char *mounted_dir = NULL;
×
1649
        _cleanup_close_ int rfd = -EBADF;
141✔
1650
        int r;
141✔
1651

1652
        r = parse_argv(argc, argv);
141✔
1653
        if (r <= 0)
141✔
1654
                return r;
1655

1656
        log_setup();
139✔
1657

1658
        umask(0022);
139✔
1659

1660
        bool offline = arg_root || arg_image;
139✔
1661

1662
        if (!offline) {
104✔
1663
                /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1664
                 * command line option, because we are called to provision the host with basic settings (as
1665
                 * opposed to some other file system tree/image) */
1666

1667
                bool enabled;
104✔
1668
                r = proc_cmdline_get_bool("systemd.firstboot", /* flags = */ 0, &enabled);
104✔
1669
                if (r < 0)
104✔
1670
                        return log_error_errno(r, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
×
1671
                if (r > 0 && !enabled) {
104✔
1672
                        log_debug("Found systemd.firstboot=no kernel command line argument, turning off all prompts.");
104✔
1673
                        arg_prompt_locale = arg_prompt_keymap = arg_prompt_timezone = arg_prompt_hostname = arg_prompt_root_password = arg_prompt_root_shell = false;
104✔
1674
                }
1675
        }
1676

1677
        r = mac_init();
139✔
1678
        if (r < 0)
139✔
1679
                return r;
1680

1681
        if (arg_image) {
139✔
1682
                assert(!arg_root);
×
1683

1684
                r = mount_image_privately_interactively(
×
1685
                                arg_image,
1686
                                arg_image_policy,
1687
                                DISSECT_IMAGE_GENERIC_ROOT |
1688
                                DISSECT_IMAGE_REQUIRE_ROOT |
1689
                                DISSECT_IMAGE_VALIDATE_OS |
1690
                                DISSECT_IMAGE_RELAX_VAR_CHECK |
1691
                                DISSECT_IMAGE_FSCK |
1692
                                DISSECT_IMAGE_GROWFS |
1693
                                DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1694
                                &mounted_dir,
1695
                                &rfd,
1696
                                &loop_device);
1697
                if (r < 0)
×
1698
                        return r;
1699

1700
                arg_root = strdup(mounted_dir);
×
1701
                if (!arg_root)
×
1702
                        return log_oom();
×
1703
        } else {
1704
                rfd = open(empty_to_root(arg_root), O_DIRECTORY|O_CLOEXEC);
139✔
1705
                if (rfd < 0)
139✔
1706
                        return log_error_errno(errno, "Failed to open %s: %m", empty_to_root(arg_root));
×
1707
        }
1708

1709
        LOG_SET_PREFIX(arg_image ?: arg_root);
280✔
1710
        DEFER_VOID_CALL(chrome_hide);
×
1711

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

1715
        if (arg_keymap && !keymap_is_ok(arg_keymap, FD_TO_PTR(rfd)))
139✔
1716
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Keymap %s is not installed.", arg_keymap);
×
1717
        if (arg_locale && !locale_is_ok(arg_locale, FD_TO_PTR(rfd)))
139✔
1718
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale);
×
1719
        if (arg_locale_messages && !locale_is_ok(arg_locale_messages, FD_TO_PTR(rfd)))
139✔
1720
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Locale %s is not installed.", arg_locale_messages);
×
1721

1722
        if (arg_root_shell) {
139✔
1723
                r = find_shell(rfd, arg_root_shell);
5✔
1724
                if (r < 0)
5✔
1725
                        return r;
1726
        }
1727

1728
        r = process_reset(rfd);
138✔
1729
        if (r < 0)
138✔
1730
                return r;
1731

1732
        _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *mute_console_link = NULL;
139✔
1733
        r = process_locale(rfd, &mute_console_link);
138✔
1734
        if (r < 0)
138✔
1735
                return r;
1736
        if (r > 0 && !offline)
138✔
1737
                (void) reload_system_manager(&bus);
×
1738

1739
        r = process_keymap(rfd, &mute_console_link);
138✔
1740
        if (r < 0)
138✔
1741
                return r;
1742
        if (r > 0 && !offline)
138✔
1743
                (void) reload_vconsole(&bus);
×
1744

1745
        r = process_timezone(rfd, &mute_console_link);
138✔
1746
        if (r < 0)
138✔
1747
                return r;
1748

1749
        r = process_hostname(rfd, &mute_console_link);
138✔
1750
        if (r < 0)
138✔
1751
                return r;
1752

1753
        r = process_root_account(rfd, &mute_console_link);
138✔
1754
        if (r < 0)
138✔
1755
                return r;
1756

1757
        r = process_kernel_cmdline(rfd);
138✔
1758
        if (r < 0)
138✔
1759
                return r;
1760

1761
        r = process_machine_id(rfd);
138✔
1762
        if (r < 0)
138✔
1763
                return r;
×
1764

1765
        return 0;
1766
}
1767

1768
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