netcf-transaction is a shell script that can take a snapshot of
network config files, and later either commit the changes made (by
deleting the snapshot) or revert back to the original config. If
uncommitted changes exist at boottime, netcf-transaction will
automatically revert them.
Available commands:
snapshot-config: save a copy of the current network interface config
commit-config: delete the saved snapshot
rollback-config: archive the current state of config into a rollback
directory, and copy the original snapshotted state
back into place
snapshot-dir: return the path to the snapshot directory.
start, restart, reload, force-reload, condrestart, and try-restart are
all synomyms for snapshot-config.
---
This script (which is based on Dan Kenigsberg's patch to
/etc/rc.d/init.d/network) will be used by the new APIs
ncf_change_begin(), ncf_change_commit(), and ncf_change_rollback(),
which will also be submitted sometime today.
netcf.spec.in | 1 +
src/Makefile.am | 28 ++++++++-
src/netcf-transaction.init.sh | 145 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 173 insertions(+), 1 deletions(-)
create mode 100755 src/netcf-transaction.init.sh
diff --git a/netcf.spec.in b/netcf.spec.in
index 04224fe..0da97a0 100644
--- a/netcf.spec.in
+++ b/netcf.spec.in
@@ -64,6 +64,7 @@ rm -rf $RPM_BUILD_ROOT
%defattr(-,root,root,-)
%{_datadir}/netcf
%{_libdir}/*.so.*
+%{_sysconfdir}/rc.d/init.d/netcf-transaction
%doc AUTHORS COPYING NEWS
%files devel
diff --git a/src/Makefile.am b/src/Makefile.am
index 9d8a3d7..2c79f25 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -14,7 +14,8 @@ bin_PROGRAMS = ncftool
noinst_PROGRAMS = ncftransform
EXTRA_DIST = netcf_public.syms \
- netcf_private.syms
+ netcf_private.syms \
+ netcf-transaction.init.sh
if NETCF_DRIVER_INITSCRIPTS
DRIVER_SOURCES = drv_initscripts.c dutil.h dutil.c \
@@ -57,4 +58,29 @@ internal.h: datadir.h
datadir.h: $(top_builddir)/config.status
echo '#define DATADIR "$(datadir)"' > datadir.h
+# This is for the initscript that handles network config change
+# transactions.
+install-data-local: install-init
+
+uninstall-local: uninstall-init
+
+install-init: netcf-transaction.init
+ mkdir -p $(DESTDIR)$(sysconfdir)/rc.d/init.d
+ $(INSTALL_SCRIPT) netcf-transaction.init \
+ $(DESTDIR)$(sysconfdir)/rc.d/init.d/netcf-transaction
+
+uninstall-init:
+ rm -f $(DESTDIR)$(sysconfdir)/rc.d/init.d/netcf-transaction \
+ $(DESTDIR)$(sysconfdir)/sysconfig/netcf-transaction
+
+BUILT_SOURCES += netcf-transaction.init
+
+netcf-transaction.init: netcf-transaction.init.sh $(top_builddir)/config.status
+ $(AM_V_GEN)sed \
+ -e 's!\@localstatedir\@!$(localstatedir)!g' \
+ -e 's!\@sysconfdir\@!$(sysconfdir)!g' \
+ < $< > $@-t && \
+ chmod a+x $@-t && \
+ mv $@-t $@
+
DISTCLEANFILES += $(BUILT_SOURCES)
diff --git a/src/netcf-transaction.init.sh b/src/netcf-transaction.init.sh
new file mode 100755
index 0000000..5750e27
--- /dev/null
+++ b/src/netcf-transaction.init.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+#
+# netcf-transaction: save/restore current network interface configuration
+#
+# chkconfig: - 09 91
+# description: Potentially rolls back network interface config to a \
+# known good state at boot time. Also includes utility \
+# commands to create this "known good state", remove it \
+# (ie, accept the current config as "known good"), and \
+# to manually rollback to the known good state, if it \
+# exists.
+
+### BEGIN INIT INFO
+# Provides: netcf-transaction
+# Required-Start: $local_fs
+# Short-Description: save/restore network configuration files
+# Description: This script can save the current state of network config,
+# and later revert to that config, or commit the new config
+# (by deleting the snapshot). At boottime, if there are
+# uncomitted changes to the network config, they are
+# reverted (and the discarded changes are archived in
+# /var/lib/netcf/network-rollback-*).
+#
+### END INIT INFO
+
+sysconfdir="@sysconfdir@"
+localstatedir="@localstatedir@"
+
+netconfdir="$sysconfdir"/sysconfig/network-scripts
+snapshotdir="$localstatedir"/lib/netcf/network-snapshot
+rollbackdirbase="$localstatedir"/lib/netcf/network-rollback
+
+# Source function library.
+test ! -r "$sysconfdir"/rc.d/init.d/functions ||
+ . "$sysconfdir"/rc.d/init.d/functions
+
+# take a snapshot of current network configuration scripts
+snapshot_config ()
+{
+ if test -e "$snapshotdir"
+ then
+ echo "snapshot directory $snapshotdir already exists"
+ return 1;
+ fi
+ mkdir -p "$snapshotdir"
+ for f in "$netconfdir"/{ifcfg,route,rule}-*
+ do
+ test -f "$f" && cp -p "$f" "$snapshotdir"
+ done
+}
+
+commit_config ()
+{
+ rm -rf "$snapshotdir"
+}
+
+# rollback network configuration to last snapshot (if one exists)
+rollback_config ()
+{
+ # return immediately if there is no snapshot
+ test ! -d "$snapshotdir" && return 0
+
+ # eliminate all but the last 20 rollback saves
+ ls -d "$rollbackdirbase"-* 2>/dev/null | head -n -20 |\
+ while read r; do rm -rf "$r"; done
+
+ # save a copy of the current config before rollback "just in case"
+ rollbackdir=$rollbackdirbase-`date +%Y.%m.%d-%H:%M:%S`
+ mkdir -p "$rollbackdir"
+ for f in "$netconfdir"/{ifcfg,route,rule}-*
+ do
+ test -f "$f" && cp -p "$f" "$rollbackdir"
+ done
+
+ # There are 4 classes of files, each handled slightly differently to minimize
+ # disruption in services:
+ # 1) file in both, unmodified - just erase the snapshot copy
+ # 2) file in both, modified - copy the snapshot version over the current &
erase copy
+ # 3) file in current only - remove the file
+ # 4) file in snapshot only - copy the snapshot file to current
+ #
+ # We handle the 1st three cases in one loop going through all current config
+ # files, and what is left over in snapshotdir is, by definition, case 4.
+ #
+ # (NB: we can't mv the files, because then the selinux labels
+ # don't get reset properly.)
+
+ for f in "$netconfdir"/{ifcfg,route,rule}-*
+ do
+ test ! -f "$f" && continue
+
+ rollbackf=$snapshotdir/`basename "$f"`
+ if test -f "$rollbackf"
+ then
+ # Case (1) & (2) (only copies if they're different)
+ ! diff "$rollbackf" "$f" >/dev/null 2>&1
&& cp -pf "$rollbackf" "$f"
+ rm -f "$rollbackf"
+ else
+ # Case (3)
+ rm -f "$f"
+ fi
+ done
+
+ # Case (4)
+ for f in "$snapshotdir"/{ifcfg,route,rule}-*
+ do
+ test ! -f "$f" && continue
+ cp -pf "$f" "$netconfdir"
+ done
+ rm -rf "$snapshotdir"
+}
+
+case "$1" in
+ # commands required in all Fedora initscripts
+ start|restart|reload|force-reload|condrestart|try-restart)
+ echo -n $"Running $prog $1: "
+ rollback_config
+ retval=$?
+ echo
+ ;;
+ stop|status)
+ ;;
+
+
+ # specific to netcf-transaction
+ snapshot-config)
+ snapshot_config
+ ;;
+ commit-config)
+ commit_config
+ ;;
+ rollback-config)
+ rollback_config
+ retval=$?
+ ;;
+ snapshot-dir)
+ echo $snapshotdir
+ retval=0
+ ;;
+ *)
+ echo $"Usage: $0
{start|snapshot-config|commit-config|rollback-config|snapshot-dir}"
+ exit 2
+esac
+
+exit $retval
--
1.7.3.4