aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/slugos-init/files/sysconf
blob: 41aa7858842a5b5178c1b41680f1e1a41283cff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#!/bin/sh
# sysconf
#
# utility to manipulate system configuration information help
# in a RedBoot SysConf partition
#
# load the utility functions (unless this is being called just
# to load these functions!)
test "$1" != sysconf && . /etc/default/functions

case "$(machine)" in
nslu2)
	kpart="Kernel"
	syspart="SysConf"
	ffspart="Flashdisk";;
*)
	kpart="kernel"
	syspart="sysconfig"
	ffspart="filesystem";;
esac
#
# 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<&!'
}

#
# 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 mac 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
		# always fix up the hardware addr if it is present
		mac="$(config mac)"
		if test -n "$mac"
		then
			sedcmd="$sedcmd"'
				s/^hw_addr=.*$/hw_addr='"$mac"'/;'
		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 hw_addr mac
		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 is used by DHCP to register the hostname
		test -n "$host"    && echo "	hostname $host"
		# 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 "Host MAC:            $(config mac)"
		echo "Network boot method: $boot"
		case "$boot" in
		static) echo "Host IP address:     $ip";;
		esac
		echo "Use 'turnup init' to reset the configuration"
		echo "Use 'turnup preserve' to save the configuration permanently"
		echo "Use 'turnup restore' to restore a previously saved configuration"
		echo "Use 'turnup disk|nfs -i <device> options to initialise a non-flash root"
		echo "Use 'turnup 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/opkg/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 restored is written to the third argument (restore),
#  but is not required (/dev/null would be ok).
#
#  the list of files to restore is read from stdin, along with the
#  processing option for each file (the format is as produced by
#  sysconf_save_conffiles in the 'list' output).
sysconf_restore_conffiles(){
	local ffsdir saved restore
	# these are the globals used by the above function
	ffsdir="$1"
	saved="$2"
	restore="$3"
	test -n "$ffsdir" -a -r "$ffsdir/etc/default/conffiles" -a -d "$saved" -a -n "$restore" || {
		echo "restore_conffiles: invalid arguments: '$*'" >&2
		echo " usage sysconf_restore_conffiles <flash-directory> <source-dir> <list>" >&2
		return 1
	}
	#
	# read the list and process each given file
	while read op file
	do
		# handle .configured specially (to preserve the original datestamp)
		if test "$file" = "etc/.configured"
		then
			# this should definately not fail because of the test above!
			if cp -a "$saved/$file" "$ffsdir/$file"
			then
				echo "$file" >&3
			else
				echo "sysconf_restore_conffiles: $file: timestamp copy failed (ignored)" >&2
			fi
		elif test -h "$saved/$file" -o -h "$ffsdir/$file"
		then
			# new or old symbolic link
			if test -h "$saved/$file" -a -h "$ffsdir/$file" &&
				test "$(readlink "$saved/$file")" = "$(readlink "$ffsdir/$file")"
			then
				# no change
				echo "$file" >&3
			else
				# assume a change regardless
				case "$op" in
				preserve)
					echo "$file"
					echo "$file" >&3;;
				diff)	# need user input
					if sysconf_verify_link "$file" </dev/tty >/dev/tty 2>&1
					then
						echo "$file"
						echo "$file" >&3
					fi;;
				esac
			fi
		else
			# only overwrite if necessary
			if test -e "$ffsdir/$file" && cmp -s "$saved/$file" "$ffsdir/$file"
			then
				# do not overwrite
				echo "$file" >&3
			elif test ! -e "$ffsdir/$file"
			then
				# always preserve
				echo "$file"
				echo "$file" >&3
			else
				case "$op" in
				preserve)
					echo "$file"
					echo "$file" >&3;;
				diff)	# the files are different, get user input
					if sysconf_verify "$file" </dev/tty >/dev/tty 2>&1
					then
						echo "$file"
						echo "$file" >&3
					fi;;
				esac
			fi
		fi
	done 3>"$restore" | (cd "$saved"; exec cpio -p -d -u "$ffsdir")
}

