1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include <linux/loop.h>
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
13 #include "creds-util.h"
14 #include "dissect-image.h"
19 #include "hostname-util.h"
21 #include "libcrypt-util.h"
22 #include "locale-util.h"
23 #include "main-func.h"
24 #include "memory-util.h"
26 #include "mount-util.h"
28 #include "parse-argument.h"
29 #include "parse-util.h"
30 #include "path-util.h"
31 #include "pretty-print.h"
32 #include "proc-cmdline.h"
33 #include "pwquality-util.h"
34 #include "random-util.h"
35 #include "string-util.h"
37 #include "terminal-util.h"
38 #include "time-util.h"
39 #include "tmpfile-util-label.h"
40 #include "tmpfile-util.h"
41 #include "umask-util.h"
42 #include "user-util.h"
44 static char *arg_root
= NULL
;
45 static char *arg_image
= NULL
;
46 static char *arg_locale
= NULL
; /* $LANG */
47 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
48 static char *arg_keymap
= NULL
;
49 static char *arg_timezone
= NULL
;
50 static char *arg_hostname
= NULL
;
51 static sd_id128_t arg_machine_id
= {};
52 static char *arg_root_password
= NULL
;
53 static char *arg_root_shell
= NULL
;
54 static char *arg_kernel_cmdline
= NULL
;
55 static bool arg_prompt_locale
= false;
56 static bool arg_prompt_keymap
= false;
57 static bool arg_prompt_timezone
= false;
58 static bool arg_prompt_hostname
= false;
59 static bool arg_prompt_root_password
= false;
60 static bool arg_prompt_root_shell
= false;
61 static bool arg_copy_locale
= false;
62 static bool arg_copy_keymap
= false;
63 static bool arg_copy_timezone
= false;
64 static bool arg_copy_root_password
= false;
65 static bool arg_copy_root_shell
= false;
66 static bool arg_force
= false;
67 static bool arg_delete_root_password
= false;
68 static bool arg_root_password_is_hashed
= false;
69 static bool arg_welcome
= true;
71 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
80 static bool press_any_key(void) {
84 printf("-- Press any key to proceed --");
87 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
95 static void print_welcome(void) {
96 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
97 static bool done
= false;
107 r
= parse_os_release(
109 "PRETTY_NAME", &pretty_name
,
110 "ANSI_COLOR", &ansi_color
);
112 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
113 "Failed to read os-release file, ignoring: %m");
115 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
116 ac
= isempty(ansi_color
) ? "0" : ansi_color
;
118 if (colors_enabled())
119 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac
, pn
);
121 printf("\nWelcome to your new installation of %s!\n", pn
);
123 printf("\nPlease configure your system!\n\n");
130 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
131 unsigned break_lines
, break_modulo
;
132 size_t n
, per_column
, i
, j
;
134 assert(n_columns
> 0);
137 per_column
= DIV_ROUND_UP(n
, n_columns
);
139 break_lines
= lines();
143 /* The first page gets two extra lines, since we want to show
145 break_modulo
= break_lines
;
146 if (break_modulo
> 3)
149 for (i
= 0; i
< per_column
; i
++) {
151 for (j
= 0; j
< n_columns
; j
++) {
152 _cleanup_free_
char *e
= NULL
;
154 if (j
* per_column
+ i
>= n
)
157 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
161 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
166 /* on the first screen we reserve 2 extra lines for the title */
167 if (i
% break_lines
== break_modulo
) {
168 if (!press_any_key())
176 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
184 _cleanup_free_
char *p
= NULL
;
187 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
188 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
190 return log_error_errno(r
, "Failed to query user: %m");
193 log_warning("No data entered, skipping.");
197 if (streq(p
, "list")) {
198 r
= show_menu(l
, 3, 22, percentage
);
206 r
= safe_atou(p
, &u
);
208 if (u
<= 0 || u
> strv_length(l
)) {
209 log_error("Specified entry number out of range.");
213 log_info("Selected '%s'.", l
[u
-1]);
214 return free_and_strdup_warn(ret
, l
[u
-1]);
218 log_error("Entered data invalid.");
222 return free_and_replace(*ret
, p
);
226 static bool locale_is_ok(const char *name
) {
229 return locale_is_valid(name
);
231 return locale_is_installed(name
) > 0;
234 static int prompt_locale(void) {
235 _cleanup_strv_free_
char **locales
= NULL
;
236 bool acquired_from_creds
= false;
239 if (arg_locale
|| arg_locale_messages
)
242 r
= read_credential("firstboot.locale", (void**) &arg_locale
, NULL
);
244 log_debug_errno(r
, "Failed to read credential firstboot.locale, ignoring: %m");
246 acquired_from_creds
= true;
248 r
= read_credential("firstboot.locale-messages", (void**) &arg_locale_messages
, NULL
);
250 log_debug_errno(r
, "Failed to read credential firstboot.locale-message, ignoring: %m");
252 acquired_from_creds
= true;
254 if (acquired_from_creds
) {
255 log_debug("Acquired locale from credentials.");
259 if (!arg_prompt_locale
)
262 r
= get_locales(&locales
);
264 return log_error_errno(r
, "Cannot query locales list: %m");
266 if (strv_isempty(locales
))
267 log_debug("No locales found, skipping locale selection.");
268 else if (strv_length(locales
) == 1) {
270 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
271 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
273 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
275 arg_locale
= strdup(locales
[0]);
279 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
284 r
= prompt_loop("Please enter system locale name or number",
285 locales
, 60, locale_is_ok
, &arg_locale
);
289 if (isempty(arg_locale
))
292 r
= prompt_loop("Please enter system message locale name or number",
293 locales
, 60, locale_is_ok
, &arg_locale_messages
);
297 /* Suppress the messages setting if it's the same as the main locale anyway */
298 if (streq_ptr(arg_locale
, arg_locale_messages
))
299 arg_locale_messages
= mfree(arg_locale_messages
);
305 static int process_locale(void) {
306 const char *etc_localeconf
;
311 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
312 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
315 if (arg_copy_locale
&& arg_root
) {
317 (void) mkdir_parents(etc_localeconf
, 0755);
318 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
321 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
323 log_info("%s copied.", etc_localeconf
);
332 if (!isempty(arg_locale
))
333 locales
[i
++] = strjoina("LANG=", arg_locale
);
334 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
335 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
342 (void) mkdir_parents(etc_localeconf
, 0755);
343 r
= write_env_file(etc_localeconf
, locales
);
345 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
347 log_info("%s written.", etc_localeconf
);
351 static int prompt_keymap(void) {
352 _cleanup_strv_free_
char **kmaps
= NULL
;
358 r
= read_credential("firstboot.keymap", (void**) &arg_keymap
, NULL
);
360 log_debug_errno(r
, "Failed to read credential firstboot.keymap, ignoring: %m");
362 log_debug("Acquired keymap from credential.");
366 if (!arg_prompt_keymap
)
369 r
= get_keymaps(&kmaps
);
370 if (r
== -ENOENT
) /* no keymaps installed */
373 return log_error_errno(r
, "Failed to read keymaps: %m");
377 return prompt_loop("Please enter system keymap name or number",
378 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
381 static int process_keymap(void) {
382 const char *etc_vconsoleconf
;
386 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
387 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
390 if (arg_copy_keymap
&& arg_root
) {
392 (void) mkdir_parents(etc_vconsoleconf
, 0755);
393 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
396 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
398 log_info("%s copied.", etc_vconsoleconf
);
405 return 0; /* don't fail if no keymaps are installed */
409 if (isempty(arg_keymap
))
412 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
414 r
= mkdir_parents(etc_vconsoleconf
, 0755);
416 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
418 r
= write_env_file(etc_vconsoleconf
, keymap
);
420 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
422 log_info("%s written.", etc_vconsoleconf
);
426 static bool timezone_is_valid_log_error(const char *name
) {
427 return timezone_is_valid(name
, LOG_ERR
);
430 static int prompt_timezone(void) {
431 _cleanup_strv_free_
char **zones
= NULL
;
437 r
= read_credential("firstboot.timezone", (void**) &arg_timezone
, NULL
);
439 log_debug_errno(r
, "Failed to read credential firstboot.timezone, ignoring: %m");
441 log_debug("Acquired timezone from credential.");
445 if (!arg_prompt_timezone
)
448 r
= get_timezones(&zones
);
450 return log_error_errno(r
, "Cannot query timezone list: %m");
454 r
= prompt_loop("Please enter timezone name or number",
455 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
462 static int process_timezone(void) {
463 const char *etc_localtime
, *e
;
466 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
467 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
470 if (arg_copy_timezone
&& arg_root
) {
471 _cleanup_free_
char *p
= NULL
;
473 r
= readlink_malloc("/etc/localtime", &p
);
476 return log_error_errno(r
, "Failed to read host timezone: %m");
478 (void) mkdir_parents(etc_localtime
, 0755);
479 if (symlink(p
, etc_localtime
) < 0)
480 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
482 log_info("%s copied.", etc_localtime
);
487 r
= prompt_timezone();
491 if (isempty(arg_timezone
))
494 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
496 (void) mkdir_parents(etc_localtime
, 0755);
497 if (symlink(e
, etc_localtime
) < 0)
498 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
500 log_info("%s written", etc_localtime
);
504 static int prompt_hostname(void) {
510 if (!arg_prompt_hostname
)
517 _cleanup_free_
char *h
= NULL
;
519 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
521 return log_error_errno(r
, "Failed to query hostname: %m");
524 log_warning("No hostname entered, skipping.");
528 if (!hostname_is_valid(h
, VALID_HOSTNAME_TRAILING_DOT
)) {
529 log_error("Specified hostname invalid.");
533 /* Get rid of the trailing dot that we allow, but don't want to see */
534 arg_hostname
= hostname_cleanup(h
);
542 static int process_hostname(void) {
543 const char *etc_hostname
;
546 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
547 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
550 r
= prompt_hostname();
554 if (isempty(arg_hostname
))
557 r
= write_string_file(etc_hostname
, arg_hostname
,
558 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
559 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
561 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
563 log_info("%s written.", etc_hostname
);
567 static int process_machine_id(void) {
568 const char *etc_machine_id
;
569 char id
[SD_ID128_STRING_MAX
];
572 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
573 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
576 if (sd_id128_is_null(arg_machine_id
))
579 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
580 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
581 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
583 return log_error_errno(r
, "Failed to write machine id: %m");
585 log_info("%s written.", etc_machine_id
);
589 static int prompt_root_password(void) {
590 const char *msg1
, *msg2
;
593 if (arg_root_password
)
596 r
= read_credential("passwd.hashed-password.root", (void**) &arg_root_password
, NULL
);
598 r
= read_credential("passwd.plaintext-password.root", (void**) &arg_root_password
, NULL
);
600 log_debug_errno(r
, "Couldn't read credential 'passwd.{hashed|plaintext}-password.root', ignoring: %m");
602 arg_root_password_is_hashed
= false;
606 log_debug_errno(r
, "Couldn't read credential 'passwd.hashed-password.root', ignoring: %m");
608 arg_root_password_is_hashed
= true;
612 if (!arg_prompt_root_password
)
618 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
619 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
624 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
625 _cleanup_free_
char *error
= NULL
;
627 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
629 return log_error_errno(r
, "Failed to query root password: %m");
630 if (strv_length(a
) != 1)
631 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
632 "Received multiple passwords, where we expected one.");
635 log_warning("No password entered, skipping.");
639 r
= quality_check_password(*a
, "root", &error
);
641 return log_error_errno(r
, "Failed to check quality of password: %m");
643 log_warning("Password is weak, accepting anyway: %s", error
);
645 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
647 return log_error_errno(r
, "Failed to query root password: %m");
648 if (strv_length(b
) != 1)
649 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
650 "Received multiple passwords, where we expected one.");
652 if (!streq(*a
, *b
)) {
653 log_error("Entered passwords did not match, please try again.");
657 arg_root_password
= TAKE_PTR(*a
);
664 static int find_shell(const char *path
, const char *root
) {
669 if (!valid_shell(path
))
670 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
672 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, NULL
, NULL
);
675 p
= prefix_roota(root
, path
);
676 return log_error_errno(r
, "Failed to resolve shell %s: %m", p
);
682 static int prompt_root_shell(void) {
688 r
= read_credential("passwd.shell.root", (void**) &arg_root_shell
, NULL
);
690 log_debug_errno(r
, "Failed to read credential passwd.shell.root, ignoring: %m");
692 log_debug("Acquired root shell from credential.");
696 if (!arg_prompt_root_shell
)
703 _cleanup_free_
char *s
= NULL
;
705 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
707 return log_error_errno(r
, "Failed to query root shell: %m");
710 log_warning("No shell entered, skipping.");
714 r
= find_shell(s
, arg_root
);
718 arg_root_shell
= TAKE_PTR(s
);
725 static int write_root_passwd(const char *passwd_path
, const char *password
, const char *shell
) {
726 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
727 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
732 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
736 original
= fopen(passwd_path
, "re");
740 r
= copy_rights(fileno(original
), fileno(passwd
));
744 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
746 if (streq(i
->pw_name
, "root")) {
747 i
->pw_passwd
= (char *) password
;
749 i
->pw_shell
= (char *) shell
;
752 r
= putpwent_sane(i
, passwd
);
760 struct passwd root
= {
761 .pw_name
= (char *) "root",
762 .pw_passwd
= (char *) password
,
765 .pw_gecos
= (char *) "Super User",
766 .pw_dir
= (char *) "/root",
767 .pw_shell
= (char *) (shell
?: "/bin/sh"),
773 r
= fchmod(fileno(passwd
), 0644);
777 r
= putpwent_sane(&root
, passwd
);
782 r
= fflush_sync_and_check(passwd
);
786 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
793 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
794 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
795 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
798 assert(hashed_password
);
800 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
804 original
= fopen(shadow_path
, "re");
808 r
= copy_rights(fileno(original
), fileno(shadow
));
812 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
814 if (streq(i
->sp_namp
, "root")) {
815 i
->sp_pwdp
= (char *) hashed_password
;
816 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
819 r
= putspent_sane(i
, shadow
);
828 .sp_namp
= (char*) "root",
829 .sp_pwdp
= (char *) hashed_password
,
830 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
836 .sp_flag
= ULONG_MAX
, /* this appears to be what everybody does ... */
842 r
= fchmod(fileno(shadow
), 0000);
846 r
= putspent_sane(&root
, shadow
);
851 r
= fflush_sync_and_check(shadow
);
855 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
862 static int process_root_args(void) {
863 _cleanup_close_
int lock
= -1;
864 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
865 const char *password
, *hashed_password
;
866 const char *etc_passwd
, *etc_shadow
;
869 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
870 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
872 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
873 * tightly coupled and hence we make sure we have permission from the user to create/modify both
875 if ((laccess(etc_passwd
, F_OK
) >= 0 || laccess(etc_shadow
, F_OK
) >= 0) && !arg_force
)
877 /* Don't create/modify passwd and shadow if not asked */
878 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
879 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
))
882 (void) mkdir_parents(etc_passwd
, 0755);
884 lock
= take_etc_passwd_lock(arg_root
);
886 return log_error_errno(lock
, "Failed to take a lock on %s: %m", etc_passwd
);
888 if (arg_copy_root_shell
&& arg_root
) {
892 p
= getpwnam("root");
894 return log_error_errno(errno_or_else(EIO
), "Failed to find passwd entry for root: %m");
896 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
901 r
= prompt_root_shell();
905 if (arg_copy_root_password
&& arg_root
) {
909 p
= getspnam("root");
911 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
913 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
917 arg_root_password_is_hashed
= true;
920 r
= prompt_root_password();
924 if (arg_root_password
&& arg_root_password_is_hashed
) {
926 hashed_password
= arg_root_password
;
927 } else if (arg_root_password
) {
928 r
= hash_password(arg_root_password
, &_hashed_password
);
930 return log_error_errno(r
, "Failed to hash password: %m");
933 hashed_password
= _hashed_password
;
935 } else if (arg_delete_root_password
)
936 password
= hashed_password
= "";
938 password
= hashed_password
= "!";
940 r
= write_root_passwd(etc_passwd
, password
, arg_root_shell
);
942 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
944 log_info("%s written", etc_passwd
);
946 r
= write_root_shadow(etc_shadow
, hashed_password
);
948 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
950 log_info("%s written.", etc_shadow
);
954 static int process_kernel_cmdline(void) {
955 const char *etc_kernel_cmdline
;
958 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
959 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
962 if (!arg_kernel_cmdline
)
965 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
966 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
967 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
969 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
971 log_info("%s written.", etc_kernel_cmdline
);
975 static int help(void) {
976 _cleanup_free_
char *link
= NULL
;
979 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
983 printf("%s [OPTIONS...]\n\n"
984 "Configures basic settings of the system.\n\n"
985 " -h --help Show this help\n"
986 " --version Show package version\n"
987 " --root=PATH Operate on an alternate filesystem root\n"
988 " --image=PATH Operate on an alternate filesystem image\n"
989 " --locale=LOCALE Set primary locale (LANG=)\n"
990 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
991 " --keymap=KEYMAP Set keymap\n"
992 " --timezone=TIMEZONE Set timezone\n"
993 " --hostname=NAME Set hostname\n"
994 " --machine-ID=ID Set machine ID\n"
995 " --root-password=PASSWORD Set root password from plaintext password\n"
996 " --root-password-file=FILE Set root password from file\n"
997 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
998 " --root-shell=SHELL Set root shell\n"
999 " --prompt-locale Prompt the user for locale settings\n"
1000 " --prompt-keymap Prompt the user for keymap settings\n"
1001 " --prompt-timezone Prompt the user for timezone\n"
1002 " --prompt-hostname Prompt the user for hostname\n"
1003 " --prompt-root-password Prompt the user for root password\n"
1004 " --prompt-root-shell Prompt the user for root shell\n"
1005 " --prompt Prompt for all of the above\n"
1006 " --copy-locale Copy locale from host\n"
1007 " --copy-keymap Copy keymap from host\n"
1008 " --copy-timezone Copy timezone from host\n"
1009 " --copy-root-password Copy root password from host\n"
1010 " --copy-root-shell Copy root shell from host\n"
1011 " --copy Copy locale, keymap, timezone, root password\n"
1012 " --setup-machine-id Generate a new random machine ID\n"
1013 " --force Overwrite existing files\n"
1014 " --delete-root-password Delete root password\n"
1015 " --welcome=no Disable the welcome text\n"
1016 "\nSee the %s for details.\n",
1017 program_invocation_short_name
,
1023 static int parse_argv(int argc
, char *argv
[]) {
1026 ARG_VERSION
= 0x100,
1030 ARG_LOCALE_MESSAGES
,
1036 ARG_ROOT_PASSWORD_FILE
,
1037 ARG_ROOT_PASSWORD_HASHED
,
1039 ARG_KERNEL_COMMAND_LINE
,
1043 ARG_PROMPT_TIMEZONE
,
1044 ARG_PROMPT_HOSTNAME
,
1045 ARG_PROMPT_ROOT_PASSWORD
,
1046 ARG_PROMPT_ROOT_SHELL
,
1051 ARG_COPY_ROOT_PASSWORD
,
1052 ARG_COPY_ROOT_SHELL
,
1053 ARG_SETUP_MACHINE_ID
,
1055 ARG_DELETE_ROOT_PASSWORD
,
1059 static const struct option options
[] = {
1060 { "help", no_argument
, NULL
, 'h' },
1061 { "version", no_argument
, NULL
, ARG_VERSION
},
1062 { "root", required_argument
, NULL
, ARG_ROOT
},
1063 { "image", required_argument
, NULL
, ARG_IMAGE
},
1064 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1065 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1066 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1067 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1068 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1069 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1070 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1071 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1072 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1073 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1074 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1075 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1076 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1077 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1078 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1079 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1080 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1081 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1082 { "copy", no_argument
, NULL
, ARG_COPY
},
1083 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1084 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1085 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1086 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1087 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1088 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1089 { "force", no_argument
, NULL
, ARG_FORCE
},
1090 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1091 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1100 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1111 r
= parse_path_argument(optarg
, true, &arg_root
);
1117 r
= parse_path_argument(optarg
, false, &arg_image
);
1123 r
= free_and_strdup(&arg_locale
, optarg
);
1129 case ARG_LOCALE_MESSAGES
:
1130 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1137 if (!keymap_is_valid(optarg
))
1138 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1139 "Keymap %s is not valid.", optarg
);
1141 r
= free_and_strdup(&arg_keymap
, optarg
);
1148 if (!timezone_is_valid(optarg
, LOG_ERR
))
1149 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1150 "Timezone %s is not valid.", optarg
);
1152 r
= free_and_strdup(&arg_timezone
, optarg
);
1158 case ARG_ROOT_PASSWORD
:
1159 r
= free_and_strdup(&arg_root_password
, optarg
);
1163 arg_root_password_is_hashed
= false;
1166 case ARG_ROOT_PASSWORD_FILE
:
1167 arg_root_password
= mfree(arg_root_password
);
1169 r
= read_one_line_file(optarg
, &arg_root_password
);
1171 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1173 arg_root_password_is_hashed
= false;
1176 case ARG_ROOT_PASSWORD_HASHED
:
1177 r
= free_and_strdup(&arg_root_password
, optarg
);
1181 arg_root_password_is_hashed
= true;
1184 case ARG_ROOT_SHELL
:
1185 r
= find_shell(optarg
, arg_root
);
1189 r
= free_and_strdup(&arg_root_shell
, optarg
);
1196 if (!hostname_is_valid(optarg
, VALID_HOSTNAME_TRAILING_DOT
))
1197 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1198 "Host name %s is not valid.", optarg
);
1200 r
= free_and_strdup(&arg_hostname
, optarg
);
1204 hostname_cleanup(arg_hostname
);
1207 case ARG_MACHINE_ID
:
1208 r
= sd_id128_from_string(optarg
, &arg_machine_id
);
1210 return log_error_errno(r
, "Failed to parse machine id %s.", optarg
);
1214 case ARG_KERNEL_COMMAND_LINE
:
1215 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1222 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1223 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1226 case ARG_PROMPT_LOCALE
:
1227 arg_prompt_locale
= true;
1230 case ARG_PROMPT_KEYMAP
:
1231 arg_prompt_keymap
= true;
1234 case ARG_PROMPT_TIMEZONE
:
1235 arg_prompt_timezone
= true;
1238 case ARG_PROMPT_HOSTNAME
:
1239 arg_prompt_hostname
= true;
1242 case ARG_PROMPT_ROOT_PASSWORD
:
1243 arg_prompt_root_password
= true;
1246 case ARG_PROMPT_ROOT_SHELL
:
1247 arg_prompt_root_shell
= true;
1251 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1252 arg_copy_root_shell
= true;
1255 case ARG_COPY_LOCALE
:
1256 arg_copy_locale
= true;
1259 case ARG_COPY_KEYMAP
:
1260 arg_copy_keymap
= true;
1263 case ARG_COPY_TIMEZONE
:
1264 arg_copy_timezone
= true;
1267 case ARG_COPY_ROOT_PASSWORD
:
1268 arg_copy_root_password
= true;
1271 case ARG_COPY_ROOT_SHELL
:
1272 arg_copy_root_shell
= true;
1275 case ARG_SETUP_MACHINE_ID
:
1276 r
= sd_id128_randomize(&arg_machine_id
);
1278 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1286 case ARG_DELETE_ROOT_PASSWORD
:
1287 arg_delete_root_password
= true;
1291 r
= parse_boolean(optarg
);
1293 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1302 assert_not_reached("Unhandled option");
1305 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1306 * account when looking for the locale files. */
1308 if (arg_locale
&& !locale_is_ok(arg_locale
))
1309 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1310 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1311 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1313 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1314 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1315 "--delete-root-password cannot be combined with other root password options");
1317 if (arg_image
&& arg_root
)
1318 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1323 static int run(int argc
, char *argv
[]) {
1324 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1325 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1326 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1329 r
= parse_argv(argc
, argv
);
1337 if (!arg_root
&& !arg_image
) {
1340 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1341 * command line option, because we are called to provision the host with basic settings (as
1342 * opposed to some other file system tree/image) */
1344 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1346 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1347 if (r
> 0 && !enabled
)
1348 return 0; /* disabled */
1354 r
= mount_image_privately_interactively(
1356 DISSECT_IMAGE_GENERIC_ROOT
|
1357 DISSECT_IMAGE_REQUIRE_ROOT
|
1358 DISSECT_IMAGE_VALIDATE_OS
|
1359 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1367 arg_root
= strdup(unlink_dir
);
1372 r
= process_locale();
1376 r
= process_keymap();
1380 r
= process_timezone();
1384 r
= process_hostname();
1388 r
= process_machine_id();
1392 r
= process_root_args();
1396 r
= process_kernel_cmdline();
1403 DEFINE_MAIN_FUNCTION(run
);