diff --git a/elements/simple-init/README.rst b/elements/simple-init/README.rst new file mode 100644 index 00000000..b5ad1022 --- /dev/null +++ b/elements/simple-init/README.rst @@ -0,0 +1,35 @@ +=========== +simple-init +=========== +Basic network and system configuration that can't be done until boot + +Unfortunately, as much as we'd like to bake it in to an image, we can't +know in advance how many network devices will be present, nor if DHCP is +present in the host cloud. Additionally, in environments where cloud-init +is not used, there are a couple of small things, like mounting config-drive +and pulling ssh keys from it, that need to be done at boot time. + +Autodetect network interfaces during boot and configure them +------------------------------------------------------------ + +The rationale for this is that we are likely to require multiple +network interfaces for use cases such as baremetal and there is no way +to know ahead of time which one is which, so we will simply run a +DHCP client on all interfaces with real MAC addresses (except lo) that +are visible on the first boot. + +The script `/usr/local/sbin/simple-init.sh` will be called +early in each boot and will scan available network interfaces and +ensure they are configured properly before networking services are started. + +Processing startup information from config-drive +------------------------------------------------ + +On most systems, the DHCP approach desribed above is fine. But in some clouds, +such as Rackspace Public cloud, there is no DHCP. Instead, there is static +network config via `config-drive`. `simple-init` will happily call +`glean` which will do nothing if static network information is +not there. + +Finally, glean will handle ssh-keypair-injection from config +drive if cloud-init is not installed. diff --git a/elements/simple-init/element-deps b/elements/simple-init/element-deps new file mode 100644 index 00000000..d78d4eeb --- /dev/null +++ b/elements/simple-init/element-deps @@ -0,0 +1,3 @@ +dib-init-system +install-types +source-repositories diff --git a/elements/simple-init/install.d/50-simple-init b/elements/simple-init/install.d/50-simple-init new file mode 100755 index 00000000..daa70980 --- /dev/null +++ b/elements/simple-init/install.d/50-simple-init @@ -0,0 +1,34 @@ +#!/bin/bash +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. + +if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +SCRIPTDIR=$(dirname $0) + +install -D -g root -o root -m 0755 ${SCRIPTDIR}/simple-init.sh /usr/local/sbin/simple-init.sh + +if [ "$DIB_INIT_SYSTEM" == "upstart" ]; then + install -D -g root -o root -m 0755 ${SCRIPTDIR}/simple-init.conf /etc/init/simple-init.conf +elif [ "$DIB_INIT_SYSTEM" == "systemd" ]; then + install -D -g root -o root -m 0755 ${SCRIPTDIR}/simple-init@.service /usr/lib/systemd/system/simple-init@.service + install -D -g root -o root -m 0644 ${SCRIPTDIR}/simple-init-udev.rules /etc/udev/rules.d/99-simple-init.rules +elif [ "$DIB_INIT_SYSTEM" == "sysv" ]; then + install -D -g root -o root -m 0755 ${SCRIPTDIR}/simple-init.init /etc/init.d/simple-init + update-rc.d simple-init defaults +fi diff --git a/elements/simple-init/install.d/60-simple-init-remove-interfaces b/elements/simple-init/install.d/60-simple-init-remove-interfaces new file mode 100755 index 00000000..6d2cc621 --- /dev/null +++ b/elements/simple-init/install.d/60-simple-init-remove-interfaces @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ ${DIB_DEBUG_TRACE:-0} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +# Cloud images may hard code the eth0 interfaces so they +# boot with DHCP. + +# Fedora +rm -f /etc/sysconfig/network-scripts/ifcfg-eth0 + +# Ubuntu +rm -f /etc/network/interfaces.d/eth0.cfg + +# Debian +rm -f /etc/network/interfaces.d/eth0 + +# /etc/network/interfaces distributions +if [ -f "/etc/network/interfaces" ]; then + printf "auto lo\niface lo inet loopback\n\n" > /etc/network/interfaces + if [ -d "/etc/network/interfaces.d/" ]; then + printf "source-directory interfaces.d\n\n" >> /etc/network/interfaces + fi +fi diff --git a/elements/simple-init/install.d/simple-init-repo-install/40-glean b/elements/simple-init/install.d/simple-init-repo-install/40-glean new file mode 100755 index 00000000..5ba29a53 --- /dev/null +++ b/elements/simple-init/install.d/simple-init-repo-install/40-glean @@ -0,0 +1,24 @@ +#!/bin/bash +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. + +if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +pip install /tmp/glean.git diff --git a/elements/simple-init/install.d/simple-init-source-install/40-glean b/elements/simple-init/install.d/simple-init-source-install/40-glean new file mode 100755 index 00000000..11266c4e --- /dev/null +++ b/elements/simple-init/install.d/simple-init-source-install/40-glean @@ -0,0 +1,24 @@ +#!/bin/bash +# Copyright (c) 2015 Hewlett-Packard Development Company, L.P. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. + +if [ ${DIB_DEBUG_TRACE:-1} -gt 0 ]; then + set -x +fi +set -eu +set -o pipefail + +pip install glean diff --git a/elements/simple-init/install.d/simple-init-udev.rules b/elements/simple-init/install.d/simple-init-udev.rules new file mode 100644 index 00000000..8a52b200 --- /dev/null +++ b/elements/simple-init/install.d/simple-init-udev.rules @@ -0,0 +1 @@ +SUBSYSTEM=="net", ACTION=="add", TAG+="systemd", ENV{SYSTEMD_WANTS}+="simple-init@$name.service" diff --git a/elements/simple-init/install.d/simple-init.conf b/elements/simple-init/install.d/simple-init.conf new file mode 100644 index 00000000..6dbcf516 --- /dev/null +++ b/elements/simple-init/install.d/simple-init.conf @@ -0,0 +1,11 @@ +# Call a script to generate a /etc/network/interfaces file to DHCP all available interfaces +# Then remove this config file so the script is never run again + +description "DHCP any connected, but unconfigured network interfaces" + +start on starting network-interface +instance $INTERFACE + +task + +exec /usr/local/sbin/simple-init.sh $INTERFACE diff --git a/elements/simple-init/install.d/simple-init.init b/elements/simple-init/install.d/simple-init.init new file mode 100755 index 00000000..17b125a0 --- /dev/null +++ b/elements/simple-init/install.d/simple-init.init @@ -0,0 +1,31 @@ +#!/bin/sh -e +### BEGIN INIT INFO +# Provides: simple-init +# Required-Start: $local_fs +# Required-Stop: $local_fs +# Default-Start: S +# Default-Stop: 0 6 +# X-Start-Before: networking +# Short-Description: Autodetect network interfaces +# Description: Autodetect network interfaces during boot and configure them for DHCP +### END INIT INFO + +NAME=simple-init +INIT_NAME=/etc/init.d/${NAME} +SCRIPT_NAME=/usr/local/sbin/${NAME}.sh + +[ -x $SCRIPT_NAME ] || exit 0 + +case "$1" in + start) + $SCRIPT_NAME + ;; + stop) + ;; + *) + echo "Usage: $INIT_NAME {start|stop}" + exit 1 + ;; +esac + +exit 0 diff --git a/elements/simple-init/install.d/simple-init.sh b/elements/simple-init/install.d/simple-init.sh new file mode 100755 index 00000000..52686f0d --- /dev/null +++ b/elements/simple-init/install.d/simple-init.sh @@ -0,0 +1,49 @@ +#!/bin/bash +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# +# See the License for the specific language governing permissions and +# limitations under the License. + +# dib-lint: disable=dibdebugtrace +set -eu +set -o pipefail + +INTERFACE=${1:-} #optional, if not specified configure all available interfaces + +function config_exists() { + local interface=$1 + if [ "$CONF_TYPE" == "netscripts" ]; then + if [ -f "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then + return 0 + fi + else + ifquery $interface >/dev/null 2>&1 && return 0 || return 1 + fi + return 1 +} + + +# Test to see if config-drive exists. If not, skip and assume DHCP networking +# will work becasue sanity +if blkid -t LABEL="config-2" ; then + # Mount config drive + mkdir -p /mnt/config + mount -o mode=0700 $(blkid -t LABEL="config-2" | cut -d ':' -f 1) /mnt/config || true + /usr/local/bin/glean --ssh --skip-network +fi + + +if [ -n "$INTERFACE" ]; then + /usr/local/bin/glean --interface $INTERFACE +else + /usr/local/bin/glean +fi diff --git a/elements/simple-init/install.d/simple-init@.service b/elements/simple-init/install.d/simple-init@.service new file mode 100644 index 00000000..054c27b0 --- /dev/null +++ b/elements/simple-init/install.d/simple-init@.service @@ -0,0 +1,15 @@ +[Unit] +Description=DHCP interface %I +After=network.service network.target + +ConditionPathExists=!/etc/sysconfig/network-scripts/ifcfg-%I + +[Service] +Type=oneshot +User=root +ExecStartPre=/usr/local/sbin/simple-init.sh %I +ExecStart=/sbin/ifup %I +RemainAfterExit=true + +[Install] +WantedBy=multi-user.target diff --git a/elements/simple-init/package-installs.yaml b/elements/simple-init/package-installs.yaml new file mode 100644 index 00000000..48034a95 --- /dev/null +++ b/elements/simple-init/package-installs.yaml @@ -0,0 +1,3 @@ +isc-dhcp-client: +net-tools: +python-pip: diff --git a/elements/simple-init/pkg-map b/elements/simple-init/pkg-map new file mode 100644 index 00000000..12ef943c --- /dev/null +++ b/elements/simple-init/pkg-map @@ -0,0 +1,13 @@ +{ + "family": { + "redhat": { + "isc-dhcp-client": "dhclient" + }, + "debian": { + "isc-dhcp-client": "isc-dhcp-client" + } + }, + "default": { + "isc-dhcp-client": "isc-dhcp-client" + } +} diff --git a/elements/simple-init/source-repository-simple-init b/elements/simple-init/source-repository-simple-init new file mode 100644 index 00000000..210f758e --- /dev/null +++ b/elements/simple-init/source-repository-simple-init @@ -0,0 +1 @@ +glean git /tmp/glean.git https://gitorious.org/mordred-temp/glean.git diff --git a/elements/ubuntu-minimal/package-installs.yaml b/elements/ubuntu-minimal/package-installs.yaml index 3e434d5a..4b6f08c0 100644 --- a/elements/ubuntu-minimal/package-installs.yaml +++ b/elements/ubuntu-minimal/package-installs.yaml @@ -2,5 +2,6 @@ adduser: locales: ca-certificates: cloud-initramfs-growroot: +linux-image-generic: lsb-release: phase: pre-install.d