#
# sysconf_test_restore <flash-directory> <source-dir>
#  return true only if the restore does not need to do an interactive
#  compare
sysconf_test_restore(){
	local ffsdir saved
	# these are the globals used by the above function
	ffsdir="$1"
	saved="$2"
	# this is an error case, but return 0 so that the error is
	# detected later
	test -n "$ffsdir" -a -r "$ffsdir/etc/default/conffiles" -a -d "$saved" ||
		return 0
	#
	# read the list and check each diff file (this is just a copy of the
	# logic above with all the work removed!)
	while read op file
	do
		# handle .configured specially (to preserve the original datestamp)
		if test "$op" != diff
		then
			: # no diff required
		elif test "$file" = "etc/.configured"
		then
			: # special handling
		elif test -h "$saved/$file" -o -h "$ffsdir/$file"
		then
			# new or old symbolic link
			if test -h "$saved/$file" -a -h "$ffsdir/$file" &&
				test "$(readlink "$saved/$file")" = "$(readlink "$ffsdir/$file")"
			then
				: # no change
			else
				# assume a change regardless
				return 1
			fi
		else
			# only overwrite if necessary
			if test -e "$ffsdir/$file" && cmp -s "$saved/$file" "$ffsdir/$file"
			then
				: # do not overwrite
			elif test ! -e "$ffsdir/$file"
			then
				: # always preserve
			else
				# a change
				return 1
			fi
		fi
	done

	return 0
}

#
# sysconf_save
#  save the system configuration to $syspart - $syspart must exist and
#  there must be a writeable device for it.
sysconf_save(){
	local sysdev ffsdev ffsdir saved list size status
	ffsdev="$(mtblockdev $ffspart)"
	[ -n "$ffsdev" ] || \
	ffsdev="$(mtblockdev rootfs)"
	sysdev="$(mtblockdev $syspart)"
	status=1
	if test -n "$sysdev" -a -b "$sysdev" -a -n "$ffsdev" -a -b "$ffsdev"
	then
		# this will succeed silently if the flash device is on /
		umountflash "$ffsdev" || exit 1
		#
		# Everything is umounted, now remount on a temporary directory.
		ffsdir="/tmp/flashdisk.$$"
		mkdir "$ffsdir" || {
			echo "$0: $ffsdir: failed to create temporary directory" >&2
			exit 1
		}
		#
		mountflash "$ffsdev" "$ffsdir" -o ro || {
			rmdir "$ffsdir"
			exit 1
		}
		# need temporary files for the cpio output and the listing
		saved=/tmp/cpio.$$
		list=/tmp/preserve.$$
		rm -rf "$saved" "$list"
		sysconf_save_conffiles "$ffsdir" "$saved" "$list" || {
			echo "$0: $saved: archive of saved configuration files failed" >&2
			rm -rf "$saved"
			rm "$list"
			umount "$ffsdir" && rmdir "$ffsdir" ||
				echo "$0: $ffsdir: temporary directory cleanup failed" >&2
			return 1
		}
		# ignore the error in this case:
		umount "$ffsdir" && rmdir "$ffsdir" ||
			echo "$0: $ffsdir: temporary directory cleanup failed" >&2
		#
		# we now have:
		#  /etc/default/sysconf	the basic config
		#  /tmp/preserve.$$	the list of saved files
		#  /tmp/cpio.$$		the CPIO archive of those files
		#
		# make one big file with the sysconf data followed by the
		# compressed archive in /tmp/sysconf.$$
		{	{	cat /etc/default/sysconf
				echo '[preserve]'
			} | sed -n '1,/^\[preserve\]^/p'
			while read op file
			do
				echo "$op"="$file"
			done <"$list"
		} >/tmp/sysconf.$$
		size="$(devio "<</tmp/sysconf.$$" 'pr$')"
		gzip -9 <"$saved" >>/tmp/sysconf.$$
		#
		# more cleanup, then try to write the new sysconf to $syspart
		# the format is a 4 byte big-endian length then the text data
		# if the data won't fit exit with error code 7
		rm "$saved" "$list"
		devio -p "<</tmp/sysconf.$$" ">>$sysdev" '
			$( $4+ # >
			!! 7
			$) 0
			wb '"$size"',4
			cp $'
		case $? in
		0)	echo " done" >&2
			status=0;;
		1)	echo " failed" >&2
			echo " $syspart could not be written (no changes made)" >&2;;
		3)	echo " failed" >&2
			echo " $syspart partially written, you may want to reset it" >&2;;
		7)	echo " failed" >&2
			echo " $syspart is too small: $size bytes required" >&2
			echo " No change made" >&2;;
		*)	echo " failed" >&2
			echo " Internal error writing $syspart" >&2;;
		esac
		#
		rm -f /tmp/sysconf.$$
	else
		echo "sysconf save: $syspart or $ffspart partition not found" >&2
		echo " A RedBoot partition named '$syspart' must exist in the system" >&2
		echo " flash memory for this command to work, and there must be a" >&2
		echo " block device to access this partition (udev will normally" >&2
		echo " create this automatically.  The flash partition contents must" >&2
		echo " also be accessible in a partition called '$ffspart'" >&2
		echo
		echo " To create the $syspart partition use the 'fis create' command" >&2
		echo " in the RedBoot boot loader, it is sufficient to make the" >&2
		echo " partition one erase block in size unless you have substantially" >&2
		echo " increased the size of the files listed in /etc/default/conffiles" >&2
	fi

	return $status
}

#
# sysconf_restore [auto]
#  restore previously saved configuration information from $syspart
sysconf_restore_error(){
	local root
	root="$1"
	shift
	#       -------------------------------------------------------------------------------
	{	echo "          WARNING: saved configuration files not restored"
		test -n "$1" && echo "$*"
		echo
		echo "The configuration of this machine has been reinitialised using the values"
		echo "from /etc/default/sysconf, however configuration files saved in the $syspart"
		echo "partition have not been restored."
		echo
		echo "You can restore these files by correcting any reported errors then running"
		echo
		echo "  sysconf restore"
		echo
		echo "from the command line.  This will completely reinitialise the configuration"
		echo "using the information in the $syspart partition."
	} >"$root/etc/motd"
	cat "$root/etc/motd" >&2
}
#
sysconf_restore(){
	local sysdev ffsdev ffsdir saved restore size status sysconf_noninteractive config_root

	# if set this means 'do no diff' - this avoids the code above which
	# would open /dev/tty and therefore allows this stuff to be done from
	# an init script
	sysconf_noninteractive=
	test "$1" = auto && sysconf_noninteractive=1

	ffsdev="$(mtblockdev $ffspart)"
	[ -n "$ffsdev" ] || \
	ffsdev="$(mtblockdev rootfs)"
	sysdev="$(mtblockdev $syspart)"
	status=1
	if test -n "$sysdev" -a -b "$sysdev" -a -n "$ffsdev" -a -b "$ffsdev" &&
		sysconf_valid
	then
		# this will succeed silently if the flash device is on /
		umountflash "$ffsdev" || exit 1
		#
		# Everything is umounted, now remount on a temporary directory.
		ffsdir="/tmp/flashdisk.$$"
		config_root="$ffsdir"
		mkdir "$ffsdir" || {
			echo "$0: $ffsdir: failed to create temporary directory" >&2
			exit 1
		}
		#
		mountflash "$ffsdev" "$ffsdir" || {
			rmdir "$ffsdir"
			exit 1
		}
		#
		# first restore the $syspart section
		sysconf_read "$ffsdir" || sysconf_default "$ffsdir"
		#
		# now use this to regenerate the system files
		sysconf_reload "$ffsdir"
		#
		# now examine the [preserve] section, if it is there restore
		# it if possible.
		if test -n "$(syssection preserve)"
		then
			# 'saved' is a directory, 'restore' is a file (which is
			# used to detect unrestored files).  The directory needs
			# to be populated with files.
			saved=/tmp/cpio.$$
			restore=/tmp/restore.$$
			rm -rf "$saved" "$restore"
			#
			mkdir "$saved" || {
				sysconf_restore_error "$ffsdir" "$saved: failed to create temporary directory"
				umount "$ffsdir" && rmdir "$ffsdir" ||
					echo "$0: $ffsdir: temporary directory cleanup failed" >&2
				return 1
			}
			#
			# the CPIO archive is gzip compressed after the text part
			# of sysconf, gzip will handle the LZ stream termination
			# correctly (and break the pipe) so we don't need to know
			# the real length of the data
			devio "<<$sysdev" '<=b4+.' 'cp $s-' | gunzip | (
				cd "$saved"
				exec cpio -i -d -m -u
			) || {
				rm -rf "$saved"
				sysconf_restore_error "$ffsdir" "$saved: cpio -i failed"
				umount "$ffsdir" && rmdir "$ffsdir" ||
					echo "$0: $ffsdir: temporary directory cleanup failed" >&2
				return 1
			}
			# either there must be no 'diff' files or it must
			# be possible to interact with a real user.
			if test -z "$sysconf_noninteractive" ||
				syssection preserve | sysconf_test_restore "$ffsdir" "$saved"
			then
				#
				# remove the 'init' motd from sysconf_reload
				rm "$ffsdir/etc/motd"
				#
				# now restore from the directory, using the information in
				# the preserve section, if this fails in a non-interactive
				# setting the system might not reboot
				syssection preserve |
					sysconf_restore_conffiles "$ffsdir" "$saved" "$restore" || {
					# there is a chance of the user cleaning this up
#------------------------------------------------------------------------------
					sysconf_restore_error "$ffsdir" \
"$0: $saved: restore of saved configuration files failed.
  The flash file system is mounted on $ffsdir.
  The saved files are in $saved and the list of files selected for
  restore is in $restore.
  You should restore any required configuration from $saved, then umount
  $ffsdir and reboot."
  						# this prevents cleanup/umount
						return 1
				}
				#
				# remove the copied files (i.e. the ones which were preserved)
				(	cd "$saved"
					exec rm $(cat "$restore")
				)
				rm "$restore"
				#
				# clean up, files left in $saved need to be handled by the user
				files="$(find "$saved" ! -type d -print)"
				if test -n "$files"
				then
#------------------------------------------------------------------------------
					sysconf_restore_error "$ffsdir" \
"$0: some saved configuration files have not been handled:

$files

These files can be examined in $saved and restored to
$ffsdir if required.  The saved files are in a temporary
directory and will not be retained across a reboot - copy then elsewhere if
you are unsure whether they are needed."
					return 1
				fi
				#
				# so this is safe now (no files, links etc)
				rm -rf "$saved"
			else
				rm -rf "$saved"
				# non-interactive and some changed diff files
				sysconf_restore_error "$ffsdir" \
"$0: some of the saved configuration files must be
examined before restoration"
				# but continue to the umount
			fi
		fi
		#
		# ignore the error in this case:
		umount "$ffsdir" && rmdir "$ffsdir" ||
			echo "$0: $ffsdir: temporary directory cleanup failed" >&2
		status=0
	else
		echo "sysconf restore: $syspart or $ffspart partition not found" >&2
		echo " You must have used 'sysconf save' to save configuration data" >&2
		echo " into the $syspart partition before using this command.  The command" >&2
		echo " will restore the configuration data to the flash root partition" >&2
		echo " named '$ffspart' - this must also be accessible." >&2
	fi

	return $status
}

#
# sysconf_help
#  help text
sysconf_help(){
	#     -------------------------------------------------------------------------------
	echo "sysconf: usage: sysconf read|default|reload|save|restore" >&2
	echo " read:    the current $syspart partition is read into /etc/default/sysconf" >&2
	echo " default: a default /etc/default/sysconf is created" >&2
	echo " reload:  system configuration files are recreated from /etc/default/sysconf" >&2
	echo " save:    /etc/default/sysconf and the files listed in /etc/default/conffiles" >&2
	echo "          are written to the $syspart partition" >&2
	echo " restore: the configuration information in the $syspart partition saved by" >&2
	echo "          'sysconf save' is restored" >&2
}

#
# the real commands
sysconf_command="$1"
test $# -gt 0 && shift
case "$sysconf_command" in
read)	sysconf_read "$@";;
default)sysconf_default "$@";;
reload)	sysconf_reload "$@";;
save)	sysconf_save "$@";;
restore)sysconf_restore "$@";;
valid)  sysconf_valid "$@";;

sysconf)# just load the functions
	;;

*)	# help text
	sysconf_help "$@";;
esac