fwcf.sh 13 KB

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