Advertisement
Guest User

/init от genkernel

a guest
Feb 7th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.29 KB | None | 0 0
  1. #!/bin/ash
  2.  
  3. # Copyright 1999-2011 Gentoo Foundation
  4. # Distributed under the terms of the GNU General Public License, v2 or later
  5.  
  6. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  7.  
  8. #// Path, basic vars
  9. #//--------------------------------------------------------------------------------
  10.  
  11. BasicSetup() {
  12.     #// Copyright year, Build date in YYYYMMDD format, and in MMDDYYYY to make busybox 'date' happy
  13.     MYDATE="`/bin/cat /etc/build_date`"
  14.     CPYYEAR="$(echo ${MYDATE} | cut -c 1-4)"
  15.     BBDATE="$(echo ${MYDATE} | cut -c 5-8)$(echo ${MYDATE} | cut -c 1-4)"
  16.     DISDATE="$(echo ${MYDATE} | cut -c 7-8) $(echo ${MYDATE} | cut -c 5-6) $(echo ${MYDATE} | cut -c 1-4)"
  17.  
  18.     . /etc/initrd.defaults
  19.     # Clean input/output
  20.     exec >${CONSOLE} <${CONSOLE} 2>&1
  21. }
  22. #//--------------------------------------------------------------------------------
  23.  
  24.  
  25.  
  26. #// Startup Tasks
  27. #//--------------------------------------------------------------------------------
  28.  
  29. StartUp() {
  30.     if [ ! -f "/tmp/.startup" ]; then
  31.         #// Mount proc && sys
  32.         mount proc  /proc       -t proc         # /proc
  33.         mount sys   /sys        -t sysfs        # /sys
  34.         mount mdev  /dev        -t tmpfs  -o size=800k  # /dev for mdev
  35.  
  36.         #// Let busybox build its applets
  37.         /bin/busybox --install -s
  38.  
  39.         #// Create additional mount points
  40.         mkdir -m 0755 /dev/pts
  41.         mkdir       /dev/shm
  42.         mkdir -p    /mnt/cdrom
  43.         mkdir       /mnt/floppy
  44.         mkdir       /mnt/gentoo
  45.         mkdir       /tmp
  46.  
  47.         #// Mount remaining filesystems
  48.         mount tmp   /tmp        -t tmpfs        # /tmp
  49.         mount devpts    /dev/pts    -t devpts -o gid=5,mode=0620    # /dev/pts
  50.         mount shm   /dev/shm    -t tmpfs -o size=512k   # /dev/shm
  51.  
  52.         #// Create mtab
  53.         ln -sf  /proc/mounts        /etc/mtab       # mtab (symlink -> /proc/mounts)
  54.  
  55.         #// Start mdev
  56.         echo "/sbin/mdev" > /proc/sys/kernel/hotplug        # mdev handles hotplug events
  57.         /sbin/mdev -s                       # have mdev populate /dev
  58.  
  59.         #// Create standard (non-mdev) devices
  60.         if [ ! -e /dev/md0 ]
  61.         then
  62.             makedevs    /dev/md     b 9 0 0 7
  63.         fi
  64.  
  65.         if [ ! -e /dev/tty0 ]
  66.         then
  67.             makedevs    /dev/tty    c 4 0 0 12
  68.         fi
  69.  
  70.         # We probably don't need any of these anymore with mdev
  71. #       makedevs    /dev/ptyp   c 2 0 0 9
  72. #       makedevs    /dev/ttyp   c 3 0 0 9
  73. #       makedevs    /dev/ttyq   c 3 16 0 9
  74. #       makedevs    /dev/ttyS   c 4 64 0 3
  75. #       mknod       /dev/console    c 5 1
  76. #       mknod       /dev/kmsg   c 1 11
  77. #       mknod       /dev/null   c 1 3
  78. #       mknod       /dev/tty    c 5 0
  79. #       mknod       /dev/urandom    c 1 9
  80. #       ln -s       /dev/urandom    /dev/random
  81. #       mknod       /dev/zero   c 1 5
  82.  
  83.         #// Create std* devices
  84.         ln -snf /proc/self/fd /dev/fd
  85.         ln -snf /proc/self/fd/0 /dev/stdin
  86.         ln -snf /proc/self/fd/1 /dev/stdout
  87.         ln -snf /proc/self/fd/2 /dev/stderr
  88.  
  89.         #// Make some misc directories
  90.         mkdir   /var/log
  91.         mkdir   /var/run
  92.  
  93.         #// Start a minimal logger
  94.         klogd
  95.         syslogd
  96.  
  97.         #// Hostname
  98.         hostname netboot
  99.  
  100.         if [ -n "`which dropbear 2>/dev/null`" ]
  101.         then
  102.             # Setup dropbear (sshd)
  103.             echo -e ""
  104.             mkdir /etc/dropbear
  105.             echo -e ">>> Generating RSA hostkey ..."
  106.             dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
  107.             echo -e ""
  108.             echo -e ">>> Generating DSS hostkey ..."
  109.             dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
  110.             echo -e ""
  111.             dropbear
  112.         fi
  113.  
  114.         #// Misc tasks
  115.         chmod +x /bin/net-setup
  116.         chmod +x /bin/ashlogin
  117.     fi
  118. }
  119.  
  120. #//--------------------------------------------------------------------------------
  121.  
  122.  
  123.  
  124. #// Informative Message (copied from Gentoo /sbin/functions.sh)
  125. #//--------------------------------------------------------------------------------
  126.  
  127. #// show an informative message (with a newline)
  128. einfo() {
  129.     echo -e " * ${*}"
  130.     return 0
  131. }
  132.  
  133. #//--------------------------------------------------------------------------------
  134.  
  135.  
  136.  
  137. #// Determine Mips Machine Type
  138. #//--------------------------------------------------------------------------------
  139.  
  140. DetectMips() {
  141.     MYARCH="MIPS"
  142.     MACHINFO="$(cat /proc/cpuinfo | grep "system type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
  143.     CPUINFO="$(cat /proc/cpuinfo | grep "cpu model" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
  144.     case "${MACHINFO}" in
  145.         "SGI Indy")         MACHTYPE="SGI Indy"     ;;  # Indy R4x00/R5000
  146.         "SGI Indigo2")
  147.             case "${CPUINFO}" in
  148.                 R4*)        MACHTYPE="SGI Indigo2"      ;;  # I2 R4x00
  149.                 R8*)        MACHTYPE="SGI Indigo2 Power"    ;;  # I2 R8000
  150.                 R10*)       MACHTYPE="SGI Indigo2 Impact"   ;;  # I2 R10000
  151.             esac
  152.             ;;
  153.         "SGI O2"|"SGI IP32")        MACHTYPE="SGI O2"       ;;  # O2 R5K/RM5K2/RM7K/R10K/R12K
  154.         "SGI Octane"|"SGI IP30")    MACHTYPE="SGI Octane"       ;;  # Octane R10K/R12K/R14K
  155.         "SGI Origin"|"SGI IP27")    MACHTYPE="SGI Origin"       ;;  # Origin 200/2000 R10K/R12K
  156.         "MIPS Cobalt"|*RaQ*|*Qube*) MACHTYPE="Cobalt Microserver"   ;;  # Cobalt Qube/RaQ (1/2)
  157.         *)              MACHTYPE="Unknown MIPS"     ;;  # ???
  158.     esac
  159. }
  160.  
  161. #//--------------------------------------------------------------------------------
  162.  
  163.  
  164.  
  165. #// Determine Sparc Machine Type
  166. #//--------------------------------------------------------------------------------
  167.  
  168. DetectSparc() {
  169.     MYARCH="SPARC"
  170.     MACHINFO="$(cat /proc/cpuinfo | grep "type" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
  171.  
  172.     case "${MACHINFO}" in
  173.         sun4u)          MACHTYPE="Sun UltraSparc"   ;;      # Sparc64
  174.         sun4c|sun4d|sun4m)  MACHTYPE="Sun Sparc32"      ;;      # Sparc32
  175.         *)          MACHTYPE="Unknown SPARC"    ;;      # ???
  176.     esac
  177. }
  178.  
  179. #//--------------------------------------------------------------------------------
  180.  
  181.  
  182.  
  183. #// Determine Ppc Machine Type
  184. #//--------------------------------------------------------------------------------
  185.  
  186. DetectPpc() {
  187.     MACHINFO="$(cat /proc/cpuinfo | grep "machine" | tr -d "\t" | sed -e "s/: /:/g" | cut -d":" -f2)"
  188.  
  189.     case "${ARCHINFO}" in
  190.         ppc)
  191.             MYARCH="PPC"
  192.             case "${MACHINFO}" in
  193.                 PowerMac*)  MACHTYPE="Apple PowerMac"   ;;  # PowerMac
  194.                 PowerBook*) MACHTYPE="Apple PowerBook"  ;;  # PowerBook
  195.                 "CHRP Pegasos") MACHTYPE="Pegasos"      ;;  # Pegasos
  196.                 CHRP*|PReP) MACHTYPE="IBM PPC-Based"    ;;  # IBM PPC
  197.                 Amiga)      MACHTYPE="Amiga"        ;;  # Amiga
  198.                 *)      MACHTYPE="Unknown PPC"      ;;  # ???
  199.             esac
  200.         ;;
  201.  
  202.         ppc64)
  203.             MYARCH="PPC64"
  204.             case "${MACHINFO}" in
  205.                 PowerMac*)  MACHTYPE="Apple G5"     ;;  # Apple G5
  206.                 CHRP*|PReP) MACHTYPE="IBM PPC-Based"    ;;  # IBM PPC
  207.                 *iSeries*)  MACHTYPE="iSeries (Old)"    ;;  # Old iSeries
  208.                 *)      MACHTYPE="Unknown PPC64"    ;;  # ???
  209.             esac
  210.         ;;
  211.     esac
  212. }
  213.  
  214. #//--------------------------------------------------------------------------------
  215.  
  216.  
  217.  
  218. #// Discover if the network is already running for us or not
  219. #//--------------------------------------------------------------------------------
  220.  
  221. DetectNetwork() {
  222.     if [ ! -f "/tmp/.startup" ]; then
  223.         #// If this image is loaded via NFS Root, chances are the network is autoconfigured for us
  224.         if [ ! -z "$(ifconfig | grep "eth0")" ]; then
  225.             MYIP="$(ifconfig | grep "inet addr" | cut -d":" -f2 | cut -d" " -f1 | head -n 1)"
  226.             MYGW="$(route | grep default | cut -d" " -f10)"
  227.         fi
  228.     fi
  229. }
  230.  
  231. #//--------------------------------------------------------------------------------
  232.  
  233.  
  234.  
  235. #// For those in the Church of the SubGenius...
  236. #//--------------------------------------------------------------------------------
  237.  
  238. SubGenius() {
  239.     BUILDDATE="Build Date: $(date -d ${BBDATE} +"%B %d, %Y")"
  240.     for CMDLINE in $(cat /proc/cmdline); do
  241.         if [ "${CMDLINE}" = "discord" ]; then
  242.             BUILDDATE="$(ddate +'Built on %{%A, the %e day of %B%} in the YOLD %Y. %NCelebrate %H!' ${DISDATE})"
  243.         fi
  244.     done
  245. }
  246. #//--------------------------------------------------------------------------------
  247.  
  248.  
  249.  
  250. #// Basic Startup Stuff
  251. #//--------------------------------------------------------------------------------
  252.  
  253. GenMotd() {
  254.     echo -e ""                                      > /etc/motd
  255.     echo -e ""                                      >> /etc/motd
  256.     echo -e "Gentoo Linux; http://www.gentoo.org/"                      >> /etc/motd
  257.     echo -e " Copyright 2001-${CPYYEAR} Gentoo Foundation; Distributed under the GPL"   >> /etc/motd
  258.     echo -e ""                                      >> /etc/motd
  259.     echo -e " Gentoo/${MYARCH} Netboot for ${MACHTYPE} Systems"             >> /etc/motd
  260.     echo -e " ${BUILDDATE}"                                 >> /etc/motd
  261.     echo -e ""                                      >> /etc/motd
  262.  
  263.     #// If this is the initial startup, then display some messages, otherwise just execute a shell for the user
  264.     if [ ! -f "/tmp/.startup" ]; then
  265.         if [ -z "${MYIP}" ]; then
  266.             einfo "To configure networking (eth0), do the following:"       > /etc/motd2
  267.             echo -e ""                              >> /etc/motd2
  268.             einfo "For Static IP:"                          >> /etc/motd2
  269.             einfo "/bin/net-setup <IP Address> <Gateway Address>"           >> /etc/motd2
  270.             echo -e ""                              >> /etc/motd2
  271.             einfo "For Dynamic IP:"                         >> /etc/motd2
  272.             einfo "/bin/net-setup dhcp"                     >> /etc/motd2
  273.             echo -e ""                              >> /etc/motd2
  274.         else
  275.             echo -e ""                              > /etc/motd2
  276.             einfo "Network interface eth0 has been started:"            >> /etc/motd2
  277.             einfo "  IP Address: ${MYIP}"                       >> /etc/motd2
  278.             einfo "  Gateway:    ${MYGW}"                       >> /etc/motd2
  279.             echo -e ""                              >> /etc/motd2
  280.             einfo "An sshd server is available on port 22.  Please set a root"  >> /etc/motd2
  281.             einfo "password via \"passwd\" before using."               >> /etc/motd2
  282.             echo -e ""                              >> /etc/motd2
  283.         fi
  284.     fi
  285. }
  286.  
  287. #//--------------------------------------------------------------------------------
  288.  
  289.  
  290.  
  291. #// Display Motd
  292. #//--------------------------------------------------------------------------------
  293.  
  294. DisplayMotd() {
  295.     cat /etc/motd
  296.     [ -f "/etc/motd2" ] && cat /etc/motd2
  297. }
  298.  
  299. #//--------------------------------------------------------------------------------
  300.  
  301.  
  302.  
  303. #// Launch Shell
  304. #//--------------------------------------------------------------------------------
  305.  
  306. LaunchShell() {
  307.     #// Completed Startup
  308.     touch /tmp/.startup
  309.  
  310.     for i in 2 3 4 5 6; do
  311.         getty -n -l /bin/ashlogin 38400 tty${i} &
  312.     done
  313.  
  314. #   # We run the getty for tty1 in the foreground so our pid 1 doesn't end
  315. #   getty -n -l /bin/ashlogin 38400 tty1
  316.  
  317.     # We were running the above code, but that doesn't work well on serial. Until
  318.     # we can autodetect a serial console and start a getty there, we'll just run
  319.     # ash on /dev/console
  320.     /bin/ash
  321. }
  322.  
  323. #//--------------------------------------------------------------------------------
  324.  
  325.  
  326.  
  327. #// Main
  328. #//--------------------------------------------------------------------------------
  329.  
  330. BasicSetup
  331. StartUp
  332.  
  333. #// Detect Arch
  334. ARCHINFO="$(uname -m)"
  335. case "${ARCHINFO}" in
  336.     mips*)      DetectMips  ;;
  337.     sparc*)     DetectSparc
  338.         mount -t openpromfs openprom /proc/openprom
  339.     ;;
  340.     ppc*)       DetectPpc   ;;
  341.     *)          MACHTYPE=$ARCHINFO  ;;
  342. esac
  343.  
  344. DetectNetwork
  345. SubGenius
  346. GenMotd
  347. DisplayMotd
  348. LaunchShell
  349.  
  350. #//--------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement