From nobody@FreeBSD.org  Thu Jun  2 17:26:13 2011
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 69DFA106566C
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  2 Jun 2011 17:26:13 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from red.freebsd.org (red.freebsd.org [IPv6:2001:4f8:fff6::22])
	by mx1.freebsd.org (Postfix) with ESMTP id 58EA18FC12
	for <freebsd-gnats-submit@FreeBSD.org>; Thu,  2 Jun 2011 17:26:13 +0000 (UTC)
Received: from red.freebsd.org (localhost [127.0.0.1])
	by red.freebsd.org (8.14.4/8.14.4) with ESMTP id p52HQDHs055737
	for <freebsd-gnats-submit@FreeBSD.org>; Thu, 2 Jun 2011 17:26:13 GMT
	(envelope-from nobody@red.freebsd.org)
Received: (from nobody@localhost)
	by red.freebsd.org (8.14.4/8.14.4/Submit) id p52HQDIb055736;
	Thu, 2 Jun 2011 17:26:13 GMT
	(envelope-from nobody)
Message-Id: <201106021726.p52HQDIb055736@red.freebsd.org>
Date: Thu, 2 Jun 2011 17:26:13 GMT
From: Alex Bakhtin <Alex.Bakhtin@gmail.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [nanobsd][patch] save_cfg improvements
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         157533
>Category:       misc
>Synopsis:       [nanobsd][patch] save_cfg improvements
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    imp
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jun 02 17:30:12 UTC 2011
>Closed-Date:    Fri Mar 14 14:31:04 MDT 2014
>Last-Modified:  Fri Mar 14 14:31:04 MDT 2014
>Originator:     Alex Bakhtin
>Release:        
>Organization:
>Environment:
>Description:
I have written the improved nanobsd save_cfg script with the following features:

1. It recursivelly handles new (modified by user) files in /etc and ask user what to do.
2. It recursivelly handles file deletion from /etc and ask user what to do.
3. It keeps 'ignore' list for files that user decided to never add to /cfg

I'm pretty sure that this script is a little bit better than http://www.freebsd.org/cgi/query-pr.cgi?pr=misc/145962 because it doesn't require to remember - which files was modified. It scans the whole /etc, and if it found new file - it ask user - what to do with it.
User can:
a) Add file to /cfg.
b) Skip this file for current script run
c) Add this file to ignore list (/cfg/.ignore) and skip this file for all script runs.

One of the most annoying things I found in nanobsd administration is that user have to keep in mind ALL files he is going to add to /cfg. This script was used on about 10 nanobsd hosts for 1.5 years in production environment and is extremely helpfull. The chance that after adding some new configuration file to nanobsd user would forget to add it to /cfg was decreased drammatically.

Please consider adding this to Files.


>How-To-Repeat:

>Fix:


Patch attached with submission follows:

#!/bin/sh
#
# Copyright (c) 2006 Mathieu Arnold
# Copyright (c) 2010 Alex Bakhtin
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD: src/tools/tools/nanobsd/Files/root/save_cfg,v 1.1.4.1 2009/08/03 08:13:06 kensmith Exp $
#

set -e

trap "umount /cfg" 1 2 15 EXIT
mount /cfg
(
cd /etc
for filename in "$@" `find * -type f`
do
	if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
	then

		#
		# If file doesn't exist in /cfg and file is not in the 'ignore' list
		# then check if this file is exactly the same as original file
		# in nanobsd image
		#
		if ! cmp -s /etc/$filename /conf/base/etc/$filename 
		then
			file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
			if [ $file_path != $filename ]
			then
				if [ ! -d /etc/$file_path ]
				then
					# should never go here unless we have some errors in
					# sed script extracting file path
					echo "Error: Path /etc/$file_path is not directory."
					exit 1;
				fi
			fi

			#
			# Ask user - how should we handle this file.
			# Add to cfg (y/n/i)?
			#	y) -> save this file in /cfg
			#	n) -> do not save this file in /cfg for current script invocation ONLY
			#	i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
			#	      try to add this file to /cfg.
			#
			# touch is ised to add files to /cfg to keep the script flow straight and easy
			#
			read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
			case "$key" in
			[yY])
				if [ $file_path != $filename ]
				then
					mkdir -vp /cfg/$file_path
				fi
				touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
				;;
			[iI])
				mkdir -vp /cfg/.ignore
				if [ $file_path != $filename ]
				then
					mkdir -vp /cfg/.ignore/$file_path
				fi
				touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
				;;
			esac
		fi
	fi
done

#
# Actually check all files in /cfg and save if necessary
#
cd /cfg
for filename in "$@" `find * -type f`
do
	if [ -f /etc/$filename ]
	then
        	cmp -s /etc/$filename /cfg/$filename || cp -pfv /etc/$filename /cfg/$filename
	else

		#
		# Give user an option to remove file from /cfg if this file is removed from /etc
		#
		read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
		case "$key" in
		[yY])
			rm /cfg/$filename && echo "File /cfg/$filename removed"
			;;
		esac
	fi
