Skip to content

Instantly share code, notes, and snippets.

@jperkin
Created February 22, 2021 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jperkin/bb92df019ff877c851ebdad30017def6 to your computer and use it in GitHub Desktop.
Save jperkin/bb92df019ff877c851ebdad30017def6 to your computer and use it in GitHub Desktop.

/etc/rc.d/triton

#!/bin/sh
#
# $NetBSD$
#
# Runs before network, configuring all networking-related files based on Triton
# metadata, leaving network to actually perform the bringup.
#

# PROVIDE: triton
# REQUIRE: root bootconf CRITLOCALMOUNTED tty
# BEFORE: network

$_rc_subr_loaded . /etc/rc.subr

name="triton"
start_cmd="triton_start"
stop_cmd=":"

PATH="/usr/sdc/bin:/usr/sbin:/usr/bin:/sbin:/bin"

triton_start()
{
	echo "Starting Triton network configuration."

	#
	# Configure /etc/ifconfig.vioif0.
	#
	ip=$(mdata-get sdc:nics 2>/dev/null | jq -r '.[].ip')
	nm=$(mdata-get sdc:nics 2>/dev/null | jq -r '.[].netmask')
	cat >/etc/ifconfig.vioif0 <<-EOF
		#
		# Generated by /etc/rc.d/triton at boot.
		#
		up
		inet ${ip} netmask ${nm}
	EOF

	#
	# Configure /etc/mygate
	#
	gw=$(mdata-get sdc:nics 2>/dev/null | jq -r '.[].gateway')
	if [ $? -eq 0 ]; then
		echo "${gw}" >/etc/mygate
	fi

	#
	# Configure /etc/resolv.conf
	#
	cat >/etc/resolv.conf <<-EOF
		#
		# Generated by /etc/rc.d/triton at boot.
		#
	EOF
	domain=$(mdata-get sdc:dns_domain 2>/dev/null)
	if [ $? -eq 0 ]; then
		cat >>/etc/resolv.conf <<-EOF
			search ${domain}
		EOF
	fi
	set -- $(mdata-get sdc:resolvers 2>/dev/null | jq -r '.[]')
	for ns; do
		cat >>/etc/resolv.conf <<-EOF
			nameserver ${ns}
		EOF
	done

	#
	# Configure /etc/myname
	#
	hostname=$(mdata-get sdc:hostname 2>/dev/null)
	alias=$(mdata-get sdc:alias 2>/dev/null)
	if [ -n "${hostname}" ]; then
		echo ${hostname} >/etc/myname
	elif [ -n "${alias}" ]; then
		echo ${alias} >/etc/myname
	fi

	#
	# Generate /root/.ssh/authorized_keys
	#
	mkdir -m 0700 -p /root/.ssh
	mdata-get root_authorized_keys >/root/.ssh/authorized_keys.mdata 2>&1
	if [ $? -ne 0 ]; then
		rm -f /root/.ssh/authorized_keys.mdata
	else
		grep '^[[:alnum:]]' /root/.ssh/authorized_keys \
		    >/root/.ssh/authorized_keys.orig
		cat >/root.ssh/authorized_keys.new <<-EOF
			#
			# Generated by /etc/rc.d/triton
			#
		EOF
		sort /root/.ssh/authorized_keys.mdata \
		    /root/.ssh/authorized_keys.orig \
		    >>/root.ssh/authorized_keys.new
		mv /root.ssh/authorized_keys.new \
		    /root.ssh/authorized_keys
		rm -f /root/.ssh/authorized_keys.mdata \
		    /root/.ssh/authorized_keys.orig
	fi
}

load_rc_config $name
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment