fwcf.sh 11 KB

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