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 "glyph-util.h"
20 #include "hostname-util.h"
22 #include "libcrypt-util.h"
23 #include "locale-util.h"
24 #include "main-func.h"
25 #include "memory-util.h"
27 #include "mount-util.h"
29 #include "parse-argument.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "pretty-print.h"
33 #include "proc-cmdline.h"
34 #include "pwquality-util.h"
35 #include "random-util.h"
36 #include "smack-util.h"
37 #include "string-util.h"
39 #include "terminal-util.h"
40 #include "time-util.h"
41 #include "tmpfile-util-label.h"
42 #include "tmpfile-util.h"
43 #include "umask-util.h"
44 #include "user-util.h"
46 static char *arg_root
= NULL
;
47 static char *arg_image
= NULL
;
48 static char *arg_locale
= NULL
; /* $LANG */
49 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
50 static char *arg_keymap
= NULL
;
51 static char *arg_timezone
= NULL
;
52 static char *arg_hostname
= NULL
;
53 static sd_id128_t arg_machine_id
= {};
54 static char *arg_root_password
= NULL
;
55 static char *arg_root_shell
= NULL
;
56 static char *arg_kernel_cmdline
= NULL
;
57 static bool arg_prompt_locale
= false;
58 static bool arg_prompt_keymap
= false;
59 static bool arg_prompt_timezone
= false;
60 static bool arg_prompt_hostname
= false;
61 static bool arg_prompt_root_password
= false;
62 static bool arg_prompt_root_shell
= false;
63 static bool arg_copy_locale
= false;
64 static bool arg_copy_keymap
= false;
65 static bool arg_copy_timezone
= false;
66 static bool arg_copy_root_password
= false;
67 static bool arg_copy_root_shell
= false;
68 static bool arg_force
= false;
69 static bool arg_delete_root_password
= false;
70 static bool arg_root_password_is_hashed
= false;
71 static bool arg_welcome
= true;
73 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
77 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
78 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
79 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
80 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
82 static bool press_any_key(void) {
86 printf("-- Press any key to proceed --");
89 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
97 static void print_welcome(void) {
98 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
99 static bool done
= false;
109 r
= parse_os_release(
111 "PRETTY_NAME", &pretty_name
,
112 "ANSI_COLOR", &ansi_color
);
114 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
115 "Failed to read os-release file, ignoring: %m");
117 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
118 ac
= isempty(ansi_color
) ? "0" : ansi_color
;
120 if (colors_enabled())
121 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac
, pn
);
123 printf("\nWelcome to your new installation of %s!\n", pn
);
125 printf("\nPlease configure your system!\n\n");
132 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
133 unsigned break_lines
, break_modulo
;
134 size_t n
, per_column
, i
, j
;
136 assert(n_columns
> 0);
139 per_column
= DIV_ROUND_UP(n
, n_columns
);
141 break_lines
= lines();
145 /* The first page gets two extra lines, since we want to show
147 break_modulo
= break_lines
;
148 if (break_modulo
> 3)
151 for (i
= 0; i
< per_column
; i
++) {
153 for (j
= 0; j
< n_columns
; j
++) {
154 _cleanup_free_
char *e
= NULL
;
156 if (j
* per_column
+ i
>= n
)
159 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
163 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
168 /* on the first screen we reserve 2 extra lines for the title */
169 if (i
% break_lines
== break_modulo
) {
170 if (!press_any_key())
178 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
186 _cleanup_free_
char *p
= NULL
;
189 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
190 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
192 return log_error_errno(r
, "Failed to query user: %m");
195 log_warning("No data entered, skipping.");
199 if (streq(p
, "list")) {
200 r
= show_menu(l
, 3, 22, percentage
);
208 r
= safe_atou(p
, &u
);
210 if (u
<= 0 || u
> strv_length(l
)) {
211 log_error("Specified entry number out of range.");
215 log_info("Selected '%s'.", l
[u
-1]);
216 return free_and_strdup_warn(ret
, l
[u
-1]);
220 log_error("Entered data invalid.");
224 return free_and_replace(*ret
, p
);
228 static bool locale_is_ok(const char *name
) {
231 return locale_is_valid(name
);
233 return locale_is_installed(name
) > 0;
236 static int prompt_locale(void) {
237 _cleanup_strv_free_
char **locales
= NULL
;
238 bool acquired_from_creds
= false;
241 if (arg_locale
|| arg_locale_messages
)
244 r
= read_credential("firstboot.locale", (void**) &arg_locale
, NULL
);
246 log_debug_errno(r
, "Failed to read credential firstboot.locale, ignoring: %m");
248 acquired_from_creds
= true;
250 r
= read_credential("firstboot.locale-messages", (void**) &arg_locale_messages
, NULL
);
252 log_debug_errno(r
, "Failed to read credential firstboot.locale-message, ignoring: %m");
254 acquired_from_creds
= true;
256 if (acquired_from_creds
) {
257 log_debug("Acquired locale from credentials.");
261 if (!arg_prompt_locale
)
264 r
= get_locales(&locales
);
266 return log_error_errno(r
, "Cannot query locales list: %m");
268 if (strv_isempty(locales
))
269 log_debug("No locales found, skipping locale selection.");
270 else if (strv_length(locales
) == 1) {
272 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
273 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
275 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
277 arg_locale
= strdup(locales
[0]);
281 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
286 r
= prompt_loop("Please enter system locale name or number",
287 locales
, 60, locale_is_ok
, &arg_locale
);
291 if (isempty(arg_locale
))
294 r
= prompt_loop("Please enter system message locale name or number",
295 locales
, 60, locale_is_ok
, &arg_locale_messages
);
299 /* Suppress the messages setting if it's the same as the main locale anyway */
300 if (streq_ptr(arg_locale
, arg_locale_messages
))
301 arg_locale_messages
= mfree(arg_locale_messages
);
307 static int process_locale(void) {
308 const char *etc_localeconf
;
313 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
314 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
317 if (arg_copy_locale
&& arg_root
) {
319 (void) mkdir_parents(etc_localeconf
, 0755);
320 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
323 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
325 log_info("%s copied.", etc_localeconf
);
334 if (!isempty(arg_locale
))
335 locales
[i
++] = strjoina("LANG=", arg_locale
);
336 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
337 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
344 (void) mkdir_parents(etc_localeconf
, 0755);
345 r
= write_env_file(etc_localeconf
, locales
);
347 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
349 log_info("%s written.", etc_localeconf
);
353 static int prompt_keymap(void) {
354 _cleanup_strv_free_
char **kmaps
= NULL
;
360 r
= read_credential("firstboot.keymap", (void**) &arg_keymap
, NULL
);
362 log_debug_errno(r
, "Failed to read credential firstboot.keymap, ignoring: %m");
364 log_debug("Acquired keymap from credential.");
368 if (!arg_prompt_keymap
)
371 r
= get_keymaps(&kmaps
);
372 if (r
== -ENOENT
) /* no keymaps installed */
375 return log_error_errno(r
, "Failed to read keymaps: %m");
379 return prompt_loop("Please enter system keymap name or number",
380 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
383 static int process_keymap(void) {
384 const char *etc_vconsoleconf
;
388 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
389 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
392 if (arg_copy_keymap
&& arg_root
) {
394 (void) mkdir_parents(etc_vconsoleconf
, 0755);
395 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
398 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
400 log_info("%s copied.", etc_vconsoleconf
);
407 return 0; /* don't fail if no keymaps are installed */
411 if (isempty(arg_keymap
))
414 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
416 r
= mkdir_parents(etc_vconsoleconf
, 0755);
418 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
420 r
= write_env_file(etc_vconsoleconf
, keymap
);
422 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
424 log_info("%s written.", etc_vconsoleconf
);
428 static bool timezone_is_valid_log_error(const char *name
) {
429 return timezone_is_valid(name
, LOG_ERR
);
432 static int prompt_timezone(void) {
433 _cleanup_strv_free_
char **zones
= NULL
;
439 r
= read_credential("firstboot.timezone", (void**) &arg_timezone
, NULL
);
441 log_debug_errno(r
, "Failed to read credential firstboot.timezone, ignoring: %m");
443 log_debug("Acquired timezone from credential.");
447 if (!arg_prompt_timezone
)
450 r
= get_timezones(&zones
);
452 return log_error_errno(r
, "Cannot query timezone list: %m");
456 r
= prompt_loop("Please enter timezone name or number",
457 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
464 static int process_timezone(void) {
465 const char *etc_localtime
, *e
;
468 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
469 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
472 if (arg_copy_timezone
&& arg_root
) {
473 _cleanup_free_
char *p
= NULL
;
475 r
= readlink_malloc("/etc/localtime", &p
);
478 return log_error_errno(r
, "Failed to read host timezone: %m");
480 (void) mkdir_parents(etc_localtime
, 0755);
481 if (symlink(p
, etc_localtime
) < 0)
482 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
484 log_info("%s copied.", etc_localtime
);
489 r
= prompt_timezone();
493 if (isempty(arg_timezone
))
496 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
498 (void) mkdir_parents(etc_localtime
, 0755);
499 if (symlink(e
, etc_localtime
) < 0)
500 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
502 log_info("%s written", etc_localtime
);
506 static int prompt_hostname(void) {
512 if (!arg_prompt_hostname
)
519 _cleanup_free_
char *h
= NULL
;
521 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
523 return log_error_errno(r
, "Failed to query hostname: %m");
526 log_warning("No hostname entered, skipping.");
530 if (!hostname_is_valid(h
, VALID_HOSTNAME_TRAILING_DOT
)) {
531 log_error("Specified hostname invalid.");
535 /* Get rid of the trailing dot that we allow, but don't want to see */
536 arg_hostname
= hostname_cleanup(h
);
544 static int process_hostname(void) {
545 const char *etc_hostname
;
548 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
549 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
552 r
= prompt_hostname();
556 if (isempty(arg_hostname
))
559 r
= write_string_file(etc_hostname
, arg_hostname
,
560 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
561 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
563 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
565 log_info("%s written.", etc_hostname
);
569 static int process_machine_id(void) {
570 const char *etc_machine_id
;
573 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
574 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
577 if (sd_id128_is_null(arg_machine_id
))
580 r
= write_string_file(etc_machine_id
, SD_ID128_TO_STRING(arg_machine_id
),
581 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
582 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
584 return log_error_errno(r
, "Failed to write machine id: %m");
586 log_info("%s written.", etc_machine_id
);
590 static int prompt_root_password(void) {
591 const char *msg1
, *msg2
;
594 if (arg_root_password
)
597 r
= read_credential("passwd.hashed-password.root", (void**) &arg_root_password
, NULL
);
599 r
= read_credential("passwd.plaintext-password.root", (void**) &arg_root_password
, NULL
);
601 log_debug_errno(r
, "Couldn't read credential 'passwd.{hashed|plaintext}-password.root', ignoring: %m");
603 arg_root_password_is_hashed
= false;
607 log_debug_errno(r
, "Couldn't read credential 'passwd.hashed-password.root', ignoring: %m");
609 arg_root_password_is_hashed
= true;
613 if (!arg_prompt_root_password
)
619 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
620 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
625 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
626 _cleanup_free_
char *error
= NULL
;
628 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
630 return log_error_errno(r
, "Failed to query root password: %m");
631 if (strv_length(a
) != 1)
632 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
633 "Received multiple passwords, where we expected one.");
636 log_warning("No password entered, skipping.");
640 r
= quality_check_password(*a
, "root", &error
);
642 return log_error_errno(r
, "Failed to check quality of password: %m");
644 log_warning("Password is weak, accepting anyway: %s", error
);
646 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
648 return log_error_errno(r
, "Failed to query root password: %m");
649 if (strv_length(b
) != 1)
650 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
651 "Received multiple passwords, where we expected one.");
653 if (!streq(*a
, *b
)) {
654 log_error("Entered passwords did not match, please try again.");
658 arg_root_password
= TAKE_PTR(*a
);
665 static int find_shell(const char *path
, const char *root
) {
670 if (!valid_shell(path
))
671 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
673 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, NULL
, NULL
);
676 p
= prefix_roota(root
, path
);
677 return log_error_errno(r
, "Failed to resolve shell %s: %m", p
);
683 static int prompt_root_shell(void) {
689 r
= read_credential("passwd.shell.root", (void**) &arg_root_shell
, NULL
);
691 log_debug_errno(r
, "Failed to read credential passwd.shell.root, ignoring: %m");
693 log_debug("Acquired root shell from credential.");
697 if (!arg_prompt_root_shell
)
704 _cleanup_free_
char *s
= NULL
;
706 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
708 return log_error_errno(r
, "Failed to query root shell: %m");
711 log_warning("No shell entered, skipping.");
715 r
= find_shell(s
, arg_root
);
719 arg_root_shell
= TAKE_PTR(s
);
726 static int write_root_passwd(const char *passwd_path
, const char *password
, const char *shell
) {
727 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
728 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
733 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
737 original
= fopen(passwd_path
, "re");
741 r
= copy_rights(fileno(original
), fileno(passwd
));
745 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
747 if (streq(i
->pw_name
, "root")) {
748 i
->pw_passwd
= (char *) password
;
750 i
->pw_shell
= (char *) shell
;
753 r
= putpwent_sane(i
, passwd
);
761 struct passwd root
= {
762 .pw_name
= (char *) "root",
763 .pw_passwd
= (char *) password
,
766 .pw_gecos
= (char *) "Super User",
767 .pw_dir
= (char *) "/root",
768 .pw_shell
= (char *) (shell
?: "/bin/sh"),
774 r
= fchmod(fileno(passwd
), 0644);
778 r
= putpwent_sane(&root
, passwd
);
783 r
= fflush_sync_and_check(passwd
);
787 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
794 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
795 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
796 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
799 assert(hashed_password
);
801 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
805 original
= fopen(shadow_path
, "re");
809 r
= copy_rights(fileno(original
), fileno(shadow
));
813 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
815 if (streq(i
->sp_namp
, "root")) {
816 i
->sp_pwdp
= (char *) hashed_password
;
817 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
820 r
= putspent_sane(i
, shadow
);
829 .sp_namp
= (char*) "root",
830 .sp_pwdp
= (char *) hashed_password
,
831 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
837 .sp_flag
= ULONG_MAX
, /* this appears to be what everybody does ... */
843 r
= fchmod(fileno(shadow
), 0000);
847 r
= putspent_sane(&root
, shadow
);
852 r
= fflush_sync_and_check(shadow
);
856 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
863 static int process_root_args(void) {
864 _cleanup_close_
int lock
= -1;
865 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
866 const char *password
, *hashed_password
;
867 const char *etc_passwd
, *etc_shadow
;
870 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
871 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
873 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
874 * tightly coupled and hence we make sure we have permission from the user to create/modify both
876 if ((laccess(etc_passwd
, F_OK
) >= 0 || laccess(etc_shadow
, F_OK
) >= 0) && !arg_force
)
878 /* Don't create/modify passwd and shadow if not asked */
879 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
880 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
))
883 (void) mkdir_parents(etc_passwd
, 0755);
885 lock
= take_etc_passwd_lock(arg_root
);
887 return log_error_errno(lock
, "Failed to take a lock on %s: %m", etc_passwd
);
889 if (arg_copy_root_shell
&& arg_root
) {
893 p
= getpwnam("root");
895 return log_error_errno(errno_or_else(EIO
), "Failed to find passwd entry for root: %m");
897 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
902 r
= prompt_root_shell();
906 if (arg_copy_root_password
&& arg_root
) {
910 p
= getspnam("root");
912 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
914 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
918 arg_root_password_is_hashed
= true;
921 r
= prompt_root_password();
925 if (arg_root_password
&& arg_root_password_is_hashed
) {
926 password
= PASSWORD_SEE_SHADOW
;
927 hashed_password
= arg_root_password
;
928 } else if (arg_root_password
) {
929 r
= hash_password(arg_root_password
, &_hashed_password
);
931 return log_error_errno(r
, "Failed to hash password: %m");
933 password
= PASSWORD_SEE_SHADOW
;
934 hashed_password
= _hashed_password
;
936 } else if (arg_delete_root_password
)
937 password
= hashed_password
= PASSWORD_NONE
;
939 password
= hashed_password
= PASSWORD_LOCKED_AND_INVALID
;
941 r
= write_root_passwd(etc_passwd
, password
, arg_root_shell
);
943 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
945 log_info("%s written", etc_passwd
);
947 r
= write_root_shadow(etc_shadow
, hashed_password
);
949 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
951 log_info("%s written.", etc_shadow
);
955 static int process_kernel_cmdline(void) {
956 const char *etc_kernel_cmdline
;
959 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
960 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
963 if (!arg_kernel_cmdline
)
966 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
967 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
968 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
970 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
972 log_info("%s written.", etc_kernel_cmdline
);
976 static int help(void) {
977 _cleanup_free_
char *link
= NULL
;
980 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
984 printf("%s [OPTIONS...]\n\n"
985 "Configures basic settings of the system.\n\n"
986 " -h --help Show this help\n"
987 " --version Show package version\n"
988 " --root=PATH Operate on an alternate filesystem root\n"
989 " --image=PATH Operate on an alternate filesystem image\n"
990 " --locale=LOCALE Set primary locale (LANG=)\n"
991 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
992 " --keymap=KEYMAP Set keymap\n"
993 " --timezone=TIMEZONE Set timezone\n"
994 " --hostname=NAME Set hostname\n"
995 " --machine-ID=ID Set machine ID\n"
996 " --root-password=PASSWORD Set root password from plaintext password\n"
997 " --root-password-file=FILE Set root password from file\n"
998 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
999 " --root-shell=SHELL Set root shell\n"
1000 " --prompt-locale Prompt the user for locale settings\n"
1001 " --prompt-keymap Prompt the user for keymap settings\n"
1002 " --prompt-timezone Prompt the user for timezone\n"
1003 " --prompt-hostname Prompt the user for hostname\n"
1004 " --prompt-root-password Prompt the user for root password\n"
1005 " --prompt-root-shell Prompt the user for root shell\n"
1006 " --prompt Prompt for all of the above\n"
1007 " --copy-locale Copy locale from host\n"
1008 " --copy-keymap Copy keymap from host\n"
1009 " --copy-timezone Copy timezone from host\n"
1010 " --copy-root-password Copy root password from host\n"
1011 " --copy-root-shell Copy root shell from host\n"
1012 " --copy Copy locale, keymap, timezone, root password\n"
1013 " --setup-machine-id Generate a new random machine ID\n"
1014 " --force Overwrite existing files\n"
1015 " --delete-root-password Delete root password\n"
1016 " --welcome=no Disable the welcome text\n"
1017 "\nSee the %s for details.\n",
1018 program_invocation_short_name
,
1024 static int parse_argv(int argc
, char *argv
[]) {
1027 ARG_VERSION
= 0x100,
1031 ARG_LOCALE_MESSAGES
,
1037 ARG_ROOT_PASSWORD_FILE
,
1038 ARG_ROOT_PASSWORD_HASHED
,
1040 ARG_KERNEL_COMMAND_LINE
,
1044 ARG_PROMPT_TIMEZONE
,
1045 ARG_PROMPT_HOSTNAME
,
1046 ARG_PROMPT_ROOT_PASSWORD
,
1047 ARG_PROMPT_ROOT_SHELL
,
1052 ARG_COPY_ROOT_PASSWORD
,
1053 ARG_COPY_ROOT_SHELL
,
1054 ARG_SETUP_MACHINE_ID
,
1056 ARG_DELETE_ROOT_PASSWORD
,
1060 static const struct option options
[] = {
1061 { "help", no_argument
, NULL
, 'h' },
1062 { "version", no_argument
, NULL
, ARG_VERSION
},
1063 { "root", required_argument
, NULL
, ARG_ROOT
},
1064 { "image", required_argument
, NULL
, ARG_IMAGE
},
1065 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1066 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1067 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1068 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1069 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1070 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1071 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1072 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1073 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1074 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1075 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1076 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1077 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1078 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1079 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1080 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1081 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1082 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1083 { "copy", no_argument
, NULL
, ARG_COPY
},
1084 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1085 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1086 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1087 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1088 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1089 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1090 { "force", no_argument
, NULL
, ARG_FORCE
},
1091 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1092 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1101 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1112 r
= parse_path_argument(optarg
, true, &arg_root
);
1118 r
= parse_path_argument(optarg
, false, &arg_image
);
1124 r
= free_and_strdup(&arg_locale
, optarg
);
1130 case ARG_LOCALE_MESSAGES
:
1131 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1138 if (!keymap_is_valid(optarg
))
1139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1140 "Keymap %s is not valid.", optarg
);
1142 r
= free_and_strdup(&arg_keymap
, optarg
);
1149 if (!timezone_is_valid(optarg
, LOG_ERR
))
1150 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1151 "Timezone %s is not valid.", optarg
);
1153 r
= free_and_strdup(&arg_timezone
, optarg
);
1159 case ARG_ROOT_PASSWORD
:
1160 r
= free_and_strdup(&arg_root_password
, optarg
);
1164 arg_root_password_is_hashed
= false;
1167 case ARG_ROOT_PASSWORD_FILE
:
1168 arg_root_password
= mfree(arg_root_password
);
1170 r
= read_one_line_file(optarg
, &arg_root_password
);
1172 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1174 arg_root_password_is_hashed
= false;
1177 case ARG_ROOT_PASSWORD_HASHED
:
1178 r
= free_and_strdup(&arg_root_password
, optarg
);
1182 arg_root_password_is_hashed
= true;
1185 case ARG_ROOT_SHELL
:
1186 r
= find_shell(optarg
, arg_root
);
1190 r
= free_and_strdup(&arg_root_shell
, optarg
);
1197 if (!hostname_is_valid(optarg
, VALID_HOSTNAME_TRAILING_DOT
))
1198 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1199 "Host name %s is not valid.", optarg
);
1201 r
= free_and_strdup(&arg_hostname
, optarg
);
1205 hostname_cleanup(arg_hostname
);
1208 case ARG_MACHINE_ID
:
1209 r
= sd_id128_from_string(optarg
, &arg_machine_id
);
1211 return log_error_errno(r
, "Failed to parse machine id %s.", optarg
);
1215 case ARG_KERNEL_COMMAND_LINE
:
1216 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1223 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1224 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1227 case ARG_PROMPT_LOCALE
:
1228 arg_prompt_locale
= true;
1231 case ARG_PROMPT_KEYMAP
:
1232 arg_prompt_keymap
= true;
1235 case ARG_PROMPT_TIMEZONE
:
1236 arg_prompt_timezone
= true;
1239 case ARG_PROMPT_HOSTNAME
:
1240 arg_prompt_hostname
= true;
1243 case ARG_PROMPT_ROOT_PASSWORD
:
1244 arg_prompt_root_password
= true;
1247 case ARG_PROMPT_ROOT_SHELL
:
1248 arg_prompt_root_shell
= true;
1252 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1253 arg_copy_root_shell
= true;
1256 case ARG_COPY_LOCALE
:
1257 arg_copy_locale
= true;
1260 case ARG_COPY_KEYMAP
:
1261 arg_copy_keymap
= true;
1264 case ARG_COPY_TIMEZONE
:
1265 arg_copy_timezone
= true;
1268 case ARG_COPY_ROOT_PASSWORD
:
1269 arg_copy_root_password
= true;
1272 case ARG_COPY_ROOT_SHELL
:
1273 arg_copy_root_shell
= true;
1276 case ARG_SETUP_MACHINE_ID
:
1277 r
= sd_id128_randomize(&arg_machine_id
);
1279 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1287 case ARG_DELETE_ROOT_PASSWORD
:
1288 arg_delete_root_password
= true;
1292 r
= parse_boolean(optarg
);
1294 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1303 assert_not_reached();
1306 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1307 * account when looking for the locale files. */
1309 if (arg_locale
&& !locale_is_ok(arg_locale
))
1310 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1311 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1312 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1314 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1315 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1316 "--delete-root-password cannot be combined with other root password options");
1318 if (arg_image
&& arg_root
)
1319 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1324 static int run(int argc
, char *argv
[]) {
1325 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1326 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1327 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1330 r
= parse_argv(argc
, argv
);
1338 if (!arg_root
&& !arg_image
) {
1341 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1342 * command line option, because we are called to provision the host with basic settings (as
1343 * opposed to some other file system tree/image) */
1345 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1347 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1348 if (r
> 0 && !enabled
)
1349 return 0; /* disabled */
1355 r
= mount_image_privately_interactively(
1357 DISSECT_IMAGE_GENERIC_ROOT
|
1358 DISSECT_IMAGE_REQUIRE_ROOT
|
1359 DISSECT_IMAGE_VALIDATE_OS
|
1360 DISSECT_IMAGE_RELAX_VAR_CHECK
|
1361 DISSECT_IMAGE_FSCK
|
1362 DISSECT_IMAGE_GROWFS
,
1369 arg_root
= strdup(unlink_dir
);
1374 r
= process_locale();
1378 r
= process_keymap();
1382 r
= process_timezone();
1386 r
= process_hostname();
1390 r
= process_machine_id();
1394 r
= process_root_args();
1398 r
= process_kernel_cmdline();
1405 DEFINE_MAIN_FUNCTION(run
);