Add element to process config-drive network info

If you don't want cloud-init, you may need to get a few things
from config-drive because you may be operating on a cloud with no DHCP.
In that case, simply reading some values from config-drive and writing
out either DHCP or static network info, in addition to grabbing ssh keys
is helpful. Both Infra and bifrost want this for their images.

Co-Authored-By: Gregory Haynes <greg@greghaynes.net>

Change-Id: I2746ed256b9783eab058b803130d3ccac484eaeb
This commit is contained in:
Monty Taylor 2015-03-23 16:48:51 -04:00
parent 0c58eb11a0
commit a33ddb89f1
15 changed files with 272 additions and 0 deletions

View File

@ -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.

View File

@ -0,0 +1,3 @@
dib-init-system
install-types
source-repositories

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
SUBSYSTEM=="net", ACTION=="add", TAG+="systemd", ENV{SYSTEMD_WANTS}+="simple-init@$name.service"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,3 @@
isc-dhcp-client:
net-tools:
python-pip:

View File

@ -0,0 +1,13 @@
{
"family": {
"redhat": {
"isc-dhcp-client": "dhclient"
},
"debian": {
"isc-dhcp-client": "isc-dhcp-client"
}
},
"default": {
"isc-dhcp-client": "isc-dhcp-client"
}
}

View File

@ -0,0 +1 @@
glean git /tmp/glean.git https://gitorious.org/mordred-temp/glean.git

View File

@ -2,5 +2,6 @@ adduser:
locales:
ca-certificates:
cloud-initramfs-growroot:
linux-image-generic:
lsb-release:
phase: pre-install.d