firstboot 2.6 KB

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