[oe-commits] org.oe.dev sysconf: A general utility for storing network configuration and CONFFILES in a flash partition

rwhitby commit openembedded-commits at lists.openembedded.org
Tue Mar 27 08:34:54 UTC 2007


sysconf: A general utility for storing network configuration and CONFFILES in a flash partition

Author: rwhitby at nslu2-linux.org
Branch: org.openembedded.dev
Revision: ddfdf5465cba5ef37981c5ee035af2d3f40b248b
ViewMTN: http://monotone.openembedded.org/revision.psp?id=ddfdf5465cba5ef37981c5ee035af2d3f40b248b
Files:
1
packages/sysconf
packages/sysconf/files
packages/sysconf/files/conffiles
packages/sysconf/files/sysconf
packages/sysconf/files/sysconfsetup
packages/sysconf/sysconf_0.1.bb
Diffs:

#
# mt diff -rd7246509b8205fe055e0aaeb26db2a49e7069bb7 -rddfdf5465cba5ef37981c5ee035af2d3f40b248b
#
# 
# 
# add_dir "packages/sysconf"
# 
# add_dir "packages/sysconf/files"
# 
# add_file "packages/sysconf/files/conffiles"
#  content [0527a6aca4e6b951237620432e7dc9d5e6ef9e64]
# 
# add_file "packages/sysconf/files/sysconf"
#  content [dee6b68f404efd4d0b6cbe4970d25a7cb05db7c6]
# 
# add_file "packages/sysconf/files/sysconfsetup"
#  content [ddc88d8e4348aff222f3bb6e541cd1690f4fbf18]
# 
# add_file "packages/sysconf/sysconf_0.1.bb"
#  content [c2f6cbb90c5cfda98bd909014b0e7a6fa158295b]
# 
============================================================
--- packages/sysconf/files/conffiles	0527a6aca4e6b951237620432e7dc9d5e6ef9e64
+++ packages/sysconf/files/conffiles	0527a6aca4e6b951237620432e7dc9d5e6ef9e64
@@ -0,0 +1,56 @@
+# conffiles
+# Known configuration files.  These files are preserved on
+# a flash upgrade.  Other configuration files, found from:
+#
+#    /usr/lib/ipkg/*.conffiles
+#    /etc/*.conf
+#
+# are preserved too with an operation of 'diff' if they have been
+# changed since /etc/.configured was created.
+#
+# Lines starting with # are comments, other lines have
+# two fields:
+#
+# operation file 
+#
+# The file must *NOT* have a leading /
+#
+# operation may be:
+#   ignore    Do not preserve this file
+#   preserve  Preserve this file unconditionally
+#   diff      Compare file with the new version, ask if different
+#
+preserve	linuxrc
+preserve	etc/.configured
+preserve	etc/TZ
+diff		etc/default/conffiles
+diff		etc/default/devpts
+preserve	etc/default/rcS
+preserve	etc/default/sysconf
+diff		etc/default/usbd
+preserve	etc/defaultdomain
+preserve	etc/dropbear/dropbear_dss_host_key
+preserve	etc/dropbear/dropbear_rsa_host_key
+preserve	etc/ssh/ssh_host_dsa_key
+preserve	etc/ssh/ssh_host_dsa_key.pub
+preserve	etc/ssh/ssh_host_rsa_key
+preserve	etc/ssh/ssh_host_rsa_key.pub
+preserve	etc/fstab
+preserve	etc/group
+preserve	etc/gshadow
+preserve	etc/hostname
+preserve	etc/hosts
+preserve	etc/localtime
+ignore		etc/modules
+ignore		etc/modules.conf
+preserve	etc/motd
+preserve	etc/network/interfaces
+preserve	etc/ntp.drift
+preserve	etc/passwd
+preserve	etc/profile
+preserve	etc/resolv.conf
+preserve	etc/shadow
+preserve	etc/syslog.conf
+preserve	etc/timezone
+preserve	root/.ssh/authorized_keys
+preserve	home/root/.ssh/authorized_keys
============================================================
--- packages/sysconf/files/sysconf	dee6b68f404efd4d0b6cbe4970d25a7cb05db7c6
+++ packages/sysconf/files/sysconf	dee6b68f404efd4d0b6cbe4970d25a7cb05db7c6
@@ -0,0 +1,1041 @@
+#!/bin/sh
+# sysconf
+#
+# Utility to manipulate system configuration information held
+# in a System Configuration partition
+#
+
+# hardware
+#  the 'Hardware' string from cpuinfo
+hardware(){
+	sed -n 's!^Hardware	*: !!p' /proc/cpuinfo
+}
+#
+# machine
+#  outputs an identifier of the current machine - i.e. the board
+#  slugos is running on.
+machine(){
+	case "$(hardware)" in
+	*Coyote*)	echo coyote;;
+	*IXDPG425*)	echo ixdpg425;;
+	*WRV54G*)	echo wrv54g;;
+	*IXDP425*)	echo ixdp425;;
+	*IXDP465*)	echo ixdp465;;
+	*IXCDP1100*)	echo ixcdp1100*;;
+	*Avila*)	echo avila;;
+	*Loft*)		echo loft;;
+	*NAS?100d*)	echo nas100d;;
+	*DSM?G600*)	echo dsmg600;;
+	*NSLU2*)	echo nslu2;;
+	*FSG?3*)	echo fsg3;;
+	*)		echo unknown;;
+	esac
+}
+
+# NSLU2 flash layout is non-standard.
+case "$(machine)" in
+nslu2)
+	kpart="Kernel"
+	syspart="SysConf"
+	ffspart="Flashdisk";;
+*)
+	kpart="kernel"
+	syspart="sysconfig"
+	ffspart="filesystem";;
+esac
+
+#
+# mtblockdev "name"
+#  return (output) the block device name for flash parition "name"
+#  /proc/mtd has the general form:
+#    dev:    size   erasesize  name
+#    mtd5: 00020000 00020000 "FIS directory"
+#  use this rather than hard-wiring the device because the partition
+#  table can change - looking in /proc/mtd is more reliable.
+mtblockdev(){
+	sed -n 's!^mtd\([0-9][0-9]*\):[^"]*"'"$1"'"$!/dev/mtdblock\1!p' /proc/mtd
+}
+
+#
+# sysconf_valid
+#  return true if the SysConf partition exists and seems to be
+#  potentially valid (it starts with a reasonable length).
+sysconf_valid(){
+	local sysdev
+	sysdev="$(mtblockdev $syspart)"
+	test -n "$sysdev" -a -b "$sysdev" &&
+		devio "<<$sysdev" '!! b.10>s32768<&!'
+}
+
+#
+# sysvalmatch "section" "name" 'pattern' "configuration file"
+# sysvalof "section" "name" "configuration file"
+# sysval "section" "name"
+#  outputs the value of the SysConf variable 'name' from section 'section',
+#  if there are multiple definitions only the last is output
+# NOTE: these functions should only be used internally, add entries to 'config'
+#  below if necessary.  This is because 'config' does the defaulting.
+sysvalmatch(){
+	sed -n '/^\['"$1"'\]$/,/^\[.*\]$/s/^'"$2"'=\('"$3"'\)$/\1/p' "$4" | sed -n '$p'
+}
+sysvalof(){
+	sysvalmatch "$1" "$2" '.*' "$3"
+}
+sysval(){
+	test -r "$config_root/etc/default/sysconf" &&
+		sysvalof "$1" "$2" "$config_root/etc/default/sysconf"
+}
+#
+# syssection "section"
+#  outputs all the values from the given section changed to the format "name value"
+#  (i.e. the '=' is dropped).
+syssection(){
+	test -r "$config_root/etc/default/sysconf" &&
+		sed -n '/^\['"$1"'\]$/,/^\[.*\]$/s/^\([^=]*\)=\(.*\)$/\1 \2/p' "$config_root/etc/default/sysconf"
+}
+#
+# config "value"
+#  convenience callers for specific values to avoid mis-typing in scripts
+#  NOTE: this function does the defaulting, 'sysval' does not!
+# config_root: if set this will override the root where config/sysval
+#              looks for /etc/default/sysconf
+config(){
+	case "$1" in
+	host)	if test -n "$(sysval network disk_server_name)"
+		then
+			sysval network disk_server_name
+		elif test -n "$(sysval network default_server_name)"
+		then
+			sysval network default_server_name
+		else
+			# because we want the name to remain constant:
+			echo "brokenslug"
+		fi;;
+	domain)	sysval network w_d_name;;
+	iface)  if test -n "$(sysval network lan_interface)"
+		then
+			sysval network lan_interface
+		else
+			echo eth0
+		fi;;
+	ip)	if test -n "$(sysval network ip_addr)"
+		then
+			sysval network ip_addr
+		else
+			echo 192.168.1.77
+		fi;;
+	netmask)sysval network netmask;;
+	gateway)sysval network gateway;;
+	dns)	sysval network dns_server1;;
+	dns2)	sysval network dns_server2;;
+	dns3)	sysval network dns_server3;;
+	boot)	if test -n "$(sysval network bootproto)"
+		then
+			sysval network bootproto
+		else
+			echo dhcp
+		fi;;
+	valid)	test -r "$config_root/etc/default/sysconf";;
+	*)	return 1;;
+	esac
+}
+
+#
+# sysconf_read [prefix]
+#  read the $syspart partition (if present) writing the result into
+#  /etc/default/sysconf, if the result is empty it will be removed.
+sysconf_read(){
+	local sysdev sedcmd config_root
+	config_root="$1"
+	rm -f /tmp/sysconf.new
+	sysdev="$(mtblockdev $syspart)"
+	if sysconf_valid
+	then
+		# Read the defined part of $syspart into /etc/default/sysconf.
+		# $syspart has lines of two forms:
+		#
+		#  [section]
+		#  name=value
+		#
+		# In practice $syspart also contains other stuff, use the command:
+		#
+		#  devio '<</dev/mtd1;cpb'
+		#
+		# to examine the current settings.  The badly formatted stuff
+		# is removed (to be exact, the sed script selects only lines
+		# which match one of the two above).  The lan interface, which
+		# on NSLU2 defaults to ixp0, is changed to the correct value for
+		# slugos, eth0.  The bootproto, which LinkSys sets to static in
+		# manufacturing, is reset to dhcp if the IP is still the
+		# original (192.168.1.77)
+		sedcmd='/^\[[^][]*\]$/p;'
+		# only do the ip_addr and lan_interface fixups on NSLU2
+		if test "$(machine)" = nslu2
+		then
+			sedcmd="$sedcmd"'
+				s/^lan_interface=ixp0$/lan_interface=eth0/;
+				/^ip_addr=192\.168\.1\.77$/,/^bootproto/s/^bootproto=static$/bootproto=dhcp/;'
+		fi
+		# and only print lines of the correct form
+		sedcmd="$sedcmd"'
+			/^[-a-zA-Z0-9_][-a-zA-Z0-9_]*=/p'
+
+		devio "<<$sysdev" cpb fb1,10 | sed -n "$sedcmd" >/tmp/sysconf.new
+	fi
+	#
+	# test the result - sysconf must be non-empty
+	if test -s /tmp/sysconf.new
+	then
+		mv /tmp/sysconf.new "$config_root/etc/default/sysconf"
+	else
+		rm -f /tmp/sysconf.new
+		return 1
+	fi
+}
+
+#
+# sysconf_default [prefix]
+#  Provde a default /etc/default/sysconf when there is no $syspart partition,
+#  or when it is invalid, this function will read from an existing sysconf,
+#  copying the values into the new one.
+# sysconf_line tag config-tag
+#  write an appropriate line if the config value is non-empty
+sysconf_line(){
+	config "$2" | {
+		local value
+		read value
+		test -n "$value" && echo "$1"="$value"
+	}
+}
+#
+sysconf_default(){
+	local config_root
+	config_root="$1"
+	{	echo '[network]'
+		sysconf_line disk_server_name host
+		sysconf_line w_d_name domain
+		sysconf_line lan_interface iface
+		sysconf_line ip_addr ip
+		sysconf_line netmask netmask
+		sysconf_line gateway gateway
+		sysconf_line dns_server1 dns
+		sysconf_line dns_server2 dns2
+		sysconf_line dns_server3 dns3
+		sysconf_line bootproto boot
+	} >/tmp/sysconf.new
+	mv /tmp/sysconf.new "$config_root/etc/default/sysconf"
+}
+
+#
+# sysconf_reload [prefix]
+#  read the values from /etc/default/sysconf and use these values to set
+#  up the following system files:
+#
+#   /etc/hostname
+#   /etc/defaultdomain
+#   /etc/resolv.conf
+#   /etc/network/interfaces
+#   /etc/motd
+#
+sysconf_reload(){
+	local config_root host domain iface boot ip netmask gateway ifname iftype
+	config_root="$1"
+	host="$(config host)"
+	test -n "$host" && echo "$host" >"$config_root/etc/hostname"
+	domain="$(config domain)"
+	test -n "$domain" && echo "$domain" >"$config_root/etc/defaultdomain"
+	#
+	# The DNS server information gives up to three nameservers,
+	# but this currently only binds in the first.
+	{
+		test -n "$domain" && echo "search $domain"
+		test -n "$(config dns)" && echo "nameserver $(config dns)"
+		test -n "$(config dns2)" && echo "nameserver $(config dns2)"
+		test -n "$(config dns3)" && echo "nameserver $(config dns3)"
+	} >"$config_root/etc/resolv.conf"
+	#
+	# Ethernet information.  This goes into /etc/network/interfaces,
+	# however this is only used for static setup (and this is not
+	# the default).  With dhcp the slugos udhcp script,
+	# /etc/udhcpc.d/50default, loads the values from sysconf.
+	iface="$(config iface)"
+	boot="$(config boot)"
+	# Only dhcp and static are supported at present - bootp
+	# support requires installation of appropriate packages
+	# dhcp is the fail-safe
+	case "$boot" in
+	dhcp|static) ;;
+	*) boot=dhcp;;
+	esac
+	#
+	ip="$(config ip)"
+	netmask="$(config netmask)"
+	gateway="$(config gateway)"
+	{
+		echo "# /etc/network/interfaces"
+		echo "# configuration file for ifup(8), ifdown(8)"
+		echo "#"
+		echo "# The loopback interface"
+		echo "auto lo"
+		echo "iface lo inet loopback"
+		echo "#"
+		echo "# The interface used by default during boot"
+		echo "auto $iface"
+		echo "# Automatically generated from /etc/default/sysconf"
+		echo "# address, netmask and gateway are ignored for 'dhcp'"
+		echo "# but required for 'static'"
+		echo "iface $iface inet $boot"
+		# The following are ignored for DHCP but are harmless
+		test -n "$ip"      && echo "	address $ip"
+		test -n "$netmask" && echo "	netmask $netmask"
+		test -n "$gateway" && echo "	gateway $gateway"
+		#
+		# Now read all the other ARPHRD_ETHER (type=1) interfaces
+		# and add an entry for each.
+		for ifname in $(test -d /sys/class/net && ls /sys/class/net)
+		do
+			if test -r "/sys/class/net/$ifname/type" -a "$ifname" != "$iface"
+			then
+				read iftype <"/sys/class/net/$ifname/type"
+				case "$iftype" in
+				1)	echo "#"
+					echo "# /sys/class/net/$ifname:"
+					echo "auto $ifname"
+					echo "iface $ifname inet dhcp";;
+				esac
+			fi
+		done
+	} >"$config_root/etc/network/interfaces"
+	#
+	# Finally rewrite /etc/motd
+	{	echo "Host name:           $host"
+		echo "Domain name:         $domain"
+		echo "Network boot method: $boot"
+		case "$boot" in
+		static) echo "Host IP address:     $ip";;
+		esac
+		echo "Use 'sysconf init' to reset the configuration"
+		echo "Use 'sysconf save' to save the configuration permanently"
+		echo "Use 'sysconf restore' to restore a previously saved configuration"
+		echo "Use 'sysconf help' for more information"
+	} >"$config_root/etc/motd"
+}
+
+#
+# sysconf_save_conffiles <flash-directory> <dest> <list>
+#  preserve the configuration files in a directory or in a CPIO archive
+#  (which is *not* compressed).  If <dest> is a directory the files are
+#  copied, otherwise a CPIO archive is made with that name.  <list> is
+#  the listing file giving the preserved files and the processing option.
+sysconf_save_conffiles(){
+	local ffsdir dest list file
+	ffsdir="$1"
+	saved="$2"
+	list="$3"
+	test -n "$ffsdir" -a -r "$ffsdir/etc/default/conffiles" -a -n "$saved" -a -n "$list" || {
+		echo "sysconf_save_conffiles: invalid arguments: '$*'" >&2
+		echo " usage sysconf_save_conffiles <flash-directory> <dest> <list>" >&2
+		return 1
+	}
+	#
+	(	cd "$ffsdir"
+		find etc/*.conf $(sed 's!^/!!' usr/lib/ipkg/info/*.conffiles) ! -type d -newer etc/.configured -print |
+			sed 's/^/diff /'
+		exec sed 's/#.*$//;/^[ 	]*$/d' etc/default/conffiles
+	) | sed 's!^/*!!' |
+	awk '{ op=$1; $1=""; file[$0]=op }
+		END{ for (f in file) if (file[f] != "ignore") print file[f] f }' |
+	while read op file
+	do
+		if test -e "$ffsdir/$file"
+		then
+			echo "$op $file" >&3
+			echo "$file"
+		fi
+	done 3>"$list" | (
+		cd "$ffsdir"
+		if test -d "$saved"
+		then
+			exec cpio -p -d -m -u "$saved"
+		else
+			exec cpio -o -H crc >"$saved"
+		fi
+	)
+}
+
+#
+# sysconf_verify file
+#  this is called with the name of a 'diff' file which is, indeed,
+#  different and with all the std streams connected to the tty.  It
+#  returns a status code to say whether (0) or not (1) to copy the
+#  file over.
+#
+# globals: the following must be defined in the calling context!
+#  saved:  the directory containing the unpacked saved files
+#  ffsdir: the flash directory to which the files are being restored (/)
+#
+sysconf_verify_help() {
+	echo "Please specify how to handle this file or link, the options are as follows,"
+	echo "two character abbreviations may be used:"
+	echo
+	echo " keep:    retain the old file, overwrite the new flash image file"
+	echo " upgrade: retain the new file, the old (saved) file is not used"
+	echo " diff:    display the differences between the old and the new using diff -u"
+	echo " shell:   temporarily start an interactive shell (sh -i), exit to continue"
+	echo " skip:    ignore this file for the moment.  The file is left in the directory"
+	echo "          $saved and many be handled after this script has completed"
+}
+#
+sysconf_verify() {
+	local command file
+
+	# return 1 here causes the file not to be overwritten,
+	# control should never get here!
+	test -n "$sysconf_noninteractive" && {
+		echo "$0: $*: changed file cannot be handled non-interactively" >&2
+		return 1
+	}
+
+	file="$1"
+	echo "$0: $file: configuration file changed."
+	sysconf_verify_help "$file"
+	while :
+	do
+		echo -n "option: "
+		read command
+		case "$command" in
+		ke*)	return 0;;
+		up*)	rm "$saved/$file"
+			return 1;;
+		di*)	echo "DIFF OLD($saved) NEW($ffsdir)"
+			diff -u "$saved/$file" "$ffsdir/$file";;
+		sh*)	PS1="$file: " sh -i;;
+		sk*)	return 1;;
+		*)	sysconf_verify_help "$file";;
+		esac
+	done
+}
+# the same, but for a link
+sysconf_verify_link() {
+	local command link
+
+	# return 1 here causes the file not to be overwritten,
+	# control should never get here!
+	test -n "$sysconf_noninteractive" && {
+		echo "$0: $*: changed link cannot be handled non-interactively" >&2
+		return 1
+	}
+
+	link="$1"
+	echo "reflash: $link: configuration link changed."
+	sysconf_verify_help "$link"
+	while :
+	do
+		echo -n "option: "
+		read command
+		case "$command" in
+		ke*)	return 0;;
+		up*)	rm "$saved/$link"
+			return 1;;
+		di*)	echo "DIFF:"
+			echo "OLD($saved): $link -> $(readlink "$saved/$link")"
+			echo "NEW($ffsdir): $link -> $(readlink "$ffsdir/$link")";;
+		sh*)	PS1="$link: " sh -i;;
+		sk*)	return 1;;
+		*)	sysconf_verify_help "$link";;
+		esac
+	done
+}
+
+#
+# sysconf_restore_conffiles <flash-directory> <source-dir> <restore>
+#  restore the configuration files from a directory.  'source-dir'
+#  If <source> is a directory of files from sysconf_save_conffiles.  The
+#  list of files r%s
>>> DIFF TRUNCATED @ 16K






More information about the Openembedded-commits mailing list