console.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/bin/bash
  2. set -e -x
  3. setup_ssh()
  4. {
  5. for i in rsa dsa ecdsa ed25519; do
  6. local output=/etc/ssh/ssh_host_${i}_key
  7. if [ ! -s $output ]; then
  8. local saved="$(ros config get rancher.ssh.keys.${i})"
  9. local pub="$(ros config get rancher.ssh.keys.${i}-pub)"
  10. if [[ -n "$saved" && -n "$pub" ]]; then
  11. (
  12. umask 077
  13. temp_file=$(mktemp)
  14. echo "$saved" > ${temp_file}
  15. mv ${temp_file} ${output}
  16. temp_file=$(mktemp)
  17. echo "$pub" > ${temp_file}
  18. mv ${temp_file} ${output}.pub
  19. )
  20. else
  21. ssh-keygen -f $output -N '' -t $i
  22. ros config set -- rancher.ssh.keys.${i} "$(<${output})"
  23. ros config set -- rancher.ssh.keys.${i}-pub "$(<${output}.pub)"
  24. fi
  25. fi
  26. done
  27. mkdir -p /var/run/sshd
  28. }
  29. setup_cgroup()
  30. {
  31. local cgroup=$(grep name=systemd /proc/$$/cgroup | cut -f3 -d:)
  32. if [ -n "$cgroup" ]; then
  33. mkdir -p /sys/fs/cgroup/systemd${cgroup}
  34. fi
  35. }
  36. setup_cgroup || true
  37. RANCHER_HOME=/home/rancher
  38. if [ ! -d ${RANCHER_HOME} ]; then
  39. mkdir -p ${RANCHER_HOME}
  40. chown rancher:rancher ${RANCHER_HOME}
  41. chmod 2755 ${RANCHER_HOME}
  42. fi
  43. DOCKER_HOME=/home/docker
  44. if [ ! -d ${DOCKER_HOME} ]; then
  45. mkdir -p ${DOCKER_HOME}
  46. chown docker:docker ${DOCKER_HOME}
  47. chmod 2755 ${DOCKER_HOME}
  48. fi
  49. echo 1000000000 > /proc/sys/fs/file-max
  50. for i in $(</proc/cmdline); do
  51. case $i in
  52. rancher.password=*)
  53. PASSWORD=$(echo $i | sed 's/rancher.password=//')
  54. ;;
  55. esac
  56. done
  57. if [ -n "$PASSWORD" ]; then
  58. echo "rancher:$PASSWORD" | chpasswd
  59. fi
  60. setup_ssh
  61. cat > /etc/respawn.conf << EOF
  62. /sbin/getty 115200 tty6
  63. /sbin/getty 115200 tty5
  64. /sbin/getty 115200 tty4
  65. /sbin/getty 115200 tty3
  66. /sbin/getty 115200 tty2
  67. /sbin/getty 115200 tty1
  68. /usr/sbin/sshd -D
  69. EOF
  70. for i in ttyS{0..4} ttyAMA0; do
  71. if grep -q 'console='$i /proc/cmdline; then
  72. echo '/sbin/getty 115200' $i >> /etc/respawn.conf
  73. fi
  74. done
  75. if ! grep -q '^UseDNS no' /etc/ssh/sshd_config; then
  76. echo "UseDNS no" >> /etc/ssh/sshd_config
  77. fi
  78. if ! grep -q '^PermitRootLogin no' /etc/ssh/sshd_config; then
  79. echo "PermitRootLogin no" >> /etc/ssh/sshd_config
  80. fi
  81. if ! grep -q '^ServerKeyBits 2048' /etc/ssh/sshd_config; then
  82. echo "ServerKeyBits 2048" >> /etc/ssh/sshd_config
  83. fi
  84. if ! grep -q '^AllowGroups docker' /etc/ssh/sshd_config; then
  85. echo "AllowGroups docker" >> /etc/ssh/sshd_config
  86. fi
  87. VERSION="$(ros os version)"
  88. ID_TYPE="busybox"
  89. if [ -e /etc/os-release ] && grep -q 'ID_LIKE=' /etc/os-release; then
  90. ID_TYPE=$(grep 'ID_LIKE=' /etc/os-release | cut -d'=' -f2)
  91. fi
  92. cat > /etc/os-release << EOF
  93. NAME="RancherOS"
  94. VERSION=$VERSION
  95. ID=rancheros
  96. ID_LIKE=$ID_TYPE
  97. VERSION_ID=$VERSION
  98. PRETTY_NAME="RancherOS"
  99. HOME_URL=
  100. SUPPORT_URL=
  101. BUG_REPORT_URL=
  102. BUILD_ID=
  103. EOF
  104. echo 'RancherOS \n \l' > /etc/issue
  105. echo $(/sbin/ifconfig | grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3}') >> /etc/issue
  106. cloud-init -execute
  107. if [ -x /var/lib/rancher/conf/cloud-config-script ]; then
  108. echo "Running /var/lib/rancher/conf/cloud-config-script"
  109. /var/lib/rancher/conf/cloud-config-script || true
  110. fi
  111. if [ -x /opt/rancher/bin/start.sh ]; then
  112. echo Executing custom script
  113. /opt/rancher/bin/start.sh || true
  114. fi
  115. touch /run/console-done
  116. if [ -x /etc/rc.local ]; then
  117. echo Executing rc.local
  118. /etc/rc.local || true
  119. fi
  120. export TERM=linux
  121. exec respawn -f /etc/respawn.conf