1
0
mirror of https://gitlab.alpinelinux.org/alpine/cloud/tiny-cloud.git synced 2025-12-14 19:02:45 +03:00

Remove Deprecated Network and Hotplug Stuff

This commit is contained in:
Jake Buchholz Göktürk 2024-10-19 18:04:45 -07:00
parent b1ae3ab611
commit e75ec5b4f6
15 changed files with 31 additions and 538 deletions

View File

@ -1,12 +1,21 @@
# CHANGELOG
## 2024-10-XX - Tiny Cloud v3.0.8
* Officially add Makefile targets and flesh out tests for newer cloud modules:
`hetzner` (@hmrodrigues), `incus` (@ncopa), and `scaleway` (@hmrodrigues).
* Remove deprecated hotplug modules. `nvme-ebs-symlinks` has been deprecated
since v3.0.0, and `imds-net-sync` hasn't been supported for AWS since
tiny-cloud-network-2.2.2-r0 in Alpine Linux 3.17.
* Remove code related to the installation/testing of hotplug modules.
## 2023-09-25 - Tiny Cloud v3.0.2
* Replace non-functioning `tiny-cloud --setup` with working `--enable` and
`--disable` to enable/disable the set of Tiny Cloud init scripts.
* Reload user-data if bootstrap is incomplete, even if previous user-data file
exists.
* Init actions that fail sould cause the `tiny-cloud` to also terminate with an
* Init actions that fail should cause `tiny-cloud` to also terminate with an
error.
## 2023-06-12 - Tiny Cloud v3.0.1

View File

@ -1,6 +1,6 @@
PREFIX?=/
SUBPACKAGES = core network openrc aws azure gcp hetzner incus nocloud oci scaleway
SUBPACKAGES = core openrc aws azure gcp hetzner incus nocloud oci scaleway
.PHONY: check install $(SUBPACKAGES)
@ -12,7 +12,6 @@ core:
install -Dm644 -t "$(PREFIX)"/lib/tiny-cloud \
lib/tiny-cloud/common \
lib/tiny-cloud/init \
lib/tiny-cloud/mdev \
lib/tiny-cloud/tiny-cloud.conf
install -Dm644 -t "$(PREFIX)"/lib/tiny-cloud/user-data \
lib/tiny-cloud/user-data/alpine-config \
@ -24,24 +23,11 @@ core:
install -Dm755 -t "$(PREFIX)"/sbin \
sbin/tiny-cloud
network:
install -Dm644 -t "$(PREFIX)"/etc/network/interfaces.d \
etc/network/interfaces.d/*
install -Dm755 -t "$(PREFIX)"/lib/mdev \
lib/mdev/vnic-eth-hotplug
install -Dm755 -t "$(PREFIX)"/sbin \
sbin/assemble-interfaces \
sbin/imds-net-sync
install -Dm755 -t "$(PREFIX)"/usr/libexec/ifupdown-ng \
usr/libexec/ifupdown-ng/imds
openrc:
install -Dm755 -t "$(PREFIX)"/etc/init.d \
dist/openrc/*
aws:
install -Dm755 -t "$(PREFIX)"/lib/mdev \
lib/mdev/nvme-ebs-links
install -Dm644 -t "$(PREFIX)"/lib/tiny-cloud/cloud/aws \
lib/tiny-cloud/cloud/aws/*

View File

@ -11,10 +11,13 @@ works with multiple cloud providers. Currently, the following are supported:
* [AWS](https://aws.amazon.com) - Amazon Web Services
* [Azure](https://azure.microsoft.com) - Microsoft Azure
* [GCP](https://cloud.google.com) - Google Cloud Platform
* [OCI](https://cloud.oracle.com) - Oracle Cloud Infrastructure
* [Hetzner](https://www.hetzner.com)
* [Incus](https://linuxcontainers.org/incus)
* [NoCloud](
https://cloudinit.readthedocs.io/en/latest/reference/datasources/nocloud.html
) - cloud-init's NoCloud AWS-compatible user provided data source
* [OCI](https://cloud.oracle.com) - Oracle Cloud Infrastructure
[Scaleway](https://www.scaleway.com)
Tiny Cloud is also used for Alpine Linux's experimental "auto-install" feature.
@ -34,10 +37,6 @@ instance:
runlevel
* mark the bootstrap of the instance as "complete"
Optional features, which may not be universally necessary:
* manage hotpluggable virtual network interfaces
* sync IMDS-provided secondary IPv4 and IPv6 network configuration
Other cloud- and user-data-specific actions may also occur.
Also included is a handy `imds` client script for easy access to an instance's
@ -54,8 +53,6 @@ As Tiny Cloud is meant to be tiny, it has few dependencies:
* [`yx`](https://gitlab.com/tomalok/yx) (for extracting data from YAML files)
Optional dependencies:
* `ifupdown-ng` (for network management)
* `iproute2-minimal` (for syncing IPv4/IPv6 from IMDS)
* `nvme-cli` (for AWS nitro NVMe symlinks)
_Tiny Cloud has been developed specifically for use with the
@ -93,9 +90,12 @@ By default, Tiny Cloud expects configuration at `/etc/tiny-cloud.conf`,
The stock [lib/tiny-cloud/tiny-cloud.conf`](lib/tiny-cloud/tiny-cloud.conf)
file contains details of all tuneable settings.
_Because Tiny Cloud does not currently do auto-detection, you **MUST** set a
configuration value for `CLOUD` indicating which cloud provider will be used.
Current valid values are `aws`, `azure`, `gcp`, `oci`, and `nocloud`._
_Because Tiny Cloud does not universally do auto-detection, you may need to set
a configuration value for `CLOUD` indicating which cloud provider will be used.
Current valid values are `aws`, `azure`, `gcp`, `hetzner`, `incus`, `nocloud`,
`oci`, and `scaleway`. A value of `auto` will attempt to auto-detect which
cloud provider the instance is running on -- currently, only `incus` and
`nocloud` support auto-detection._
## Operation
@ -171,14 +171,3 @@ To check the status of the Tiny Cloud bootstrap, use...
tiny-cloud --bootstrap status
```
...which will either respond with `complete` or `incomplete`
## Cloud Hotplug Modules
### `vnic_eth_hotplug`
This hotplug module adds and removes ethernet interfaces as virtual NICs are
attached/detached from the instance.
An `ifupdown-ng` executor also syncs the interfaces' secondary IPv4 and IPV6
addresses associated with those VNICs, if the cloud's IMDS provides that
configuration data.

28
TODO.md
View File

@ -2,30 +2,16 @@
## SOON-ish
* Should the extra stuff that the `alpine` installer cloud does also apply to
the `nocloud` cloud? If so, move it there, and the installer is entirely
handled by the user-data handler.
* Package user-data handlers separately?
* `#cloud-config` user-data handler (support a useful subset)
* Move the bulk of `#alpine-config` handler that is compatible with
`#cloud-config` to that handler (which is currently just a stub), and only
`#alpine-config` extensions remain there.
* Support cloud auto-detection, where it's possible to do so.
## FUTURE
* cloud auto-detection?
* Support additional features of `#cloud-config` as needed
* `#tiny-config` user-data handler (should be simple-yet-flexible)
* Support LVM partitioning and non-`ext[234]` filesystems?
* `imds-net-sync` improvements
* Feature parity with current [amazon-ec2-net-utils](
https://github.com/amazonlinux/amazon-ec2-net-utils)
* Support for non-AWS clouds
* Daemonize to pick up IMDS network changes between reboots
* ***OR DEPRECATE AND NOT INSTALL FOR AWS BY DEFAULT***
* `dhcpcd` seems to work just fine with multiple IPv6 delivered via DHCPv6
* secondary IPv4 on an interface may be an edge case
* probably want to keep the `vnic-eth-hotplug` portion around for adding
and removing VNICs
* Support LVM partitioning and non-`ext[234]` filesystems
* Other cloud providers?

View File

@ -1,49 +0,0 @@
#!/bin/sh
# vim:set filetype=sh:
# NOTE: The mdev-conf APK handles this now, but only for xvd or sd links (not
# both)
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"
# nvme tool not installed?
[ -x /usr/sbin/nvme ] || log crit "nvme cli not installed"
raw_ebs_alias() {
/usr/sbin/nvme id-ctrl "/dev/$BASE" -b 2>/dev/null |
dd bs=32 skip=96 count=1 2>/dev/null
}
case $ACTION in
add|"")
BASE=$(echo "$MDEV" | sed -re 's/^(nvme[0-9]+n[0-9]+).*/\1/')
PART=$(echo "$MDEV" | sed -re 's/nvme[0-9]+n[0-9]+p?//g')
# TODO: deadline instead of max tries
MAXTRY=30
TRY=0
until [ -n "$EBS" ]; do
EBS=$(raw_ebs_alias | sed -nre '/^(\/dev\/)?(s|xv)d[a-z]{1,2} /p' | tr -d ' ')
[ -n "$EBS" ] && break
TRY=$((TRY + 1))
if [ $TRY -eq $MAXTRY ]; then
log err "Failed to get EBS volume alias for $MDEV after $MAXTRY attempts ($(raw_ebs_alias))"
exit 1
fi
sleep 0.1
done
# remove any leading '/dev/', 'sd', or 'xvd', and append partition
EBS=${EBS#/dev/}
EBS=${EBS#sd}
EBS=${EBS#xvd}$PART
ln -sf "$MDEV" "sd$EBS" && log notice "Added sd$EBS symlink for $MDEV"
ln -sf "$MDEV" "xvd$EBS" && log notice "Added xvd$EBS symlink for $MDEV"
;;
remove)
for TARGET in sd* xvd*
do
[ "$(readlink "$TARGET" 2>/dev/null)" = "$MDEV" ] && rm -f "$TARGET" && \
log notice "Removed $TARGET symlink for $MDEV"
done
;;
esac

View File

@ -1,87 +0,0 @@
#!/bin/sh
# vim:set filetype=sh:
set -e
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"
if [ -z "$MDEV" ] || [ -z "$ACTION" ]; then
log crit "MDEV or ACTION undefined, aborting"
fi
IFACE_CFG=/etc/network/interfaces
ip() {
local v=-4 lev=info
if [ "$1" = '-4' ] || [ "$1" = '-6' ]; then
v="$1"
shift
fi
local op="$2"
[ "$op" = show ] && lev=debug
if /sbin/ip "$v" "$@" || [ -n "$FAIL_OK" ]; then
log "$lev" "OK: ip $v $*"
else
log err "FAIL: ip $v $*"
fi
}
interface_up() {
log info "Bringing up $MDEV"
# umask so udhcpc PID file isn't non-owner writeable
(umask 0022 && ifup "$MDEV")
}
cleanup_interface() {
local v pref rtable="${MDEV#eth}"
let rtable+=10000
log info "Cleaning up $MDEV"
# kill related udhcpc, don't panic if it's not there
kill "$(cat "/run/udhcpc.$MDEV.pid")" || true
# tidy up /run/ifstate, if it exists
[ -f /run/ifstate ] && sed -i -e "/^$MDEV=/d" /run/ifstate
rm -f /run/ifstate."$MDEV".lock
# remove related rules
for v in 4 6; do
for pref in $(ip -"$v" rule show table "$rtable" | cut -d: -f1); do
ip -"$v" rule del pref "$pref"
done
done
}
is_networking_started() { service networking status -q 2>/dev/null; }
log info "STARTING: $ACTION $MDEV"
if exec 200>>"$IFACE_CFG"; then
if flock 200; then
case $ACTION in
add|"")
assemble-interfaces
is_networking_started && interface_up
;;
remove)
assemble-interfaces
is_networking_started && cleanup_interface
;;
*)
log err "Unknown action '$ACTION'"
exit 1
;;
esac
else
log err "Unable to flock $IFACE_CFG"
exit 1
fi
else
log err "Unable to assign fd 200 to flock $IFACE_CFG"
exit 1
fi
log info "FINISHED: $ACTION $MDEV"

