Advertisement
Guest User

Untitled

a guest
May 9th, 2017
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 20.27 KB | None | 0 0
  1. # -*-Shell-script-*-
  2. #
  3. # functions This file contains functions to be used by most or all
  4. #       shell scripts in the /etc/init.d directory.
  5. #
  6.  
  7. TEXTDOMAIN=initscripts
  8.  
  9. # Make sure umask is sane
  10. umask 022
  11.  
  12. # Set up a default search path.
  13. PATH="/sbin:/usr/sbin:/bin:/usr/bin"
  14. export PATH
  15.  
  16. # Get a sane screen width
  17. [ -z "${COLUMNS:-}" ] && COLUMNS=80
  18.  
  19. [ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="$(/sbin/consoletype)"
  20.  
  21. if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" -a -z "${LANGSH_SOURCED:-}" ] ; then
  22.   . /etc/profile.d/lang.sh 2>/dev/null
  23.   # avoid propagating LANGSH_SOURCED any further
  24.   unset LANGSH_SOURCED
  25. fi
  26.  
  27. # Read in our configuration
  28. if [ -z "${BOOTUP:-}" ]; then
  29.   if [ -f /etc/sysconfig/init ]; then
  30.       . /etc/sysconfig/init
  31.   else
  32.     # This all seem confusing? Look in /etc/sysconfig/init,
  33.     # or in /usr/doc/initscripts-*/sysconfig.txt
  34.     BOOTUP=color
  35.     RES_COL=60
  36.     MOVE_TO_COL="echo -en \\033[${RES_COL}G"
  37.     SETCOLOR_SUCCESS="echo -en \\033[1;32m"
  38.     SETCOLOR_FAILURE="echo -en \\033[1;31m"
  39.     SETCOLOR_WARNING="echo -en \\033[1;33m"
  40.     SETCOLOR_NORMAL="echo -en \\033[0;39m"
  41.     LOGLEVEL=1
  42.   fi
  43.   if [ "$CONSOLETYPE" = "serial" ]; then
  44.       BOOTUP=serial
  45.       MOVE_TO_COL=
  46.       SETCOLOR_SUCCESS=
  47.       SETCOLOR_FAILURE=
  48.       SETCOLOR_WARNING=
  49.       SETCOLOR_NORMAL=
  50.   fi
  51. fi
  52.  
  53. # Interpret escape sequences in an fstab entry
  54. fstab_decode_str() {
  55.     fstab-decode echo "$1"
  56. }
  57.  
  58. # Check if any of $pid (could be plural) are running
  59. checkpid() {
  60.     local i
  61.  
  62.     for i in $* ; do
  63.         [ -d "/proc/$i" ] && return 0
  64.     done
  65.     return 1
  66. }
  67.  
  68. __readlink() {
  69.     ls -bl "$@" 2>/dev/null| awk '{ print $NF }'
  70. }
  71.  
  72. __fgrep() {
  73.     s=$1
  74.     f=$2
  75.     while read line; do
  76.     if strstr "$line" "$s"; then
  77.         echo $line
  78.         return 0
  79.     fi
  80.     done < $f
  81.     return 1
  82. }
  83.  
  84. # __umount_loop awk_program fstab_file first_msg retry_msg retry_umount_args
  85. # awk_program should process fstab_file and return a list of fstab-encoded
  86. # paths; it doesn't have to handle comments in fstab_file.
  87. __umount_loop() {
  88.     local remaining sig=
  89.     local retry=3 count
  90.  
  91.     remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  92.     while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  93.         if [ "$retry" -eq 3 ]; then
  94.             action "$3" fstab-decode umount $remaining
  95.         else
  96.             action "$4" fstab-decode umount $5 $remaining
  97.         fi
  98.         count=4
  99.         remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  100.         while [ "$count" -gt 0 ]; do
  101.             [ -z "$remaining" ] && break
  102.             count=$(($count-1))
  103.             usleep 500000
  104.             remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  105.         done
  106.         [ -z "$remaining" ] && break
  107.         kill $sig $(fstab-decode /sbin/fuser -m $remaining 2>/dev/null  | sed -e "s/\b$$\b//g") > /dev/null
  108.         sleep 3
  109.         retry=$(($retry -1))
  110.         sig=-9
  111.     done
  112. }
  113.  
  114. # Similar to __umount loop above, specialized for loopback devices
  115. __umount_loopback_loop() {
  116.     local remaining devremaining sig=
  117.     local retry=3
  118.  
  119.     remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
  120.     devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
  121.     while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  122.         if [ "$retry" -eq 3 ]; then
  123.             action $"Unmounting loopback filesystems: " \
  124.                 fstab-decode umount $remaining
  125.         else
  126.             action $"Unmounting loopback filesystems (retry):" \
  127.                 fstab-decode umount $remaining
  128.         fi
  129.         for dev in $devremaining ; do
  130.             losetup $dev > /dev/null 2>&1 && \
  131.                 action $"Detaching loopback device $dev: " \
  132.                 losetup -d $dev
  133.         done
  134.         remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
  135.         devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
  136.         [ -z "$remaining" ] && break
  137.         fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
  138.         sleep 3
  139.         retry=$(($retry -1))
  140.         sig=-9
  141.     done
  142. }
  143.  
  144. # __proc_pids {program} [pidfile]
  145. # Set $pid to pids from /var/run* for {program}.  $pid should be declared
  146. # local in the caller.
  147. # Returns LSB exit code for the 'status' action.
  148. __pids_var_run() {
  149.     local base=${1##*/}
  150.     local pid_file=${2:-/var/run/$base.pid}
  151.     local pid_dir=$(/usr/bin/dirname $pid_file)
  152.     local binary=$3
  153.  
  154.     [ -d "$pid_dir" -a ! -r "$pid_dir" ] && return 4
  155.  
  156.     pid=
  157.     if [ -f "$pid_file" ] ; then
  158.             local line p
  159.  
  160.         [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
  161.         while : ; do
  162.             read line
  163.             [ -z "$line" ] && break
  164.             for p in $line ; do
  165.                 if [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] ; then
  166.                     if [ -n "$binary" ] ; then
  167.                         local b=$(readlink /proc/$p/exe | sed -e 's/\s*(deleted)$//')
  168.                         [ "$b" != "$binary" ] && continue
  169.                     fi
  170.                     pid="$pid $p"
  171.                 fi
  172.             done
  173.         done < "$pid_file"
  174.  
  175.             if [ -n "$pid" ]; then
  176.                     return 0
  177.             fi
  178.         return 1 # "Program is dead and /var/run pid file exists"
  179.     fi
  180.     return 3 # "Program is not running"
  181. }
  182.  
  183. # Output PIDs of matching processes, found using pidof
  184. __pids_pidof() {
  185.     pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
  186.         pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
  187. }
  188.  
  189.  
  190. # A function to start a program.
  191. daemon() {
  192.     # Test syntax.
  193.     local gotbase= force= nicelevel corelimit
  194.     local pid base= user= nice= bg= pid_file=
  195.     local cgroup=
  196.     nicelevel=0
  197.     while [ "$1" != "${1##[-+]}" ]; do
  198.       case $1 in
  199.         '')    echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  200.                return 1;;
  201.         --check)
  202.            base=$2
  203.            gotbase="yes"
  204.            shift 2
  205.            ;;
  206.         --check=?*)
  207.                base=${1#--check=}
  208.            gotbase="yes"
  209.            shift
  210.            ;;
  211.         --user)
  212.            user=$2
  213.            shift 2
  214.            ;;
  215.         --user=?*)
  216.                user=${1#--user=}
  217.            shift
  218.            ;;
  219.         --pidfile)
  220.            pid_file=$2
  221.            shift 2
  222.            ;;
  223.         --pidfile=?*)
  224.            pid_file=${1#--pidfile=}
  225.            shift
  226.            ;;
  227.         --force)
  228.                force="force"
  229.            shift
  230.            ;;
  231.         [-+][0-9]*)
  232.                nice="nice -n $1"
  233.                shift
  234.            ;;
  235.         *)     echo $"$0: Usage: daemon [+/-nicelevel] {program}"
  236.                return 1;;
  237.       esac
  238.     done
  239.  
  240.         # Save basename.
  241.         [ -z "$gotbase" ] && base=${1##*/}
  242.  
  243.         # See if it's already running. Look *only* at the pid file.
  244.     __pids_var_run "$base" "$pid_file"
  245.  
  246.     [ -n "$pid" -a -z "$force" ] && return
  247.  
  248.     # make sure it doesn't core dump anywhere unless requested
  249.     corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
  250.    
  251.     # if they set NICELEVEL in /etc/sysconfig/foo, honor it
  252.     [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
  253.    
  254.     # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
  255.     if [ -n "${CGROUP_DAEMON}" ]; then
  256.         if [ ! -x /bin/cgexec ]; then
  257.             echo -n "Cgroups not installed"; warning
  258.             echo
  259.         else
  260.             cgroup="/bin/cgexec";
  261.             for i in $CGROUP_DAEMON; do
  262.                 cgroup="$cgroup -g $i";
  263.             done
  264.         fi
  265.     fi
  266.  
  267.     # Echo daemon
  268.         [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
  269.  
  270.     # And start it up.
  271.     if [ -z "$user" ]; then
  272.        $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
  273.     else
  274.        $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
  275.     fi
  276.  
  277.     [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
  278. }
  279.  
  280. # A function to stop a program.
  281. killproc() {
  282.     local RC killlevel= base pid pid_file= delay try binary=
  283.  
  284.     RC=0; delay=3; try=0
  285.     # Test syntax.
  286.     if [ "$#" -eq 0 ]; then
  287.         echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
  288.         return 1
  289.     fi
  290.     if [ "$1" = "-p" ]; then
  291.         pid_file=$2
  292.         shift 2
  293.     fi
  294.     if [ "$1" = "-b" ]; then
  295.         if [ -z $pid_file ]; then
  296.             echo $"-b option can be used only with -p"
  297.             echo $"Usage: killproc -p pidfile -b binary program"
  298.             return 1
  299.         fi
  300.         binary=$2
  301.         shift 2
  302.     fi
  303.     if [ "$1" = "-d" ]; then
  304.         delay=$(echo $2 | awk -v RS=' ' -v IGNORECASE=1 '{if($1!~/^[0-9.]+[smhd]?$/) exit 1;d=$1~/s$|^[0-9.]*$/?1:$1~/m$/?60:$1~/h$/?60*60:$1~/d$/?24*60*60:-1;if(d==-1) exit 1;delay+=d*$1} END {printf("%d",delay+0.5)}')
  305.         if [ "$?" -eq 1 ]; then
  306.             echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
  307.             return 1
  308.         fi
  309.         shift 2
  310.     fi
  311.        
  312.  
  313.     # check for second arg to be kill level
  314.     [ -n "${2:-}" ] && killlevel=$2
  315.  
  316.         # Save basename.
  317.         base=${1##*/}
  318.  
  319.         # Find pid.
  320.     __pids_var_run "$1" "$pid_file" "$binary"
  321.     RC=$?
  322.     if [ -z "$pid" ]; then
  323.         if [ -z "$pid_file" ]; then
  324.             pid="$(__pids_pidof "$1")"
  325.         else
  326.             [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
  327.         fi
  328.     fi
  329.  
  330.         # Kill it.
  331.         if [ -n "$pid" ] ; then
  332.                 [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
  333.         if [ -z "$killlevel" ] ; then
  334.                if checkpid $pid 2>&1; then
  335.                # TERM first, then KILL if not dead
  336.                kill -TERM $pid >/dev/null 2>&1
  337.                usleep 100000
  338.                if checkpid $pid ; then
  339.                 try=0
  340.                 while [ $try -lt $delay ] ; do
  341.                     checkpid $pid || break
  342.                     sleep 1
  343.                     let try+=1
  344.                 done
  345.                 if checkpid $pid ; then
  346.                     kill -KILL $pid >/dev/null 2>&1
  347.                     usleep 100000
  348.                 fi
  349.                fi
  350.                 fi
  351.             checkpid $pid
  352.             RC=$?
  353.             [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
  354.             RC=$((! $RC))
  355.         # use specified level only
  356.         else
  357.                 if checkpid $pid; then
  358.                         kill $killlevel $pid >/dev/null 2>&1
  359.                 RC=$?
  360.                 [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
  361.             elif [ -n "${LSB:-}" ]; then
  362.                 RC=7 # Program is not running
  363.             fi
  364.         fi
  365.     else
  366.         if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
  367.             RC=7 # Program is not running
  368.         else
  369.             failure $"$base shutdown"
  370.             RC=0
  371.         fi
  372.     fi
  373.  
  374.         # Remove pid file if any.
  375.     if [ -z "$killlevel" ]; then
  376.             rm -f "${pid_file:-/var/run/$base.pid}"
  377.     fi
  378.     return $RC
  379. }
  380.  
  381. # A function to find the pid of a program. Looks *only* at the pidfile
  382. pidfileofproc() {
  383.     local pid
  384.  
  385.     # Test syntax.
  386.     if [ "$#" = 0 ] ; then
  387.         echo $"Usage: pidfileofproc {program}"
  388.         return 1
  389.     fi
  390.  
  391.     __pids_var_run "$1"
  392.     [ -n "$pid" ] && echo $pid
  393.     return 0
  394. }
  395.  
  396. # A function to find the pid of a program.
  397. pidofproc() {
  398.     local RC pid pid_file=
  399.  
  400.     # Test syntax.
  401.     if [ "$#" = 0 ]; then
  402.         echo $"Usage: pidofproc [-p pidfile] {program}"
  403.         return 1
  404.     fi
  405.     if [ "$1" = "-p" ]; then
  406.         pid_file=$2
  407.         shift 2
  408.     fi
  409.     fail_code=3 # "Program is not running"
  410.  
  411.     # First try "/var/run/*.pid" files
  412.     __pids_var_run "$1" "$pid_file"
  413.     RC=$?
  414.     if [ -n "$pid" ]; then
  415.         echo $pid
  416.         return 0
  417.     fi
  418.  
  419.     [ -n "$pid_file" ] && return $RC
  420.     __pids_pidof "$1" || return $RC
  421. }
  422.  
  423. status() {
  424.     local base pid lock_file= pid_file= binary=
  425.  
  426.     # Test syntax.
  427.     if [ "$#" = 0 ] ; then
  428.         echo $"Usage: status [-p pidfile] {program}"
  429.         return 1
  430.     fi
  431.     if [ "$1" = "-p" ]; then
  432.         pid_file=$2
  433.         shift 2
  434.     fi
  435.     if [ "$1" = "-l" ]; then
  436.         lock_file=$2
  437.         shift 2
  438.     fi
  439.     if [ "$1" = "-b" ]; then
  440.         if [ -z $pid_file ]; then
  441.             echo $"-b option can be used only with -p"
  442.             echo $"Usage: status -p pidfile -b binary program"
  443.             return 1
  444.         fi
  445.         binary=$2
  446.         shift 2
  447.     fi
  448.     base=${1##*/}
  449.  
  450.     # First try "pidof"
  451.     __pids_var_run "$1" "$pid_file" "$binary"
  452.     RC=$?
  453.     if [ -z "$pid_file" -a -z "$pid" ]; then
  454.         pid="$(__pids_pidof "$1")"
  455.     fi
  456.     if [ -n "$pid" ]; then
  457.             echo $"${base} (pid $pid) is running..."
  458.             return 0
  459.     fi
  460.  
  461.     case "$RC" in
  462.         0)
  463.             echo $"${base} (pid $pid) is running..."
  464.             return 0
  465.             ;;
  466.         1)
  467.                     echo $"${base} dead but pid file exists"
  468.                     return 1
  469.             ;;
  470.         4)
  471.             echo $"${base} status unknown due to insufficient privileges."
  472.             return 4
  473.             ;;
  474.     esac
  475.     if [ -z "${lock_file}" ]; then
  476.         lock_file=${base}
  477.     fi
  478.     # See if /var/lock/subsys/${lock_file} exists
  479.     if [ -f /var/lock/subsys/${lock_file} ]; then
  480.         echo $"${base} dead but subsys locked"
  481.         return 2
  482.     fi
  483.     echo $"${base} is stopped"
  484.     return 3
  485. }
  486.  
  487. echo_success() {
  488.   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  489.   echo -n "["
  490.   [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  491.   echo -n $"  OK  "
  492.   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  493.   echo -n "]"
  494.   echo -ne "\r"
  495.   return 0
  496. }
  497.  
  498. echo_failure() {
  499.   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  500.   echo -n "["
  501.   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  502.   echo -n $"FAILED"
  503.   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  504.   echo -n "]"
  505.   echo -ne "\r"
  506.   return 1
  507. }
  508.  
  509. echo_passed() {
  510.   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  511.   echo -n "["
  512.   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  513.   echo -n $"PASSED"
  514.   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  515.   echo -n "]"
  516.   echo -ne "\r"
  517.   return 1
  518. }
  519.  
  520. echo_warning() {
  521.   [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  522.   echo -n "["
  523.   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  524.   echo -n $"WARNING"
  525.   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  526.   echo -n "]"
  527.   echo -ne "\r"
  528.   return 1
  529. }
  530.  
  531. # Inform the graphical boot of our current state
  532. update_boot_stage() {
  533.   if [ -x /bin/plymouth ]; then
  534.       /bin/plymouth --update="$1"
  535.   fi
  536.   return 0
  537. }
  538.  
  539. # Log that something succeeded
  540. success() {
  541.   [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
  542.   return 0
  543. }
  544.  
  545. # Log that something failed
  546. failure() {
  547.   local rc=$?
  548.   [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
  549.   [ -x /bin/plymouth ] && /bin/plymouth --details
  550.   return $rc
  551. }
  552.  
  553. # Log that something passed, but may have had errors. Useful for fsck
  554. passed() {
  555.   local rc=$?
  556.   [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
  557.   return $rc
  558. }  
  559.  
  560. # Log a warning
  561. warning() {
  562.   local rc=$?
  563.   [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
  564.   return $rc
  565. }  
  566.  
  567. # Run some action. Log its output.
  568. action() {
  569.   local STRING rc
  570.  
  571.   STRING=$1
  572.   echo -n "$STRING "
  573.   shift
  574.   "$@" && success $"$STRING" || failure $"$STRING"
  575.   rc=$?
  576.   echo
  577.   return $rc
  578. }
  579.  
  580. # Run some action. Silently.
  581. action_silent() {
  582.   local STRING rc
  583.  
  584.   STRING=$1
  585.   echo -n "$STRING "
  586.   shift
  587.   "$@" >/dev/null && success $"$STRING" || failure $"$STRING"
  588.   rc=$?
  589.   echo
  590.   return $rc
  591. }
  592.  
  593. # returns OK if $1 contains $2
  594. strstr() {
  595.   [ "${1#*$2*}" = "$1" ] && return 1
  596.   return 0
  597. }
  598.  
  599. # Confirm whether we really want to run this service
  600. confirm() {
  601.   [ -x /bin/plymouth ] && /bin/plymouth --hide-splash
  602.   while : ; do
  603.       echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
  604.       read answer
  605.       if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
  606.          return 0
  607.       elif strstr $"cC" "$answer" ; then
  608.      rm -f /var/run/confirm
  609.      [ -x /bin/plymouth ] && /bin/plymouth --show-splash
  610.          return 2
  611.       elif strstr $"nN" "$answer" ; then
  612.          return 1
  613.       fi
  614.   done
  615. }
  616.  
  617. # resolve a device node to its major:minor numbers in decimal or hex
  618. get_numeric_dev() {
  619. (
  620.     fmt="%d:%d"
  621.     if [ "$1" == "hex" ]; then
  622.         fmt="%x:%x"
  623.     fi
  624.     ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
  625. ) 2>/dev/null
  626. }
  627.  
  628. # Check whether file $1 is a backup or rpm-generated file and should be ignored
  629. is_ignored_file() {
  630.     case "$1" in
  631.     *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
  632.         return 0
  633.         ;;
  634.     esac
  635.     return 1
  636. }
  637.  
  638. # Evaluate shvar-style booleans
  639. is_true() {
  640.     case "$1" in
  641.     [tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
  642.     return 0
  643.     ;;
  644.     esac
  645.     return 1
  646. }
  647.  
  648. # Evaluate shvar-style booleans
  649. is_false() {
  650.     case "$1" in
  651.     [fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
  652.     return 0
  653.     ;;
  654.     esac
  655.     return 1
  656. }
  657.  
  658. # Apply sysctl settings, including files in /etc/sysctl.d
  659. apply_sysctl() {
  660.     sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1
  661.     for file in /etc/sysctl.d/* ; do
  662.         is_ignored_file "$file" && continue
  663.         test -f "$file" && sysctl -e -p "$file" >/dev/null 2>&1
  664.     done
  665. }
  666.  
  667. key_is_random() {
  668.     [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" \
  669.     -o "$1" = "/dev/random" ]
  670. }
  671.  
  672. find_crypto_mount_point() {
  673.     local fs_spec fs_file fs_vfstype remaining_fields
  674.     local fs
  675.     while read fs_spec fs_file remaining_fields; do
  676.     if [ "$fs_spec" = "/dev/mapper/$1" ]; then
  677.         echo $fs_file
  678.         break;
  679.     fi
  680.     done < /etc/fstab
  681. }
  682.  
  683. # Because of a chicken/egg problem, init_crypto must be run twice.  /var may be
  684. # encrypted but /var/lib/random-seed is needed to initialize swap.
  685. init_crypto() {
  686.     local have_random dst src key opt mode owner params makeswap skip arg opt
  687.     local param value rc ret mke2fs mdir prompt mount_point
  688.  
  689.     ret=0
  690.     have_random=$1
  691.     while read dst src key opt; do
  692.     [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
  693.         [ -b "/dev/mapper/$dst" ] && continue;
  694.     if [ "$have_random" = 0 ] && key_is_random "$key"; then
  695.         continue
  696.     fi
  697.     if [ -n "$key" -a "x$key" != "xnone" ]; then
  698.         if test -e "$key" ; then
  699.         owner=$(ls -l $key | (read a b owner rest; echo $owner))
  700.         if ! key_is_random "$key"; then
  701.             mode=$(ls -l "$key" | cut -c 5-10)
  702.             if [ "$mode" != "------" ]; then
  703.                echo $"INSECURE MODE FOR $key"
  704.             fi
  705.         fi
  706.         if [ "$owner" != root ]; then
  707.             echo $"INSECURE OWNER FOR $key"
  708.         fi
  709.         else
  710.         echo $"Key file for $dst not found, skipping"
  711.         ret=1
  712.         continue
  713.         fi
  714.     else
  715.         key=""
  716.     fi
  717.     params=""
  718.     makeswap=""
  719.     mke2fs=""
  720.     skip=""
  721.     # Parse the src field for UUID= and convert to real device names
  722.     if [ "${src%%=*}" == "UUID" ]; then
  723.         src=$(/sbin/blkid -t "$src" -l -o device)
  724.     elif [ "${src/^\/dev\/disk\/by-uuid\/}" != "$src" ]; then
  725.         src=$(__readlink $src)
  726.     fi
  727.     # Is it a block device?
  728.     [ -b "$src" ] || continue
  729.     # Is it already a device mapper slave? (this is gross)
  730.     devesc=${src##/dev/}
  731.     devesc=${devesc//\//!}
  732.     for d in /sys/block/dm-*/slaves ; do
  733.         [ -e $d/$devesc ] && continue 2
  734.     done
  735.     # Parse the options field, convert to cryptsetup parameters and
  736.     # contruct the command line
  737.     while [ -n "$opt" ]; do
  738.         arg=${opt%%,*}
  739.         opt=${opt##$arg}
  740.         opt=${opt##,}
  741.         param=${arg%%=*}
  742.         value=${arg##$param=}
  743.  
  744.         case "$param" in
  745.         cipher)
  746.         params="$params -c $value"
  747.         if [ -z "$value" ]; then
  748.             echo $"$dst: no value for cipher option, skipping"
  749.             skip="yes"
  750.         fi
  751.         ;;
  752.         size)
  753.         params="$params -s $value"
  754.         if [ -z "$value" ]; then
  755.             echo $"$dst: no value for size option, skipping"
  756.             skip="yes"
  757.         fi
  758.         ;;
  759.         hash)
  760.         params="$params -h $value"
  761.         if [ -z "$value" ]; then
  762.             echo $"$dst: no value for hash option, skipping"
  763.             skip="yes"
  764.         fi
  765.         ;;
  766.         verify)
  767.             params="$params -y"
  768.         ;;
  769.         swap)
  770.         makeswap=yes
  771.         ;;
  772.         tmp)
  773.         mke2fs=yes
  774.         esac
  775.     done
  776.     if [ "$skip" = "yes" ]; then
  777.         ret=1
  778.         continue
  779.     fi
  780.     if [ -z "$makeswap" ] && cryptsetup isLuks "$src" 2>/dev/null ; then
  781.         if key_is_random "$key"; then
  782.         echo $"$dst: LUKS requires non-random key, skipping"
  783.         ret=1
  784.         continue
  785.         fi
  786.         if [ -n "$params" ]; then
  787.         echo "$dst: options are invalid for LUKS partitions," \
  788.             "ignoring them"
  789.         fi
  790.         if [ -n "$key" ]; then
  791.         /sbin/cryptsetup -d $key luksOpen "$src" "$dst" <&1 2>/dev/null && success || failure
  792.         rc=$?
  793.         else
  794.         mount_point="$(find_crypto_mount_point $dst)"
  795.         [ -n "$mount_point" ] || mount_point=${src##*/}
  796.         prompt=$(printf $"%s is password protected" "$mount_point")
  797.                 if [ -x /usr/bin/plymouth ] ; then
  798.             plymouth ask-for-password --prompt "$prompt" --command="/sbin/cryptsetup luksOpen -T1 $src $dst" <&1
  799.                 else
  800.                     /sbin/cryptsetup luksOpen -T1 --key-file=- $src $dst
  801.                 fi
  802.         rc=$?
  803.         fi
  804.     else
  805.         [ -z "$key" ] && [ -x /usr/bin/plymouth ] && plymouth --hide-splash
  806.         /sbin/cryptsetup $params ${key:+-d $key} create "$dst" "$src" <&1 2>/dev/null && success || failure
  807.         rc=$?
  808.         [ -z "$key" ] && [ -x /usr/bin/plymouth ] && plymouth --show-splash
  809.     fi
  810.     if [ $rc -ne 0 ]; then
  811.         ret=1
  812.         continue
  813.     fi
  814.     if [ -b "/dev/mapper/$dst" ]; then
  815.         if [ "$makeswap" = "yes" ]; then
  816.         mkswap "/dev/mapper/$dst" 2>/dev/null >/dev/null
  817.         fi
  818.         if [ "$mke2fs" = "yes" ]; then
  819.         if mke2fs "/dev/mapper/$dst" 2>/dev/null >/dev/null \
  820.             && mdir=$(mktemp -d /tmp/mountXXXXXX); then
  821.             mount "/dev/mapper/$dst" "$mdir" && chmod 1777 "$mdir"
  822.             umount "$mdir"
  823.             rmdir "$mdir"
  824.         fi
  825.         fi
  826.     fi
  827.     done < /etc/crypttab
  828.     return $ret
  829. }
  830.  
  831. # A sed expression to filter out the files that is_ignored_file recognizes
  832. __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
  833.  
  834. #if we have privileges lets log to kmsg, otherwise to stderr
  835. if strstr "$(cat /proc/cmdline)" "rc.debug"; then
  836.         [ -w /dev/kmsg ] && exec 30>/dev/kmsg && BASH_XTRACEFD=30
  837.         set -x
  838. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement