1
0

init.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ##
  2. ## Copyright (c) 2014 Minoca Corp.
  3. ##
  4. ## This file is licensed under the terms of the GNU General Public License
  5. ## version 3. Alternative licensing terms are available. Contact
  6. ## info@minocacorp.com for details. See the LICENSE file at the root of this
  7. ## project for complete licensing information..
  8. ##
  9. ## Script Name:
  10. ##
  11. ## init.sh
  12. ##
  13. ## Abstract:
  14. ##
  15. ## This script performs user mode initialization.
  16. ##
  17. ## Author:
  18. ##
  19. ## Evan Green 20-Dec-2014
  20. ##
  21. ## Environment:
  22. ##
  23. ## Minoca OS
  24. ##
  25. ##
  26. ## Define the location to set up and ultimately chroot into. If such a world
  27. ## does not exist, then try to fire up a shell directly.
  28. ##
  29. if test -z "$WORLD"; then
  30. WORLD="apps"
  31. fi
  32. if ! test -d "$WORLD"; then
  33. if test -z "$CONSOLE"; then
  34. CONSOLE=/Terminal/Slave0
  35. fi
  36. 0<>$CONSOLE
  37. 1>$CONSOLE
  38. 2>$CONSOLE
  39. export LD_LIBRARY_PATH=$PWD
  40. exec $PWD/swiss sh -i
  41. fi
  42. ##
  43. ## Set up some working environment variables.
  44. ##
  45. export LD_LIBRARY_PATH="$PWD/$WORLD/lib"
  46. export PATH="$PWD/$WORLD/bin"
  47. ##
  48. ## Mount the special devices if needed.
  49. ##
  50. if ! test -c "$WORLD/dev/null"; then
  51. mkdir -p "$WORLD/dev"
  52. touch "$WORLD/dev/null"
  53. touch "$WORLD/dev/full"
  54. touch "$WORLD/dev/zero"
  55. touch "$WORLD/dev/urandom"
  56. touch "$WORLD/dev/console"
  57. touch "$WORLD/dev/tty"
  58. mkdir -p "$WORLD/dev/Volume"
  59. mkdir -p "$WORLD/dev/Terminal"
  60. mkdir -p "$WORLD/dev/Devices"
  61. mount --bind "/Device/null" "$WORLD/dev/null"
  62. mount --bind "/Device/full" "$WORLD/dev/full"
  63. mount --bind "/Device/zero" "$WORLD/dev/zero"
  64. mount --bind "/Device/urandom" "$WORLD/dev/urandom"
  65. mount --bind "/Device/tty" "$WORLD/dev/tty"
  66. mount --bind "/Terminal/Slave0" "$WORLD/dev/console"
  67. mount --bind "/Volume" "$WORLD/dev/Volume"
  68. mount --bind "/Terminal" "$WORLD/dev/Terminal"
  69. mount --bind "/Device" "$WORLD/dev/Devices"
  70. mkdir -p "$WORLD/dev/Pipe"
  71. mount --bind "/Pipe" "$WORLD/dev/Pipe"
  72. fi
  73. ##
  74. ## Set up the home environment.
  75. ##
  76. mkdir -p "$WORLD/root" \
  77. "$WORLD/var/run" \
  78. "$WORLD/var/log" \
  79. "$WORLD/home"
  80. export HOME="/root"
  81. export TERM=xterm
  82. ##
  83. ## Clean out the tmp and dev directories.
  84. ##
  85. rm -rf "$WORLD/tmp" "$WORLD/dev"
  86. mkdir -p "$WORLD/dev"
  87. mkdir -p -m1777 "$WORLD/tmp"
  88. ##
  89. ## Symlink swiss binaries.
  90. ##
  91. if ! test -x $WORLD/bin/chroot; then
  92. ##
  93. ## If there is no swiss, move sh to swiss, setuid on swiss, and then link
  94. ## all the other binaries to swiss.
  95. ##
  96. if ! test -r $WORLD/bin/swiss ; then
  97. mv $WORLD/bin/sh $WORLD/bin/swiss
  98. chmod u+s $WORLD/bin/swiss
  99. fi
  100. for app in `swiss --list`; do
  101. if ! test -x $WORLD/bin/$app; then
  102. ln -s swiss $WORLD/bin/$app
  103. fi
  104. done
  105. ##
  106. ## Also check on root's home and ssh directories, as having those set wrong
  107. ## prevents logging in via SSH.
  108. ##
  109. chmod -f go-rwx $WORLD/root $WORLD/root/.ssh \
  110. $WORLD/root/.ssh/authorized_keys \
  111. $WORLD/root/.ssh/id_rsa
  112. fi
  113. ##
  114. ## Run the final user shell.
  115. ##
  116. exec $WORLD/bin/chroot "$WORLD" -- /bin/init