View File

@ -1,12 +0,0 @@
# AWS mdev Hotplug Modules
# vim:set filetype=sh:
# shellcheck shell=sh
# makes symlinks for NVMe devices that correlate to AWS EBS sd/xvd devices
mod__nvme_ebs_links() {
# nvme-cli not installed?
[ -x /usr/sbin/nvme ] || return 1
install_before '^nvme\.\*' \
'nvme[0-9]+n.* root:disk 0660 */lib/mdev/nvme-ebs-links'
}

View File

@ -7,13 +7,11 @@
. "$LIBDIR/tiny-cloud/common"
: "${SKIP_INIT_ACTIONS:=}"
: "${HOTPLUG_TYPE:=mdev}"
### default phase actions (without leading 'init__')
DEFAULT_ACTIONS_BOOT="
expand_root
install_hotplugs
set_ephemeral_network
set_default_interfaces
enable_sshd
@ -78,34 +76,6 @@ init__expand_root() {
fi
}
init__install_hotplugs() {
local level result rc=0
[ ! -n "$HOTPLUG_MODULES" ] && return
if [ -f "$LIBDIR/tiny-cloud/$HOTPLUG_TYPE" ]; then
. "$LIBDIR/tiny-cloud/$HOTPLUG_TYPE"
fi
for module in $HOTPLUG_MODULES; do
result='unknown'
level='err'
printf " >> " >&2
log -i -t "$phase/$ACTION" info "$module: installing"
if type "mod__$module" | grep -q -w "function"; then
if "mod__$module"; then
result='installed'
level='info'
else
result='failed'
rc=1
fi
fi
printf " >> " >&2
log -i -t "$phase/$ACTION" info "$module: $result"
done
return $rc
}
# collect ethernet interfaces, sorted by index
ethernets() {
for i in "$ROOT/sys/class/net/"*; do

View File

@ -1,40 +0,0 @@
# Tiny Cloud - mdev hotplug functions
# vim:set filetype=sh:
# shellcheck shell=sh
# generic helper function to install mdev rules
install_before() {
local before="$1"
shift
local line="$*"
# already installed
fgrep -q "$line" /etc/mdev.conf && return 0
if grep -q "$before" /etc/mdev.conf; then
# install before existing rule
line="-$line"
else
# no rule exists, put it before the catch-all fallback
before="^# fallback"
line="$line\n"
fi
sed -i -Ee "s|($before.*)|$line\n\1|" /etc/mdev.conf
}
# hotpluggable VNICs (multi-cloud)
mod__vnic_eth_hotplug() {
[ -f /lib/mdev/vnic-eth-hotplug ] || return 1
install_before "^eth" \
"eth[0-9] root:root 0644 */lib/mdev/vnic-eth-hotplug"
# NICs attached at launch don't get added with mdev -s
assemble-interfaces
}
# load cloud-specific functions
: "${LIBDIR:=$PREFIX/lib}"
if [ -f "$LIBDIR/tiny-cloud/cloud/$CLOUD/mdev" ]; then
. "$LIBDIR/tiny-cloud/cloud/$CLOUD/mdev"
fi

View File

@ -2,7 +2,7 @@
# /etc/tiny-cloud.conf
# REQUIRED: The instance's cloud provider
# valid: aws, azure, gcp, oci, nocloud
# valid: aws, azure, gcp, hetzner, incus, nocloud, oci, scaleway
#CLOUD=
# User account where instance SSH keys are installed
@ -17,14 +17,7 @@
# Location of log directory
#TINY_CLOUD_LOGS=/var/log
# Hotplug Method (valid: mdev)
#HOTPLUG_TYPE=mdev
# Cloud-related Hotplug Modules
# valid: vnic_eth_hotplug, nvme_ebs_links (aws)
#HOTPLUG_MODULES=
# Explicitly skip these (whitespace delimited) things during init
# valid: expand_root install_hotplugs set_hostname set_ssh_keys
# save_userdata decompress_userdata run_userdata
# examples: expand_root set_hostname set_ssh_keys save_userdata
# decompress_userdata run_userdata
#SKIP_INIT_ACTIONS=

View File

@ -1,44 +0,0 @@
#!/bin/sh
# vim:set filetype=sh:
set -e
IFACE_CFG="$ROOT/etc/network/interfaces"
IFACE_DIR="${IFACE_CFG}.d"
cd "$IFACE_DIR"
cat > "$IFACE_CFG.new" <<EOT
# NOTE: $0 rewrites this file. Edit files in
# /etc/network/interfaces.d/ to persist any customizations.
EOT
# existing loopback and eths
for i in $ROOT/sys/class/net/*; do
IFACE="$(basename "$i")"
case $IFACE in
lo|eth*)
[ ! -f "$IFACE" ] && sed -e "s/%%/$IFACE/g" DEFAULT > "$IFACE"
printf "%s\n\n" "$(cat "$IFACE")" >> "$IFACE_CFG.new"
;;
*) continue ;;
esac
done
# all the rest
for i in "$IFACE_DIR"/*; do
IFACE="$(basename "$i")"
case $IFACE in
DEFAULT|lo|eth*)
continue
;;
*)
printf "%s\n\n" "$(cat "$IFACE")" >> "$IFACE_CFG.new"
;;
esac
done
# install new interfaces config
[ -f "$IFACE_CFG" ] && cp -a "$IFACE_CFG" "$IFACE_CFG.bak"
mv "$IFACE_CFG.new" "$IFACE_CFG"

View File

@ -1,149 +0,0 @@
#!/bin/sh
# vim:set filetype=sh:
# Sync interface's network configuration with IMDS
[ -z "$VERBOSE" ] || set -x
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR/tiny-cloud/common"
[ -z "${IFACE}" ] && log -s crit "IFACE not set, aborting"
# kill interface's imds-net-sync daemon
[ "$1" = '-k' ] && PHASE=pre-down && shift
: "${PHASE:=post-up}"
# route table number
RTABLE=${IFACE#eth}
let RTABLE+=10000
# ip [+F] [-4|-6] <object> <command> [<parameters>]
ip() {
local fail_ok v=-4 cmd level
[ "$1" = '+F' ] && fail_ok=1 && shift
if [ "$1" = '-4' ] || [ "$1" = '-6' ]; then
v="$1"
shift
fi
cmd="$2"
[ "$cmd" = show ] && level=debug || level=info
if /sbin/ip "$v" "$@" || [ -n "$fail_ok" ]; then
log -s "$level" "OK: ip $v $*"
else
log -s err "FAIL: ip $v $*"
fi
}
# get secondary IPv4s currently on the interface
iface_ip4s() {
ip -4 addr show "$IFACE" secondary |
sed -E -e '/inet /!d' -e 's/.*inet ([0-9.]+).*/\1/'
}
# get IPv6s currently on the interface
iface_ip6s() {
ip -6 addr show "$IFACE" scope global |
sed -E -e '/inet6/!d' -e 's/.*inet6 ([0-9a-f:]+).*/\1/'
}
imds_ip4s() {
local ip4=$(imds -E "@nic:$IFACE,@ipv4")
local ip4s=$(echo "$ip4" | tail +2) # secondary IPv4s
local ip4p ip4_cidr ip4_gw
# non-eth0 interfaces need custom route tables
#
if [ "$IFACE" != eth0 ] && [ -n "$ip4s" ] &&
[ -z $(ip +F -4 route show table "$RTABLE" 2>/dev/null) ]; then
ip4p=$(echo "$ip4" | head -1) # primary IPv4
ip4_cidr=$(imds -E "@nic:$IFACE,@ipv4-net") # TODO: get from iface instead?
# TODO: this may not hold true for non-AWS clouds
ip4_gw=$(echo "$ip4_cidr" | cut -d/ -f1 |
awk -F. '{ print $1"."$2"."$3"."$4+1 }')
ip -4 route add default via "$ip4_gw" dev "$IFACE" table "$RTABLE"
ip -4 route add "$ip4_cidr" dev "$IFACE" proto kernel scope link \
src "$ip4p" table "$RTABLE"
fi
echo "$ip4s"
}
# TODO: 3.18+ when we use dhcpcd for ipv4 & ipv6, we only need to do secondary IPv6s
# circle back and see how amazon-ec2-net-utils is handling everything these days
imds_ip6s() {
local ip6s gw tries=20
ip6s=$(imds -E "@nic:$IFACE,@ipv6")
# non-eth0 interfaces need custom route tables
#
# NOTE: busybox iproute2 doesn't do 'route show table' properly for IPv6,
# so iproute2-minimal package is required!
#
if [ "$IFACE" != eth0 ] && [ -n "$ip6s" ] &&
[ -z $(ip +F -6 route show table "$RTABLE" 2>/dev/null) ]; then
while true; do
gw=$(ip -6 route show dev "$IFACE" default | awk '{ print $3 }')
[ -n "$gw" ] && break
let tries--
if [ "$tries" -eq 0 ]; then
log -s warn "Unable to get IPv6 gateway RA after 10s"
break
fi
sleep 0.5
done
ip -6 route add default via "$gw" dev "$IFACE" table "$RTABLE"
# TODO? match imds_ip4s() with ip -6 route add "ip6_cidr" dev "$IFACE" ...
fi
echo "$ip6s"
}
in_list() {
echo "$2" | grep -q "^$1$"
}
# ip_addr {4|6} {add|del} <ip>
ip_addr() {
local mask=32 # IPv4 always /32
[ "$1" -eq 6 ] && mask=128 # IPv6 always /128
ip -"$1" addr "$2" "$3/$mask" dev "$IFACE"
# TODO? delegated ipv[46] prefixes?
# non-eth0 interfaces get rules associating IPs with route tables
[ "$IFACE" = eth0 ] && return
ip -"$1" rule "$2" from "$3" lookup "$RTABLE"
ip -"$1" rule "$2" to "$3" lookup "$RTABLE"
}
# sync_ips {4|6} "<imds-ips>" "<iface-ips>"
sync_ips() {
local i
# remove extra IPs
for i in $3; do
in_list "$i" "$2" || ip_addr "$1" del "$i"
done
# add missing IPs
# NOTE: this adds an extra <IPv4>/32 for the primary IP
for i in $2; do
in_list "$i" "$3" || ip_addr "$1" add "$i"
done
}
imds_iface_sync() {
log -s info "SYNCING: $IFACE"
sync_ips 4 "$(imds_ip4s)" "$(iface_ip4s)"
sync_ips 6 "$(imds_ip6s)" "$(iface_ip6s)"
log -s info "FINISHED: $IFACE"
}
case "$PHASE" in
post-up)
# TODO: daemonize this
imds_iface_sync
;;
pre-down)
# TODO: kill daemon, maybe some cleanup
;;
*)
esac

