Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/dynamic aliases #294

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/pot
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ usage() {
mount-in -- Mount directory, ZFS dataset, or fscomp into a pot
mount-out -- Unmount directory, ZFS dataset, or fscomp from a pot
add-dep -- Add a dependency
set-aliases -- Set alias hostname for the pot's addr in /etc/hosts
set-rss -- Set a resource constraint
get-rss -- Get the current resource usage
set-cmd -- Set the command to start the pot
Expand Down Expand Up @@ -190,7 +191,7 @@ case "${CMD}" in
create-base|create-fscomp|create|\
create-private-bridge|\
copy-in|copy-out|mount-in|mount-out|prune|set-hook|\
destroy|add-dep|set-rss|get-rss|set-cmd|set-env|set-hosts|\
destroy|add-dep|set-aliases|set-rss|get-rss|set-cmd|set-env|set-hosts|\
export|import|prepare|\
export-ports|set-attribute|get-attribute|\
start|stop|term|\
Expand Down
4 changes: 2 additions & 2 deletions share/pot/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
: "${ECHO:=echo}"
: "${SED:=sed}"

_POT_RW_ATTRIBUTES="start-at-boot early-start-at-boot persistent no-rc-script prunable localhost-tunnel no-tmpfs no-etc-hosts"
_POT_RW_ATTRIBUTES="start-at-boot early-start-at-boot persistent no-rc-script prunable localhost-tunnel no-tmpfs no-etc-hosts dynamic-etc-hosts"
_POT_RO_ATTRIBUTES="to-be-pruned"
_POT_NETWORK_TYPES="inherit alias public-bridge private-bridge"

# not devfs handles separately
_POT_JAIL_RW_ATTRIBUTES='enforce_statfs mount fdescfs linprocfs nullfs procfs tmpfs zfs raw_sockets sysvshm sysvsem sysvmsg children mlock devfs_ruleset exec_stop stop_timeout'

# N: arg name jail command, T: type of data, D: deafult value
# N: arg name jail command, T: type of data, D: default value
# devfs is always mounted
_POT_DEFAULT_mount_N='allow.mount'
_POT_DEFAULT_mount_T='bool'
Expand Down
3 changes: 3 additions & 0 deletions share/pot/prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ pot-prepare()
if ! pot-cmd set-attribute -A no-etc-hosts -V YES -p "$_new_pname" ; then
_error "Couldn't disable the enrichment of /etc/hosts - ignoring"
fi
if ! pot-cmd set-attribute -A dynamic-etc-hosts -V NO -p "$_new_pname" ; then
_error "Couldn't disable the enrichment of dynamic /etc/hosts - ignoring"
fi
if ! pot-cmd set-attribute -A no-tmpfs -V YES -p "$_new_pname" ; then
_error "Couldn't disable tmpfs for /tmp - ignoring"
fi
Expand Down
92 changes: 92 additions & 0 deletions share/pot/set-aliases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/sh
# shellcheck disable=SC3033,SC3040,SC3043
:

set-aliases-help() {
cat <<-"EOH"
pot set-aliases [-hv] -p pot -A alias_A[ alias_B alias_C ...]
-h print this help
-v verbose
-p pot : the working pot
-A alias_A[ alias_B alias_C ...] : adding alias for an alternative name resolution
via /etc/hosts file
EOH
}

# $1 pot
# $2 hostfile
_set_aliases()
{
local _pname _tmpfile _cfile
_pname="$1"
_tmpfile="$2"
_cfile=$POT_FS_ROOT/jails/$_pname/conf/pot.conf
${SED} -i '' -e "/^pot.aliases=.*/d" "$_cfile"
sed 's/.*/pot.aliases=&/g' "$_tmpfile" >> "$_cfile"
}


pot-set-aliases()
{
local _pname _tmpfile _aliases _alias
_pname=
if ! _is_pot_tmp_dir ; then
_error "Failed to create the POT_TMP directory"
return 1
fi
_tmpfile="$(mktemp "${POT_TMP:-/tmp}/pot-set-aliases${POT_MKTMP_SUFFIX}")" || exit 1
OPTIND=1
while getopts "hvp:A:" _o ; do
case "$_o" in
h)
set-aliases-help
rm -f "$_tmpfile"
return 0
;;
v)
_POT_VERBOSITY=$(( _POT_VERBOSITY + 1))
;;
A)
# validate IP address
_alias=$OPTARG
if [ -z "$_alias" ] ; then
_error "Submitted alias is empty"
set-aliases-help
rm -f "$_tmpfile"
return 1
fi
echo "alias=$_alias"
_aliases=$(echo "$_aliases $_alias" | sed -e "s/^ *//g")
echo "aliases $_aliases "
;;
p)
_pname="$OPTARG"
;;
?)
set-aliases-help
rm -f "$_tmpfile"
return 1
esac
done

