firstboot 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/bin/sh
  2. . /etc/functions.sh
  3. partname="rootfs_data"
  4. mtdpart="$(find_mtd_part $partname)"
  5. rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
  6. jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
  7. dupe() { # <new_root> <old_root>
  8. cd $1
  9. echo -n "creating directories... "
  10. {
  11. cd $2
  12. find . -xdev -type d
  13. echo "./dev ./jffs ./mnt ./proc ./tmp"
  14. # xdev skips mounted directories
  15. cd $1
  16. } | xargs mkdir -p
  17. echo "done"
  18. echo -n "setting up symlinks... "
  19. for file in $(cd $2; find . -xdev -type f;); do
  20. case "$file" in
  21. ./rom/note) ;; #nothing
  22. ./etc/config*|\
  23. ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
  24. *) ln -sf /rom/${file#./*} $file;;
  25. esac
  26. done
  27. for file in $(cd $2; find . -xdev -type l;); do
  28. cp -af $2/${file#./*} $file
  29. done
  30. echo "done"
  31. }
  32. pivot() { # <new_root> <old_root>
  33. mount -o move /proc $1/proc && \
  34. pivot_root $1 $1$2 && {
  35. mount -o move $2/dev /dev
  36. mount -o move $2/tmp /tmp
  37. mount -o move $2/sys /sys 2>&-
  38. mount -o move $2/jffs /jffs 2>&-
  39. return 0
  40. }
  41. }
  42. fopivot() { # <rw_root> <ro_root> <dupe?>
  43. root=$1
  44. {
  45. mount -t mini_fo -o base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
  46. } || {
  47. [ "$3" = "1" ] && {
  48. mount | grep "on $1 type" 2>&- 1>&- || mount -o bind $1 $1
  49. dupe $1 $rom
  50. }
  51. }
  52. pivot $root $2
  53. }
  54. ramoverlay() {
  55. mkdir -p /tmp/root
  56. fopivot /tmp/root /rom 1
  57. }
  58. # invoked as an executable
  59. [ "${0##*/}" = "firstboot" ] && {
  60. [ -z "$mtdpart" ] && {
  61. echo "MTD partition not found."
  62. exit 1
  63. }
  64. [ -z "$rom" ] && {
  65. echo "You do not have a squashfs partition; aborting"
  66. echo "(firstboot cannot be run on jffs2 based firmwares)"
  67. exit 1
  68. }
  69. [ "$1" = "switch2jffs" ] && {
  70. mount "$mtdpart" /rom/jffs -t jffs2 || exit
  71. # try to avoid fs changing while copying
  72. mount -o remount,ro none / 2>&-
  73. # copy ramoverlay to jffs2
  74. echo -n "copying files ... "
  75. cp -a /tmp/root/* /rom/jffs 2>&-
  76. echo "done"
  77. # switch back to squashfs (temporarily)
  78. # and park the ramdisk ontop of /tmp/root
  79. pivot /rom /mnt
  80. mount -o move /mnt /tmp/root
  81. # /jffs is the overlay
  82. # /rom is the readonly
  83. fopivot /jffs /rom
  84. # try to get rid of /tmp/root
  85. # this will almost always fail
  86. umount /tmp/root 2>&-
  87. exit 0
  88. }
  89. # script run manually
  90. [ \! -z "$jffs" ] && {
  91. echo "firstboot has already been run"
  92. echo "jffs2 partition is mounted, only resetting files"
  93. grep mini_fo /proc/filesystems >&-
  94. [ $? != 0 ] && {
  95. dupe $jffs $rom
  96. exit 0
  97. } || {
  98. rm -rf $jffs/* 2>&-
  99. mount -o remount $jffs / 2>&-
  100. exit 0
  101. }
  102. }
  103. mtd erase "$partname"
  104. mount "$mtdpart" /jffs -t jffs2
  105. fopivot /jffs /rom 1
  106. }