View File

@ -1,37 +0,0 @@
#!/usr/bin/env atf-sh
# vim:set filetype=sh:
# shellcheck shell=sh
. $(atf_get_srcdir)/test_env.sh
init_tests \
assemble_eth0 \
assemble_missing_interfaces_d
assemble_eth0_body() {
mkdir -p etc/network/interfaces.d \
sys/class/net/eth0
cat > etc/network/interfaces.d/DEFAULT <<-EOF
auto %%
iface %% inet dhcp
EOF
atf_check assemble-interfaces
atf_check \
-o match:"# NOTE:" \
-o match:"auto eth0" \
-o match:"iface eth0 inet dhcp" \
cat etc/network/interfaces
}
# test what happens if etc/network/interfaces.d is missing
assemble_missing_interfaces_d_body() {
atf_check -s not-exit:0 \
-e match:"([Nn]o such file or directory|can't cd to)" \
assemble-interfaces
if [ -f etc/network/interfaces ]; then
atf_fail "should not create etc/network/interfaces"
fi
}

View File

@ -11,7 +11,6 @@ lib="$srcdir"/lib/tiny-cloud/init
init_tests \
expand_root \
expand_root_partition \
install_hotplugs_fail \
ethernets \
find_first_interface_up \
auto_detect_ethernet_interface \
@ -60,12 +59,6 @@ expand_root_partition_body() {
done
}
install_hotplugs_fail_body() {
CLOUD=aws atf_check -s not-exit:0 \
-e match:"vnic_eth_hotplug: failed" \
sh -c ". $lib; HOTPLUG_MODULES='vnic_eth_hotplug'; init__install_hotplugs"
}
ethernets_body() {
fake_interfaces lo br0 eth0 eth2 eth11

View File

@ -1,15 +0,0 @@
#!/bin/sh
# vim:set filetype=sh:
# Tiny Cloud IMDS ifupdown-ng executor
case "$PHASE" in
post-up)
/sbin/imds-net-sync
;;
pre-down)
# TODO: someday make imds-net-sync a daemon
# /sbin/imds-net-sync -k
;;
*) ;;
esac