if [ -z "$_pname" ]; then
_error "A pot name is mandatory"
set-aliases-help
rm -f "$_tmpfile"
return 1
fi
if ! _is_pot "$_pname" ; then
_error "pot $_pname is not valid"
set-aliases-help
rm -f "$_tmpfile"
return 1
fi
if ! _is_uid0 ; then
rm -f "$_tmpfile"
return 1
fi
cat $_tmpfile
echo "$_aliases " >> "$_tmpfile"
_set_aliases "$_pname" "$_tmpfile"
rm -f "$_tmpfile"
}
1 change: 1 addition & 0 deletions share/pot/set-attribute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pot-set-attribute()
"persistent"|\
"no-rc-script"|\
"no-etc-hosts"|\
"dynamic-etc-hosts"|\
"prunable"|\
"localhost-tunnel")
_cmd=_set_boolean_attribute
Expand Down
25 changes: 20 additions & 5 deletions share/pot/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,41 @@ _js_resolv()
# $1 pot name
_js_etc_hosts()
{
local _pname _phosts _hostname _bridge_name _cfile
local _pname _phosts _hostname _bridge_name _cfile _dynamics
_pname="$1"
_phosts="${POT_FS_ROOT}/jails/$_pname/m/etc/hosts"
_hostname="$( _get_conf_var "$_pname" host.hostname )"
_dynamics="$( _get_conf_var "$_pname" pot.attr.dynamic-etc-hosts )"
[ "$_dynamics" = "YES" ] && aopt="--aliases-included" || aopt=""
printf "::1 localhost %s\n" "$_hostname" > "$_phosts"
printf "127.0.0.1 localhost %s\n" "$_hostname" >> "$_phosts"
_cfile="${POT_FS_ROOT}/jails/$_pname/conf/pot.conf"
grep '^pot.hosts=' "$_cfile" | sed 's/^pot.hosts=//g' >> "$_phosts"
if [ "$(_get_conf_var "$_pname" "pot.attr.no-etc-hosts")" = "YES" ]; then
_debug "Attribute no-etchosts: no additional /etc/hosts entries injected"
else
case "$( _get_conf_var "$_pname" network_type )" in
"public-bridge")
potnet etc-hosts >> "$_phosts"
potnet etc-hosts $aopt >> "$_phosts"
;;
"private-bridge")
_bridge_name="$( _get_conf_var "$_pname" bridge )"
potnet etc-hosts -b "$_bridge_name" >> "$_phosts"
potnet etc-hosts -b "$_bridge_name" $aopt >> "$_phosts"
;;
esac
fi
_cfile="${POT_FS_ROOT}/jails/$_pname/conf/pot.conf"
grep '^pot.hosts=' "$_cfile" | sed 's/^pot.hosts=//g' >> "$_phosts"
}

_update_dynamic_hosts() {
local _pots _pot _dynamic
_pots=$( _get_pot_list )
for _pot in $_pots ; do
_dynamic="$( _get_conf_var "$_pot" pot.attr.dynamic-etc-hosts)"
if [ "$_dynamic" = "YES" ]; then
_debug "updating etc-hosts for ${_pot}"
_js_etc_hosts $_pot
fi
done
}

# returns interface names of epaira and epairb
Expand Down Expand Up @@ -864,5 +878,6 @@ pot-start()
_error "$_pname failed to start"
return 1
fi
_update_dynamic_hosts
return 0
}
3 changes: 3 additions & 0 deletions share/pot/stop.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ pot-stop()
${EXIT} 1
fi
_js_rm_resolv "$_pname"
. "${_POT_INCLUDE}/start.sh"
_update_dynamic_hosts
_pot_umount "$_pname"
lockf "${POT_TMP:-/tmp}/pot-lock-$_pname" "${_POT_PATHNAME}" set-status -p "$_pname" -s stopped
_set_pot_status "$_pname" stopped
rc=$?
if [ $rc -eq 2 ]; then
Expand Down
8 changes: 8 additions & 0 deletions share/pot/update-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,18 @@ _update_one_pot()
_debug "pot.attr.no-tmpfs=NO"
echo "pot.attr.no-tmpfs=NO" >> "$_conf"
fi
if [ -z "$(_get_conf_var "$_pname" "pot.attr.aliases")" ]; then
_debug "updating extra hosts in $_pname"
echo "pot.attr.aliases=NO" >> "$_conf"
fi
if [ -z "$(_get_conf_var "$_pname" "pot.attr.no-etc-hosts")" ]; then
_debug "pot.attr.no-etc-hosts=NO"
echo "pot.attr.no-etc-hosts=NO" >> "$_conf"
fi
if [ -z "$(_get_conf_var "$_pname" "pot.attr.dynamic-etc-hosts")" ]; then
_debug "pot.attr.dynamic-etc-hosts=NO"
echo "pot.attr.dynamic-etc-hosts=NO" >> "$_conf"
fi

# convert pot.export.static.ports=80 to the new format pot.export.ports=80:80
# being aware that pot.export.ports may already exist
Expand Down
Loading