mirror of
https://github.com/samba-team/samba.git
synced 2025-08-26 01:49:31 +03:00
ctdb-scripts: Factor out possible creation of rt_tables file
Signed-off-by: Martin Schwenke <martin@meltin.net> Reviewed-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Jose A. Rivera <jarrpa@samba.org> Reviewed-by: Michael Adam <obnox@samba.org>
This commit is contained in:
committed by
Michael Adam
parent
677467682a
commit
c58d582ffb
@ -93,6 +93,19 @@ ipv4_host_addr_to_net ()
|
||||
|
||||
######################################################################
|
||||
|
||||
ensure_rt_tables ()
|
||||
{
|
||||
rt_tables="$CTDB_ETCDIR/iproute2/rt_tables"
|
||||
|
||||
# This file should always exist. Even if this didn't exist on the
|
||||
# system, adding a route will have created it. What if we startup
|
||||
# and immediately shutdown? Let's be sure.
|
||||
if [ ! -f "$rt_tables" ] ; then
|
||||
mkdir -p "${rt_tables%/*}" # dirname
|
||||
touch "$rt_tables"
|
||||
fi
|
||||
}
|
||||
|
||||
# Setup a table id to use for the given IP. We don't need to know it,
|
||||
# it just needs to exist in /etc/iproute2/rt_tables. Fail if no free
|
||||
# table id could be found in the configured range.
|
||||
@ -100,12 +113,7 @@ ensure_table_id_for_ip ()
|
||||
{
|
||||
_ip=$1
|
||||
|
||||
_f="$CTDB_ETCDIR/iproute2/rt_tables"
|
||||
# This file should always exist, but...
|
||||
if [ ! -f "$_f" ] ; then
|
||||
mkdir -p $(dirname "$_f")
|
||||
touch "$_f"
|
||||
fi
|
||||
ensure_rt_tables
|
||||
|
||||
# Maintain a table id for each IP address we've ever seen in
|
||||
# rt_tables. We use a "ctdb." prefix on the label.
|
||||
@ -117,7 +125,7 @@ ensure_table_id_for_ip ()
|
||||
(
|
||||
# Note that die() just gets us out of the subshell...
|
||||
flock --timeout 30 0 || \
|
||||
die "ensure_table_id_for_ip: failed to lock file $_f"
|
||||
die "ensure_table_id_for_ip: failed to lock file $rt_tables"
|
||||
|
||||
_new=$CTDB_PER_IP_ROUTING_TABLE_ID_LOW
|
||||
while read _t _l ; do
|
||||
@ -140,44 +148,38 @@ ensure_table_id_for_ip ()
|
||||
# If the new table id is legal then add it to the file and
|
||||
# print it.
|
||||
if [ $_new -le $CTDB_PER_IP_ROUTING_TABLE_ID_HIGH ] ; then
|
||||
printf "%d\t%s\n" "$_new" "$_label" >>"$_f"
|
||||
printf "%d\t%s\n" "$_new" "$_label" >>"$rt_tables"
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
) <"$_f"
|
||||
) <"$rt_tables"
|
||||
}
|
||||
|
||||
# Clean up all the table ids that we might own.
|
||||
clean_up_table_ids ()
|
||||
{
|
||||
_f="$CTDB_ETCDIR/iproute2/rt_tables"
|
||||
# Even if this didn't exist on the system, adding a route will
|
||||
# have created it. What if we startup and immediately shutdown?
|
||||
if [ ! -f "$_f" ] ; then
|
||||
mkdir -p $(dirname "$_f")
|
||||
touch "$_f"
|
||||
fi
|
||||
ensure_rt_tables
|
||||
|
||||
(
|
||||
# Note that die() just gets us out of the subshell...
|
||||
flock --timeout 30 0 || \
|
||||
die "clean_up_table_ids: failed to lock file $_f"
|
||||
die "clean_up_table_ids: failed to lock file $rt_tables"
|
||||
|
||||
# Delete any items from the file that have a table id in our
|
||||
# range or a label matching our label. Preserve comments.
|
||||
_tmp="${_f}.$$.ctdb"
|
||||
_tmp="${rt_tables}.$$.ctdb"
|
||||
awk -v min="$CTDB_PER_IP_ROUTING_TABLE_ID_LOW" \
|
||||
-v max="$CTDB_PER_IP_ROUTING_TABLE_ID_HIGH" \
|
||||
-v pre="$table_id_prefix" \
|
||||
'/^#/ || \
|
||||
!(min <= $1 && $1 <= max) && \
|
||||
!(index($2, pre) == 1) \
|
||||
{ print $0 }' "$_f" >"$_tmp"
|
||||
{ print $0 }' "$rt_tables" >"$_tmp"
|
||||
|
||||
mv "$_tmp" "$_f"
|
||||
mv "$_tmp" "$rt_tables"
|
||||
# The lock is gone - don't do anything else here
|
||||
) <"$_f"
|
||||
) <"$rt_tables"
|
||||
}
|
||||
|
||||
######################################################################
|
||||
|
Reference in New Issue
Block a user