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
;
571 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
572 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
575 if (sd_id128_is_null(arg_machine_id
))
578 r
= write_string_file(etc_machine_id
, SD_ID128_TO_STRING(arg_machine_id
),
579 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
580 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
582 return log_error_errno(r
, "Failed to write machine id: %m");
584 log_info("%s written.", etc_machine_id
);
588 static int prompt_root_password(void) {
589 const char *msg1
, *msg2
;
592 if (arg_root_password
)
595 r
= read_credential("passwd.hashed-password.root", (void**) &arg_root_password
, NULL
);
597 r
= read_credential("passwd.plaintext-password.root", (void**) &arg_root_password
, NULL
);
599 log_debug_errno(r
, "Couldn't read credential 'passwd.{hashed|plaintext}-password.root', ignoring: %m");
601 arg_root_password_is_hashed
= false;
605 log_debug_errno(r
, "Couldn't read credential 'passwd.hashed-password.root', ignoring: %m");
607 arg_root_password_is_hashed
= true;
611 if (!arg_prompt_root_password
)
617 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
618 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
623 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
624 _cleanup_free_
char *error
= NULL
;
626 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
628 return log_error_errno(r
, "Failed to query root password: %m");
629 if (strv_length(a
) != 1)
630 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
631 "Received multiple passwords, where we expected one.");
634 log_warning("No password entered, skipping.");
638 r
= quality_check_password(*a
, "root", &error
);
640 return log_error_errno(r
, "Failed to check quality of password: %m");
642 log_warning("Password is weak, accepting anyway: %s", error
);
644 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
646 return log_error_errno(r
, "Failed to query root password: %m");
647 if (strv_length(b
) != 1)
648 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
649 "Received multiple passwords, where we expected one.");
651 if (!streq(*a
, *b
)) {
652 log_error("Entered passwords did not match, please try again.");
656 arg_root_password
= TAKE_PTR(*a
);
663 static int find_shell(const char *path
, const char *root
) {
668 if (!valid_shell(path
))
669 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
671 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, NULL
, NULL
);
674 p
= prefix_roota(root
, path
);
675 return log_error_errno(r
, "Failed to resolve shell %s: %m", p
);
681 static int prompt_root_shell(void) {
687 r
= read_credential("passwd.shell.root", (void**) &arg_root_shell
, NULL
);
689 log_debug_errno(r
, "Failed to read credential passwd.shell.root, ignoring: %m");
691 log_debug("Acquired root shell from credential.");
695 if (!arg_prompt_root_shell
)
702 _cleanup_free_
char *s
= NULL
;
704 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
706 return log_error_errno(r
, "Failed to query root shell: %m");
709 log_warning("No shell entered, skipping.");
713 r
= find_shell(s
, arg_root
);
717 arg_root_shell
= TAKE_PTR(s
);
724 static int write_root_passwd(const char *passwd_path
, const char *password
, const char *shell
) {
725 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
726 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
731 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
735 original
= fopen(passwd_path
, "re");
739 r
= copy_rights(fileno(original
), fileno(passwd
));
743 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
745 if (streq(i
->pw_name
, "root")) {
746 i
->pw_passwd
= (char *) password
;
748 i
->pw_shell
= (char *) shell
;
751 r
= putpwent_sane(i
, passwd
);
759 struct passwd root
= {
760 .pw_name
= (char *) "root",
761 .pw_passwd
= (char *) password
,
764 .pw_gecos
= (char *) "Super User",
765 .pw_dir
= (char *) "/root",
766 .pw_shell
= (char *) (shell
?: "/bin/sh"),
772 r
= fchmod(fileno(passwd
), 0644);
776 r
= putpwent_sane(&root
, passwd
);
781 r
= fflush_sync_and_check(passwd
);
785 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
792 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
793 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
794 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
797 assert(hashed_password
);
799 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
803 original
= fopen(shadow_path
, "re");
807 r
= copy_rights(fileno(original
), fileno(shadow
));
811 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
813 if (streq(i
->sp_namp
, "root")) {
814 i
->sp_pwdp
= (char *) hashed_password
;
815 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
818 r
= putspent_sane(i
, shadow
);
827 .sp_namp
= (char*) "root",
828 .sp_pwdp
= (char *) hashed_password
,
829 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
835 .sp_flag
= ULONG_MAX
, /* this appears to be what everybody does ... */
841 r
= fchmod(fileno(shadow
), 0000);
845 r
= putspent_sane(&root
, shadow
);
850 r
= fflush_sync_and_check(shadow
);
854 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
861 static int process_root_args(void) {
862 _cleanup_close_
int lock
= -1;
863 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
864 const char *password
, *hashed_password
;
865 const char *etc_passwd
, *etc_shadow
;
868 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
869 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
871 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
872 * tightly coupled and hence we make sure we have permission from the user to create/modify both
874 if ((laccess(etc_passwd
, F_OK
) >= 0 || laccess(etc_shadow
, F_OK
) >= 0) && !arg_force
)
876 /* Don't create/modify passwd and shadow if not asked */
877 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
878 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
))
881 (void) mkdir_parents(etc_passwd
, 0755);
883 lock
= take_etc_passwd_lock(arg_root
);
885 return log_error_errno(lock
, "Failed to take a lock on %s: %m", etc_passwd
);
887 if (arg_copy_root_shell
&& arg_root
) {
891 p
= getpwnam("root");
893 return log_error_errno(errno_or_else(EIO
), "Failed to find passwd entry for root: %m");
895 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
900 r
= prompt_root_shell();
904 if (arg_copy_root_password
&& arg_root
) {
908 p
= getspnam("root");
910 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
912 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
916 arg_root_password_is_hashed
= true;
919 r
= prompt_root_password();
923 if (arg_root_password
&& arg_root_password_is_hashed
) {
924 password
= PASSWORD_SEE_SHADOW
;
925 hashed_password
= arg_root_password
;
926 } else if (arg_root_password
) {
927 r
= hash_password(arg_root_password
, &_hashed_password
);
929 return log_error_errno(r
, "Failed to hash password: %m");
931 password
= PASSWORD_SEE_SHADOW
;
932 hashed_password
= _hashed_password
;
934 } else if (arg_delete_root_password
)
935 password
= hashed_password
= PASSWORD_NONE
;
937 password
= hashed_password
= PASSWORD_LOCKED_AND_INVALID
;
939 r
= write_root_passwd(etc_passwd
, password
, arg_root_shell
);
941 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
943 log_info("%s written", etc_passwd
);
945 r
= write_root_shadow(etc_shadow
, hashed_password
);
947 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
949 log_info("%s written.", etc_shadow
);
953 static int process_kernel_cmdline(void) {
954 const char *etc_kernel_cmdline
;
957 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
958 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
961 if (!arg_kernel_cmdline
)
964 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
965 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
966 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
968 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
970 log_info("%s written.", etc_kernel_cmdline
);
974 static int help(void) {
975 _cleanup_free_
char *link
= NULL
;
978 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
982 printf("%s [OPTIONS...]\n\n"
983 "Configures basic settings of the system.\n\n"
984 " -h --help Show this help\n"
985 " --version Show package version\n"
986 " --root=PATH Operate on an alternate filesystem root\n"
987 " --image=PATH Operate on an alternate filesystem image\n"
988 " --locale=LOCALE Set primary locale (LANG=)\n"
989 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
990 " --keymap=KEYMAP Set keymap\n"
991 " --timezone=TIMEZONE Set timezone\n"
992 " --hostname=NAME Set hostname\n"
993 " --machine-ID=ID Set machine ID\n"
994 " --root-password=PASSWORD Set root password from plaintext password\n"
995 " --root-password-file=FILE Set root password from file\n"
996 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
997 " --root-shell=SHELL Set root shell\n"
998 " --prompt-locale Prompt the user for locale settings\n"
999 " --prompt-keymap Prompt the user for keymap settings\n"
1000 " --prompt-timezone Prompt the user for timezone\n"
1001 " --prompt-hostname Prompt the user for hostname\n"
1002 " --prompt-root-password Prompt the user for root password\n"
1003 " --prompt-root-shell Prompt the user for root shell\n"
1004 " --prompt Prompt for all of the above\n"
1005 " --copy-locale Copy locale from host\n"
1006 " --copy-keymap Copy keymap from host\n"
1007 " --copy-timezone Copy timezone from host\n"
1008 " --copy-root-password Copy root password from host\n"
1009 " --copy-root-shell Copy root shell from host\n"
1010 " --copy Copy locale, keymap, timezone, root password\n"
1011 " --setup-machine-id Generate a new random machine ID\n"
1012 " --force Overwrite existing files\n"
1013 " --delete-root-password Delete root password\n"
1014 " --welcome=no Disable the welcome text\n"
1015 "\nSee the %s for details.\n",
1016 program_invocation_short_name
,
1022 static int parse_argv(int argc
, char *argv
[]) {
1025 ARG_VERSION
= 0x100,
1029 ARG_LOCALE_MESSAGES
,
1035 ARG_ROOT_PASSWORD_FILE
,
1036 ARG_ROOT_PASSWORD_HASHED
,
1038 ARG_KERNEL_COMMAND_LINE
,
1042 ARG_PROMPT_TIMEZONE
,
1043 ARG_PROMPT_HOSTNAME
,
1044 ARG_PROMPT_ROOT_PASSWORD
,
1045 ARG_PROMPT_ROOT_SHELL
,
1050 ARG_COPY_ROOT_PASSWORD
,
1051 ARG_COPY_ROOT_SHELL
,
1052 ARG_SETUP_MACHINE_ID
,
1054 ARG_DELETE_ROOT_PASSWORD
,
1058 static const struct option options
[] = {
1059 { "help", no_argument
, NULL
, 'h' },
1060 { "version", no_argument
, NULL
, ARG_VERSION
},
1061 { "root", required_argument
, NULL
, ARG_ROOT
},
1062 { "image", required_argument
, NULL
, ARG_IMAGE
},
1063 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1064 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1065 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1066 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1067 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1068 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1069 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1070 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1071 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1072 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1073 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1074 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1075 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1076 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1077 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1078 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1079 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1080 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1081 { "copy", no_argument
, NULL
, ARG_COPY
},
1082 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1083 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1084 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1085 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1086 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1087 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1088 { "force", no_argument
, NULL
, ARG_FORCE
},
1089 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1090 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1099 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1110 r
= parse_path_argument(optarg
, true, &arg_root
);
1116 r
= parse_path_argument(optarg
, false, &arg_image
);
1122 r
= free_and_strdup(&arg_locale
, optarg
);
1128 case ARG_LOCALE_MESSAGES
:
1129 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1136 if (!keymap_is_valid(optarg
))
1137 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1138 "Keymap %s is not valid.", optarg
);
1140 r
= free_and_strdup(&arg_keymap
, optarg
);
1147 if (!timezone_is_valid(optarg
, LOG_ERR
))
1148 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1149 "Timezone %s is not valid.", optarg
);
1151 r
= free_and_strdup(&arg_timezone
, optarg
);
1157 case ARG_ROOT_PASSWORD
:
1158 r
= free_and_strdup(&arg_root_password
, optarg
);
1162 arg_root_password_is_hashed
= false;
1165 case ARG_ROOT_PASSWORD_FILE
:
1166 arg_root_password
= mfree(arg_root_password
);
1168 r
= read_one_line_file(optarg
, &arg_root_password
);
1170 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1172 arg_root_password_is_hashed
= false;
1175 case ARG_ROOT_PASSWORD_HASHED
:
1176 r
= free_and_strdup(&arg_root_password
, optarg
);
1180 arg_root_password_is_hashed
= true;
1183 case ARG_ROOT_SHELL
:
1184 r
= find_shell(optarg
, arg_root
);
1188 r
= free_and_strdup(&arg_root_shell
, optarg
);
1195 if (!hostname_is_valid(optarg
, VALID_HOSTNAME_TRAILING_DOT
))
1196 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1197 "Host name %s is not valid.", optarg
);
1199 r
= free_and_strdup(&arg_hostname
, optarg
);
1203 hostname_cleanup(arg_hostname
);
1206 case ARG_MACHINE_ID
:
1207 r
= sd_id128_from_string(optarg
, &arg_machine_id
);
1209 return log_error_errno(r
, "Failed to parse machine id %s.", optarg
);
1213 case ARG_KERNEL_COMMAND_LINE
:
1214 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1221 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1222 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1225 case ARG_PROMPT_LOCALE
:
1226 arg_prompt_locale
= true;
1229 case ARG_PROMPT_KEYMAP
:
1230 arg_prompt_keymap
= true;
1233 case ARG_PROMPT_TIMEZONE
:
1234 arg_prompt_timezone
= true;
1237 case ARG_PROMPT_HOSTNAME
:
1238 arg_prompt_hostname
= true;
1241 case ARG_PROMPT_ROOT_PASSWORD
:
1242 arg_prompt_root_password
= true;
1245 case ARG_PROMPT_ROOT_SHELL
:
1246 arg_prompt_root_shell
= true;
1250 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1251 arg_copy_root_shell
= true;
1254 case ARG_COPY_LOCALE
:
1255 arg_copy_locale
= true;
1258 case ARG_COPY_KEYMAP
:
1259 arg_copy_keymap
= true;
1262 case ARG_COPY_TIMEZONE
:
1263 arg_copy_timezone
= true;
1266 case ARG_COPY_ROOT_PASSWORD
:
1267 arg_copy_root_password
= true;
1270 case ARG_COPY_ROOT_SHELL
:
1271 arg_copy_root_shell
= true;
1274 case ARG_SETUP_MACHINE_ID
:
1275 r
= sd_id128_randomize(&arg_machine_id
);
1277 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1285 case ARG_DELETE_ROOT_PASSWORD
:
1286 arg_delete_root_password
= true;
1290 r
= parse_boolean(optarg
);
1292 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1301 assert_not_reached();
1304 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1305 * account when looking for the locale files. */
1307 if (arg_locale
&& !locale_is_ok(arg_locale
))
1308 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1309 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1310 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1312 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1313 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1314 "--delete-root-password cannot be combined with other root password options");
1316 if (arg_image
&& arg_root
)
1317 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1322 static int run(int argc
, char *argv
[]) {
1323 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1324 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1325 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1328 r
= parse_argv(argc
, argv
);
1336 if (!arg_root
&& !arg_image
) {
1339 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1340 * command line option, because we are called to provision the host with basic settings (as
1341 * opposed to some other file system tree/image) */
1343 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1345 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1346 if (r
> 0 && !enabled
)
1347 return 0; /* disabled */
1353 r
= mount_image_privately_interactively(
1355 DISSECT_IMAGE_GENERIC_ROOT
|
1356 DISSECT_IMAGE_REQUIRE_ROOT
|
1357 DISSECT_IMAGE_VALIDATE_OS
|
1358 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1359 DISSECT_IMAGE_FSCK
|
1360 DISSECT_IMAGE_GROWFS
,
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
);