done

)
umount /cfg
trap 1 2 15 EXIT


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->imp 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Sun Jun 5 05:41:56 UTC 2011 
Responsible-Changed-Why:  
Over to maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=157533 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: misc/157533: commit references a PR
Date: Fri, 14 Mar 2014 20:20:40 +0000 (UTC)

 Author: imp
 Date: Fri Mar 14 20:20:32 2014
 New Revision: 263189
 URL: http://svnweb.freebsd.org/changeset/base/263189
 
 Log:
   NanoBSD has a utility shell script called save_cfg which helps keep
   /cfg updated with the modified configuration files in /etc. I have
   written an improved version with the following features:
   
   * Recurses directories.
   * Only requires file arguments the first time the file/directory is
   * added to /cfg.
   * Handles file deletions.
   
   PR: 145962, 157533
   Submitted by: Aragon Gouveia and Alex Bakhtin
 
 Modified:
   head/tools/tools/nanobsd/Files/root/save_cfg
 
 Modified: head/tools/tools/nanobsd/Files/root/save_cfg
 ==============================================================================
 --- head/tools/tools/nanobsd/Files/root/save_cfg	Fri Mar 14 19:46:32 2014	(r263188)
 +++ head/tools/tools/nanobsd/Files/root/save_cfg	Fri Mar 14 20:20:32 2014	(r263189)
 @@ -1,6 +1,7 @@
  #!/bin/sh
  #
  # Copyright (c) 2006 Mathieu Arnold
 +# Copyright (c) 2010 Alex Bakhtin
  # All rights reserved.
  #
  # Redistribution and use in source and binary forms, with or without
 @@ -32,11 +33,86 @@ set -e
  trap "umount /cfg" 1 2 15 EXIT
  mount /cfg
  (
 +cd /etc
 +for filename in "$@" `find * -type f`
 +do
 +	if [ ! -f /cfg/$filename -a ! -f /cfg/.ignore/$filename ]
 +	then
 +
 +		#
 +		# If file doesn't exist in /cfg and file is not in the 'ignore' list
 +		# then check if this file is exactly the same as original file
 +		# in nanobsd image
 +		#
 +		if ! cmp -s /etc/$filename /conf/base/etc/$filename 
 +		then
 +			file_path=`echo "$filename" | sed 's/\/[^/]*$//'`
 +			if [ $file_path != $filename ]
 +			then
 +				if [ ! -d /etc/$file_path ]
 +				then
 +					# should never go here unless we have some errors in
 +					# sed script extracting file path
 +					echo "Error: Path /etc/$file_path is not directory."
 +					exit 1;
 +				fi
 +			fi
 +
 +			#
 +			# Ask user - how should we handle this file.
 +			# Add to cfg (y/n/i)?
 +			#	y) -> save this file in /cfg
 +			#	n) -> do not save this file in /cfg for current script invocation ONLY
 +			#	i) -> add file to ignore list (/cfg/.ignore hiereachy) and never save
 +			#	      try to add this file to /cfg.
 +			#
 +			# touch is ised to add files to /cfg to keep the script flow straight and easy
 +			#
 +			read -p "New file /etc/$filename found. Add to /cfg (y/n/i)? " key
 +			case "$key" in
 +			[yY])
 +				if [ $file_path != $filename ]
 +				then
 +					mkdir -vp /cfg/$file_path
 +				fi
 +				touch /cfg/$filename && echo "File /etc/$filename added to /cfg."
 +				;;
 +			[iI])
 +				mkdir -vp /cfg/.ignore
 +				if [ $file_path != $filename ]
 +				then
 +					mkdir -vp /cfg/.ignore/$file_path
 +				fi
 +				touch /cfg/.ignore/$filename && echo "File /etc/$filename added to ignore list."
 +				;;
 +			esac
 +		fi
 +	fi
 +done
 +
 +#
 +# Actually check all files in /cfg and save if necessary
 +#
  cd /cfg
 -for i in "$@" `find * -type f`
 +for filename in "$@" `find * -type f`
  do
 -        cmp -s /etc/$i /cfg/$i || cp -pfv /etc/$i /cfg/$i
 +	if [ -f /etc/$filename ]
 +	then
 +        	cmp -s /etc/$filename /cfg/$filename || cp -pfv /etc/$filename /cfg/$filename
 +	else
 +
 +		#
 +		# Give user an option to remove file from /cfg if this file is removed from /etc
 +		#
 +		read -p "File /cfg/$filename not found in /etc. Remove from /cfg (y/n)? " key
 +		case "$key" in
 +		[yY])
 +			rm /cfg/$filename && echo "File /cfg/$filename removed"
 +			;;
 +		esac
 +	fi
  done
 +
  )
  umount /cfg
  trap 1 2 15 EXIT
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->closed 
State-Changed-By: imp 
State-Changed-When: Fri Mar 14 14:30:54 MDT 2014 
State-Changed-Why:  
Now in tree. Thanks. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=157533 
>Unformatted:
