2
0

firstboot 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/sh
  2. # Copyright (C) 2006 OpenWrt.org
  3. rom=$(awk '/squashfs/ {print $2}' /proc/mounts)
  4. jffs=$(awk '/jffs2/ {print $2}' /proc/mounts)
  5. dupe() { # <new_root> <old_root>
  6. cd $1
  7. echo -n "creating directories... "
  8. {
  9. cd $2
  10. find . -xdev -type d
  11. echo "./dev ./jffs ./mnt ./proc ./tmp ./sys"
  12. # xdev skips mounted directories
  13. cd $1
  14. } | xargs mkdir -p
  15. echo "done"
  16. echo -n "setting up symlinks... "
  17. for file in $(cd $2; find . -xdev -type f;); do
  18. case "$file" in
  19. ./rom/note) ;; #nothing
  20. ./etc/config*|\
  21. ./etc/resolv.conf|\
  22. ./usr/lib/ipkg/info) cp -af $2/$file $file;;
  23. *) ln -sf /rom/${file#./*} $file;;
  24. esac
  25. done
  26. for file in $(cd $2; find . -xdev -type l;); do
  27. cp -af $2/${file#./*} $file
  28. done
  29. echo "done"
  30. }
  31. pivot() { # <new_root> <old_root>
  32. mount -o move /proc $1/proc && \
  33. pivot_root $1 $1$2 && {
  34. mount -o move $2/dev /dev
  35. mount -o move $2/tmp /tmp
  36. mount -o move $2/sys /sys
  37. return 0
  38. }
  39. }
  40. mountdp() { # <device> <mount_point> <ignored> <fs>
  41. dev=$1; mnt=$2; shift 2; opt=$*
  42. mount $dev $mnt $opt
  43. dupe $mnt $rom
  44. pivot $mnt /rom
  45. }
  46. ramoverlay() {
  47. mkdir -p /tmp/root
  48. mountdp /tmp/root /mnt -o bind
  49. }
  50. [ "${0##*/}" = "firstboot" ] && {
  51. [ -z "$rom" ] && {
  52. echo "You do not have a squashfs partition; aborting"
  53. echo "(firstboot cannot be run on jffs2 based firmwares)"
  54. exit 1
  55. }
  56. [ "$1" = "switch2jffs" ] && {
  57. mtd erase OpenWrt
  58. mount -o remount,ro none / # try to avoid fs changing while copying
  59. mount -o bind / /mnt
  60. mount /dev/mtdblock/4 /rom/jffs -t jffs2
  61. echo -n "copying files ... "
  62. cp -a /mnt/* /rom/jffs
  63. umount /mnt
  64. echo "done"
  65. pivot /rom /mnt
  66. mount -o move /mnt /tmp/root
  67. pivot /jffs /rom
  68. jffs2root --clean
  69. exit 0
  70. }
  71. # script run manually
  72. [ \! -z "$jffs" ] && {
  73. echo "firstboot has already been run"
  74. echo "jffs2 partition is mounted, only resetting files"
  75. dupe $jffs $rom
  76. exit 0
  77. }
  78. mtd erase OpenWrt
  79. mountdp /dev/mtdblock/4 /jffs -t jffs2
  80. }