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

Support "local-hostname"

This commit is contained in:
Jake Buchholz Göktürk 2023-04-30 23:23:54 +00:00
parent fb2d658d93
commit 3894cd9f8d
21 changed files with 117 additions and 65 deletions

View File

@ -1,6 +1,6 @@
# CHANGELOG
## 2023-04-XX - Tiny Cloud v3.0.0
## 2023-05-XX - Tiny Cloud v3.0.0
* Tiny Cloud init functionality has been consolidated into **/sbin/tiny-cloud**
and init scripts should use `tiny-cloud <phase>` to indicate whether `early`,
@ -20,6 +20,10 @@
**/dev/sd** or **/dev/xvd** symlinks are created as indicated in NVMe device
metadata, *but NOT both*!
* `imds` now supports `@local-hostname` alias. For most clouds this is the
same as `@hostname`.
* Fixed setting `local-hostname` metadata from **/proc/cmdline** for NoCloud.
----
_CHANGELOG begins 2023-04-29_

View File

@ -65,7 +65,7 @@ Typically, Tiny Cloud is installed and configured when building a cloud image,
and is available on Alpine Linux as the [`tiny-cloud`](
https://pkgs.alpinelinux.org/packages?name=tiny-cloud*) APKs...
```
apk install tiny-cloud-<cloud>
apk add tiny-cloud-<cloud>
```
This will install the necessary init scripts, libraries, etc. plus any missing
dependencies for Tiny Cloud to support _`<cloud>`_.

View File

@ -6,7 +6,7 @@
### configuration, common functions
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$LIBDIR/tiny-cloud/common"
if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
cat <<EOT
@ -15,10 +15,11 @@ Usage: imds [-h] { -e | +e | +n | +s | +t | @<alias> | <imds-path> } ...
-e / +e : ignore / catch errors
+n / +s / +t : insert newline / space / tab
<alias> :-
hostname : instance hostname
ssh-keys : instance SSH keys
userdata : instance user data
nics : instance NICs
hostname : instance hostname
local-hostname : instance local hostname
ssh-keys : instance SSH keys
userdata : instance user data
nics : instance NICs
nic:<iface>[,<nic-key> ...] : specific NIC interface
<iface> : network interface (i.e. eth1)
<nic-key> :- { -e | +e | +n | +s | +t | @<nic-alias> | <nic-path> }
@ -53,6 +54,7 @@ IMDS_ENDPOINT="169.254.169.254"
# Common to AWS and NoCloud(ish)
IMDS_HOSTNAME="meta-data/hostname"
IMDS_LOCAL_HOSTNAME="meta-data/local-hostname"
IMDS_SSH_KEYS="meta-data/public-keys"
IMDS_USERDATA="user-data"
IMDS_NICS="meta-data/network/interfaces/macs"
@ -84,10 +86,10 @@ _imds_nic_index() { cat "/sys/class/net/$1/address"; }
### load cloud-specific variables and functions
if [ ! -d "$LIBDIR"/tiny-cloud/cloud/"$CLOUD" ]; then
if [ ! -d "$LIBDIR/tiny-cloud/cloud/$CLOUD" ]; then
echo "ERROR: Unknown Cloud '$CLOUD'" >&2
fi
. "$LIBDIR"/tiny-cloud/cloud/"$CLOUD"/imds
. "$LIBDIR/tiny-cloud/cloud/$CLOUD/imds"
### non-overrideable functions
@ -107,10 +109,11 @@ imds() {
+s) printf " "; continue ;; # insert space
+t) printf "\t"; continue ;; # insert tab
# key aliasing
@hostname) args="$IMDS_HOSTNAME" ;;
@ssh-keys) cmd=_imds_ssh_keys ;;
@userdata) cmd=_imds_userdata ;;
@nics) args="$IMDS_NICS" ;;
@hostname) args="$IMDS_HOSTNAME" ;;
@local-hostname) args="$IMDS_LOCAL_HOSTNAME" ;;
@ssh-keys) cmd=_imds_ssh_keys ;;
@userdata) cmd=_imds_userdata ;;
@nics) args="$IMDS_NICS" ;;
@nic:*)
cmd=imds
args=$(_imds_nic_args $(echo "${key#@nic:}" | tr , ' '))

View File

@ -2,13 +2,13 @@
# vim:set ts=2 et:
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$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 |
/usr/sbin/nvme id-ctrl "/dev/$BASE" -b 2>/dev/null |
dd bs=32 skip=96 count=1 2>/dev/null
}

View File

@ -4,7 +4,7 @@
set -e
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$LIBDIR/tiny-cloud/common"
if [ -z "$MDEV" ] || [ -z "$ACTION" ]; then
log crit "MDEV or ACTION undefined, aborting"

View File

@ -6,6 +6,7 @@ IMDS_QUERY="?format=text&api-version=2021-05-01"
IMDS_URI="metadata/instance"
IMDS_HOSTNAME="compute/name"
IMDS_LOCAL_HOSTNAME="$IMDS_HOSTNAME"
IMDS_SSH_KEYS="compute/publicKeys"
IMDS_USERDATA="compute/userData"
IMDS_NICS="network/interface"

View File

@ -5,6 +5,7 @@ IMDS_HEADER="Metadata-Flavor"
IMDS_URI="computeMetadata/v1"
IMDS_HOSTNAME="instance/hostname"
IMDS_LOCAL_HOSTNAME="$IMDS_HOSTNAME"
IMDS_SSH_KEYS="
project/attributes/ssh-keys
instance/attributes/ssh-keys

View File

@ -8,14 +8,14 @@ is_nocloud_loaded() { [ -f "$TINY_CLOUD_VAR/.nocloud_loaded" ]; }
_load_nocloud_cmdline() {
local kopt kv k v data
for kopt in $(cat "$ROOT"/proc/cmdline 2>/dev/null); do
for kopt in $(cat "$ROOT/proc/cmdline" 2>/dev/null); do
echo "$kopt" | grep -qE '(^|=)ds=nocloud(-net)?;' || continue
for kv in $(echo "${kopt#*;}" | tr \; ' '); do
k=$(echo "$kv" | cut -d= -f1)
v=$(echo "$kv" | cut -d= -f2-)
case "$k" in
h|hostname)
printf "\nhostname: %s" "$v" >> "$TINY_CLOUD_VAR/meta-data"
h|local-hostname)
printf "\nlocal-hostname: %s" "$v" >> "$TINY_CLOUD_VAR/meta-data"
;;
i|instance-id)
printf "\ninstance-id: %s" "$v" >> "$TINY_CLOUD_VAR/meta-data"
@ -46,7 +46,7 @@ _load_nocloud_cmdline() {
}
_load_nocloud_volume() {
local mntdir=$(mktemp -d "$ROOT"/mnt/cidata-XXXXXX)
local mntdir=$(mktemp -d "$ROOT/mnt/cidata-XXXXXX")
local data mounted
mkdir -p "$mntdir"
@ -87,7 +87,7 @@ load_nocloud() {
_imds() {
mkdir -p "$TINY_CLOUD_VAR"
local file="$TINY_CLOUD_VAR"/$(echo "$1" | cut -d/ -f1)
local file="$TINY_CLOUD_VAR/$(echo "$1" | cut -d/ -f1)"
local keypath="$(echo "$1" | cut -d/ -f2- | tr / ' ')"
is_nocloud_loaded || load_nocloud

View File

@ -5,6 +5,7 @@ IMDS_HEADER="Authorization"
IMDS_URI="opc/v2"
IMDS_HOSTNAME="instance/hostname"
IMDS_LOCAL_HOSTNAME="$IMDS_HOSTNAME"
IMDS_SSH_KEYS="instance/metadata/ssh_authorized_keys"
IMDS_USERDATA="instance/metadata/userdata"
IMDS_NICS="nics"
@ -28,7 +29,7 @@ _imds_ssh_keys() { _imds "$IMDS_SSH_KEYS"; }
_imds_nic_index() {
local m n=0
local mac=$(cat "/sys/class/net/$1/mac")
while m=$(imds $IMDS_NICS/$n/mac | tr A-F a-f); do
while m=$(imds "$IMDS_NICS/$n/mac" | tr A-F a-f); do
[ "$m" = "$mac" ] && echo $n; return 0
done
return 1

View File

@ -2,7 +2,7 @@
# vim: ts=4 et ft=sh:
# set defaults
[ -f "$ROOT"/etc/tiny-cloud.conf ] && . "$ROOT"/etc/tiny-cloud.conf
[ -f "$ROOT/etc/tiny-cloud.conf" ] && . "$ROOT/etc/tiny-cloud.conf"
: "${CLOUD:=unknown}"
: "${CLOUD_USER:=alpine}"
: "${TINY_CLOUD_LOGS:=$ROOT/var/log}"

View File

@ -3,7 +3,7 @@
# set defaults
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$LIBDIR/tiny-cloud/common"
: "${SKIP_INIT_ACTIONS:=}"
: "${HOTPLUG_TYPE:=mdev}"
@ -43,8 +43,8 @@ install_hotplugs() {
local result rc=0
if [ -f "$LIBDIR"/tiny-cloud/"$HOTPLUG_TYPE" ]; then
. "$LIBDIR"/tiny-cloud/"$HOTPLUG_TYPE"
if [ -f "$LIBDIR/tiny-cloud/$HOTPLUG_TYPE" ]; then
. "$LIBDIR/tiny-cloud/$HOTPLUG_TYPE"
fi
for module in $HOTPLUG_MODULES; do
@ -154,11 +154,11 @@ run_userdata() {
}
# load cloud-specific init functions / vars
: "${LIBDIR:=$PREFIX/lib}"
if [ -f "$LIBDIR"/tiny-cloud/cloud/"$CLOUD"/init ]; then
. "$LIBDIR"/tiny-cloud/cloud/"$CLOUD"/init
if [ -f "$LIBDIR/tiny-cloud/cloud/$CLOUD/init" ]; then
. "$LIBDIR/tiny-cloud/cloud/$CLOUD/init"
fi
# TODO: load user-data type-specific init functions / vars
### non-overrideable functions

View File

@ -34,6 +34,6 @@ mod__vnic_eth_hotplug() {
# load cloud-specific functions
: "${LIBDIR:=$PREFIX/lib}"
if [ -f "$LIBDIR"/tiny-cloud/cloud/"$CLOUD"/mdev ]; then
. "$LIBDIR"/tiny-cloud/cloud/"$CLOUD"/mdev
if [ -f "$LIBDIR/tiny-cloud/cloud/$CLOUD/mdev" ]; then
. "$LIBDIR/tiny-cloud/cloud/$CLOUD/mdev"
fi

View File

@ -0,0 +1,4 @@
# CloudConfig UserData Functions
# vim:set ts=4 et ft=sh:
# TODO

View File

@ -0,0 +1,4 @@
# Missing UserData Functions
# vim:set ts=4 et ft=sh:
# TODO: what to do if we have NO user-data yet

View File

@ -0,0 +1,4 @@
# Script UserData Functions
# vim:set ts=4 et ft=sh:
# TODO

View File

@ -0,0 +1,4 @@
# Unknown UserData Functions
# vim:set ts=4 et ft=sh:
# TODO: this would probably be mostly NOOPs

View File

@ -3,7 +3,7 @@
set -e
IFACE_CFG="$ROOT"/etc/network/interfaces
IFACE_CFG="$ROOT/etc/network/interfaces"
IFACE_DIR="${IFACE_CFG}.d"
cd "$IFACE_DIR"

View File

@ -6,7 +6,7 @@
[ -z "$VERBOSE" ] || set -x
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$LIBDIR/tiny-cloud/common"
[ -z "${IFACE}" ] && log -s crit "IFACE not set, aborting"

View File

@ -1,15 +1,12 @@
#!/bin/sh
# vim:set ts=4 et ft=sh:
# MacOS testing
getopt=/opt/homebrew/Cellar/gnu-getopt/2.38.1/bin/getopt
# Tiny Cloud
set -e
: "${LIBDIR:=$PREFIX/lib}"
. "$LIBDIR"/tiny-cloud/common
. "$LIBDIR/tiny-cloud/common"
usage() {
cat <<EOF
@ -19,15 +16,15 @@ EOF
bootstrap_complete() {
echo "Marking Instance Bootstrap Complete"
touch "$TINY_CLOUD_VAR"/.bootstrap-complete
touch "$TINY_CLOUD_VAR/.bootstrap-complete"
}
bootstrap_incomplete() {
echo "Marking Instance Bootstrap Incomplete"
rm -f "$TINY_CLOUD_VAR"/.bootstrap-complete
rm -f "$TINY_CLOUD_VAR/.bootstrap-complete"
}
args=$($getopt -o hb: --long help,bootstrap: -n ${0##*/} -- "$@")
args=$(getopt -o hb: --long help,bootstrap: -n ${0##*/} -- "$@")
if [ $? -ne 0 ]; then
usage >&2
exit 1
@ -60,13 +57,15 @@ case "$phase" in
esac
# is initial bootstrap already done?
if [ -f "$TINY_CLOUD_VAR"/.bootstrap-complete ]; then
if [ -f "$TINY_CLOUD_VAR/.bootstrap-complete" ]; then
log -s "Already bootstrapped"
exit 0;
fi
### default phase actions
# TODO? represent as vars containing lists of funcs?
early() {
expand_root
install_hotplugs
@ -84,6 +83,7 @@ final() {
}
# load init functions
. "$LIBDIR"/tiny-cloud/init
. "$LIBDIR/tiny-cloud/init"
# TODO? for loop over list of funcs? -- better for ebegin/eend-ish output
echo $phase "$@"

View File

@ -8,9 +8,11 @@ init_tests \
imds_help \
imds_space \
imds_aws_hostname \
imds_aws_local_hostname \
imds_aws_ssh_keys \
imds_nocloud_cmdline_hostname \
imds_nocloud_cidata_hostname
imds_nocloud_cmdline_local_hostname \
imds_nocloud_cidata_hostname \
imds_nocloud_cidata_local_hostname
imds_help_body() {
atf_check -o match:"Usage: imds" imds -h
@ -45,6 +47,12 @@ imds_aws_hostname_body() {
CLOUD=aws atf_check -o match:"myhostname" imds @hostname
}
imds_aws_local_hostname_body() {
aws_create_fake_nc
aws_set_fake_meta local-hostname myhostname
CLOUD=aws atf_check -o match:"myhostname" imds @local-hostname
}
imds_aws_ssh_keys_body() {
aws_create_fake_nc
aws_set_fake_meta public-keys 0=testuser
@ -53,40 +61,34 @@ imds_aws_ssh_keys_body() {
CLOUD=aws atf_check -o match:"ssh-ed25519 keydata" imds @ssh-keys
}
imds_nocloud_cmdline_hostname_body() {
imds_nocloud_cmdline_local_hostname_body() {
atf_require_prog yx
mkdir proc
for key in h hostname; do
for key in h local-hostname; do
echo "BOOT_IMAGE=/boot/vmlinuz-lts ro ds=nocloud;$key=myhostname" > proc/cmdline
CLOUD=nocloud atf_check \
-o match:'^myhostname$' \
imds @hostname
imds @local-hostname
done
}
imds_nocloud_cidata_hostname_body() {
atf_require_prog yx
fake_bin mount <<-EOF
#!/bin/sh
# find last arg which is the mount dir
while ! [ -d "\$1" ]; do
shift
done
printf "#cloud-config\nhostname: myhostname\n" \
> "\$1"/meta-data
fake_metadata_nocloud <<-EOF
hostname: myhostname
EOF
fake_bin umount <<-EOF
#!/bin/sh
while ! [ -d "\$1" ]; do
shift
done
rm -f "\$1"/meta-data
EOF
mkdir -p mnt
CLOUD=nocloud atf_check \
-o match:'^myhostname$' \
imds @hostname
}
imds_nocloud_cidata_local_hostname_body() {
atf_require_prog yx
fake_metadata_nocloud <<-EOF
local-hostname: my-local-hostname
EOF
CLOUD=nocloud atf_check \
-o match:'^my-local-hostname$' \
imds @local-hostname
}

View File

@ -29,6 +29,16 @@ fake_bin() {
PATH="$PWD/bin:$PATH"
}
fake_umount() {
fake_bin umount <<-EOF
#!/bin/sh
while ! [ -d "\$1" ]; do
shift
done
rm -f "\$1"/meta-data "\$1"/user-data
EOF
}
fake_userdata_nocloud() {
local file="$(mktemp -p "$PWD")"
cat > "$file"
@ -43,3 +53,17 @@ fake_userdata_nocloud() {
mkdir -p mnt
}
fake_metadata_nocloud() {
local file="$(mktemp -p "$PWD")"
cat > "$file"
fake_bin mount <<-EOF
#!/bin/sh
# find last arg which is the mount dir
while ! [ -d "\$1" ]; do
shift
done
cp "$file" "\$1"/meta-data
EOF
mkdir -p mnt
fake_umount
}