gh-131: Improvements to FreeBSD contextualization

pull/135/head
Vlastimil Holer 6 years ago
parent b57c047c4a
commit ef254cb0af
No known key found for this signature in database
GPG Key ID: EB549BADEFD07C53

@ -14,14 +14,16 @@ Latest versions can be downloaded from the
| Platform | Versions |
|-------------------------------------|----------------------------------------|
| CentOS | 6, 7 |
| Fedora | 27, 28 |
| Red Hat Enterprise Linux | 7 |
| Fedora | 28, 29 |
| openSUSE | 42.3, 15 |
| SUSE Linux Enterprise Server (SLES) | 12 SP3 |
| Debian | 8, 9 |
| Debian | 8, 9, 10 |
| Devuan | 1, 2 |
| Ubuntu | 14.04, 16.04, 18.04 |
| Ubuntu | 14.04, 16.04, 18.04, 18.10 |
| Alpine Linux | 3.6, 3.7, 3.8 |
| Amazon Linux | 2 |
| FreeBSD | 11.2, 12.0 |
## Development

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
NETWORK=${NETWORK:-yes}
NETWORK_EC2=${NETWORK_EC2:-no}
@ -46,6 +46,9 @@ elif which rc-update >/dev/null 2>&1; then
rc-update add "${S}" boot >/dev/null 2>&1
done
elif [ -f /etc/rc.conf ]; then
:
else
echo 'WARNING: Contextualization service not enabled automatically' >&2
fi
@ -131,3 +134,16 @@ USERCONTROL=yes
EOT
fi
fi
# FreeBSD based distros
#TODO: pfsense paths?
if [ -f /etc/rc.conf ]; then
rm -f /etc/rc.conf.d/network /etc/rc.conf.d/routing
sed -i '' \
-e '/^ifconfig_/d' \
-e '/^route_/d' \
-e '/^static_routes/d' \
-e '/^defaultrouter/d' \
/etc/rc.conf
fi

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
NETWORK_EC2=yes
SERVICES='one-context'

@ -1,4 +0,0 @@
#!/bin/bash
rm /etc/one-context.d/loc-05-grow-rootfs /etc/one-context.d/loc-10-network-pci /etc/one-context.d/loc-14-mount-swap
rm /etc/one-context.d/loc-30-console /etc/one-context.d/net-11-fix-loopback /etc/one-context.d/net-99-report-ready

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
udevadm control --reload >/dev/null 2>&1 || :

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
udevadm control --reload >/dev/null 2>&1 || :

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
SERVICES=${SERVICES:-one-context one-context-local}

@ -1,3 +1,3 @@
#!/bin/bash
#!/usr/bin/env bash
SERVICES='one-context'

@ -1,6 +1,13 @@
#!/bin/sh
#!/usr/bin/env bash
set -e
# FreeBSD
if [ -x /etc/rc.d/growfs ]; then
/etc/rc.d/growfs onestart
exit $?
fi
MOUNT_LINE=$(cat /etc/mtab | grep ' / ' | grep -v '^rootfs')
DEVICE=$(echo "$MOUNT_LINE" | cut -d' ' -f1)
FSTYPE=$(echo "$MOUNT_LINE" | cut -d' ' -f3)

@ -31,7 +31,7 @@ get_pci_interfaces()
get_dev_from_pci()
{
DEV=$(find /sys/class/net/*/device -lname "*$1" | awk -F '/' '{print $5}')
DEV=$(find /sys/class/net/*/device -lname "*$1" 2>/dev/null | awk -F '/' '{print $5}')
if [ -z "$DEV" ]; then
echo "PCI Device $1 not found" >&2

@ -1,15 +1,14 @@
#!/usr/bin/env bash
activate_swaps() {
activate_swaps_linux() {
SWAP_DRIVES=$(blkid -t TYPE="swap" -o device)
for SWAP in $SWAP_DRIVES ; do
if [ -z "$(swapon -s | grep $SWAP)" ]; then
swapon $SWAP
fi
done
}
activate_swaps
if [ "$(uname -s)" = 'Linux' ]; then
activate_swaps_linux
fi

@ -18,47 +18,85 @@
# defaults
USERNAME=${USERNAME:-root}
USERNAME_SHELL=${USERNAME_SHELL:-/bin/bash}
USERNAME_SUDO=${USERNAME_SUDO:-${GRANT_SUDO:-YES}}
USERNAME_SUDO=$(echo "${USERNAME_SUDO}" | tr '[:lower:]' '[:upper:]')
USERNAME_PASSWORD_RESET=${USERNAME_PASSWORD_RESET:-NO}
USERNAME_PASSWORD_RESET=$(echo "${USERNAME_PASSWORD_RESET}" | tr '[:lower:]' '[:upper:]')
_kernel="$(uname -s)"
case "${_kernel}" in
'FreeBSD')
USERNAME_SHELL=${USERNAME_SHELL:-/usr/local/bin/bash}
_sudoers_file='/usr/local/etc/sudoers.d/one-context'
;;
*)
USERNAME_SHELL=${USERNAME_SHELL:-/bin/bash}
_sudoers_file='/etc/sudoers.d/one-context'
;;
esac
# create user if missing
if ! getent passwd "${USERNAME}" > /dev/null 2>&1; then
useradd -m "${USERNAME}" -p '*' -s "${USERNAME_SHELL}"
if [ "${_kernel}" = 'FreeBSD' ]; then
pw user add "${USERNAME}" -m -s "${USERNAME_SHELL}" -w no
else
useradd -m "${USERNAME}" -p '*' -s "${USERNAME_SHELL}"
fi
fi
# enable sudo
if [ "${USERNAME_SUDO}" == "YES" ] && [ "${USERNAME}" != "root" ]; then
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >/etc/sudoers.d/one-context
chmod 0440 /etc/sudoers.d/one-context
elif [ -f /etc/sudoers.d/one-context ]; then
unlink /etc/sudoers.d/one-context
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >"${_sudoers_file}"
chmod 0440 "${_sudoers_file}"
elif [ -f "${_sudoers_file}" ]; then
unlink "${_sudoers_file}"
fi
# set password
if [ -n "${CRYPTED_PASSWORD_BASE64}" ]; then
CRYPTED_PASSWORD=$(echo $CRYPTED_PASSWORD_BASE64 | base64 -d)
usermod -p "${CRYPTED_PASSWORD}" "${USERNAME}"
if [ "${_kernel}" = 'FreeBSD' ]; then
echo "${CRYPTED_PASSWORD}" | pw user mod "${USERNAME}" -H 0
else
usermod -p "${CRYPTED_PASSWORD}" "${USERNAME}"
fi
elif [ -n "${PASSWORD_BASE64}" ]; then
PASSWORD=$(echo $PASSWORD_BASE64 | base64 -d)
chpasswd <<< "${USERNAME}:${PASSWORD}"
if [ "${_kernel}" = 'FreeBSD' ]; then
echo $PASSWORD | pw user mod "${USERNAME}" -h 0
else
chpasswd <<< "${USERNAME}:${PASSWORD}"
fi
if [ $? -ne 0 ]; then
passwd "${USERNAME}" <<EOF
${PASSWORD}
${PASSWORD}
EOF
fi
fi
elif [ -n "${CRYPTED_PASSWORD}" ]; then
usermod -p "${CRYPTED_PASSWORD}" "${USERNAME}"
if [ "${_kernel}" = 'FreeBSD' ]; then
echo $CRYPTED_PASSWORD | pw user mod "${USERNAME}" -H 0
else
usermod -p "${CRYPTED_PASSWORD}" "${USERNAME}"
fi
elif [ -n "${PASSWORD}" ]; then
chpasswd <<< "${USERNAME}:${PASSWORD}"
if [ "${_kernel}" = 'FreeBSD' ]; then
echo $PASSWORD | pw user mod "${USERNAME}" -h 0
else
chpasswd <<< "${USERNAME}:${PASSWORD}"
fi
if [ $? -ne 0 ]; then
passwd "${USERNAME}" <<EOF
${PASSWORD}
${PASSWORD}
EOF
fi
fi
elif [ "${USERNAME_PASSWORD_RESET}" = 'YES' ]; then
usermod -p '*' "${USERNAME}"
if [ "${_kernel}" = 'FreeBSD' ]; then
pw user mod "${USERNAME}" -w no
else
usermod -p '*' "${USERNAME}"
fi
fi

@ -1,66 +0,0 @@
#!/usr/bin/env bash
# -------------------------------------------------------------------------- #
# Copyright 2010-2016, OpenNebula Systems #
# #
# 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. #
#--------------------------------------------------------------------------- #
# defaults
USERNAME=${USERNAME:-root}
USERNAME_SHELL=${USERNAME_SHELL:-/usr/local/bin/bash}
USERNAME_SUDO=${USERNAME_SUDO:-${GRANT_SUDO:-YES}}
USERNAME_SUDO=$(echo "${USERNAME_SUDO}" | tr '[:lower:]' '[:upper:]')
USERNAME_PASSWORD_RESET=${USERNAME_PASSWORD_RESET:-NO}
USERNAME_PASSWORD_RESET=$(echo "${USERNAME_PASSWORD_RESET}" | tr '[:lower:]' '[:upper:]')
if ! getent passwd "${USERNAME}" > /dev/null 2>&1; then
pw useradd "${USERNAME}" -m -s "${USERNAME_SHELL}"
fi
if [ "${USERNAME_SUDO}" == "YES" ] && [ "${USERNAME}" != "root" ]; then
echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" >/usr/local/etc/sudoers.d/one-context
chmod 0440 /etc/sudoers.d/one-context
elif [ -f /etc/sudoers.d/one-context ]; then
unlink /etc/sudoers.d/one-context
fi
if [ -n "${CRYPTED_PASSWORD_BASE64}" ]; then
CRYPTED_PASSWORD=$(echo $CRYPTED_PASSWORD_BASE64 | base64 -d)
echo $CRYPTED_PASSWORD | pw mod user $USERNAME -H 0
elif [ -n "${PASSWORD_BASE64}" ]; then
PASSWORD=$(echo $PASSWORD_BASE64 | base64 -d)
echo $PASSWORD | pw mod user $USERNAME -h 0
if [ $? -ne 0 ]; then
passwd "${USERNAME}" <<EOF
${PASSWORD}
${PASSWORD}
EOF
fi
elif [ -n "${CRYPTED_PASSWORD}" ]; then
echo $CRYPTED_PASSWORD | pw mod user $USERNAME -H 0
elif [ -n "${PASSWORD}" ]; then
echo $PASSWORD | pw mod user $USERNAME -h 0
if [ $? -ne 0 ]; then
passwd "${USERNAME}" <<EOF
${PASSWORD}
${PASSWORD}
EOF
fi
elif [ "${USERNAME_PASSWORD_RESET}" = 'YES' ]; then
if command -v usermod >/dev/null 2>&1; then
pw usermod -p '*' "${USERNAME}"
fi
fi

@ -1,5 +1,6 @@
#!/usr/bin/env bash
for DEV_TTY in /dev/tty[0-9]*; do
TERM=linux setterm -blank 0 -powerdown 0 >>"${DEV_TTY}"
# Linux
for _dev_tty in $(find /dev -type c -name 'tty[0-9]*'); do
TERM=linux setterm -blank 0 -powerdown 0 >>"${_dev_tty}"
done

@ -1,14 +1,23 @@
#!/usr/bin/env bash
_kernel="$(uname -s)"
if [ "${_kernel}" = 'FreeBSD' ]; then
SED_I="sed -i ''"
else
SED_I="sed -i''"
fi
function set_hostname() {
hostname=$1
local hostname=$1
if [ -d /run/systemd/system/ ]; then
hostnamectl set-hostname --static "${hostname}"
else
if [ -f /etc/sysconfig/network ]; then
sed -i '/^HOSTNAME=.*$/d' /etc/sysconfig/network
${SED_I} '/^HOSTNAME=.*$/d' /etc/sysconfig/network
echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network
elif [ "${_kernel}" = 'FreeBSD' ]; then
sysrc hostname="${hostname}"
else
echo "${hostname}" >/etc/hostname
fi
@ -19,7 +28,7 @@ function set_hostname() {
function set_domainname() {
domain=$1
sed -i '/^domain .*/d' /etc/resolv.conf
${SED_I} -e '/^domain .*/d' /etc/resolv.conf
echo "domain ${domain}" >>/etc/resolv.conf
}
@ -28,6 +37,7 @@ function get_first_ip() {
ip=${ip:-$(ip route get 1 2>/dev/null | grep 'src [0-9\.]\+' | head -1 | sed -e 's/^.*src \([0-9\.]*\).*$/\1/')}
ip=${ip:-$(ip -4 address show scope global up 2>/dev/null | awk '/inet / { gsub(/\/[^\/]+$/, "", $2); print $2; exit}')}
ip=${ip:-$(ifconfig 2>/dev/null | awk '/inet / { gsub(/\/[^\/]+$/, "", $2); print $2; exit}')}
ip=${ip:-$(hostname -I 2>/dev/null | cut -d' ' -f1)}
ip=${ip:-$(hostname -i 2>/dev/null)}
@ -54,22 +64,22 @@ function update_hosts() {
fi
note='# one-contextd'
entry="${ip}\t${hosts}\t\t${note}"
entry="${ip} ${hosts} ${note}"
# update our old entry
if grep -qi "${note}" /etc/hosts; then
sed -i -e "s/^.*${note}\$/${entry}/" /etc/hosts
${SED_I} -e "s/^.*${note}\$/${entry}/" /etc/hosts
# update entry with same IP (but not localhost)
elif grep -E "^${ip}\s" /etc/hosts | grep -qv localhost; then
sed -i -e "/localhost/! s/^${ip}\s.*\$/${entry}/" /etc/hosts
elif grep -E "^${ip}[[:space:]]" /etc/hosts | grep -qv localhost; then
${SED_I} -e "/localhost/! s/^${ip}[[:space:]].*\$/${entry}/" /etc/hosts
# update entry with same name
elif grep -qE "\s${name}(\s|#|\$)" /etc/hosts; then
sed -i -re "s/^.*\s${name}([ #\t].*|$)/${entry}/" /etc/hosts
elif grep -qE "[[:space:]]${name}([[:space:]]|#|\$)" /etc/hosts; then
${SED_I} -re "s/^.*[[:space:]]${name}([[:space:]#].*|$)/${entry}/" /etc/hosts
# create new entry
elif [ -f /etc/hosts ]; then
sed -i -e "1s/^/${entry}\n/" /etc/hosts
${SED_I} -e "1s/^/${entry}"$'\\\n/' /etc/hosts
else
echo -e "${entry}" >>/etc/hosts
echo "${entry}" >>/etc/hosts
fi
}
@ -104,7 +114,13 @@ if [ -n "${name}" ]; then
domain=''
fi
set_hostname "${hostname}"
# FreeBSD
if [ "${_kernel}" = 'FreeBSD' ]; then
set_hostname "${name}"
else
set_hostname "${hostname}"
fi
if [ -n "${domain}" ]; then
set_domainname "${domain}"
fi

@ -1,133 +0,0 @@
#!/usr/bin/env bash
function set_hostname() {
local hostname=$1
if [ -d /run/systemd/system/ ]; then
hostnamectl set-hostname --static "${hostname}"
else
if [ -f /etc/sysconfig/network ]; then
sed -i '/^HOSTNAME=.*$/d' /etc/sysconfig/network
echo "HOSTNAME=${hostname}" >>/etc/sysconfig/network
else
echo "${hostname}" >/etc/hostname
fi
hostname "${hostname}"
fi
}
function set_domainname() {
domain=$1
sed -i '/^domain .*/d' /etc/resolv.conf
echo "domain ${domain}" >>/etc/resolv.conf
}
function get_first_ip() {
local ip
ip=${ip:-$(ifconfig | grep inet | head -n 1 | awk '{print $2}')}
echo "${ip}"
}
function get_dns_name() {
text=$(LC_ALL=C getent hosts "$1" 2>/dev/null)
[ $? = 0 ] || exit 0
[[ $text == *"has no PTR record" ]] && exit 0
name=$(echo "$text" | awk '/(has address|name pointer)/ {print $(NF)}' | sed 's/\.$//')
echo $name
}
function update_hosts() {
ip=$1
name=$2
hostname=$3
if [ "x${hostname}" = "x${name}" ]; then
hosts="${name}"
else
hosts="${name} ${hostname}"
fi
note='# one-contextd'
entry="${ip}$(printf '\t')${hosts}$(printf '\t')$(printf '\t')${note}"
newline="$(printf '\n')"
# update our old entry
if grep -qi "${note}" /etc/hosts; then
sed -i -e "s/^.*${note}/${entry}/" /etc/hosts
# update entry with same IP (but not localhost)
elif grep -E "^${ip}\s" /etc/hosts | grep -qv localhost; then
sed -i -e "/localhost/! s/^${ip}\s.*\$/${entry}/" /etc/hosts
# update entry with same name
elif grep -qE "\s${name}(\s|#|\$)" /etc/hosts; then
sed -i -re "s/^.*\s${name}([ #\t].*|$)/${entry}/" /etc/hosts
# create new entry
elif [ -f /etc/hosts ]; then
sed -i -e '1s/^/'"${entry}"$'\\\n/' /etc/hosts
else
echo -e "${entry}" >>/etc/hosts
fi
}
#####
first_ip=$(get_first_ip)
if [ -n "$SET_HOSTNAME" ]; then
name=$(echo "$SET_HOSTNAME" | \
sed -e 's/[^-a-zA-Z0-9\.]/-/g' -e 's/^-*//g' -e 's/-*$//g')
elif [ -n "$DNS_HOSTNAME" ]; then
name=$(get_dns_name "${first_ip}")
elif [ "${EC2_HOSTNAME}" = 'YES' ]; then
# try to quickly get hostname from the EC2 metadata server or
# create hostname based on the first IPv4 (format: "ip-1-2-3-4")
name=$(curl -sf -m 5 'http://169.254.169.254/latest/meta-data/local-hostname' 2>/dev/null)
if [ -z "${name}" ]; then
name="$(echo "${first_ip}" | grep -x '[0-9\.]\+' | tr . -)"
if [ -n "${name}" ]; then
name="ip-${name}"
fi
fi
fi
if [ -n "${name}" ]; then
# split host and domain names
hostname=${name%%.*}
domain=${name#*.}
if [ "x${domain}" = "x${hostname}" ]; then
domain=''
fi
if [ -n "${domain}" ]; then
set_hostname "${hostname}.${domain}"
else
set_hostname "${hostname}"
fi
if [ -n "${domain}" ]; then
set_domainname "${domain}"
fi
if [ -n "${DNS_HOSTNAME}" ]; then
host_ip=$first_ip
else
# If selected hostname resolves on first IP,
# use first IP for local hostname in /etc/hosts.
# Otherwise use loopback IP.
name_ip=$(get_dns_name "${name}")
if [ "x${first_ip}" = "x${name_ip}" ]; then
host_ip=$first_ip
elif [ -f /etc/debian_version ]; then
host_ip='127.0.1.1'
else
host_ip='127.0.0.1'
fi
fi
if [ -n "${host_ip}" ]; then
update_hosts "${host_ip}" "${name}" "${hostname}"
fi
fi

@ -17,11 +17,12 @@
#--------------------------------------------------------------------------- #
### BEGIN INIT INFO
# REQUIRE: LOGIN netif
# PROVIDE: one-context
# REQUIRE: LOGIN NETWORKING
### END INIT INFO
. /etc/rc.subr
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
name="one_context"

@ -23,6 +23,7 @@
### END INIT INFO
. /etc/rc.subr
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
name="one_context_force"
@ -33,6 +34,5 @@ command="/usr/sbin/one-context-run"
command_args="force"
load_rc_config $name
: ${one_context_force_enable:="yes"}
: ${one_context_force_enable:="no"}
run_rc_command "$1"

@ -17,11 +17,13 @@
#--------------------------------------------------------------------------- #
### BEGIN INIT INFO
# REQUIRE: FILESYSTEMS
# PROVIDE: one-context-local
# REQUIRE: FILESYSTEMS
# BEFORE: NETWORKING hostname
### END INIT INFO
. /etc/rc.subr
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
name="one_context_local"
@ -34,4 +36,3 @@ command_args="local"
load_rc_config $name
: ${one_context_local_enable:="yes"}
run_rc_command "$1"

@ -23,14 +23,17 @@
. /etc/rc.subr
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
name="one_context_reconfigure"
desc="OpenNebula reconfiguration"
rcvar="${name}_enable"
command="/usr/sbin/one-context-run"
# run without any delay
export TIMEOUT=0
load_rc_config $name
: ${one_context_reconfigure_enable:="yes"}
: ${one_context_reconfigure_enable:="no"}
run_rc_command "$1"

@ -22,6 +22,7 @@
### END INIT INFO
. /etc/rc.subr
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin
name="one_context_reconfigure_delayed"
@ -31,6 +32,5 @@ rcvar="${name}_enable"
command="/usr/sbin/one-context-run"
load_rc_config $name
: ${one_context_reconfigure_delayed_enable:="yes"}
: ${one_context_reconfigure_delayed_enable:="no"}
run_rc_command "$1"

@ -22,11 +22,11 @@ case "${TARGET}" in
TYPE=${TYPE:-freebsd}
EXT=${EXT:-txz}
TAGS=${TAGS:-bsd bsd_rc one sysv}
DEPENDS=${DEPENDS:-sudo bash curl base64}
DEPENDS=${DEPENDS:-sudo bash curl base64 ruby open-vm-tools-nox11}
PROVIDES=${PROVIDES:-}
REPLACES=${REPLACES:-addon-context}
CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2}
POSTIN=${POSTINST:-pkg/postinstall.freebsd}
POSTIN=${POSTINST:-pkg/postinstall}
PREUN=${PREUN:-}
POSTUN=${POSTUN:-}
POSTUP=${POSTUP:-}

Loading…
Cancel
Save