1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/loop.h>
10 #include "alloc-util.h"
11 #include "ask-password-api.h"
13 #include "dissect-image.h"
18 #include "hostname-util.h"
20 #include "libcrypt-util.h"
21 #include "locale-util.h"
22 #include "main-func.h"
23 #include "memory-util.h"
25 #include "mount-util.h"
27 #include "parse-util.h"
28 #include "path-util.h"
29 #include "pretty-print.h"
30 #include "proc-cmdline.h"
31 #include "pwquality-util.h"
32 #include "random-util.h"
33 #include "string-util.h"
35 #include "terminal-util.h"
36 #include "time-util.h"
37 #include "tmpfile-util-label.h"
38 #include "tmpfile-util.h"
39 #include "umask-util.h"
40 #include "user-util.h"
42 static char *arg_root
= NULL
;
43 static char *arg_image
= NULL
;
44 static char *arg_locale
= NULL
; /* $LANG */
45 static char *arg_keymap
= NULL
;
46 static char *arg_locale_messages
= NULL
; /* $LC_MESSAGES */
47 static char *arg_timezone
= NULL
;
48 static char *arg_hostname
= NULL
;
49 static sd_id128_t arg_machine_id
= {};
50 static char *arg_root_password
= NULL
;
51 static char *arg_root_shell
= NULL
;
52 static char *arg_kernel_cmdline
= NULL
;
53 static bool arg_prompt_locale
= false;
54 static bool arg_prompt_keymap
= false;
55 static bool arg_prompt_timezone
= false;
56 static bool arg_prompt_hostname
= false;
57 static bool arg_prompt_root_password
= false;
58 static bool arg_prompt_root_shell
= false;
59 static bool arg_copy_locale
= false;
60 static bool arg_copy_keymap
= false;
61 static bool arg_copy_timezone
= false;
62 static bool arg_copy_root_password
= false;
63 static bool arg_copy_root_shell
= false;
64 static bool arg_force
= false;
65 static bool arg_delete_root_password
= false;
66 static bool arg_root_password_is_hashed
= false;
67 static bool arg_welcome
= true;
69 STATIC_DESTRUCTOR_REGISTER(arg_root
, freep
);
70 STATIC_DESTRUCTOR_REGISTER(arg_image
, freep
);
71 STATIC_DESTRUCTOR_REGISTER(arg_locale
, freep
);
72 STATIC_DESTRUCTOR_REGISTER(arg_locale_messages
, freep
);
73 STATIC_DESTRUCTOR_REGISTER(arg_keymap
, freep
);
74 STATIC_DESTRUCTOR_REGISTER(arg_timezone
, freep
);
75 STATIC_DESTRUCTOR_REGISTER(arg_hostname
, freep
);
76 STATIC_DESTRUCTOR_REGISTER(arg_root_password
, erase_and_freep
);
78 static bool press_any_key(void) {
82 printf("-- Press any key to proceed --");
85 (void) read_one_char(stdin
, &k
, USEC_INFINITY
, &need_nl
);
93 static void print_welcome(void) {
94 _cleanup_free_
char *pretty_name
= NULL
, *ansi_color
= NULL
;
95 static bool done
= false;
105 r
= parse_os_release(
107 "PRETTY_NAME", &pretty_name
,
108 "ANSI_COLOR", &ansi_color
,
111 log_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_WARNING
, r
,
112 "Failed to read os-release file, ignoring: %m");
114 pn
= isempty(pretty_name
) ? "Linux" : pretty_name
;
115 ac
= isempty(ansi_color
) ? "0" : ansi_color
;
117 if (colors_enabled())
118 printf("\nWelcome to your new installation of \x1B[%sm%s\x1B[0m!\n", ac
, pn
);
120 printf("\nWelcome to your new installation of %s!\n", pn
);
122 printf("\nPlease configure your system!\n\n");
129 static int show_menu(char **x
, unsigned n_columns
, unsigned width
, unsigned percentage
) {
130 unsigned break_lines
, break_modulo
;
131 size_t n
, per_column
, i
, j
;
133 assert(n_columns
> 0);
136 per_column
= DIV_ROUND_UP(n
, n_columns
);
138 break_lines
= lines();
142 /* The first page gets two extra lines, since we want to show
144 break_modulo
= break_lines
;
145 if (break_modulo
> 3)
148 for (i
= 0; i
< per_column
; i
++) {
150 for (j
= 0; j
< n_columns
; j
++) {
151 _cleanup_free_
char *e
= NULL
;
153 if (j
* per_column
+ i
>= n
)
156 e
= ellipsize(x
[j
* per_column
+ i
], width
, percentage
);
160 printf("%4zu) %-*s", j
* per_column
+ i
+ 1, width
, e
);
165 /* on the first screen we reserve 2 extra lines for the title */
166 if (i
% break_lines
== break_modulo
) {
167 if (!press_any_key())
175 static int prompt_loop(const char *text
, char **l
, unsigned percentage
, bool (*is_valid
)(const char *name
), char **ret
) {
183 _cleanup_free_
char *p
= NULL
;
186 r
= ask_string(&p
, "%s %s (empty to skip, \"list\" to list options): ",
187 special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), text
);
189 return log_error_errno(r
, "Failed to query user: %m");
192 log_warning("No data entered, skipping.");
196 if (streq(p
, "list")) {
197 r
= show_menu(l
, 3, 22, percentage
);
205 r
= safe_atou(p
, &u
);
207 if (u
<= 0 || u
> strv_length(l
)) {
208 log_error("Specified entry number out of range.");
212 log_info("Selected '%s'.", l
[u
-1]);
213 if (free_and_strdup(ret
, l
[u
-1]) < 0)
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
;
240 if (arg_locale
|| arg_locale_messages
)
243 if (!arg_prompt_locale
)
246 r
= get_locales(&locales
);
248 return log_error_errno(r
, "Cannot query locales list: %m");
250 if (strv_isempty(locales
))
251 log_debug("No locales found, skipping locale selection.");
252 else if (strv_length(locales
) == 1) {
254 if (streq(locales
[0], SYSTEMD_DEFAULT_LOCALE
))
255 log_debug("Only installed locale is default locale anyway, not setting locale explicitly.");
257 log_debug("Only a single locale available (%s), selecting it as default.", locales
[0]);
259 arg_locale
= strdup(locales
[0]);
263 /* Not setting arg_locale_message here, since it defaults to LANG anyway */
268 r
= prompt_loop("Please enter system locale name or number",
269 locales
, 60, locale_is_ok
, &arg_locale
);
273 if (isempty(arg_locale
))
276 r
= prompt_loop("Please enter system message locale name or number",
277 locales
, 60, locale_is_ok
, &arg_locale_messages
);
281 /* Suppress the messages setting if it's the same as the main locale anyway */
282 if (streq_ptr(arg_locale
, arg_locale_messages
))
283 arg_locale_messages
= mfree(arg_locale_messages
);
289 static int process_locale(void) {
290 const char *etc_localeconf
;
295 etc_localeconf
= prefix_roota(arg_root
, "/etc/locale.conf");
296 if (laccess(etc_localeconf
, F_OK
) >= 0 && !arg_force
)
299 if (arg_copy_locale
&& arg_root
) {
301 (void) mkdir_parents(etc_localeconf
, 0755);
302 r
= copy_file("/etc/locale.conf", etc_localeconf
, 0, 0644, 0, 0, COPY_REFLINK
);
305 return log_error_errno(r
, "Failed to copy %s: %m", etc_localeconf
);
307 log_info("%s copied.", etc_localeconf
);
316 if (!isempty(arg_locale
))
317 locales
[i
++] = strjoina("LANG=", arg_locale
);
318 if (!isempty(arg_locale_messages
) && !streq(arg_locale_messages
, arg_locale
))
319 locales
[i
++] = strjoina("LC_MESSAGES=", arg_locale_messages
);
326 (void) mkdir_parents(etc_localeconf
, 0755);
327 r
= write_env_file(etc_localeconf
, locales
);
329 return log_error_errno(r
, "Failed to write %s: %m", etc_localeconf
);
331 log_info("%s written.", etc_localeconf
);
335 static int prompt_keymap(void) {
336 _cleanup_strv_free_
char **kmaps
= NULL
;
342 if (!arg_prompt_keymap
)
345 r
= get_keymaps(&kmaps
);
346 if (r
== -ENOENT
) /* no keymaps installed */
349 return log_error_errno(r
, "Failed to read keymaps: %m");
353 return prompt_loop("Please enter system keymap name or number",
354 kmaps
, 60, keymap_is_valid
, &arg_keymap
);
357 static int process_keymap(void) {
358 const char *etc_vconsoleconf
;
362 etc_vconsoleconf
= prefix_roota(arg_root
, "/etc/vconsole.conf");
363 if (laccess(etc_vconsoleconf
, F_OK
) >= 0 && !arg_force
)
366 if (arg_copy_keymap
&& arg_root
) {
368 (void) mkdir_parents(etc_vconsoleconf
, 0755);
369 r
= copy_file("/etc/vconsole.conf", etc_vconsoleconf
, 0, 0644, 0, 0, COPY_REFLINK
);
372 return log_error_errno(r
, "Failed to copy %s: %m", etc_vconsoleconf
);
374 log_info("%s copied.", etc_vconsoleconf
);
381 return 0; /* don't fail if no keymaps are installed */
385 if (isempty(arg_keymap
))
388 keymap
= STRV_MAKE(strjoina("KEYMAP=", arg_keymap
));
390 r
= mkdir_parents(etc_vconsoleconf
, 0755);
392 return log_error_errno(r
, "Failed to create the parent directory of %s: %m", etc_vconsoleconf
);
394 r
= write_env_file(etc_vconsoleconf
, keymap
);
396 return log_error_errno(r
, "Failed to write %s: %m", etc_vconsoleconf
);
398 log_info("%s written.", etc_vconsoleconf
);
402 static bool timezone_is_valid_log_error(const char *name
) {
403 return timezone_is_valid(name
, LOG_ERR
);
406 static int prompt_timezone(void) {
407 _cleanup_strv_free_
char **zones
= NULL
;
413 if (!arg_prompt_timezone
)
416 r
= get_timezones(&zones
);
418 return log_error_errno(r
, "Cannot query timezone list: %m");
422 r
= prompt_loop("Please enter timezone name or number",
423 zones
, 30, timezone_is_valid_log_error
, &arg_timezone
);
430 static int process_timezone(void) {
431 const char *etc_localtime
, *e
;
434 etc_localtime
= prefix_roota(arg_root
, "/etc/localtime");
435 if (laccess(etc_localtime
, F_OK
) >= 0 && !arg_force
)
438 if (arg_copy_timezone
&& arg_root
) {
439 _cleanup_free_
char *p
= NULL
;
441 r
= readlink_malloc("/etc/localtime", &p
);
444 return log_error_errno(r
, "Failed to read host timezone: %m");
446 (void) mkdir_parents(etc_localtime
, 0755);
447 if (symlink(p
, etc_localtime
) < 0)
448 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
450 log_info("%s copied.", etc_localtime
);
455 r
= prompt_timezone();
459 if (isempty(arg_timezone
))
462 e
= strjoina("../usr/share/zoneinfo/", arg_timezone
);
464 (void) mkdir_parents(etc_localtime
, 0755);
465 if (symlink(e
, etc_localtime
) < 0)
466 return log_error_errno(errno
, "Failed to create %s symlink: %m", etc_localtime
);
468 log_info("%s written", etc_localtime
);
472 static int prompt_hostname(void) {
478 if (!arg_prompt_hostname
)
485 _cleanup_free_
char *h
= NULL
;
487 r
= ask_string(&h
, "%s Please enter hostname for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
489 return log_error_errno(r
, "Failed to query hostname: %m");
492 log_warning("No hostname entered, skipping.");
496 if (!hostname_is_valid(h
, true)) {
497 log_error("Specified hostname invalid.");
501 /* Get rid of the trailing dot that we allow, but don't want to see */
502 arg_hostname
= hostname_cleanup(h
);
510 static int process_hostname(void) {
511 const char *etc_hostname
;
514 etc_hostname
= prefix_roota(arg_root
, "/etc/hostname");
515 if (laccess(etc_hostname
, F_OK
) >= 0 && !arg_force
)
518 r
= prompt_hostname();
522 if (isempty(arg_hostname
))
525 r
= write_string_file(etc_hostname
, arg_hostname
,
526 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
527 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
529 return log_error_errno(r
, "Failed to write %s: %m", etc_hostname
);
531 log_info("%s written.", etc_hostname
);
535 static int process_machine_id(void) {
536 const char *etc_machine_id
;
537 char id
[SD_ID128_STRING_MAX
];
540 etc_machine_id
= prefix_roota(arg_root
, "/etc/machine-id");
541 if (laccess(etc_machine_id
, F_OK
) >= 0 && !arg_force
)
544 if (sd_id128_is_null(arg_machine_id
))
547 r
= write_string_file(etc_machine_id
, sd_id128_to_string(arg_machine_id
, id
),
548 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
549 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
551 return log_error_errno(r
, "Failed to write machine id: %m");
553 log_info("%s written.", etc_machine_id
);
557 static int prompt_root_password(void) {
558 const char *msg1
, *msg2
;
561 if (arg_root_password
)
564 if (!arg_prompt_root_password
)
570 msg1
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter a new root password (empty to skip):");
571 msg2
= strjoina(special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
), " Please enter new root password again:");
576 _cleanup_strv_free_erase_
char **a
= NULL
, **b
= NULL
;
577 _cleanup_free_
char *error
= NULL
;
579 r
= ask_password_tty(-1, msg1
, NULL
, 0, 0, NULL
, &a
);
581 return log_error_errno(r
, "Failed to query root password: %m");
582 if (strv_length(a
) != 1)
583 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
584 "Received multiple passwords, where we expected one.");
587 log_warning("No password entered, skipping.");
591 r
= quality_check_password(*a
, "root", &error
);
593 return log_error_errno(r
, "Failed to check quality of password: %m");
595 log_warning("Password is weak, accepting anyway: %s", error
);
597 r
= ask_password_tty(-1, msg2
, NULL
, 0, 0, NULL
, &b
);
599 return log_error_errno(r
, "Failed to query root password: %m");
600 if (strv_length(b
) != 1)
601 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
602 "Received multiple passwords, where we expected one.");
604 if (!streq(*a
, *b
)) {
605 log_error("Entered passwords did not match, please try again.");
609 arg_root_password
= TAKE_PTR(*a
);
616 static int find_shell(const char *path
, const char *root
) {
621 if (!valid_shell(path
))
622 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "%s is not a valid shell", path
);
624 r
= chase_symlinks(path
, root
, CHASE_PREFIX_ROOT
, NULL
, NULL
);
627 p
= prefix_roota(root
, path
);
628 return log_error_errno(r
, "Failed to resolve shell %s: %m", p
);
634 static int prompt_root_shell(void) {
637 if (arg_root_shell
|| !arg_prompt_root_shell
)
644 _cleanup_free_
char *s
= NULL
;
646 r
= ask_string(&s
, "%s Please enter root shell for new system (empty to skip): ", special_glyph(SPECIAL_GLYPH_TRIANGULAR_BULLET
));
648 return log_error_errno(r
, "Failed to query root shell: %m");
651 log_warning("No shell entered, skipping.");
655 r
= find_shell(s
, arg_root
);
659 arg_root_shell
= TAKE_PTR(s
);
666 static int write_root_passwd(const char *passwd_path
, const char *password
, const char *shell
) {
667 _cleanup_fclose_
FILE *original
= NULL
, *passwd
= NULL
;
668 _cleanup_(unlink_and_freep
) char *passwd_tmp
= NULL
;
673 r
= fopen_temporary_label("/etc/passwd", passwd_path
, &passwd
, &passwd_tmp
);
677 original
= fopen(passwd_path
, "re");
681 r
= sync_rights(fileno(original
), fileno(passwd
));
685 while ((r
= fgetpwent_sane(original
, &i
)) > 0) {
687 if (streq(i
->pw_name
, "root")) {
688 i
->pw_passwd
= (char *) password
;
690 i
->pw_shell
= (char *) shell
;
693 r
= putpwent_sane(i
, passwd
);
701 struct passwd root
= {
702 .pw_name
= (char *) "root",
703 .pw_passwd
= (char *) password
,
706 .pw_gecos
= (char *) "Super User",
707 .pw_dir
= (char *) "/root",
708 .pw_shell
= (char *) (shell
?: "/bin/sh"),
714 r
= fchmod(fileno(passwd
), 0644);
718 r
= putpwent_sane(&root
, passwd
);
723 r
= fflush_sync_and_check(passwd
);
727 r
= rename_and_apply_smack_floor_label(passwd_tmp
, passwd_path
);
734 static int write_root_shadow(const char *shadow_path
, const char *hashed_password
) {
735 _cleanup_fclose_
FILE *original
= NULL
, *shadow
= NULL
;
736 _cleanup_(unlink_and_freep
) char *shadow_tmp
= NULL
;
739 assert(hashed_password
);
741 r
= fopen_temporary_label("/etc/shadow", shadow_path
, &shadow
, &shadow_tmp
);
745 original
= fopen(shadow_path
, "re");
749 r
= sync_rights(fileno(original
), fileno(shadow
));
753 while ((r
= fgetspent_sane(original
, &i
)) > 0) {
755 if (streq(i
->sp_namp
, "root")) {
756 i
->sp_pwdp
= (char *) hashed_password
;
757 i
->sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
);
760 r
= putspent_sane(i
, shadow
);
769 .sp_namp
= (char*) "root",
770 .sp_pwdp
= (char *) hashed_password
,
771 .sp_lstchg
= (long) (now(CLOCK_REALTIME
) / USEC_PER_DAY
),
777 .sp_flag
= (unsigned long) -1, /* this appears to be what everybody does ... */
783 r
= fchmod(fileno(shadow
), 0000);
787 r
= putspent_sane(&root
, shadow
);
792 r
= fflush_sync_and_check(shadow
);
796 r
= rename_and_apply_smack_floor_label(shadow_tmp
, shadow_path
);
803 static int process_root_args(void) {
804 _cleanup_close_
int lock
= -1;
805 _cleanup_(erase_and_freep
) char *_hashed_password
= NULL
;
806 const char *password
, *hashed_password
;
807 const char *etc_passwd
, *etc_shadow
;
810 etc_passwd
= prefix_roota(arg_root
, "/etc/passwd");
811 etc_shadow
= prefix_roota(arg_root
, "/etc/shadow");
813 /* We only mess with passwd and shadow if both do not exist or --force is specified. These files are
814 * tightly coupled and hence we make sure we have permission from the user to create/modify both
816 if ((laccess(etc_passwd
, F_OK
) >= 0 || laccess(etc_shadow
, F_OK
) >= 0) && !arg_force
)
818 /* Don't create/modify passwd and shadow if not asked */
819 if (!(arg_root_password
|| arg_prompt_root_password
|| arg_copy_root_password
|| arg_delete_root_password
||
820 arg_root_shell
|| arg_prompt_root_shell
|| arg_copy_root_shell
))
823 (void) mkdir_parents(etc_passwd
, 0755);
825 lock
= take_etc_passwd_lock(arg_root
);
827 return log_error_errno(lock
, "Failed to take a lock on %s: %m", etc_passwd
);
829 if (arg_copy_root_shell
&& arg_root
) {
833 p
= getpwnam("root");
835 return log_error_errno(errno_or_else(EIO
), "Failed to find passwd entry for root: %m");
837 r
= free_and_strdup(&arg_root_shell
, p
->pw_shell
);
842 r
= prompt_root_shell();
846 if (arg_copy_root_password
&& arg_root
) {
850 p
= getspnam("root");
852 return log_error_errno(errno_or_else(EIO
), "Failed to find shadow entry for root: %m");
854 r
= free_and_strdup(&arg_root_password
, p
->sp_pwdp
);
858 arg_root_password_is_hashed
= true;
861 r
= prompt_root_password();
865 if (arg_root_password
&& arg_root_password_is_hashed
) {
867 hashed_password
= arg_root_password
;
868 } else if (arg_root_password
) {
869 r
= hash_password(arg_root_password
, &_hashed_password
);
871 return log_error_errno(r
, "Failed to hash password: %m");
874 hashed_password
= _hashed_password
;
876 } else if (arg_delete_root_password
)
877 password
= hashed_password
= "";
879 password
= hashed_password
= "!";
881 r
= write_root_passwd(etc_passwd
, password
, arg_root_shell
);
883 return log_error_errno(r
, "Failed to write %s: %m", etc_passwd
);
885 log_info("%s written", etc_passwd
);
887 r
= write_root_shadow(etc_shadow
, hashed_password
);
889 return log_error_errno(r
, "Failed to write %s: %m", etc_shadow
);
891 log_info("%s written.", etc_shadow
);
895 static int process_kernel_cmdline(void) {
896 const char *etc_kernel_cmdline
;
899 etc_kernel_cmdline
= prefix_roota(arg_root
, "/etc/kernel/cmdline");
900 if (laccess(etc_kernel_cmdline
, F_OK
) >= 0 && !arg_force
)
903 if (!arg_kernel_cmdline
)
906 r
= write_string_file(etc_kernel_cmdline
, arg_kernel_cmdline
,
907 WRITE_STRING_FILE_CREATE
| WRITE_STRING_FILE_SYNC
| WRITE_STRING_FILE_MKDIR_0755
|
908 (arg_force
? WRITE_STRING_FILE_ATOMIC
: 0));
910 return log_error_errno(r
, "Failed to write %s: %m", etc_kernel_cmdline
);
912 log_info("%s written.", etc_kernel_cmdline
);
916 static int help(void) {
917 _cleanup_free_
char *link
= NULL
;
920 r
= terminal_urlify_man("systemd-firstboot", "1", &link
);
924 printf("%s [OPTIONS...]\n\n"
925 "Configures basic settings of the system.\n\n"
926 " -h --help Show this help\n"
927 " --version Show package version\n"
928 " --root=PATH Operate on an alternate filesystem root\n"
929 " --image=PATH Operate on an alternate filesystem image\n"
930 " --locale=LOCALE Set primary locale (LANG=)\n"
931 " --locale-messages=LOCALE Set message locale (LC_MESSAGES=)\n"
932 " --keymap=KEYMAP Set keymap\n"
933 " --timezone=TIMEZONE Set timezone\n"
934 " --hostname=NAME Set hostname\n"
935 " --machine-ID=ID Set machine ID\n"
936 " --root-password=PASSWORD Set root password from plaintext password\n"
937 " --root-password-file=FILE Set root password from file\n"
938 " --root-password-hashed=HASHED_PASSWORD Set root password from hashed password\n"
939 " --root-shell=SHELL Set root shell\n"
940 " --prompt-locale Prompt the user for locale settings\n"
941 " --prompt-keymap Prompt the user for keymap settings\n"
942 " --prompt-timezone Prompt the user for timezone\n"
943 " --prompt-hostname Prompt the user for hostname\n"
944 " --prompt-root-password Prompt the user for root password\n"
945 " --prompt-root-shell Prompt the user for root shell\n"
946 " --prompt Prompt for all of the above\n"
947 " --copy-locale Copy locale from host\n"
948 " --copy-keymap Copy keymap from host\n"
949 " --copy-timezone Copy timezone from host\n"
950 " --copy-root-password Copy root password from host\n"
951 " --copy-root-shell Copy root shell from host\n"
952 " --copy Copy locale, keymap, timezone, root password\n"
953 " --setup-machine-id Generate a new random machine ID\n"
954 " --force Overwrite existing files\n"
955 " --delete-root-password Delete root password\n"
956 " --welcome=no Disable the welcome text\n"
957 "\nSee the %s for details.\n"
958 , program_invocation_short_name
965 static int parse_argv(int argc
, char *argv
[]) {
978 ARG_ROOT_PASSWORD_FILE
,
979 ARG_ROOT_PASSWORD_HASHED
,
981 ARG_KERNEL_COMMAND_LINE
,
987 ARG_PROMPT_ROOT_PASSWORD
,
988 ARG_PROMPT_ROOT_SHELL
,
993 ARG_COPY_ROOT_PASSWORD
,
995 ARG_SETUP_MACHINE_ID
,
997 ARG_DELETE_ROOT_PASSWORD
,
1001 static const struct option options
[] = {
1002 { "help", no_argument
, NULL
, 'h' },
1003 { "version", no_argument
, NULL
, ARG_VERSION
},
1004 { "root", required_argument
, NULL
, ARG_ROOT
},
1005 { "image", required_argument
, NULL
, ARG_IMAGE
},
1006 { "locale", required_argument
, NULL
, ARG_LOCALE
},
1007 { "locale-messages", required_argument
, NULL
, ARG_LOCALE_MESSAGES
},
1008 { "keymap", required_argument
, NULL
, ARG_KEYMAP
},
1009 { "timezone", required_argument
, NULL
, ARG_TIMEZONE
},
1010 { "hostname", required_argument
, NULL
, ARG_HOSTNAME
},
1011 { "machine-id", required_argument
, NULL
, ARG_MACHINE_ID
},
1012 { "root-password", required_argument
, NULL
, ARG_ROOT_PASSWORD
},
1013 { "root-password-file", required_argument
, NULL
, ARG_ROOT_PASSWORD_FILE
},
1014 { "root-password-hashed", required_argument
, NULL
, ARG_ROOT_PASSWORD_HASHED
},
1015 { "root-shell", required_argument
, NULL
, ARG_ROOT_SHELL
},
1016 { "kernel-command-line", required_argument
, NULL
, ARG_KERNEL_COMMAND_LINE
},
1017 { "prompt", no_argument
, NULL
, ARG_PROMPT
},
1018 { "prompt-locale", no_argument
, NULL
, ARG_PROMPT_LOCALE
},
1019 { "prompt-keymap", no_argument
, NULL
, ARG_PROMPT_KEYMAP
},
1020 { "prompt-timezone", no_argument
, NULL
, ARG_PROMPT_TIMEZONE
},
1021 { "prompt-hostname", no_argument
, NULL
, ARG_PROMPT_HOSTNAME
},
1022 { "prompt-root-password", no_argument
, NULL
, ARG_PROMPT_ROOT_PASSWORD
},
1023 { "prompt-root-shell", no_argument
, NULL
, ARG_PROMPT_ROOT_SHELL
},
1024 { "copy", no_argument
, NULL
, ARG_COPY
},
1025 { "copy-locale", no_argument
, NULL
, ARG_COPY_LOCALE
},
1026 { "copy-keymap", no_argument
, NULL
, ARG_COPY_KEYMAP
},
1027 { "copy-timezone", no_argument
, NULL
, ARG_COPY_TIMEZONE
},
1028 { "copy-root-password", no_argument
, NULL
, ARG_COPY_ROOT_PASSWORD
},
1029 { "copy-root-shell", no_argument
, NULL
, ARG_COPY_ROOT_SHELL
},
1030 { "setup-machine-id", no_argument
, NULL
, ARG_SETUP_MACHINE_ID
},
1031 { "force", no_argument
, NULL
, ARG_FORCE
},
1032 { "delete-root-password", no_argument
, NULL
, ARG_DELETE_ROOT_PASSWORD
},
1033 { "welcome", required_argument
, NULL
, ARG_WELCOME
},
1042 while ((c
= getopt_long(argc
, argv
, "h", options
, NULL
)) >= 0)
1053 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
1059 r
= parse_path_argument_and_warn(optarg
, false, &arg_image
);
1065 r
= free_and_strdup(&arg_locale
, optarg
);
1071 case ARG_LOCALE_MESSAGES
:
1072 r
= free_and_strdup(&arg_locale_messages
, optarg
);
1079 if (!keymap_is_valid(optarg
))
1080 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1081 "Keymap %s is not valid.", optarg
);
1083 r
= free_and_strdup(&arg_keymap
, optarg
);
1090 if (!timezone_is_valid(optarg
, LOG_ERR
))
1091 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1092 "Timezone %s is not valid.", optarg
);
1094 r
= free_and_strdup(&arg_timezone
, optarg
);
1100 case ARG_ROOT_PASSWORD
:
1101 r
= free_and_strdup(&arg_root_password
, optarg
);
1105 arg_root_password_is_hashed
= false;
1108 case ARG_ROOT_PASSWORD_FILE
:
1109 arg_root_password
= mfree(arg_root_password
);
1111 r
= read_one_line_file(optarg
, &arg_root_password
);
1113 return log_error_errno(r
, "Failed to read %s: %m", optarg
);
1115 arg_root_password_is_hashed
= false;
1118 case ARG_ROOT_PASSWORD_HASHED
:
1119 r
= free_and_strdup(&arg_root_password
, optarg
);
1123 arg_root_password_is_hashed
= true;
1126 case ARG_ROOT_SHELL
:
1127 r
= find_shell(optarg
, arg_root
);
1131 r
= free_and_strdup(&arg_root_shell
, optarg
);
1138 if (!hostname_is_valid(optarg
, true))
1139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1140 "Host name %s is not valid.", optarg
);
1142 hostname_cleanup(optarg
);
1143 r
= free_and_strdup(&arg_hostname
, optarg
);
1149 case ARG_MACHINE_ID
:
1150 if (sd_id128_from_string(optarg
, &arg_machine_id
) < 0)
1151 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1152 "Failed to parse machine id %s.", optarg
);
1156 case ARG_KERNEL_COMMAND_LINE
:
1157 r
= free_and_strdup(&arg_kernel_cmdline
, optarg
);
1164 arg_prompt_locale
= arg_prompt_keymap
= arg_prompt_timezone
= arg_prompt_hostname
=
1165 arg_prompt_root_password
= arg_prompt_root_shell
= true;
1168 case ARG_PROMPT_LOCALE
:
1169 arg_prompt_locale
= true;
1172 case ARG_PROMPT_KEYMAP
:
1173 arg_prompt_keymap
= true;
1176 case ARG_PROMPT_TIMEZONE
:
1177 arg_prompt_timezone
= true;
1180 case ARG_PROMPT_HOSTNAME
:
1181 arg_prompt_hostname
= true;
1184 case ARG_PROMPT_ROOT_PASSWORD
:
1185 arg_prompt_root_password
= true;
1188 case ARG_PROMPT_ROOT_SHELL
:
1189 arg_prompt_root_shell
= true;
1193 arg_copy_locale
= arg_copy_keymap
= arg_copy_timezone
= arg_copy_root_password
=
1194 arg_copy_root_shell
= true;
1197 case ARG_COPY_LOCALE
:
1198 arg_copy_locale
= true;
1201 case ARG_COPY_KEYMAP
:
1202 arg_copy_keymap
= true;
1205 case ARG_COPY_TIMEZONE
:
1206 arg_copy_timezone
= true;
1209 case ARG_COPY_ROOT_PASSWORD
:
1210 arg_copy_root_password
= true;
1213 case ARG_COPY_ROOT_SHELL
:
1214 arg_copy_root_shell
= true;
1217 case ARG_SETUP_MACHINE_ID
:
1218 r
= sd_id128_randomize(&arg_machine_id
);
1220 return log_error_errno(r
, "Failed to generate randomized machine ID: %m");
1228 case ARG_DELETE_ROOT_PASSWORD
:
1229 arg_delete_root_password
= true;
1233 r
= parse_boolean(optarg
);
1235 return log_error_errno(r
, "Failed to parse --welcome= argument: %s", optarg
);
1244 assert_not_reached("Unhandled option");
1247 /* We check if the specified locale strings are valid down here, so that we can take --root= into
1248 * account when looking for the locale files. */
1250 if (arg_locale
&& !locale_is_ok(arg_locale
))
1251 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale
);
1252 if (arg_locale_messages
&& !locale_is_ok(arg_locale_messages
))
1253 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Locale %s is not installed.", arg_locale_messages
);
1255 if (arg_delete_root_password
&& (arg_copy_root_password
|| arg_root_password
|| arg_prompt_root_password
))
1256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
1257 "--delete-root-password cannot be combined with other root password options");
1259 if (arg_image
&& arg_root
)
1260 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Please specify either --root= or --image=, the combination of both is not supported.");
1265 static int run(int argc
, char *argv
[]) {
1266 _cleanup_(loop_device_unrefp
) LoopDevice
*loop_device
= NULL
;
1267 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*decrypted_image
= NULL
;
1268 _cleanup_(umount_and_rmdir_and_freep
) char *unlink_dir
= NULL
;
1271 r
= parse_argv(argc
, argv
);
1275 log_setup_service();
1279 if (!arg_root
&& !arg_image
) {
1282 /* If we are called without --root=/--image= let's honour the systemd.firstboot kernel
1283 * command line option, because we are called to provision the host with basic settings (as
1284 * opposed to some other file system tree/image) */
1286 r
= proc_cmdline_get_bool("systemd.firstboot", &enabled
);
1288 return log_error_errno(r
, "Failed to parse systemd.firstboot= kernel command line argument, ignoring: %m");
1289 if (r
> 0 && !enabled
)
1290 return 0; /* disabled */
1296 r
= mount_image_privately_interactively(
1298 DISSECT_IMAGE_REQUIRE_ROOT
|DISSECT_IMAGE_VALIDATE_OS
|DISSECT_IMAGE_RELAX_VAR_CHECK
|DISSECT_IMAGE_FSCK
,
1305 arg_root
= strdup(unlink_dir
);
1310 r
= process_locale();
1314 r
= process_keymap();
1318 r
= process_timezone();
1322 r
= process_hostname();
1326 r
= process_machine_id();
1330 r
= process_root_args();
1334 r
= process_kernel_cmdline();
1341 DEFINE_MAIN_FUNCTION(run
);