fwcf.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #!/bin/sh
  2. # Copyright (c) 2006, 2007
  3. # Thorsten Glaser <tg@mirbsd.de>
  4. # Copyright (c) 2009
  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 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.06'
  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. integer dflag=0
  68. dval=
  69. integer fflag=0
  70. integer nocfgfs=0
  71. integer 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 == 0 && fflag == 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 )) && me="$me -f"
  90. (( nflag )) && me="$me -n"
  91. (( dflag )) && 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. # find backend device, first try to find partition with ID 88
  108. part=$(fdisk -l|awk '$5 == 88 { print $1 }')
  109. if [ -z $part ]; then
  110. # otherwise search for MTD device with name cfgfs
  111. part=/dev/mtd$(fgrep '"cfgfs"' /proc/mtd 2>/dev/null | sed 's/^mtd\([^:]*\):.*$/\1/')ro
  112. fi
  113. if [[ ! -e $part ]]; then
  114. echo 'cfgfs: fatal error: no "cfgfs" partition found!'
  115. exit 1
  116. fi
  117. if test $1 = erase; then
  118. dd if="$part" 2>&1 | md5sum 2>&1 >/dev/urandom
  119. cfgfs.helper -Me | cfgfs.write
  120. exit $?
  121. fi
  122. if test $1 = setup; then
  123. if test -e /tmp/.cfgfs; then
  124. echo 'cfgfs: error: "cfgfs setup" already run!'
  125. exit 1
  126. fi
  127. mkdir /tmp/.cfgfs
  128. if test ! -d /tmp/.cfgfs; then
  129. echo 'cfgfs: error: cannot create temporary directory!'
  130. exit 4
  131. fi
  132. chown 0:0 /tmp/.cfgfs
  133. chmod 700 /tmp/.cfgfs
  134. mkdir /tmp/.cfgfs/root
  135. mount --bind /etc /tmp/.cfgfs/root
  136. mkdir /tmp/.cfgfs/temp
  137. mount -t tmpfs -o cfgfs /tmp/.cfgfs/temp
  138. (cd /tmp/.cfgfs/root; tar cf - .) | (cd /tmp/.cfgfs/temp; tar xpf -)
  139. unclean=0
  140. if [[ $1 = -N ]]; then
  141. unclean=2
  142. else
  143. x=$(dd if="$part" bs=4 count=1 2>/dev/null)
  144. [[ "$x" = "FWCF" ]] || cfgfs.helper -Me | cfgfs.write
  145. if ! cfgfs.helper -U /tmp/.cfgfs/temp <"$part"; then
  146. unclean=1
  147. echo 'cfgfs: error: cannot extract'
  148. echo unclean startup | logger -t 'cfgfs setup'
  149. fi
  150. if test -e /tmp/.cfgfs/temp/.cfgfs_deleted; then
  151. while IFS= read -r file; do
  152. rm -f "/tmp/.cfgfs/temp/$file"
  153. done </tmp/.cfgfs/temp/.cfgfs_deleted
  154. rm -f /tmp/.cfgfs/temp/.cfgfs_deleted
  155. fi
  156. fi
  157. test $unclean = 0 || echo -n >/tmp/.cfgfs/temp/.cfgfs_unclean
  158. rm -f /tmp/.cfgfs/temp/.cfgfs_done
  159. if test -e /tmp/.cfgfs/temp/.cfgfs_done; then
  160. echo 'cfgfs: fatal: this is not Kansas any more'
  161. umount /tmp/.cfgfs/temp
  162. umount /tmp/.cfgfs/root
  163. rm -rf /tmp/.cfgfs
  164. exit 3
  165. fi
  166. echo -n >/tmp/.cfgfs/temp/.cfgfs_done
  167. if test ! -e /tmp/.cfgfs/temp/.cfgfs_done; then
  168. echo 'cfgfs: fatal: cannot write to tmpfs'
  169. umount /tmp/.cfgfs/temp
  170. umount /tmp/.cfgfs/root
  171. rm -rf /tmp/.cfgfs
  172. exit 4
  173. fi
  174. chmod 755 /tmp/.cfgfs/temp
  175. mount --bind /tmp/.cfgfs/temp /etc
  176. if test ! -e /etc/.cfgfs_done; then
  177. umount /etc
  178. echo 'cfgfs: fatal: binding to /etc failed'
  179. if test $unclean = 0; then
  180. echo 'cfgfs: configuration is preserved' \
  181. in /tmp/.cfgfs/temp
  182. else
  183. umount /tmp/.cfgfs/temp
  184. fi
  185. exit 5
  186. fi
  187. umount /tmp/.cfgfs/temp
  188. echo complete, unclean=$unclean | logger -t 'cfgfs setup'
  189. cd /etc
  190. rm -f .rnd
  191. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  192. xargs md5sum | sed 's! ./! !' | \
  193. cfgfs.helper -Z - /tmp/.cfgfs/status.asz
  194. exit 0
  195. fi
  196. if test $1 = commit; then
  197. umount /tmp/.cfgfs/temp >/dev/null 2>&1
  198. if test ! -e /tmp/.cfgfs; then
  199. cat >&2 <<-EOF
  200. cfgfs: error: not yet initialised
  201. explanation: "cfgfs setup" was not yet run
  202. EOF
  203. [[ $1 = -f ]] || exit 11
  204. fi
  205. if test -e /etc/.cfgfs_unclean; then
  206. cat >&2 <<-EOF
  207. cfgfs: error: unclean startup (or setup run with -N)!
  208. explanation: during boot, the cfgfs filesystem could not
  209. be extracted; saving the current /etc to flash will
  210. result in data loss; to override this check, remove
  211. the file /etc/.cfgfs_unclean and try again.
  212. EOF
  213. [[ $1 = -f ]] || exit 7
  214. fi
  215. mount -t tmpfs -o swap /tmp/.cfgfs/temp
  216. (cd /etc; tar cf - .) | (cd /tmp/.cfgfs/temp; tar xpf -)
  217. cd /tmp/.cfgfs/temp
  218. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  219. xargs md5sum | sed 's! ./! !' | \
  220. cfgfs.helper -Z - /tmp/.cfgfs/status.asz
  221. cd /tmp/.cfgfs/root
  222. rm -f /tmp/.cfgfs/temp/.cfgfs_* /tmp/.cfgfs/temp/.rnd
  223. find /tmp/.cfgfs/temp -type d -empty -delete
  224. find . -type f | while read f; do
  225. f=${f#./}
  226. if [[ ! -e /tmp/.cfgfs/temp/$f ]]; then
  227. [[ $f = .rnd ]] && continue
  228. printf '%s\n' "$f" >>/tmp/.cfgfs/temp/.cfgfs_deleted
  229. continue
  230. fi
  231. x=$(md5sum "$f" 2>/dev/null)
  232. y=$(cd ../temp; md5sum "$f" 2>/dev/null)
  233. [[ "$x" = "$y" ]] && rm "../temp/$f"
  234. done
  235. rv=0
  236. if ! ( cfgfs.helper -M /tmp/.cfgfs/temp | cfgfs.write ); then
  237. echo 'cfgfs: error: cannot write to $part!'
  238. rv=6
  239. fi
  240. umount /tmp/.cfgfs/temp
  241. exit $rv
  242. fi
  243. if test $1 = status; then
  244. if test ! -e /tmp/.cfgfs; then
  245. cat >&2 <<-EOF
  246. cfgfs: error: not yet initialised
  247. explanation: "cfgfs setup" was not yet run
  248. EOF
  249. [[ $1 = -f ]] || exit 11
  250. fi
  251. rm -f /tmp/.cfgfs/*_status /tmp/.cfgfs/*_files
  252. rflag=0
  253. q=printf # or : (true) if -q
  254. shift
  255. while getopts "rq" ch; do
  256. case $ch in
  257. (r) rflag=1 ;;
  258. (q) q=: ;;
  259. esac
  260. done
  261. shift $((OPTIND - 1))
  262. if test $rflag = 1; then
  263. f=/tmp/.cfgfs/rom_status
  264. cd /tmp/.cfgfs/root
  265. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  266. xargs md5sum | sed 's! ./! !' >$f
  267. else
  268. f=/tmp/.cfgfs/status
  269. cfgfs.helper -Zd $f.asz $f || rm -f $f
  270. fi
  271. if [[ ! -e $f ]]; then
  272. echo 'cfgfs: error: old status file not found'
  273. exit 9
  274. fi
  275. cd /etc
  276. find . -type f | grep -v -e '^./.cfgfs' -e '^./.rnd$' | sort | \
  277. xargs md5sum | sed 's! ./! !' >/tmp/.cfgfs/cur_status || exit 255
  278. cd /tmp/.cfgfs
  279. sed 's/^[0-9a-f]* //' <$f >old_files
  280. sed 's/^[0-9a-f]* //' <cur_status >cur_files
  281. # make *_status be of exactly the same length, for benefit of the
  282. # while ... read <old, read <new loop below, and sort it
  283. comm -23 old_files cur_files | while read name; do
  284. echo "<NULL> $name" >>cur_status
  285. done
  286. comm -13 old_files cur_files | while read name; do
  287. echo "<NULL> $name" >>$f
  288. done
  289. # this implementation of sort -o sucks: doesn't do in-place edits
  290. sort -k2 -o sold_status $f
  291. sort -k2 -o snew_status cur_status
  292. gotany=0
  293. while :; do
  294. IFS=' ' read oldsum oldname <&3 || break
  295. IFS=' ' read newsum newname <&4 || exit 255
  296. [[ "$oldname" = "$newname" ]] || exit 255
  297. [[ "$oldsum" = "$newsum" ]] && continue
  298. [[ $gotany = 0 ]] && $q '%-32s %-32s %s\n' \
  299. 'MD5 hash of old file' 'MD5 hash of new file' 'filename'
  300. gotany=8
  301. test $q = : && break
  302. $q '%32s %32s %s\n' "$oldsum" "$newsum" "$oldname"
  303. done 3<sold_status 4<snew_status
  304. rm -f /tmp/.cfgfs/*_status /tmp/.cfgfs/*_files
  305. exit $gotany
  306. fi
  307. if test $1 = dump; then
  308. fn=$2
  309. [[ -n $fn ]] || fn=-
  310. rm -rf /tmp/.cfgfs.dump
  311. mkdir -m 0700 /tmp/.cfgfs.dump
  312. cd /tmp/.cfgfs.dump
  313. if ! cat "$part" | cfgfs.helper -UD dump; then
  314. cd /
  315. rm -rf /tmp/.cfgfs.dump
  316. exit 10
  317. fi
  318. dd if=/dev/urandom of=seed bs=256 count=1 >/dev/null 2>&1
  319. tar -cf - dump seed | (cd "$wd"; cfgfs.helper -Z - $fn)
  320. cd /
  321. rm -rf /tmp/.cfgfs.dump
  322. case $fn in
  323. (-) echo "cfgfs: dump to standard output complete."
  324. ;;
  325. (*) echo "cfgfs: dump to '$fn' complete."
  326. ls -l "$fn" >&2
  327. ;;
  328. esac
  329. exit 0
  330. fi
  331. if test $1 = restore; then
  332. if test -e /tmp/.cfgfs; then
  333. echo 'cfgfs: warning: "cfgfs setup" already run!'
  334. echo 'please reboot after restoring; in no event'
  335. echo 'run "cfgfs commit" to prevent data loss'
  336. echo -n >/etc/.cfgfs_unclean
  337. fi
  338. fn=$2
  339. [[ -n $fn ]] || fn=-
  340. rm -rf /tmp/.cfgfs.restore
  341. mkdir -m 0700 /tmp/.cfgfs.restore
  342. cd /tmp/.cfgfs.restore
  343. if ! (cd "$wd"; cfgfs.helper -Zd "$fn") | tar -xf -; then
  344. cd /
  345. rm -rf /tmp/.cfgfs.restore
  346. exit 12
  347. fi
  348. dd if=seed of=/dev/urandom bs=256 count=1 >/dev/null 2>&1
  349. if test ! -e dump; then
  350. echo 'cfgfs: error: invalid backup'
  351. cd /
  352. rm -rf /tmp/.cfgfs.restore
  353. exit 12
  354. fi
  355. if ! ( cfgfs.helper -MD dump | cfgfs.write ); then
  356. echo 'cfgfs: error: cannot write to $part!'
  357. exit 6
  358. fi
  359. cd /
  360. rm -rf /tmp/.cfgfs.restore
  361. case $fn in
  362. (-) echo "cfgfs: restore from standard output complete."
  363. ;;
  364. (*) echo "cfgfs: restore from '$fn' complete."
  365. ls -l "$fn" >&2
  366. ;;
  367. esac
  368. exit 0
  369. fi
  370. echo 'cfgfs: cannot be reached...'
  371. exit 255