Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 10.04 KB | None | 0 0
  1. #!/bin/busybox ash
  2.  
  3. # /etc/init.d/rcS: main init script
  4.  
  5. # usage: error [MESSAGE]
  6. # outputs an error message and does things common to all errors during init
  7. error()
  8. {
  9.  
  10.   echo
  11.   echo "Error: $1."
  12.   echo
  13.   read -p "Press any key to continue. " DUMMY
  14.   echo
  15.  
  16. }
  17.  
  18. # mount all virtual file systems
  19. /bin/busybox mount -a
  20. /bin/busybox mount -o remount,rw /
  21.  
  22. # filter boot codes; inspired by the Tiny Core init script
  23. for i in `/bin/busybox cat /proc/cmdline`; do
  24.  
  25.   case $i in
  26.  
  27.     "swap="*)
  28.    
  29.       swapPartition="${i#*=}"
  30.       ;;
  31.  
  32.     "dhcp="*)
  33.    
  34.       interface="${i#*=}"
  35.       ;;
  36.  
  37.     "wait="*)
  38.    
  39.       seconds="${i#*=}"
  40.       ;;
  41.  
  42.     "blacklist="*)
  43.    
  44.       module="${i#*=}"
  45.      
  46.       echo "Blacklisting $module"
  47.       echo "blacklist $module" >> /etc/modprobe.conf
  48.       ;;
  49.  
  50.     "live")
  51.  
  52.       live="true"
  53.       ;;
  54.  
  55.   esac
  56.  
  57. done
  58.  
  59. echo "Starting logging services"
  60.  
  61. # start logging daemons
  62. /bin/busybox klogd
  63. /bin/busybox syslogd
  64.  
  65. # start mdev to create device nodes in /dev
  66. /bin/busybox mdev -s  
  67.  
  68. # pause, if the user passed the "wait" boot code
  69. if [ "$seconds" != "" ]; then
  70.  
  71.   if [ "$((seconds + 1))" -le "1" ]; then
  72.  
  73.     error "the number of seconds to pause must be a positive integer"
  74.    
  75.   else
  76.  
  77.     echo "Pausing for $seconds seconds"
  78.     /bin/busybox sleep $seconds
  79.  
  80.   fi
  81.  
  82. fi
  83.  
  84. # set up swap, if the user passed the "swap" boot code
  85. if [ "$swapPartition" != "" ]; then
  86.    
  87.   if [ -b "$swapPartition" ]; then
  88.  
  89.     echo "Setting up swap on $swapPartition"
  90.     /bin/busybox mkswap $swapPartition
  91.     /bin/busybox swapon $swapPartition
  92.     echo "$swapPartition    swap    swap    defaults    0   0" >> /etc/fstab
  93.  
  94.   else
  95.  
  96.     error "the swap partition \"$swapPartition\" does not exist"
  97.  
  98.   fi
  99.  
  100. fi
  101.        
  102. # set the hostname
  103. /bin/busybox hostname -F /etc/hostname
  104.  
  105. # bring up a loopback device
  106. /bin/busybox ifconfig lo 127.0.0.1 up
  107. /bin/busybox route add -net 127.0.0.0 netmask 255.0.0.0 lo
  108.  
  109. . /etc/calfrc
  110.                  
  111. echo "Searching partitions for Calf files"
  112.  
  113. cd /sys/class/block
  114.  
  115. for partition in `/bin/busybox ls -1 s*[0-9]`; do
  116.  
  117.   /bin/busybox mkdir /mnt/$partition
  118.  
  119.   /bin/busybox mount /dev/$partition /mnt/$partition > /dev/null 2>&1
  120.  
  121.   case $? in
  122.  
  123.     "0") # if the partition mounting succeeded
  124.    
  125.       calfSfs=`/bin/busybox find /mnt/$partition -maxdepth 2 -type f -name calf-$calfVersion.sfs | /bin/busybox head -1`
  126.      
  127.       # if a calf sfs was found
  128.       if [ "$calfSfs" != "" ]; then
  129.  
  130.         # loaded the calf sfs
  131.         /usr/bin/loadsfs "$calfSfs" /mnt/calf/calfro > /dev/null 2>&1
  132.        
  133.         case $? in
  134.        
  135.           "0") # if the sfs loading succeeded
  136.  
  137.             # link /mnt/home to the partition mount point
  138.             /bin/busybox ln -s $partition /mnt/home
  139.            
  140.             # run udev to load the modules included in the sfs
  141.             echo "Loading drivers"
  142.             /sbin/udevd --daemon
  143.             /sbin/udevadm trigger
  144.             /sbin/udevadm settle
  145.      
  146.             # start a dhcp client on $interface, is the user passed the "dhcp" boot code
  147.             if [ "$interface" != "" ]; then
  148.  
  149.               if [ -d "/sys/class/net/$interface" ]; then
  150.  
  151.                 echo "Starting a DHCP client on $interface"
  152.                 /bin/busybox ifconfig $interface up
  153.                 /bin/busybox udhcpc -i $interface
  154.  
  155.                 case $? in
  156.  
  157.                   "1")
  158.  
  159.                     error "failed to start a DHCP client on the interface \"$interface\""
  160.                     ;;
  161.  
  162.                 esac
  163.  
  164.               else
  165.  
  166.                 error "the network interface \"$interface\" does not exist"
  167.  
  168.               fi
  169.  
  170.             fi
  171.    
  172.             # if not running live, load the extensions and the save file
  173.             if [ "$live" != "true" ]; then
  174.            
  175.               # the directory containing the sfs
  176.               calfDirectory=`/bin/busybox dirname $calfSfs`
  177.            
  178.               unset calfSfs
  179.            
  180.               # if the calf directory also contains extensions, load them
  181.               if [ -d "$calfDirectory/calf-extensions" ]; then
  182.  
  183.                 echo "Loading extensions"
  184.  
  185.                 for extension in `/bin/busybox find "$calfDirectory/calf-extensions" -name *.sfs -type f`; do
  186.  
  187.                   /usr/bin/loadsfs $extension
  188.  
  189.                 done
  190.  
  191.               fi
  192.            
  193.               # if it's a writeable partition, it must be XdY
  194.               case "${partition:1:1}" in
  195.              
  196.                 "d")
  197.                
  198.                   # if the calf directory does not contain a save file, offer the user to create one
  199.                   if [ ! -f "$calfDirectory/calfsave.img" ]; then
  200.  
  201.                     echo "No save file named \"calfsave.img\" found."
  202.  
  203.                     while [[ "$reply" != "n" && "$reply" != "y" ]]; do
  204.  
  205.                       read -p "Would you like to create a save file so your session is saved? (y/n) " reply
  206.  
  207.                     done
  208.  
  209.                     case $reply in
  210.  
  211.                       "y") # if the user chose to create a save file
  212.  
  213.                         while [ "$((size + 1))" -le "1" ]; do
  214.  
  215.                           read -p "Size (in MB): " size
  216.                  
  217.                         done
  218.  
  219.                         echo "Creating the save file"
  220.                         /bin/busybox dd if=/dev/zero of="$calfDirectory/calfsave.img" bs=1024 count=${size}000 > /dev/null 2>&1
  221.    
  222.                         case $? in
  223.    
  224.                           "0") # if the save file creation succeeded
  225.        
  226.                             echo "Creating a file system on the save file"
  227.                             /bin/busybox mkfs.ext2 -q -m 0 -F "$calfDirectory/calfsave.img" > /dev/null 2>&1
  228.        
  229.                             case $? in
  230.          
  231.                               "1") # if the file system creation failed
  232.          
  233.                                 if [ -f "$calfDirectory/calfsave.img" ]; then
  234.        
  235.                                   /bin/busybox rm "$calfDirectory/calfsave.img" > /dev/null 2>&1
  236.          
  237.                                 fi
  238.          
  239.                                 error "failed to create a save file"  
  240.          
  241.                             esac
  242.                             ;;
  243.        
  244.                           1) # if the save file creation failed
  245.  
  246.                             if [ -f "$calfDirectory/calfsave.img" ]; then
  247.        
  248.                               /bin/busybox rm "$calfDirectory/calfsave.img" > /dev/null 2>&1
  249.            
  250.                             fi
  251.        
  252.                             error "failed to create a save file"
  253.                             ;;
  254.                    
  255.                         esac
  256.                                      
  257.                     esac
  258.  
  259.                   fi
  260.            
  261.                   # check again whether a save file exists, in case it was created earlier
  262.                   if [ -f "$calfDirectory/calfsave.img" ]; then
  263.          
  264.                     echo "Loading the save file"
  265.                          
  266.                     /bin/busybox mkdir /mnt/calf/calfrw
  267.                     /bin/busybox mount "$calfDirectory/calfsave.img" /mnt/calf/calfrw -o loop > /dev/null 2>&1
  268.        
  269.                     case $? in
  270.          
  271.                       "0") # if the save file mounting succeeded
  272.  
  273.                         # link directories to the save file
  274.                         if [ ! -d /mnt/calf/calfrw/root ]; then
  275.              
  276.                           /bin/busybox mv /root /mnt/calf/calfrw
  277.  
  278.                         else
  279.  
  280.                           /bin/busybox rm -rf /root
  281.  
  282.                         fi
  283.  
  284.                         ln -s /mnt/calf/calfrw/root /root
  285.                  
  286.                         if [ ! -d /mnt/calf/calfrw/opt ]; then
  287.              
  288.                           /bin/busybox mv /opt /mnt/calf/calfrw
  289.  
  290.                         else
  291.  
  292.                           /bin/busybox rm -rf /opt
  293.  
  294.                         fi
  295.  
  296.                         ln -s /mnt/calf/calfrw/opt /opt
  297.                      
  298.                         if [ ! -d /mnt/calf/calfrw/usr/local ]; then
  299.              
  300.                           /bin/busybox mkdir /mnt/calf/calfrw/usr
  301.                           /bin/busybox mv /usr/local /mnt/calf/calfrw/usr
  302.  
  303.                         else
  304.  
  305.                           /bin/busybox rm -rf /usr/local
  306.  
  307.                         fi
  308.  
  309.                         ln -s /mnt/calf/calfrw/usr/local /usr/local
  310.                         ;;
  311.              
  312.                       1) # if the save file mounting failed
  313.              
  314.                         /bin/busybox rmdir /mnt/calf/calfrw
  315.              
  316.                         error "failed to mount the save file"
  317.                         ;;
  318.                  
  319.                     esac
  320.          
  321.                   fi
  322.                   ;;
  323.                  
  324.               esac
  325.              
  326.             fi
  327.             ;;
  328.          
  329.           1) # if the sfs loading failed
  330.          
  331.             error "failed to load calf-$calfVersion.sfs"
  332.             ;;
  333.            
  334.         esac
  335.        
  336.       fi
  337.  
  338.       /bin/busybox umount /mnt/$partition > /dev/null 2>&1
  339.  
  340.       case $? in
  341.  
  342.         "0") # if the partition unmounting succeeded
  343.      
  344.           /bin/busybox rmdir /mnt/$partition
  345.           ;;
  346.  
  347.       esac
  348.       ;;
  349.      
  350.     *) # if the partition mounting failed
  351.    
  352.       /bin/busybox rmdir /mnt/$partition
  353.       ;;
  354.  
  355.   esac
  356.  
  357. done
  358.  
  359. # fix some problematic permissions
  360. /bin/busybox chmod 4755 /bin/busybox
  361. /bin/busybox chown -R calf /home/calf
  362. /bin/busybox chmod 777 /home/calf
  363.  
  364. cd /etc/init.d
  365.  
  366. # start services (such as ALSA)
  367. for service in `/bin/busybox ls -1`; do
  368.  
  369.   if [[ "$service" != "rcS" && "$service" != "shutdown" ]]; then
  370.    
  371.     ./$service start
  372.    
  373.   fi
  374.  
  375. done
  376.      
  377. # start stuff in /root/Startup
  378. if [ -d /root/Startup ]; then
  379.  
  380.   cd /root/Startup
  381.  
  382.   for i in `/bin/busybox ls -1`; do
  383.  
  384.     ./$i &
  385.    
  386.   done
  387.  
  388. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement