From 26d4cc64196dbc80f06548f35fe1f9d516c0f021 Mon Sep 17 00:00:00 2001 From: sergiojvg Date: Mon, 26 Nov 2018 08:13:30 -0600 Subject: [PATCH] FreeBSD context (#120) * BSD context * BSD service scripts * Added ToDo * Add context scripts * Freebsd packaging * Freebsd packaging finished * Rollback OS detection * Fix bug with default gateway configuration * Add services for pfsense * Remove bsd context script * Replace /bin/bash with /usr/bin/env bash * Remove /bin/bash symlink creation * Fix service naming * Replace /bin/bash for /bin/sh on BSD service scripts * Fix hardcoded cdrom drive * Fix service start on boot bug on pfsense * Fix bug when creating new users * Fix bud with sudo * Fix bug with crypted passwords * Fix bug with hostname * Fix sed bug related to hostname * Fix bug with domains * Fixed bug: variable should be local --- pkg/postinstall.freebsd | 4 + src/etc/one-context.d/loc-10-network##apk.one | 2 +- .../one-context.d/loc-10-network##arch.one | 2 +- src/etc/one-context.d/loc-10-network##bsd.one | 255 ++++++++++++++++++ src/etc/one-context.d/loc-10-network##deb.one | 2 +- src/etc/one-context.d/loc-10-network##rpm.one | 2 +- src/etc/one-context.d/loc-10-network-pci##one | 2 +- src/etc/one-context.d/loc-11-dns##one | 2 +- src/etc/one-context.d/loc-12-firewall##apk | 2 +- src/etc/one-context.d/loc-14-mount-swap##one | 2 +- src/etc/one-context.d/loc-15-ip_forward##apk | 2 +- src/etc/one-context.d/loc-15-keepalived##apk | 2 +- src/etc/one-context.d/loc-16-gen-env | 2 +- .../loc-20-set-username-password | 2 +- .../loc-20-set-username-password##bsd | 66 +++++ src/etc/one-context.d/loc-22-ssh_public_key | 2 +- .../one-context.d/loc-24-ssh_host_key##deb | 2 +- src/etc/one-context.d/loc-30-console##one | 2 +- .../one-context.d/net-11-fix-loopback##one | 2 +- src/etc/one-context.d/net-15-hostname | 2 +- src/etc/one-context.d/net-15-hostname##bsd | 133 +++++++++ src/etc/one-context.d/net-97-start-script | 2 +- src/etc/one-context.d/net-98-execute-scripts | 2 +- src/etc/one-context.d/net-99-report-ready | 2 +- src/etc/rc.d/one-context##bsd_rc.one | 36 +++ src/etc/rc.d/one-context-force##bsd_rc.one | 38 +++ src/etc/rc.d/one-context-local##bsd_rc.one | 37 +++ ...ne-context-reconfigure-delayed##bsd_rc.one | 36 +++ .../rc.d/one-contextre-configure##bsd_rc.one | 36 +++ src/usr/bin/onegate | 2 +- .../rc.d/one-context-force.sh##pfsense_rc.one | 38 +++ .../rc.d/one-context-local.sh##pfsense_rc.one | 37 +++ ...ext-reconfigure-delayed.sh##pfsense_rc.one | 36 +++ .../etc/rc.d/one-context.sh##pfsense_rc.one | 36 +++ ...one-contextre-configure.sh##pfsense_rc.one | 36 +++ src/usr/sbin/one-context-run##one | 2 +- src/usr/sbin/one-contextd | 30 ++- targets.sh | 34 +++ 38 files changed, 906 insertions(+), 26 deletions(-) create mode 100755 pkg/postinstall.freebsd create mode 100755 src/etc/one-context.d/loc-10-network##bsd.one create mode 100755 src/etc/one-context.d/loc-20-set-username-password##bsd create mode 100755 src/etc/one-context.d/net-15-hostname##bsd create mode 100755 src/etc/rc.d/one-context##bsd_rc.one create mode 100755 src/etc/rc.d/one-context-force##bsd_rc.one create mode 100755 src/etc/rc.d/one-context-local##bsd_rc.one create mode 100755 src/etc/rc.d/one-context-reconfigure-delayed##bsd_rc.one create mode 100755 src/etc/rc.d/one-contextre-configure##bsd_rc.one create mode 100755 src/usr/local/etc/rc.d/one-context-force.sh##pfsense_rc.one create mode 100755 src/usr/local/etc/rc.d/one-context-local.sh##pfsense_rc.one create mode 100755 src/usr/local/etc/rc.d/one-context-reconfigure-delayed.sh##pfsense_rc.one create mode 100755 src/usr/local/etc/rc.d/one-context.sh##pfsense_rc.one create mode 100755 src/usr/local/etc/rc.d/one-contextre-configure.sh##pfsense_rc.one diff --git a/pkg/postinstall.freebsd b/pkg/postinstall.freebsd new file mode 100755 index 0000000..cf89a17 --- /dev/null +++ b/pkg/postinstall.freebsd @@ -0,0 +1,4 @@ +#!/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 diff --git a/src/etc/one-context.d/loc-10-network##apk.one b/src/etc/one-context.d/loc-10-network##apk.one index 15aa2d9..4925c8f 100755 --- a/src/etc/one-context.d/loc-10-network##apk.one +++ b/src/etc/one-context.d/loc-10-network##apk.one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2014, C12G Labs S.L. # diff --git a/src/etc/one-context.d/loc-10-network##arch.one b/src/etc/one-context.d/loc-10-network##arch.one index 6878ef3..70fcd3d 100755 --- a/src/etc/one-context.d/loc-10-network##arch.one +++ b/src/etc/one-context.d/loc-10-network##arch.one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-10-network##bsd.one b/src/etc/one-context.d/loc-10-network##bsd.one new file mode 100755 index 0000000..c766023 --- /dev/null +++ b/src/etc/one-context.d/loc-10-network##bsd.one @@ -0,0 +1,255 @@ +#!/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. # +#--------------------------------------------------------------------------- # + +COMMAND=${1} + +# Gets IP address from a given MAC +mac2ip() { + mac=$1 + + let ip_a=0x`echo $mac | cut -d: -f 3` + let ip_b=0x`echo $mac | cut -d: -f 4` + let ip_c=0x`echo $mac | cut -d: -f 5` + let ip_d=0x`echo $mac | cut -d: -f 6` + + ip="$ip_a.$ip_b.$ip_c.$ip_d" + + echo $ip +} + +# Gets the network part of an IP +get_network() { + network=$(get_iface_var "NETWORK") + + if [ -z "$network" ]; then + network="$(echo $IP | cut -d'.' -f1,2,3).0" + fi + + echo $network +} + +# Gets the network mask +get_mask() { + mask=$(get_iface_var "MASK") + + if [ -z "$mask" ]; then + mask="255.255.255.0" + fi + + echo $mask +} + +# Gets device MTU +get_mtu() { + mtu=$(get_iface_var "MTU") + + echo $mtu +} + +is_gateway() { + if [ -z "$GATEWAY_IFACE_NUM" ]; then + true + else + [ "$IFACE_NUM" = "$GATEWAY_IFACE_NUM" ] + fi +} + +# Gets the network gateway +get_gateway() { + if is_gateway; then + gateway=$(get_iface_var "GATEWAY") + + echo $gateway + fi +} + +# Gets the network gateway6 +get_gateway6() { + if is_gateway; then + get_iface_var "GATEWAY6" + fi +} + +get_ip() { + ip=$(get_iface_var "IP") + + echo $ip +} + +get_iface_var() { + var_name="${UPCASE_DEV}_$1" + var=$(eval "echo \"\${$var_name}\"") + + echo $var +} + +gen_iface_conf() { + cat < /etc/rc.conf.d/routing + fi + +} + +gen_iface6_conf() { + if [ "$CONFIG_PATH" = "/etc/sysconfig/network" ]; then + echo "IPADDR_0=$IP6/${IP6_PREFIX_LENGTH:-64}" + + cat <> /etc/sysconfig/network/ifsysctl-$DEV +net.ipv6.conf.\$SYSCTL_IF.autoconf = 0 +net.ipv6.conf.\$SYSCTL_IF.accept_ra = 0 +EOT + else + cat <> /etc/sysconfig/network/ifroute-$DEV + else + echo "IPV6_DEFAULTGW=$GATEWAY6" + fi + fi + + if [ -n "$MTU" ]; then + echo "IPV6_MTU=$MTU" + fi +} + +get_interface_mac() +{ + macs_array=($(ifconfig | grep ether | awk '{print $2}')) + #iface_name=() + + for mac in "${macs_array[@]}" + do + echo "$(ifconfig | grep -B 2 $mac | head -n 1 | awk '{print $1}' | cut -d ':' -f 1) $mac" + #iface_mac+=$(ifconfig | grep -B 2 $mac | head -n 1 | awk '{print $1}' | cut -d ':' -f 1) + done +} + +get_context_interfaces() +{ + env | grep -E "^ETH[0-9]+_MAC=" | sed 's/_.*$//' | sort +} + +get_dev() +{ + list="$1" + mac="$2" + + echo "$list" | grep "$mac" | cut -d' ' -f1 | tail -n1 +} + +gen_network_configuration() +{ + if [ -d /etc/rc.conf.d ]; then + CONFIG_PATH=/etc/rc.conf.d + fi + + echo > /etc/rc.conf.d/network + + INTERFACE_MAC=$(get_interface_mac) + CONTEXT_INTERFACES=$(get_context_interfaces) + GATEWAY_IFACE_NUM=$(echo "$GATEWAY_IFACE" | sed 's/^ETH//') + + for interface in $CONTEXT_INTERFACES; do + UPCASE_DEV=$interface + MAC=$(get_iface_var "MAC") + DEV=$(get_dev "$INTERFACE_MAC" "$MAC") + IFACE_NUM=$(echo "$UPCASE_DEV" | sed 's/^ETH//') + + IP=$(get_ip) + NETWORK=$(get_network) + MASK=$(get_mask) + MTU=$(get_mtu) + GATEWAY=$(get_gateway) + + IP6=$(get_iface_var "IP6") + [[ -z $IP6 ]] && IP6=$(get_iface_var "IPV6") + IP6_PREFIX_LENGTH=$(get_iface_var "IP6_PREFIX_LENGTH") + IP6_ULA=$(get_iface_var "IP6_ULA") + GATEWAY6=$(get_gateway6) + + [ -z "${IP}${IP6}" ] && continue + [ -z "${DEV}" ] && continue + + + ( + [[ -n $IP ]] && gen_iface_conf + #[[ -n $IP6 ]] && gen_iface6_conf + + + ) >> ${CONFIG_PATH}/network + + +# ( +# rm -f /etc/sysconfig/network/ifroute-$DEV +# rm -f /etc/sysconfig/network/ifsysctl-$DEV +# +# cat <> ${CONFIG_PATH}/ifcfg-${DEV} + + service netif restart + service routing restart + + done +} + +configure_network() +{ + gen_network_configuration + + if [ "${COMMAND}" = 'reconfigure' ]; then + service network restart + fi + + sleep 2 +} + +[ -z "$(env | cut -d= -f1 | grep -E '^ETH[0-9]+_IPV*6*')" ] && exit 0 + +configure_network + diff --git a/src/etc/one-context.d/loc-10-network##deb.one b/src/etc/one-context.d/loc-10-network##deb.one index aaa557a..8069d14 100755 --- a/src/etc/one-context.d/loc-10-network##deb.one +++ b/src/etc/one-context.d/loc-10-network##deb.one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-10-network##rpm.one b/src/etc/one-context.d/loc-10-network##rpm.one index f1ebc63..574e8b1 100755 --- a/src/etc/one-context.d/loc-10-network##rpm.one +++ b/src/etc/one-context.d/loc-10-network##rpm.one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-10-network-pci##one b/src/etc/one-context.d/loc-10-network-pci##one index ab5aab0..d1aae3d 100755 --- a/src/etc/one-context.d/loc-10-network-pci##one +++ b/src/etc/one-context.d/loc-10-network-pci##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-11-dns##one b/src/etc/one-context.d/loc-11-dns##one index 1a51eba..ce49b9c 100755 --- a/src/etc/one-context.d/loc-11-dns##one +++ b/src/etc/one-context.d/loc-11-dns##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-12-firewall##apk b/src/etc/one-context.d/loc-12-firewall##apk index af0a279..93445ee 100755 --- a/src/etc/one-context.d/loc-12-firewall##apk +++ b/src/etc/one-context.d/loc-12-firewall##apk @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-14-mount-swap##one b/src/etc/one-context.d/loc-14-mount-swap##one index 78967fc..68e46d8 100755 --- a/src/etc/one-context.d/loc-14-mount-swap##one +++ b/src/etc/one-context.d/loc-14-mount-swap##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash activate_swaps() { diff --git a/src/etc/one-context.d/loc-15-ip_forward##apk b/src/etc/one-context.d/loc-15-ip_forward##apk index 7b87289..bd2dffb 100755 --- a/src/etc/one-context.d/loc-15-ip_forward##apk +++ b/src/etc/one-context.d/loc-15-ip_forward##apk @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash if [ -z "${VROUTER_ID}${VROUTER_KEEPALIVED_ID}" ]; then if [ -f /etc/sysctl.d/01-one.conf ]; then diff --git a/src/etc/one-context.d/loc-15-keepalived##apk b/src/etc/one-context.d/loc-15-keepalived##apk index d75b92e..c472623 100755 --- a/src/etc/one-context.d/loc-15-keepalived##apk +++ b/src/etc/one-context.d/loc-15-keepalived##apk @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-16-gen-env b/src/etc/one-context.d/loc-16-gen-env index 52ca43c..1756154 100755 --- a/src/etc/one-context.d/loc-16-gen-env +++ b/src/etc/one-context.d/loc-16-gen-env @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ENV_FILE=/var/run/one-context/one_env MOUNT_DIR=${MOUNT_DIR:-/mnt} diff --git a/src/etc/one-context.d/loc-20-set-username-password b/src/etc/one-context.d/loc-20-set-username-password index 45e8702..414606f 100755 --- a/src/etc/one-context.d/loc-20-set-username-password +++ b/src/etc/one-context.d/loc-20-set-username-password @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-20-set-username-password##bsd b/src/etc/one-context.d/loc-20-set-username-password##bsd new file mode 100755 index 0000000..9e063a4 --- /dev/null +++ b/src/etc/one-context.d/loc-20-set-username-password##bsd @@ -0,0 +1,66 @@ +#!/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}" </dev/null 2>&1; then + pw usermod -p '*' "${USERNAME}" + fi +fi diff --git a/src/etc/one-context.d/loc-22-ssh_public_key b/src/etc/one-context.d/loc-22-ssh_public_key index 6fb7640..0b9fefa 100755 --- a/src/etc/one-context.d/loc-22-ssh_public_key +++ b/src/etc/one-context.d/loc-22-ssh_public_key @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/loc-24-ssh_host_key##deb b/src/etc/one-context.d/loc-24-ssh_host_key##deb index 8bf283a..a64f4bc 100755 --- a/src/etc/one-context.d/loc-24-ssh_host_key##deb +++ b/src/etc/one-context.d/loc-24-ssh_host_key##deb @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then DEBIAN_FRONTEND=noninteractive dpkg-reconfigure openssh-server diff --git a/src/etc/one-context.d/loc-30-console##one b/src/etc/one-context.d/loc-30-console##one index 92b83cb..9d204bd 100755 --- a/src/etc/one-context.d/loc-30-console##one +++ b/src/etc/one-context.d/loc-30-console##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash for DEV_TTY in /dev/tty[0-9]*; do TERM=linux setterm -blank 0 -powerdown 0 >>"${DEV_TTY}" diff --git a/src/etc/one-context.d/net-11-fix-loopback##one b/src/etc/one-context.d/net-11-fix-loopback##one index 4444ff6..c53a974 100755 --- a/src/etc/one-context.d/net-11-fix-loopback##one +++ b/src/etc/one-context.d/net-11-fix-loopback##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2016, OpenNebula Systems # diff --git a/src/etc/one-context.d/net-15-hostname b/src/etc/one-context.d/net-15-hostname index d70325b..9b66e03 100755 --- a/src/etc/one-context.d/net-15-hostname +++ b/src/etc/one-context.d/net-15-hostname @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash function set_hostname() { hostname=$1 diff --git a/src/etc/one-context.d/net-15-hostname##bsd b/src/etc/one-context.d/net-15-hostname##bsd new file mode 100755 index 0000000..fa2c6bf --- /dev/null +++ b/src/etc/one-context.d/net-15-hostname##bsd @@ -0,0 +1,133 @@ +#!/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 diff --git a/src/etc/one-context.d/net-97-start-script b/src/etc/one-context.d/net-97-start-script index 3dbd246..c64e9e1 100755 --- a/src/etc/one-context.d/net-97-start-script +++ b/src/etc/one-context.d/net-97-start-script @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash MOUNT_DIR=${MOUNT_DIR:-/mnt} TMP_DIR=$(mktemp -d "/tmp/one-context.XXXXXX") diff --git a/src/etc/one-context.d/net-98-execute-scripts b/src/etc/one-context.d/net-98-execute-scripts index 62da86e..8e937ea 100755 --- a/src/etc/one-context.d/net-98-execute-scripts +++ b/src/etc/one-context.d/net-98-execute-scripts @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash MOUNT_DIR=${MOUNT_DIR:-/mnt} TMP_DIR=$(mktemp -d "/tmp/one-context.XXXXXX") diff --git a/src/etc/one-context.d/net-99-report-ready b/src/etc/one-context.d/net-99-report-ready index 6f18a1b..592f2b2 100755 --- a/src/etc/one-context.d/net-99-report-ready +++ b/src/etc/one-context.d/net-99-report-ready @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ENV_FILE=${ENV_FILE:-/var/run/one-context/one_env} diff --git a/src/etc/rc.d/one-context##bsd_rc.one b/src/etc/rc.d/one-context##bsd_rc.one new file mode 100755 index 0000000..8976417 --- /dev/null +++ b/src/etc/rc.d/one-context##bsd_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: LOGIN netif +# PROVIDE: one-context +### 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" +desc="OpenNebula's context scripts" +rcvar="${name}_enable" + +command="/usr/sbin/one-contextd" +command_args="network" + +load_rc_config $name +: ${one_context_enable:="yes"} +run_rc_command "$1" diff --git a/src/etc/rc.d/one-context-force##bsd_rc.one b/src/etc/rc.d/one-context-force##bsd_rc.one new file mode 100755 index 0000000..fd37f7d --- /dev/null +++ b/src/etc/rc.d/one-context-force##bsd_rc.one @@ -0,0 +1,38 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-force +# Short-Description: OpenNebula forced reconfiguration +### 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" +desc="OpenNebula forced reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" +command_args="force" + +load_rc_config $name +: ${one_context_force_enable:="yes"} +run_rc_command "$1" + diff --git a/src/etc/rc.d/one-context-local##bsd_rc.one b/src/etc/rc.d/one-context-local##bsd_rc.one new file mode 100755 index 0000000..a702ac5 --- /dev/null +++ b/src/etc/rc.d/one-context-local##bsd_rc.one @@ -0,0 +1,37 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: FILESYSTEMS +# PROVIDE: one-context-local +### 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" +desc="OpenNebula pre-networking contextualization" +rcvar="${name}_enable" + +command="/usr/sbin/one-contextd" +command_args="local" + +load_rc_config $name +: ${one_context_local_enable:="yes"} +run_rc_command "$1" + diff --git a/src/etc/rc.d/one-context-reconfigure-delayed##bsd_rc.one b/src/etc/rc.d/one-context-reconfigure-delayed##bsd_rc.one new file mode 100755 index 0000000..2a4be52 --- /dev/null +++ b/src/etc/rc.d/one-context-reconfigure-delayed##bsd_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-reconfigure-delayed +### 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" +desc="OpenNebula delayed reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" + +load_rc_config $name +: ${one_context_reconfigure_delayed_enable:="yes"} +run_rc_command "$1" + diff --git a/src/etc/rc.d/one-contextre-configure##bsd_rc.one b/src/etc/rc.d/one-contextre-configure##bsd_rc.one new file mode 100755 index 0000000..74df7f1 --- /dev/null +++ b/src/etc/rc.d/one-contextre-configure##bsd_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-reconfigure +### END INIT INFO + +. /etc/rc.subr + +name="one_context_reconfigure" +desc="OpenNebula reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" + +load_rc_config $name +: ${one_context_reconfigure_enable:="yes"} +run_rc_command "$1" + + diff --git a/src/usr/bin/onegate b/src/usr/bin/onegate index b50d1f8..bd1f472 100755 --- a/src/usr/bin/onegate +++ b/src/usr/bin/onegate @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ENV_FILE=/var/run/one-context/one_env diff --git a/src/usr/local/etc/rc.d/one-context-force.sh##pfsense_rc.one b/src/usr/local/etc/rc.d/one-context-force.sh##pfsense_rc.one new file mode 100755 index 0000000..fd37f7d --- /dev/null +++ b/src/usr/local/etc/rc.d/one-context-force.sh##pfsense_rc.one @@ -0,0 +1,38 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-force +# Short-Description: OpenNebula forced reconfiguration +### 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" +desc="OpenNebula forced reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" +command_args="force" + +load_rc_config $name +: ${one_context_force_enable:="yes"} +run_rc_command "$1" + diff --git a/src/usr/local/etc/rc.d/one-context-local.sh##pfsense_rc.one b/src/usr/local/etc/rc.d/one-context-local.sh##pfsense_rc.one new file mode 100755 index 0000000..a702ac5 --- /dev/null +++ b/src/usr/local/etc/rc.d/one-context-local.sh##pfsense_rc.one @@ -0,0 +1,37 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: FILESYSTEMS +# PROVIDE: one-context-local +### 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" +desc="OpenNebula pre-networking contextualization" +rcvar="${name}_enable" + +command="/usr/sbin/one-contextd" +command_args="local" + +load_rc_config $name +: ${one_context_local_enable:="yes"} +run_rc_command "$1" + diff --git a/src/usr/local/etc/rc.d/one-context-reconfigure-delayed.sh##pfsense_rc.one b/src/usr/local/etc/rc.d/one-context-reconfigure-delayed.sh##pfsense_rc.one new file mode 100755 index 0000000..2a4be52 --- /dev/null +++ b/src/usr/local/etc/rc.d/one-context-reconfigure-delayed.sh##pfsense_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-reconfigure-delayed +### 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" +desc="OpenNebula delayed reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" + +load_rc_config $name +: ${one_context_reconfigure_delayed_enable:="yes"} +run_rc_command "$1" + diff --git a/src/usr/local/etc/rc.d/one-context.sh##pfsense_rc.one b/src/usr/local/etc/rc.d/one-context.sh##pfsense_rc.one new file mode 100755 index 0000000..8976417 --- /dev/null +++ b/src/usr/local/etc/rc.d/one-context.sh##pfsense_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: LOGIN netif +# PROVIDE: one-context +### 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" +desc="OpenNebula's context scripts" +rcvar="${name}_enable" + +command="/usr/sbin/one-contextd" +command_args="network" + +load_rc_config $name +: ${one_context_enable:="yes"} +run_rc_command "$1" diff --git a/src/usr/local/etc/rc.d/one-contextre-configure.sh##pfsense_rc.one b/src/usr/local/etc/rc.d/one-contextre-configure.sh##pfsense_rc.one new file mode 100755 index 0000000..74df7f1 --- /dev/null +++ b/src/usr/local/etc/rc.d/one-contextre-configure.sh##pfsense_rc.one @@ -0,0 +1,36 @@ +#!/bin/sh + +# -------------------------------------------------------------------------- # +# Copyright 2010-2017, 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. # +#--------------------------------------------------------------------------- # + +### BEGIN INIT INFO +# REQUIRE: one-context-local one-context +# PROVIDE: one-context-reconfigure +### END INIT INFO + +. /etc/rc.subr + +name="one_context_reconfigure" +desc="OpenNebula reconfiguration" +rcvar="${name}_enable" + +command="/usr/sbin/one-context-run" + +load_rc_config $name +: ${one_context_reconfigure_enable:="yes"} +run_rc_command "$1" + + diff --git a/src/usr/sbin/one-context-run##one b/src/usr/sbin/one-context-run##one index 783a199..5dee834 100755 --- a/src/usr/sbin/one-context-run##one +++ b/src/usr/sbin/one-context-run##one @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2017, OpenNebula Systems # diff --git a/src/usr/sbin/one-contextd b/src/usr/sbin/one-contextd index 71921a0..495f2c3 100755 --- a/src/usr/sbin/one-contextd +++ b/src/usr/sbin/one-contextd @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # -------------------------------------------------------------------------- # # Copyright 2010-2017, OpenNebula Systems # @@ -145,7 +145,12 @@ function vmware_context { } function get_new_context { - local dev_context=$(blkid -l -t LABEL="CONTEXT" -o device) + if [[ "$distro" == "Linux" ]]; then + local dev_context=$(blkid -l -t LABEL="CONTEXT" -o device) + elif [[ "$distro" == "BSD" ]]; then + local dev_context="/dev/"$(glabel status | grep CONTEXT | awk '{print $3}') + fi + if [ -e "${dev_context}" ]; then mount_dir if ! [ -d "${MOUNT_DIR}" ]; then @@ -154,7 +159,12 @@ function get_new_context { fi log debug "Mounting CD-ROM ${dev_context} on ${MOUNT_DIR}" - mount -t iso9660 -o ro ${dev_context} ${MOUNT_DIR} 2>/dev/null + if [[ "$distro" == "Linux" ]]; then + mount -t iso9660 -o ro ${dev_context} ${MOUNT_DIR} 2>/dev/null + elif [[ "$distro" == "BSD" ]]; then + mount_cd9660 ${dev_context} ${MOUNT_DIR} 2>/dev/null + fi + if [ "$?" != '0' ]; then log err "Error: Failed to mount ${dev_context}" 2 exit 1 @@ -259,7 +269,12 @@ function cleanup { # unmount context if [ -d "${MOUNT_DIR}" ]; then log debug "Unmounting ${MOUNT_DIR}" - umount -l "${MOUNT_DIR}" + if [[ "$distro" == "Linux" ]]; then + umount -l "${MOUNT_DIR}" + elif [[ "$distro" == "BSD" ]]; then + umount "${MOUNT_DIR}" + fi + rm -r "${MOUNT_DIR}" fi @@ -280,6 +295,13 @@ if ! [[ ${TYPE} =~ ^(local|network|all)$ ]]; then exit 1 fi +unamestr=`uname` +if [[ "$unamestr" == *"BSD"* ]]; then + distro="BSD" +else + distro="Linux" +fi + log info "Started ${TYPE:+for type $TYPE} ${COMMAND:+to $COMMAND}" acquire_lock get_new_context diff --git a/targets.sh b/targets.sh index 6416f16..9f0600f 100644 --- a/targets.sh +++ b/targets.sh @@ -1,4 +1,38 @@ case "${TARGET}" in + 'pfsense') + NAME=${NAME:-one-context} + RELSUFFIX=${RELSUFFIX:-} + TYPE=${TYPE:-freebsd} + EXT=${EXT:-pkg} + TAGS=${TAGS:-bsd pfsense_rc one sysv} + DEPENDS=${DEPENDS:-sudo bash curl base64} + PROVIDES=${PROVIDES:-} + REPLACES=${REPLACES:-addon-context} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-pkg/postinstall.freebsd} + PREUN=${PREUN:-} + POSTUN=${POSTUN:-} + POSTUP=${POSTUP:-} + ;; + + + 'freebsd') + NAME=${NAME:-one-context} + RELSUFFIX=${RELSUFFIX:-} + TYPE=${TYPE:-freebsd} + EXT=${EXT:-pkg} + TAGS=${TAGS:-bsd bsd_rc one sysv} + DEPENDS=${DEPENDS:-sudo bash curl base64} + PROVIDES=${PROVIDES:-} + REPLACES=${REPLACES:-addon-context} + CONFLICTS=${CONFLICTS:-${REPLACES} one-context-ec2} + POSTIN=${POSTINST:-pkg/postinstall.freebsd} + PREUN=${PREUN:-} + POSTUN=${POSTUN:-} + POSTUP=${POSTUP:-} + ;; + + 'el6') NAME=${NAME:-one-context} RELSUFFIX=${RELSUFFIX:-.el6}