#!/sbin/sh # # CDDL HEADER START # # The contents of this file are subject to the terms of the # Common Development and Distribution License (the "License"). # You may not use this file except in compliance with the License. # # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE # or http://www.opensolaris.org/os/licensing. # See the License for the specific language governing permissions # and limitations under the License. # # When distributing Covered Code, include this CDDL HEADER in each # file and include the License file at usr/src/OPENSOLARIS.LICENSE. # If applicable, add the following below this CDDL HEADER, with the # fields enclosed by brackets "[]" replaced with your own identifying # information: Portions Copyright [yyyy] [name of copyright owner] # # CDDL HEADER END # # # Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T. # All rights reserved. # # # Copyright 2006 Sun Microsystems, Inc. All rights reserved. # Use is subject to license terms. # # Copyright 2019 Joyent, Inc. # . /lib/svc/share/smf_include.sh . /lib/svc/share/net_include.sh set -o xtrace # Make sure that the libraries essential to this stage of booting can be found. LD_LIBRARY_PATH=/lib; export LD_LIBRARY_PATH # # For the GZ, use one of the following values for hostname, in order: # * DHCP hostname (if set on a primary interface) # * hostname value from config file # * hostname bootparam # * if not a headnode: # * admin MAC address # * any other MAC address # # If none of the above could be found, default to "headnode" for headnodes, and # "unknown" for non-headnodes. # # The hostname must be set to something, because # tooltalk will hang unless the name can be locally resolved. # Sendmail also requires the name to be resolvable locally. # Later, in inetsvc, we create a name "unknown" and create a entry # in the local /etc/inet/hosts file pairing "unknown" with the IP # address assigned by DHCP. The use of bootparams as a fallback # for all non-DHCP cases provides compatibility with the # behavior of the system before netstrategy was introduced. # # For non-global zones, fall back to the `uname -n` value provided by the # kernel if /etc/nodename does not exist, as is expected on an initial boot. # set_gz_hostname() { hostname=${CONFIG_hostname} if [ -z "$hostname" ] || [ "$hostname" == "unknown" ]; then hostname=$SYSINFO_Bootparam_hostname fi if [ -n "$hostname" ] && [ "$hostname" != "unknown" ]; then return fi # $headnode is set by load_sdc_config() if [ "$headnode" == "true" ]; then hostname="headnode" return fi if [[ -n ${SYSINFO_NIC_admin} ]]; then eval "admin_mac=\${SYSINFO_Network_Interface_${SYSINFO_NIC_admin}_MAC_Address}" if [[ -n ${admin_mac} ]]; then hostname=$(echo "${admin_mac}" | tr ':' '-') return fi fi fallback_mac=$(set | grep "^SYSINFO_Network_Interface_.*_MAC_Address" | head -n1 | cut -d'=' -f2) if [[ -n ${fallback_mac} ]]; then hostname=$(echo "${fallback_mac}" | tr ':' '-') return fi hostname="unknown" } smf_netstrategy case "$_INIT_NET_STRATEGY" in "dhcp") hostname=`/sbin/dhcpinfo Hostname` ;; "rarp") hostname=`/sbin/hostconfig -h -p bootparams` trap 'intr=1' 2 3 while [ -z "$hostname" -a ! -f /etc/.UNCONFIGURED -a \ -z "$intr" ]; do echo "re-trying host configuration..." # Restrict this to IPv4 interfaces. /sbin/ifconfig -adD4 auto-revarp up hostname=`/sbin/hostconfig -h -p bootparams` done trap 2 3 ;; # /etc/nodename defaults to "unknown" on SmartOS "none") hostname="`shcat /etc/nodename 2>/dev/null`" if [ -z "$hostname" ]; then if smf_is_globalzone; then hostname=`/sbin/hostconfig -h -p bootparams` else hostname=`/sbin/uname -n` fi fi ;; esac # Load sysinfo variables with SYSINFO_ prefix and config variables with # CONFIG_ prefix. # Note: since we're still starting up, "soft" values like network IP and such could # not be set yet. if smf_is_globalzone; then . /lib/sdc/config.sh load_sdc_sysinfo if boot_file_config_enabled; then load_boot_file_config else load_sdc_config fi fi # # If the netstrategy was unsuccessful and we haven't got a locally configured # name, default to "unknown" # if [ -z "$hostname" ] || [ "$hostname" == "unknown" ]; then hostname="`shcat /etc/nodename 2>/dev/null`" if [ -z "$hostname" ] || [ "$hostname" == "unknown" ]; then if smf_is_globalzone; then set_gz_hostname else hostname="unknown" fi fi fi if smf_is_globalzone; then echo "$hostname" > /etc/nodename fi /sbin/uname -S $hostname # Reloading sysinfo here serves two purposes: # - getting the IP info (which should exist now) # - updating the host info (which we just set) eval $(/usr/bin/sysinfo -f -p | sed -e "s/^/SYSINFO_/") # Try to add the /etc/hosts entry if we can find an IP if [[ -n ${SYSINFO_NIC_admin} ]]; then eval "ipaddr=\${SYSINFO_Network_Interface_${SYSINFO_NIC_admin}_IPv4_Address}" fi if [[ -z ${ipaddr} ]]; then ipaddr=$(set | grep "^SYSINFO_Network_Interface_.*_IPv4_Address" | head -n1 | cut -d'=' -f2) fi if [[ -n ${ipaddr} ]]; then fullname="" if [ -n "$CONFIG_dns_domain" ]; then fullname=" ${hostname}.${CONFIG_dns_domain}" fi printf "${ipaddr}\t${hostname}${fullname}\n" >> /etc/hosts fi # Reset the library path now that we are past the critical stage unset LD_LIBRARY_PATH exit $SMF_EXIT_OK