summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarco Felsch <m.felsch@pengutronix.de>2024-03-13 20:45:47 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2024-04-04 15:40:01 +0200
commitc1bb8f33009e69f2c83d5963278bcf26d88cf819 (patch)
tree51cac4e1f800385d4812ee95192a06de35d54a21
parente0d5d8bf9a81b58660f19482a723342c7506ad95 (diff)
downloadbarebox-c1bb8f33009e69f2c83d5963278bcf26d88cf819.tar.gz
barebox-c1bb8f33009e69f2c83d5963278bcf26d88cf819.tar.xz
bootm: add global.bootm.provide_hostname option
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> Link: https://lore.barebox.org/20240313194547.3725723-4-m.felsch@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
-rw-r--r--common/bootm.c25
-rw-r--r--common/misc.c2
-rw-r--r--include/bootm.h5
-rw-r--r--include/common.h1
4 files changed, 32 insertions, 1 deletions
diff --git a/common/bootm.c b/common/bootm.c
index 4cc88eed76..a59fa35008 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 &&
@@ -956,6 +979,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);
@@ -1000,3 +1024,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 7a34e010bb..530f85f6e3 100644
--- a/common/misc.c
+++ b/common/misc.c
@@ -158,7 +158,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 ee2b574521..c69da85cdd 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 b7b4d9e350..d7b5261bc9 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 *);