fwcf.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #!/bin/sh
  2. # Copyright (c) 2006, 2007
  3. # Thorsten Glaser <tg@mirbsd.de>
  4. # Copyright (c) 2009, 2010
  5. # Waldemar Brodkorb <wbx@openadk.org>
  6. #
  7. # Provided that these terms and disclaimer and all copyright notices
  8. # are retained or reproduced in an accompanying document, permission
  9. # is granted to deal in this work without restriction, including un-
  10. # limited rights to use, publicly perform, distribute, sell, modify,
  11. # merge, give away, or sublicence.
  12. #
  13. # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
  14. # the utmost extent permitted by applicable law, neither express nor
  15. # implied; without malicious intent or gross negligence. In no event
  16. # may a licensor, author or contributor be held liable for indirect,
  17. # direct, other damage, loss, or other issues arising in any way out
  18. # of dealing in the work, even if advised of the possibility of such
  19. # damage or existence of a defect, except proven that it results out
  20. # of said person's immediate fault when using the work as intended.
  21. # Possible return values:
  22. # 0 - everything ok
  23. # 1 - syntax error
  24. # 1 - no 'cfgfs' mtd/cf/nand partition found
  25. # 1 - cfgfs erase: failed
  26. # 1 - cfgfs setup: already run
  27. # 3 - cfgfs setup: mount --bind problems
  28. # 4 - cfgfs setup: can't create or write to temporary filesystem
  29. # 5 - cfgfs setup: can't bind the tmpfs to /etc
  30. # 6 - cfgfs commit: cannot write to partition
  31. # 6 - cfgfs restore: cannot write to partition
  32. # 7 - cfgfs commit: won't write to flash because of unclean setup
  33. # 8 - cfgfs status: differences found
  34. # 9 - cfgfs status: old status file not found
  35. # 10 - cfgfs dump: failed
  36. # 11 - cfgfs commit: cfgfs setup not yet run (use -f to force)
  37. # 11 - cfgfs status: cfgfs setup not yet run
  38. # 12 - cfgfs restore: cannot read the backup
  39. # 255 - cfgfs erase: failed
  40. # 255 - internal error
  41. export PATH=/bin:/sbin:/usr/bin:/usr/sbin
  42. wd=$(pwd)
  43. cd /
  44. what='Configuration Filesystem Utility (cfgfs), Version 1.07'
  45. who=$(id -u)
  46. if [ $who -ne 0 ]; then
  47. echo 'Exit. Configuration Filesystem Utility must be run as root.'
  48. exit 1
  49. fi
  50. usage() {
  51. cat >&2 <<EOF
  52. $what
  53. Usage:
  54. { halt | poweroff | reboot } [-Ffn] [-d delay]
  55. cfgfs { commit | erase | setup | status | dump | restore } [flags]
  56. EOF
  57. exit 1
  58. }
  59. case $0 in
  60. (*cfgfs*) me=cfgfs ;;
  61. (*halt*) me=halt ;;
  62. (*poweroff*) me=poweroff ;;
  63. (*reboot*) me=reboot ;;
  64. (*) usage ;;
  65. esac
  66. if [[ $me != cfgfs ]]; then
  67. dval=
  68. dflag=0
  69. fflag=0
  70. nocfgfs=0
  71. nflag=0
  72. while getopts ":d:Ffn" ch; do
  73. case $ch in
  74. (d) dflag=1; dval=$OPTARG ;;
  75. (F) nocfgfs=1 ;;
  76. (f) fflag=1 ;;
  77. (n) nflag=1 ;;
  78. (*) usage ;;
  79. esac
  80. done
  81. shift $((OPTIND - 1))
  82. [[ $nocfgfs -eq 0 ]] && [[ $fflag -eq 0 ]] && if ! cfgfs status -q; then
  83. echo "error: will not $me: unsaved changes in /etc found!"
  84. echo "Either run 'cfgfs commit' before trying to $me"
  85. echo "or retry with '$me -F${*+ }$*' to force a ${me}."
  86. echo "Run 'cfgfs status' to see which files are changed."
  87. exit 2
  88. fi
  89. [[ $fflag -eq 1 ]] && me="$me -f"
  90. [[ $nflag -eq 1 ]] && me="$me -n"
  91. [[ $dflag -eq 1 ]] && me="$me -d '$dval'"
  92. eval exec busybox $me
  93. fi
  94. case $1 in
  95. (commit|erase|setup|status|dump|restore) ;;
  96. (*) cat >&2 <<EOF
  97. $what
  98. Syntax:
  99. $0 commit [-f]
  100. $0 erase
  101. $0 setup [-N]
  102. $0 status [-rq]
  103. $0 { dump | restore } [<filename>]
  104. EOF
  105. exit 1 ;;
  106. esac
  107. mtd=0
  108. if [ -x /sbin/nand ];then
  109. mtdtool=/sbin/nand
  110. fi
  111. if [ -x /sbin/mtd ];then
  112. mtdtool=/sbin/mtd
  113. fi
  114. # find backend device, first try to find partition with ID 88
  115. part=$(fdisk -l|awk '$5 == 88 { print $1 }')
  116. if [ -z $part ]; then
  117. # otherwise search for MTD device with name cfgfs
  118. part=/dev/mtd$(fgrep '"cfgfs"' /proc/mtd 2>/dev/null | sed 's/^mtd\([^:]*\):.*$/\1/')ro
  119. mtd=1
  120. fi
  121. if [[ ! -e $part ]]; then
  122. echo 'cfgfs: fatal error: no "cfgfs" partition found!'
  123. exit 1
  124. fi
  125. if test $1 = erase; then
  126. dd if="$part" 2>&1 | md5sum 2>&1 >/dev/urandom
  127. if [ $mtd -eq 1 ]; then
  128. cfgfs.helper -Me | eval $mtdtool -F write - cfgfs
  129. else
  130. cfgfs.helper -Me | cat > $part
  131. fi
  132. exit $?
  133. fi
  134. if test $1 = setup; then
  135. if test -e /tmp/.cfgfs; then
  136. echo 'cfgfs: error: "cfgfs setup" already run!'
  137. exit 1
  138. fi
  139. mkdir /tmp/.cfgfs
  140. if test ! -d /tmp/.cfgfs; then
  141. echo 'cfgfs: error: cannot create temporary directory!'
  142. exit 4
  143. fi
  144. chown 0:0 /tmp/.cfgfs
  145. chmod 700 /tmp/.cfgfs
  146. mkdir /tmp/.cfgfs/root
  147. mount --bind /etc /tmp/.cfgfs/root
  148. mkdir /tmp/.cfgfs/temp
  149. mount -t tmpfs none /tmp/.cfgfs/temp
  150. (cd /tmp/.cfgfs/root; tar cf - .) | (cd /tmp/.cfgfs/temp; tar xpf -)
  151. unclean=0
  152. if [[ $1 = -N ]]; then
  153. unclean=2
  154. else
  155. x=$(dd if="$part" bs=4 count=1 2>/dev/null)
  156. [[ "$x" = "FWCF" ]] || \
  157. if [ $mtd -eq 1 ]; then
  158. cfgfs.helper -Me | eval $mtdtool -F write - cfgfs
  159. else
  160. cfgfs.helper -Me | cat > $part
  161. fi
  162. if ! cfgfs.helper -U /tmp/.cfgfs/temp <"$part"; then
  163. unclean=1
  164. echo 'cfgfs: error: cannot extract'
  165. echo unclean startup | logger -t 'cfgfs setup'
  166. fi
  167. if test -e /tmp/.cfgfs/temp/.cfgfs_deleted; then
  168. while IFS= read -r file; do
  169. rm -f "/tmp/.cfgfs/temp/$file"
  170. done </tmp/.cfgfs/temp/.cfgfs_deleted
  171. rm -f /tmp/.cfgfs/temp/.cfgfs_deleted
  172. fi
  173. fi
  174. test $unclean = 0 || echo -n >/tmp/.cfgfs/temp/.cfgfs_unclean
  175. rm -f /tmp/.cfgfs/temp/.cfgfs_done
  176. if test -e /tmp/.cfgfs/temp/.cfgfs_done; then
  177. echo 'cfgfs: fatal: this is not Kansas any more'
  178. umount /tmp/.cfgfs/temp
  179. umount /tmp/.cfgfs/root
  180. rm -rf /tmp/.cfgfs
  181. exit 3
  182. fi
  183. echo -n >/tmp/.cfgfs/temp/.cfgfs_done
  184. if test ! -e /tmp/.cfgfs/temp/.cfgfs_done; then
  185. echo 'cfgfs: fatal: cannot write to tmpfs'
  186. umount /tmp/.cfgfs/temp
  187. umount /tmp/.cfgfs/root
  188. rm -rf /tmp/.cfgfs
  189. exit 4
  190. fi
  191. chmod 755 /tmp/.cfgfs/temp
  192. mount --bind /tmp/.cfgfs/temp /etc
  193. if test ! -e /etc/.cfgfs_done; then
  194. umount /etc
  195. echo 'cfgfs: fatal: binding to /etc failed'
  196. if test $unclean = 0; then
  197. echo 'cfgfs: configuration is preserved' \
  198. in /tmp/.cfgfs/temp
  199. else
  200. umount /tmp/.cfgfs/temp
  201. fi
  202. exit 5
  203. fi
  204. umount /tmp/.cfgfs/temp
  205. echo complete, unclean=$unclean | logger -t 'cfgfs setup'
  206. cd /etc
  207. rm -f .rnd
  208. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  209. xargs md5sum | sed 's! ./! !' | \
  210. cfgfs.helper -Z - /tmp/.cfgfs/status.asz
  211. exit 0
  212. fi
  213. if test $1 = commit; then
  214. umount /tmp/.cfgfs/temp >/dev/null 2>&1
  215. if test ! -e /tmp/.cfgfs; then
  216. cat >&2 <<-EOF
  217. cfgfs: error: not yet initialised
  218. explanation: "cfgfs setup" was not yet run
  219. EOF
  220. [[ $1 = -f ]] || exit 11
  221. fi
  222. if test -e /etc/.cfgfs_unclean; then
  223. cat >&2 <<-EOF
  224. cfgfs: error: unclean startup (or setup run with -N)!
  225. explanation: during boot, the cfgfs filesystem could not
  226. be extracted; saving the current /etc to flash will
  227. result in data loss; to override this check, remove
  228. the file /etc/.cfgfs_unclean and try again.
  229. EOF
  230. [[ $1 = -f ]] || exit 7
  231. fi
  232. mount -t tmpfs none /tmp/.cfgfs/temp
  233. (cd /etc; tar cf - .) | (cd /tmp/.cfgfs/temp; tar xpf -)
  234. cd /tmp/.cfgfs/temp
  235. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  236. xargs md5sum | sed 's! ./! !' | \
  237. cfgfs.helper -Z - /tmp/.cfgfs/status.asz
  238. cd /tmp/.cfgfs/root
  239. rm -f /tmp/.cfgfs/temp/.cfgfs_* /tmp/.cfgfs/temp/.rnd
  240. find /tmp/.cfgfs/temp -type d -empty -delete
  241. find . -type f | while read f; do
  242. f=${f#./}
  243. if [[ ! -e /tmp/.cfgfs/temp/$f ]]; then
  244. [[ $f = .rnd ]] && continue
  245. printf '%s\n' "$f" >>/tmp/.cfgfs/temp/.cfgfs_deleted
  246. continue
  247. fi
  248. x=$(md5sum "$f" 2>/dev/null)
  249. y=$(cd ../temp; md5sum "$f" 2>/dev/null)
  250. [[ "$x" = "$y" ]] && rm "../temp/$f"
  251. done
  252. rv=0
  253. if [ $mtd -eq 1 ]; then
  254. if ! ( cfgfs.helper -M /tmp/.cfgfs/temp | eval $mtdtool -F write - cfgfs ); then
  255. echo 'cfgfs: error: cannot write to $part!'
  256. rv=6
  257. fi
  258. else
  259. if ! ( cfgfs.helper -M /tmp/.cfgfs/temp | cat > $part ); then
  260. echo 'cfgfs: error: cannot write to $part!'
  261. rv=6
  262. fi
  263. fi
  264. umount /tmp/.cfgfs/temp
  265. exit $rv
  266. fi
  267. if test $1 = status; then
  268. if test ! -e /tmp/.cfgfs; then
  269. cat >&2 <<-EOF
  270. cfgfs: error: not yet initialised
  271. explanation: "cfgfs setup" was not yet run
  272. EOF
  273. [[ $1 = -f ]] || exit 11
  274. fi
  275. rm -f /tmp/.cfgfs/*_status /tmp/.cfgfs/*_files
  276. rflag=0
  277. q=printf # or : (true) if -q
  278. shift
  279. while getopts "rq" ch; do
  280. case $ch in
  281. (r) rflag=1 ;;
  282. (q) q=: ;;
  283. esac
  284. done
  285. shift $((OPTIND - 1))
  286. if test $rflag = 1; then
  287. f=/tmp/.cfgfs/rom_status
  288. cd /tmp/.cfgfs/root
  289. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  290. xargs md5sum | sed 's! ./! !' >$f
  291. else
  292. f=/tmp/.cfgfs/status
  293. cfgfs.helper -Zd $f.asz $f || rm -f $f
  294. fi
  295. if [[ ! -e $f ]]; then
  296. echo 'cfgfs: error: old status file not found'
  297. exit 9
  298. fi
  299. cd /etc
  300. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  301. xargs md5sum | sed 's! ./! !' >/tmp/.cfgfs/cur_status || exit 255
  302. cd /tmp/.cfgfs
  303. sed 's/^[0-9a-f]* //' <$f >old_files
  304. sed 's/^[0-9a-f]* //' <cur_status >cur_files
  305. # make *_status be of exactly the same length, for benefit of the
  306. # while ... read <old, read <new loop below, and sort it
  307. comm -23 old_files cur_files | while read name; do
  308. echo "<NULL> $name" >>cur_status
  309. done
  310. comm -13 old_files cur_files | while read name; do
  311. echo "<NULL> $name" >>$f
  312. done
  313. # this implementation of sort -o sucks: doesn't do in-place edits
  314. # workaround a busybox bug?
  315. touch sold_status snew_status
  316. sort -k2 -o sold_status $f
  317. sort -k2 -o snew_status cur_status
  318. gotany=0
  319. while :; do
  320. IFS=' ' read oldsum oldname <&3 || break
  321. IFS=' ' read newsum newname <&4 || exit 255
  322. [[ "$oldname" = "$newname" ]] || exit 255
  323. [[ "$oldsum" = "$newsum" ]] && continue
  324. [[ $gotany = 0 ]] && $q '%-32s %-32s %s\n' \
  325. 'MD5 hash of old file' 'MD5 hash of new file' 'filename'
  326. gotany=8
  327. test $q = : && break
  328. $q '%32s %32s %s\n' "$oldsum" "$newsum" "$oldname"
  329. done 3<sold_status 4<snew_status
  330. rm -f /tmp/.cfgfs/*_status /tmp/.cfgfs/*_files
  331. exit $gotany
  332. fi
  333. if test $1 = dump; then
  334. fn=$2
  335. [[ -n $fn ]] || fn=-
  336. rm -rf /tmp/.cfgfs.dump
  337. mkdir -m 0700 /tmp/.cfgfs.dump
  338. cd /tmp/.cfgfs.dump
  339. if ! cat "$part" | cfgfs.helper -UD dump; then
  340. cd /
  341. rm -rf /tmp/.cfgfs.dump
  342. exit 10
  343. fi
  344. dd if=/dev/urandom of=seed bs=256 count=1 >/dev/null 2>&1
  345. tar -cf - dump seed | (cd "$wd"; cfgfs.helper -Z - $fn)
  346. cd /
  347. rm -rf /tmp/.cfgfs.dump
  348. case $fn in
  349. (-) echo "cfgfs: dump to standard output complete."
  350. ;;
  351. (*) echo "cfgfs: dump to '$fn' complete."
  352. ls -l "$fn" >&2
  353. ;;
  354. esac
  355. exit 0
  356. fi
  357. if test $1 = restore; then
  358. if test -e /tmp/.cfgfs; then
  359. echo 'cfgfs: warning: "cfgfs setup" already run!'
  360. echo 'please reboot after restoring; in no event'
  361. echo 'run "cfgfs commit" to prevent data loss'
  362. echo -n >/etc/.cfgfs_unclean
  363. fi
  364. fn=$2
  365. [[ -n $fn ]] || fn=-
  366. rm -rf /tmp/.cfgfs.restore
  367. mkdir -m 0700 /tmp/.cfgfs.restore
  368. cd /tmp/.cfgfs.restore
  369. if ! (cd "$wd"; cfgfs.helper -Zd "$fn") | tar -xf -; then
  370. cd /
  371. rm -rf /tmp/.cfgfs.restore
  372. exit 12
  373. fi
  374. dd if=seed of=/dev/urandom bs=256 count=1 >/dev/null 2>&1
  375. if test ! -e dump; then
  376. echo 'cfgfs: error: invalid backup'
  377. cd /
  378. rm -rf /tmp/.cfgfs.restore
  379. exit 12
  380. fi
  381. if [ $mtd -eq 1 ]; then
  382. if ! ( cfgfs.helper -MD dump | eval $mtdtool -F write - cfgfs ); then
  383. echo 'cfgfs: error: cannot write to $part!'
  384. exit 6
  385. fi
  386. else
  387. if ! ( cfgfs.helper -MD dump | cat > $part ); then
  388. echo 'cfgfs: error: cannot write to $part!'
  389. exit 6
  390. fi
  391. fi
  392. cd /
  393. rm -rf /tmp/.cfgfs.restore
  394. case $fn in
  395. (-) echo "cfgfs: restore from standard output complete."
  396. ;;
  397. (*) echo "cfgfs: restore from '$fn' complete."
  398. ls -l "$fn" >&2
  399. ;;
  400. esac
  401. exit 0
  402. fi
  403. echo 'cfgfs: cannot be reached...'
  404. exit 255