[PATCH 4/4] bootm: add global.bootm.provide_hostname option

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a new bootm option to automatically provide the $global.hostname via
the kernel commandline parameter systemd.hostname. The logic is based on
the provide_machine_id logic. To only provide valid hostnames the new
barebox_hostname_is_valid() is used therefore we need to make this
function public.

Signed-off-by: Marco Felsch <m.felsch@xxxxxxxxxxxxxx>
---
 common/bootm.c   | 25 +++++++++++++++++++++++++
 common/misc.c    |  2 +-
 include/bootm.h  |  5 +++++
 include/common.h |  1 +
 4 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/common/bootm.c b/common/bootm.c
index 29ea13e28cb8..17d273831558 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -44,6 +44,7 @@ static struct image_handler *bootm_find_handler(enum filetype filetype,
 static int bootm_appendroot;
 static int bootm_earlycon;
 static int bootm_provide_machine_id;
+static int bootm_provide_hostname;
 static int bootm_verbosity;
 
 void bootm_data_init_defaults(struct bootm_data *data)
@@ -61,6 +62,7 @@ void bootm_data_init_defaults(struct bootm_data *data)
 	data->verify = bootm_get_verify_mode();
 	data->appendroot = bootm_appendroot;
 	data->provide_machine_id = bootm_provide_machine_id;
+	data->provide_hostname = bootm_provide_hostname;
 	data->verbose = bootm_verbosity;
 }
 
@@ -797,6 +799,27 @@ int bootm_boot(struct bootm_data *bootm_data)
 		free(machine_id_bootarg);
 	}
 
+	if (bootm_data->provide_hostname) {
+		const char *hostname = getenv_nonempty("global.hostname");
+		char *hostname_bootarg;
+
+		if (!hostname) {
+			pr_err("Providing hostname is enabled but no hostname is set\n");
+			ret = -EINVAL;
+			goto err_out;
+		}
+
+		if (!barebox_hostname_is_valid(hostname)) {
+			pr_err("Provided hostname is not compatible to systemd hostname requirements\n");
+			ret = -EINVAL;
+			goto err_out;
+		}
+
+		hostname_bootarg = basprintf("systemd.hostname=%s", hostname);
+		globalvar_add_simple("linux.bootargs.hostname", hostname_bootarg);
+		free(hostname_bootarg);
+	}
+
 	pr_info("\nLoading %s '%s'", file_type_to_string(os_type),
 		data->os_file);
 	if (os_type == filetype_uimage &&
@@ -950,6 +973,7 @@ static int bootm_init(void)
 	globalvar_add_simple_bool("bootm.appendroot", &bootm_appendroot);
 	globalvar_add_simple_bool("bootm.earlycon", &bootm_earlycon);
 	globalvar_add_simple_bool("bootm.provide_machine_id", &bootm_provide_machine_id);
+	globalvar_add_simple_bool("bootm.provide_hostname", &bootm_provide_hostname);
 	if (IS_ENABLED(CONFIG_BOOTM_INITRD)) {
 		globalvar_add_simple("bootm.initrd", NULL);
 		globalvar_add_simple("bootm.initrd.loadaddr", NULL);
@@ -992,3 +1016,4 @@ BAREBOX_MAGICVAR(global.bootm.earlycon, "Add earlycon option to Kernel for early
 BAREBOX_MAGICVAR(global.bootm.appendroot, "Add root= option to Kernel to mount rootfs from the device the Kernel comes from (default, device can be overridden via global.bootm.root_dev)");
 BAREBOX_MAGICVAR(global.bootm.root_dev, "bootm default root device (overrides default device in global.bootm.appendroot)");
 BAREBOX_MAGICVAR(global.bootm.provide_machine_id, "If true, append systemd.machine_id=$global.machine_id to Kernel command line");
+BAREBOX_MAGICVAR(global.bootm.provide_hostname, "If true, append systemd.hostname=$global.hostname to Kernel command line");
diff --git a/common/misc.c b/common/misc.c
index dc498ad0b318..423af0005be0 100644
--- a/common/misc.c
+++ b/common/misc.c
@@ -157,7 +157,7 @@ static bool barebox_valid_ldh_char(char c)
 	       (c >= '0' && c <= '9') || c == '-';
 }
 
-static bool barebox_hostname_is_valid(const char *s)
+bool barebox_hostname_is_valid(const char *s)
 {
 	unsigned int n_dots = 0;
 	const char *p;
diff --git a/include/bootm.h b/include/bootm.h
index ee2b574521db..c69da85cdda1 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -34,6 +34,11 @@ struct bootm_data {
 	 * value of global.machine_id to Kernel.
 	 */
 	bool provide_machine_id;
+	/*
+	 * provide_hostname - if true, try to add systemd.hostname= with value
+	 * of global.hostname to Kernel.
+	 */
+	bool provide_hostname;
 	unsigned long initrd_address;
 	unsigned long os_address;
 	unsigned long os_entry;
diff --git a/include/common.h b/include/common.h
index b7b4d9e35094..d7b5261bc921 100644
--- a/include/common.h
+++ b/include/common.h
@@ -127,6 +127,7 @@ void barebox_set_model(const char *);
 const char *barebox_get_hostname(void);
 void barebox_set_hostname(const char *);
 void barebox_set_hostname_no_overwrite(const char *);
+bool barebox_hostname_is_valid(const char *s);
 
 const char *barebox_get_serial_number(void);
 void barebox_set_serial_number(const char *);
-- 
2.39.2





[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux