fwcf.sh 12 KB

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