diff --git a/file.c b/file.c index a7f37940..9a8b0f6c 100644 --- a/file.c +++ b/file.c @@ -1895,13 +1895,6 @@ void file_write_install_inf(char *dir) if(config.net.realhostname) { file_write_str(f, key_hostname, config.net.realhostname); } - else { - char buf[256]; - if(!gethostname(buf, sizeof buf)) { - log_debug("hostname = \"%s\"\n", buf); - if(*buf && strcmp(buf, "(none)")) file_write_str(f, key_hostname, buf); - } - } LXRC_WAIT diff --git a/install.c b/install.c index 586c6e3a..f4dfc09d 100644 --- a/install.c +++ b/install.c @@ -1113,6 +1113,9 @@ int inst_start_install() if(inst_choose_source()) err = 1; } + /* if the user specified a hostname, set it */ + util_set_hostname(config.net.realhostname); + if(! err && config.rescue) { /* get rid of repo */ url_umount(config.url.install); diff --git a/linuxrc.c b/linuxrc.c index a368282e..0fa27cf7 100644 --- a/linuxrc.c +++ b/linuxrc.c @@ -383,6 +383,9 @@ void lxrc_change_root() if(config.net.sshpassword) setenv("SSHPASSWORD", config.net.sshpassword, 1); if(config.net.sshpassword_enc) setenv("SSHPASSWORDENC", config.net.sshpassword_enc, 1); + /* change hostname from 'install' to 'rescue' unless we've had something better */ + if(!config.net.realhostname) util_set_hostname("rescue"); + lxrc_run_console("/mounts/initrd/scripts/prepare_rescue"); LXRC_WAIT @@ -773,6 +776,7 @@ void lxrc_init() config.net.ipv6 = 1; config.net.setup = NS_DHCP; /* unless we are told otherwise just go for dhcp */ config.net.nameservers = 1; + config.net.sethostname = 1; /* let wicked set hostname */ config.explode_win = 1; config.color = 2; @@ -855,6 +859,8 @@ void lxrc_init() config.initrd_has_ldso = 1; #endif + util_set_hostname("install"); + // read config from initrd: // - /linuxrc.config // - /etc/linuxrc.d/* diff --git a/linuxrc_hostname.md b/linuxrc_hostname.md new file mode 100644 index 00000000..76beecfe --- /dev/null +++ b/linuxrc_hostname.md @@ -0,0 +1,31 @@ +#hostname setting in installation/rescue system + +There are 3 cases: + +1. no hostname is set + +2. hostname is set via dhcp (either from HOSTNAME or FQDN) + +3. hostname is set via `hostname` boot option + +linuxrc defaults to `install`, which may be overridden by dhcp (2) or user +(3). (3) takes precedence over (2). + +##installation system + +When yast is started, the value from (3) is passed to yast via +`/etc/install.inf::Hostname` and ends up in `/etc/hostname` of the installed system. + +When linuxrc doesn't set `/etc/install.inf::Hostname` yast generates a +default `/etc/hostname` entry of the form `linux-XXXX` (+ `.suse` in sle12). +`XXXX` is some random part. + +There is no `/etc/hostname` file in the installation environment. + +##rescue system + +linuxrc changes the hostname to `rescue`, which may be overridden by user (3). + +This value is put into `/etc/hostname` of the rescue system. The value there may later +be overridden by dhcp (2). + diff --git a/linuxrc_yast_interface.txt b/linuxrc_yast_interface.txt index d8557fe0..b240527b 100644 --- a/linuxrc_yast_interface.txt +++ b/linuxrc_yast_interface.txt @@ -127,7 +127,7 @@ Device: %s # else the entry is missing ProxyURL: %s -# the hostname as set by dhcp or by 'hostname=foo' boot option +# the hostname as set by 'hostname=foo' boot option # entry is missing if unset Hostname: %s diff --git a/util.c b/util.c index 284707ea..26128933 100644 --- a/util.c +++ b/util.c @@ -5443,3 +5443,17 @@ char *util_get_caller(int skip) return buf; } + +/* + * Convenience function: + * Set hostname and log this. + */ +void util_set_hostname(char *hostname) +{ + if(!hostname) return; + + sethostname(hostname, strlen(hostname)); + + log_info("set hostname: %s\n", hostname); +} + diff --git a/util.h b/util.h index d77bed95..21e6ab0f 100644 --- a/util.h +++ b/util.h @@ -155,3 +155,4 @@ void util_log(unsigned level, char *format, ...); int util_run(char *cmd, unsigned log_stdout); void util_perror(unsigned level, char *msg); char *util_get_caller(int skip); +void util_set_hostname(char *hostname);