mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Marco Felsch <m.felsch@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 4/4] bootm: add global.bootm.provide_hostname option
Date: Wed, 13 Mar 2024 20:45:47 +0100	[thread overview]
Message-ID: <20240313194547.3725723-4-m.felsch@pengutronix.de> (raw)
In-Reply-To: <20240313194547.3725723-1-m.felsch@pengutronix.de>

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@pengutronix.de>
---
 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




  parent reply	other threads:[~2024-03-13 19:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 19:45 [PATCH 1/4] string: add isempty helper Marco Felsch
2024-03-13 19:45 ` [PATCH 2/4] common: env: make use of isempty() Marco Felsch
2024-03-13 19:45 ` [PATCH 3/4] common: hostname: validate the provided hostname Marco Felsch
2024-03-13 19:45 ` Marco Felsch [this message]
2024-03-14  6:15   ` [PATCH 4/4] bootm: add global.bootm.provide_hostname option Ahmad Fatoum
2024-03-20 10:48     ` Marco Felsch
2024-04-04 13:40 ` [PATCH 1/4] string: add isempty helper Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313194547.3725723-4-m.felsch@pengutronix.de \
    --to=m.felsch@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox