Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.78 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. __kill_pids_term_kill_checkpids() {
  85. local base_stime=$1
  86. shift 1
  87. local pid=
  88. local pids=$*
  89. local remaining=
  90. local stat=
  91. local stime=
  92.  
  93. for pid in $pids ; do
  94. [ -e "/proc/$pid" ] || continue
  95. read -r line < "/proc/$pid/stat" 2> /dev/null || continue
  96.  
  97. stat=($line)
  98. stime=${stat[21]}
  99.  
  100. [ -n "$stime" ] && [ "$base_stime" -lt "$stime" ] && continue
  101. remaining+="$pid "
  102. done
  103.  
  104. echo "$remaining"
  105. [ -n "$remaining" ] && return 1
  106.  
  107. return 0
  108. }
  109.  
  110. __kill_pids_term_kill() {
  111. local try=0
  112. local delay=3;
  113. local pid=
  114. local stat=($(< /proc/self/stat))
  115. local base_stime=${stat[21]}
  116.  
  117. if [ "$1" = "-d" ]; then
  118. delay=$2
  119. shift 2
  120. fi
  121.  
  122. local kill_list=$*
  123.  
  124. kill_list=$(__kill_pids_term_kill_checkpids $base_stime $kill_list)
  125.  
  126. [ -z "$kill_list" ] && return 0
  127.  
  128. kill -TERM $kill_list >/dev/null 2>&1
  129. usleep 100000
  130.  
  131. kill_list=$(__kill_pids_term_kill_checkpids $base_stime $kill_list)
  132. if [ -n "$kill_list" ] ; then
  133. while [ $try -lt $delay ] ; do
  134. sleep 1
  135. kill_list=$(__kill_pids_term_kill_checkpids $base_stime $kill_list)
  136. [ -z "$kill_list" ] && break
  137. let try+=1
  138. done
  139. if [ -n "$kill_list" ] ; then
  140. kill -KILL $kill_list >/dev/null 2>&1
  141. usleep 100000
  142. kill_list=$(__kill_pids_term_kill_checkpids $base_stime $kill_list)
  143. fi
  144. fi
  145.  
  146. [ -n "$kill_list" ] && return 1
  147. return 0
  148. }
  149.  
  150. # __umount_loop awk_program fstab_file first_msg retry_msg retry_umount_args
  151. # awk_program should process fstab_file and return a list of fstab-encoded
  152. # paths; it doesn't have to handle comments in fstab_file.
  153. __umount_loop() {
  154. local remaining sig=
  155. local retry=3 count
  156.  
  157. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  158. while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  159. if [ "$retry" -eq 3 ]; then
  160. action "$3" fstab-decode umount $remaining
  161. else
  162. action "$4" fstab-decode umount $5 $remaining
  163. fi
  164. count=4
  165. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  166. while [ "$count" -gt 0 ]; do
  167. [ -z "$remaining" ] && break
  168. count=$(($count-1))
  169. usleep 500000
  170. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  171. done
  172. [ -z "$remaining" ] && break
  173. kill $sig $(fstab-decode /sbin/fuser -m $remaining 2>/dev/null | sed -e "s/\b$$\b//g") > /dev/null
  174. sleep 3
  175. retry=$(($retry -1))
  176. sig=-9
  177. done
  178. }
  179.  
  180. # Similar to __umount loop above, without calling fuser
  181. __umount_loop_2() {
  182. local remaining=
  183. local count
  184. local kill_list
  185.  
  186. #call regular umount
  187. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  188. action "$3" fstab-decode umount $remaining
  189.  
  190. count=4
  191. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  192. while [ "$count" -gt 0 ]; do
  193. [ -z "$remaining" ] && break
  194. count=$(($count-1))
  195. usleep 500000
  196. remaining=$(LC_ALL=C awk "/^#/ {next} $1" "$2" | sort -r)
  197. done
  198. [ -z "$remaining" ] && return 0
  199.  
  200. devs=$(stat -c "%d" $remaining)
  201. action "$4" fstab-decode umount "-l" $remaining
  202.  
  203. # find fds that don't start with /, are not sockets or pipes or other.
  204. # these are potentially detached fds
  205. detached_fds=$(find /proc/ -regex '/proc/[0-9]+/fd/.*' -printf "%p %l\n" 2>/dev/null |\
  206. grep -Ev '/proc/[0-9]+/fd/[0-9]+ (/.*|inotify|\[.+\]|(socket|pipe):\[[0-9]+\])')
  207.  
  208. # check each detached fd to see if it has the same device
  209. # as one of our lazy umounted filesystems
  210. kill_list=
  211. [ -n "$detached_fds" ] && while read fdline; do
  212. fd=${fdline%% *}
  213. pid=$(echo $fdline | sed -r 's/\/proc\/([0-9]+).+/\1/')
  214. fd_dev=$(stat -L -c "%d" $fd)
  215. for dev in $devs ; do
  216. [ "$dev" = "$fd_dev" ] && kill_list+="$pid "
  217. done
  218. done <<< "$detached_fds"
  219.  
  220. if [ -n "$kill_list" ] ; then
  221. STRING=$"Killing processes with open filedescriptors on the unmounted disk:"
  222. __kill_pids_term_kill $kill_list && success "$STRING" || failure "$STRING"
  223. echo
  224. fi
  225. }
  226.  
  227. __source_netdevs_fstab() {
  228. NFSFSTAB=$(LC_ALL=C awk '!/^#/ && $3 ~ /^nfs/ && $3 != "nfsd" && $4 !~ /noauto/ { print $2 }' /etc/fstab)
  229. CIFSFSTAB=$(LC_ALL=C awk '!/^#/ && $3 == "cifs" && $4 !~ /noauto/ { print $2 }' /etc/fstab)
  230. NCPFSTAB=$(LC_ALL=C awk '!/^#/ && $3 == "ncpfs" && $4 !~ /noauto/ { print $2 }' /etc/fstab)
  231. GLUSTERFSFSTAB=$(LC_ALL=C awk '!/^#/ && $3 == "glusterfs" && $4 !~ /noauto/ { print $2 }' /etc/fstab)
  232. NETDEVFSTAB=$(LC_ALL=C awk '!/^#/ && $4 ~/_netdev/ && $4 !~ /noauto/ { print $1 }' /etc/fstab)
  233. }
  234.  
  235. __source_netdevs_mtab() {
  236. NFSMTAB=$(LC_ALL=C awk '$3 ~ /^nfs/ && $3 != "nfsd" && $2 != "/" { print $2 }' /proc/mounts)
  237. CIFSMTAB=$(LC_ALL=C awk '$3 == "cifs" { print $2 }' /proc/mounts)
  238. NCPMTAB=$(LC_ALL=C awk '$3 == "ncpfs" { print $2 }' /proc/mounts)
  239. GLUSTERFSMTAB=$(LC_ALL=C awk '$3 == "fuse.glusterfs" { print $2 }' /proc/mounts)
  240. NETDEVMTAB=$(LC_ALL=C awk '$4 ~ /_netdev/ && $2 != "/" { print $2 }' /etc/mtab)
  241.  
  242. ALLNETDEVMTAB="$NFSMTAB $CIFSMTAB $NCPMTAB $GLUSTERFSMTAB $NETDEVMTAB"
  243. }
  244.  
  245. # Similar to __umount loop above, specialized for loopback devices
  246. __umount_loopback_loop() {
  247. local remaining devremaining sig=
  248. local retry=3
  249.  
  250. __find_mounts() {
  251. if [ "$1" = "--netdev" ] ; then
  252. __source_netdevs_mtab
  253. remaining=
  254. devremaining=
  255. local mount= netdev= _rest
  256. while read -r dev mount _rest ; do
  257. [ "$dev" = "${dev##/dev/loop}" ] && continue
  258. local back_file=$(losetup $dev | sed -e 's/^\/dev\/loop[0-9]\+: \[[0-9a-f]\+\]:[0-9]\+ (\(.*\))$/\1/')
  259. for netdev in $ALLNETDEVMTAB ; do
  260. local netdev_decoded=
  261. netdev="${netdev}/"
  262. netdev_decoded=$(fstab_decode_str ${netdev})
  263. if [ "$mount" != "${mount##$netdev}" ] || [ "$back_file" != "${back_file##$netdev_decoded}" ] ; then
  264. remaining="$remaining $mount"
  265. #device might be mounted in other location,
  266. #but then losetup -d will be noop, so meh
  267. devremaining="$devremaining $dev"
  268. continue 2
  269. fi
  270. done
  271. done < /proc/mounts
  272. else
  273. remaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts)
  274. devremaining=$(awk '$1 ~ /^\/dev\/loop/ && $2 != "/" {print $1}' /proc/mounts)
  275. fi
  276. }
  277.  
  278. __find_mounts $1
  279.  
  280. while [ -n "$remaining" -a "$retry" -gt 0 ]; do
  281. if [ "$retry" -eq 3 ]; then
  282. action $"Unmounting loopback filesystems: " \
  283. fstab-decode umount $remaining
  284. else
  285. action $"Unmounting loopback filesystems (retry):" \
  286. fstab-decode umount $remaining
  287. fi
  288.  
  289. for dev in $devremaining ; do
  290. if [ "$1" = "--netdev" ] ; then
  291. #some loopdevices might be mounted on top of non-netdev
  292. #so ignore failures
  293. losetup -d $dev > /dev/null 2>&1
  294. else
  295. losetup $dev > /dev/null 2>&1 && \
  296. action $"Detaching loopback device $dev: " \
  297. losetup -d $dev
  298. fi
  299. done
  300. #check what is still mounted
  301. __find_mounts $1
  302. [ -z "$remaining" ] && break
  303. fstab-decode /sbin/fuser -k -m $sig $remaining >/dev/null
  304. sleep 3
  305. retry=$(($retry -1))
  306. sig=-9
  307. done
  308. }
  309.  
  310. # __proc_pids {program} [pidfile]
  311. # Set $pid to pids from /var/run* for {program}. $pid should be declared
  312. # local in the caller.
  313. # Returns LSB exit code for the 'status' action.
  314. __pids_var_run() {
  315. local base=${1##*/}
  316. local pid_file=${2:-/var/run/$base.pid}
  317. local pid_dir=$(/usr/bin/dirname $pid_file)
  318. local binary=$3
  319.  
  320. [ -d "$pid_dir" -a ! -r "$pid_dir" ] && return 4
  321.  
  322. pid=
  323. if [ -f "$pid_file" ] ; then
  324. local line p
  325.  
  326. [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"
  327. while : ; do
  328. read line
  329. [ -z "$line" ] && break
  330. for p in $line ; do
  331. if [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] ; then
  332. if [ -n "$binary" ] ; then
  333. local b=$(readlink /proc/$p/exe | sed -e 's/\s*(deleted)$//')
  334. [ "$b" != "$binary" ] && continue
  335. fi
  336. pid="$pid $p"
  337. fi
  338. done
  339. done < "$pid_file"
  340.  
  341. if [ -n "$pid" ]; then
  342. return 0
  343. fi
  344. return 1 # "Program is dead and /var/run pid file exists"
  345. fi
  346. return 3 # "Program is not running"
  347. }
  348.  
  349. # Output PIDs of matching processes, found using pidof
  350. __pids_pidof() {
  351. pidof -c -m -o $$ -o $PPID -o %PPID -x "$1" || \
  352. pidof -c -m -o $$ -o $PPID -o %PPID -x "${1##*/}"
  353. }
  354.  
  355.  
  356. # A function to start a program.
  357. daemon() {
  358. # Test syntax.
  359. local gotbase= force= nicelevel corelimit
  360. local pid base= user= nice= bg= pid_file=
  361. local cgroup=
  362. nicelevel=0
  363. while [ "$1" != "${1##[-+]}" ]; do
  364. case $1 in
  365. '') echo $"$0: Usage: daemon [+/-nicelevel] {program}" "[arg1]..."
  366. return 1;;
  367. --check)
  368. base=$2
  369. gotbase="yes"
  370. shift 2
  371. ;;
  372. --check=?*)
  373. base=${1#--check=}
  374. gotbase="yes"
  375. shift
  376. ;;
  377. --user)
  378. user=$2
  379. shift 2
  380. ;;
  381. --user=?*)
  382. user=${1#--user=}
  383. shift
  384. ;;
  385. --pidfile)
  386. pid_file=$2
  387. shift 2
  388. ;;
  389. --pidfile=?*)
  390. pid_file=${1#--pidfile=}
  391. shift
  392. ;;
  393. --force)
  394. force="force"
  395. shift
  396. ;;
  397. [-+][0-9]*)
  398. nice="nice -n $1"
  399. shift
  400. ;;
  401. *) echo $"$0: Usage: daemon [+/-nicelevel] {program}" "[arg1]..."
  402. return 1;;
  403. esac
  404. done
  405.  
  406. # Save basename.
  407. [ -z "$gotbase" ] && base=${1##*/}
  408.  
  409. # See if it's already running. Look *only* at the pid file.
  410. __pids_var_run "$base" "$pid_file"
  411.  
  412. [ -n "$pid" -a -z "$force" ] && return
  413.  
  414. # make sure it doesn't core dump anywhere unless requested
  415. corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
  416.  
  417. # if they set NICELEVEL in /etc/sysconfig/foo, honor it
  418. [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
  419.  
  420. # if they set CGROUP_DAEMON in /etc/sysconfig/foo, honor it
  421. if [ -n "${CGROUP_DAEMON}" ]; then
  422. if [ ! -x /bin/cgexec ]; then
  423. echo -n "Cgroups not installed"; warning
  424. echo
  425. else
  426. cgroup="/bin/cgexec";
  427. for i in $CGROUP_DAEMON; do
  428. cgroup="$cgroup -g $i";
  429. done
  430. fi
  431. fi
  432.  
  433. # Echo daemon
  434. [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
  435.  
  436. # And start it up.
  437. if [ -z "$user" ]; then
  438. $cgroup $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"
  439. else
  440. $cgroup $nice runuser -s /bin/bash $user -c "$corelimit >/dev/null 2>&1 ; $*"
  441. fi
  442.  
  443. [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup"
  444. }
  445.  
  446. # A function to stop a program.
  447. killproc() {
  448. local RC killlevel= base pid pid_file= delay try binary=
  449.  
  450. RC=0; delay=3; try=0
  451. # Test syntax.
  452. if [ "$#" -eq 0 ]; then
  453. echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
  454. return 1
  455. fi
  456. if [ "$1" = "-p" ]; then
  457. pid_file=$2
  458. shift 2
  459. fi
  460. if [ "$1" = "-b" ]; then
  461. if [ -z $pid_file ]; then
  462. echo $"-b option can be used only with -p"
  463. echo $"Usage: killproc -p pidfile -b binary program"
  464. return 1
  465. fi
  466. binary=$2
  467. shift 2
  468. fi
  469. if [ "$1" = "-d" ]; then
  470. 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)}')
  471. if [ "$?" -eq 1 ]; then
  472. echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
  473. return 1
  474. fi
  475. shift 2
  476. fi
  477.  
  478.  
  479. # check for second arg to be kill level
  480. [ -n "${2:-}" ] && killlevel=$2
  481.  
  482. # Save basename.
  483. base=${1##*/}
  484.  
  485. # Find pid.
  486. __pids_var_run "$1" "$pid_file" "$binary"
  487. RC=$?
  488. if [ -z "$pid" ]; then
  489. if [ -z "$pid_file" ]; then
  490. pid="$(__pids_pidof "$1")"
  491. else
  492. [ "$RC" = "4" ] && { failure $"$base shutdown" ; return $RC ;}
  493. fi
  494. fi
  495.  
  496. # Kill it.
  497. if [ -n "$pid" ] ; then
  498. [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
  499. if [ -z "$killlevel" ] ; then
  500. __kill_pids_term_kill -d $delay $pid
  501. RC=$?
  502. [ "$RC" -eq 0 ] && success $"$base shutdown" || failure $"$base shutdown"
  503. # use specified level only
  504. else
  505. if checkpid $pid; then
  506. kill $killlevel $pid >/dev/null 2>&1
  507. RC=$?
  508. [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
  509. elif [ -n "${LSB:-}" ]; then
  510. RC=7 # Program is not running
  511. fi
  512. fi
  513. else
  514. if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
  515. RC=7 # Program is not running
  516. else
  517. failure $"$base shutdown"
  518. RC=0
  519. fi
  520. fi
  521.  
  522. # Remove pid file if any.
  523. if [ -z "$killlevel" ]; then
  524. rm -f "${pid_file:-/var/run/$base.pid}"
  525. fi
  526. return $RC
  527. }
  528.  
  529. # A function to find the pid of a program. Looks *only* at the pidfile
  530. pidfileofproc() {
  531. local pid
  532.  
  533. # Test syntax.
  534. if [ "$#" = 0 ] ; then
  535. echo $"Usage: pidfileofproc {program}"
  536. return 1
  537. fi
  538.  
  539. __pids_var_run "$1"
  540. [ -n "$pid" ] && echo $pid
  541. return 0
  542. }
  543.  
  544. # A function to find the pid of a program.
  545. pidofproc() {
  546. local RC pid pid_file=
  547.  
  548. # Test syntax.
  549. if [ "$#" = 0 ]; then
  550. echo $"Usage: pidofproc [-p pidfile] {program}"
  551. return 1
  552. fi
  553. if [ "$1" = "-p" ]; then
  554. pid_file=$2
  555. shift 2
  556. fi
  557. fail_code=3 # "Program is not running"
  558.  
  559. # First try "/var/run/*.pid" files
  560. __pids_var_run "$1" "$pid_file"
  561. RC=$?
  562. if [ -n "$pid" ]; then
  563. echo $pid
  564. return 0
  565. fi
  566.  
  567. [ -n "$pid_file" ] && return $RC
  568. __pids_pidof "$1" || return $RC
  569. }
  570.  
  571. status() {
  572. local base pid lock_file= pid_file= binary=
  573.  
  574. # Test syntax.
  575. if [ "$#" = 0 ] ; then
  576. echo $"Usage: status [-p pidfile] {program}"
  577. return 1
  578. fi
  579. if [ "$1" = "-p" ]; then
  580. pid_file=$2
  581. shift 2
  582. fi
  583. if [ "$1" = "-l" ]; then
  584. lock_file=$2
  585. shift 2
  586. fi
  587. if [ "$1" = "-b" ]; then
  588. if [ -z $pid_file ]; then
  589. echo $"-b option can be used only with -p"
  590. echo $"Usage: status -p pidfile -b binary program"
  591. return 1
  592. fi
  593. binary=$2
  594. shift 2
  595. fi
  596. base=${1##*/}
  597.  
  598. # First try "pidof"
  599. __pids_var_run "$1" "$pid_file" "$binary"
  600. RC=$?
  601. if [ -z "$pid_file" -a -z "$pid" ]; then
  602. pid="$(__pids_pidof "$1")"
  603. fi
  604. if [ -n "$pid" ]; then
  605. echo $"${base} (pid $pid) is running..."
  606. return 0
  607. fi
  608.  
  609. case "$RC" in
  610. 0)
  611. echo $"${base} (pid $pid) is running..."
  612. return 0
  613. ;;
  614. 1)
  615. echo $"${base} dead but pid file exists"
  616. return 1
  617. ;;
  618. 4)
  619. echo $"${base} status unknown due to insufficient privileges."
  620. return 4
  621. ;;
  622. esac
  623. if [ -z "${lock_file}" ]; then
  624. lock_file=${base}
  625. fi
  626. # See if /var/lock/subsys/${lock_file} exists
  627. if [ -f /var/lock/subsys/${lock_file} ]; then
  628. echo $"${base} dead but subsys locked"
  629. return 2
  630. fi
  631. echo $"${base} is stopped"
  632. return 3
  633. }
  634.  
  635. echo_success() {
  636. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  637. echo -n "["
  638. [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  639. echo -n $" OK "
  640. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  641. echo -n "]"
  642. echo -ne "\r"
  643. return 0
  644. }
  645.  
  646. echo_failure() {
  647. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  648. echo -n "["
  649. [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  650. echo -n $"FAILED"
  651. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  652. echo -n "]"
  653. echo -ne "\r"
  654. return 1
  655. }
  656.  
  657. echo_passed() {
  658. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  659. echo -n "["
  660. [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  661. echo -n $"PASSED"
  662. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  663. echo -n "]"
  664. echo -ne "\r"
  665. return 1
  666. }
  667.  
  668. echo_warning() {
  669. [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  670. echo -n "["
  671. [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  672. echo -n $"WARNING"
  673. [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  674. echo -n "]"
  675. echo -ne "\r"
  676. return 1
  677. }
  678.  
  679. # Inform the graphical boot of our current state
  680. update_boot_stage() {
  681. if [ -x /bin/plymouth ]; then
  682. /bin/plymouth --update="$1"
  683. fi
  684. return 0
  685. }
  686.  
  687. # Log that something succeeded
  688. success() {
  689. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
  690. return 0
  691. }
  692.  
  693. # Log that something failed
  694. failure() {
  695. local rc=$?
  696. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_failure
  697. [ -x /bin/plymouth ] && /bin/plymouth --details
  698. return $rc
  699. }
  700.  
  701. # Log that something passed, but may have had errors. Useful for fsck
  702. passed() {
  703. local rc=$?
  704. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_passed
  705. return $rc
  706. }
  707.  
  708. # Log a warning
  709. warning() {
  710. local rc=$?
  711. [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_warning
  712. return $rc
  713. }
  714.  
  715. # Run some action. Log its output.
  716. action() {
  717. local STRING rc
  718.  
  719. STRING=$1
  720. echo -n "$STRING "
  721. shift
  722. "$@" && success $"$STRING" || failure $"$STRING"
  723. rc=$?
  724. echo
  725. return $rc
  726. }
  727.  
  728. # Run some action. Silently.
  729. action_silent() {
  730. local STRING rc
  731.  
  732. STRING=$1
  733. echo -n "$STRING "
  734. shift
  735. "$@" >/dev/null && success $"$STRING" || failure $"$STRING"
  736. rc=$?
  737. echo
  738. return $rc
  739. }
  740.  
  741. # returns OK if $1 contains $2
  742. strstr() {
  743. [ "${1#*$2*}" = "$1" ] && return 1
  744. return 0
  745. }
  746.  
  747. # Confirm whether we really want to run this service
  748. confirm() {
  749. [ -x /bin/plymouth ] && /bin/plymouth --hide-splash
  750. while : ; do
  751. echo -n $"Start service $1 (Y)es/(N)o/(C)ontinue? [Y] "
  752. read answer
  753. if strstr $"yY" "$answer" || [ "$answer" = "" ] ; then
  754. return 0
  755. elif strstr $"cC" "$answer" ; then
  756. rm -f /var/run/confirm
  757. [ -x /bin/plymouth ] && /bin/plymouth --show-splash
  758. return 2
  759. elif strstr $"nN" "$answer" ; then
  760. return 1
  761. fi
  762. done
  763. }
  764.  
  765. # resolve a device node to its major:minor numbers in decimal or hex
  766. get_numeric_dev() {
  767. (
  768. fmt="%d:%d"
  769. if [ "$1" == "hex" ]; then
  770. fmt="%x:%x"
  771. fi
  772. ls -lH "$2" | awk '{ sub(/,/, "", $5); printf("'"$fmt"'", $5, $6); }'
  773. ) 2>/dev/null
  774. }
  775.  
  776. # Check whether file $1 is a backup or rpm-generated file and should be ignored
  777. is_ignored_file() {
  778. case "$1" in
  779. *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
  780. return 0
  781. ;;
  782. esac
  783. return 1
  784. }
  785.  
  786. # Evaluate shvar-style booleans
  787. is_true() {
  788. case "$1" in
  789. [tT] | [yY] | [yY][eE][sS] | [tT][rR][uU][eE])
  790. return 0
  791. ;;
  792. esac
  793. return 1
  794. }
  795.  
  796. # Evaluate shvar-style booleans
  797. is_false() {
  798. case "$1" in
  799. [fF] | [nN] | [nN][oO] | [fF][aA][lL][sS][eE])
  800. return 0
  801. ;;
  802. esac
  803. return 1
  804. }
  805.  
  806. # Apply sysctl settings, including files in /etc/sysctl.d
  807. apply_sysctl() {
  808. sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1
  809. for file in /etc/sysctl.d/* ; do
  810. is_ignored_file "$file" && continue
  811. test -f "$file" && sysctl -e -p "$file" >/dev/null 2>&1
  812. done
  813. }
  814.  
  815. key_is_random() {
  816. [ "$1" = "/dev/urandom" -o "$1" = "/dev/hw_random" \
  817. -o "$1" = "/dev/random" ]
  818. }
  819.  
  820. find_crypto_mount_point() {
  821. local fs_spec fs_file fs_vfstype remaining_fields
  822. local fs
  823. while read fs_spec fs_file remaining_fields; do
  824. if [ "$fs_spec" = "/dev/mapper/$1" ]; then
  825. echo $fs_file
  826. break;
  827. fi
  828. done < /etc/fstab
  829. }
  830.  
  831. # Because of a chicken/egg problem, init_crypto must be run twice. /var may be
  832. # encrypted but /var/lib/random-seed is needed to initialize swap.
  833. init_crypto() {
  834. local have_random dst src key opt mode owner params makeswap skip arg opt
  835. local param value rc ret mke2fs mdir prompt mount_point
  836.  
  837. ret=0
  838. have_random=$1
  839. while read dst src key opt; do
  840. [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
  841. [ -b "/dev/mapper/$dst" ] && continue;
  842. if [ "$have_random" = 0 ] && key_is_random "$key"; then
  843. continue
  844. fi
  845. if [ -n "$key" -a "x$key" != "xnone" ]; then
  846. if test -e "$key" ; then
  847. owner=$(ls -l $key | (read a b owner rest; echo $owner))
  848. if ! key_is_random "$key"; then
  849. mode=$(ls -l "$key" | cut -c 5-10)
  850. if [ "$mode" != "------" ]; then
  851. echo $"INSECURE MODE FOR $key"
  852. fi
  853. fi
  854. if [ "$owner" != root ]; then
  855. echo $"INSECURE OWNER FOR $key"
  856. fi
  857. else
  858. echo $"Key file for $dst not found, skipping"
  859. ret=1
  860. continue
  861. fi
  862. else
  863. key=""
  864. fi
  865. params=""
  866. makeswap=""
  867. mke2fs=""
  868. skip=""
  869. # Parse the src field for UUID= and convert to real device names
  870. if [ "${src%%=*}" == "UUID" ]; then
  871. src=$(/sbin/blkid -t "$src" -l -o device)
  872. elif [ "${src/^\/dev\/disk\/by-uuid\/}" != "$src" ]; then
  873. src=$(__readlink $src)
  874. fi
  875. # Is it a block device?
  876. [ -b "$src" ] || continue
  877. # Is it already a device mapper slave? (this is gross)
  878. devesc=${src##/dev/}
  879. devesc=${devesc//\//!}
  880. for d in /sys/block/dm-*/slaves ; do
  881. [ -e $d/$devesc ] && continue 2
  882. done
  883. # Parse the options field, convert to cryptsetup parameters and
  884. # contruct the command line
  885. while [ -n "$opt" ]; do
  886. arg=${opt%%,*}
  887. opt=${opt##$arg}
  888. opt=${opt##,}
  889. param=${arg%%=*}
  890. value=${arg##$param=}
  891.  
  892. case "$param" in
  893. cipher)
  894. params="$params -c $value"
  895. if [ -z "$value" ]; then
  896. echo $"$dst: no value for cipher option, skipping"
  897. skip="yes"
  898. fi
  899. ;;
  900. size)
  901. params="$params -s $value"
  902. if [ -z "$value" ]; then
  903. echo $"$dst: no value for size option, skipping"
  904. skip="yes"
  905. fi
  906. ;;
  907. hash)
  908. params="$params -h $value"
  909. if [ -z "$value" ]; then
  910. echo $"$dst: no value for hash option, skipping"
  911. skip="yes"
  912. fi
  913. ;;
  914. verify)
  915. params="$params -y"
  916. ;;
  917. swap)
  918. makeswap=yes
  919. ;;
  920. tmp)
  921. mke2fs=yes
  922. esac
  923. done
  924. if [ "$skip" = "yes" ]; then
  925. ret=1
  926. continue
  927. fi
  928. if [ -z "$makeswap" ] && cryptsetup isLuks "$src" 2>/dev/null ; then
  929. if key_is_random "$key"; then
  930. echo $"$dst: LUKS requires non-random key, skipping"
  931. ret=1
  932. continue
  933. fi
  934. if [ -n "$params" ]; then
  935. echo "$dst: options are invalid for LUKS partitions," \
  936. "ignoring them"
  937. fi
  938. if [ -n "$key" ]; then
  939. /sbin/cryptsetup -d $key luksOpen "$src" "$dst" <&1 2>/dev/null && success || failure
  940. rc=$?
  941. else
  942. mount_point="$(find_crypto_mount_point $dst)"
  943. [ -n "$mount_point" ] || mount_point=${src##*/}
  944. prompt=$(printf $"%s is password protected" "$mount_point")
  945. plymouth ask-for-password --prompt "$prompt" --command="/sbin/cryptsetup luksOpen -T1 $src $dst" <&1
  946. rc=$?
  947. fi
  948. else
  949. [ -z "$key" ] && plymouth --hide-splash
  950. /sbin/cryptsetup $params ${key:+-d $key} create "$dst" "$src" <&1 2>/dev/null && success || failure
  951. rc=$?
  952. [ -z "$key" ] && plymouth --show-splash
  953. fi
  954. if [ $rc -ne 0 ]; then
  955. ret=1
  956. continue
  957. fi
  958. if [ -b "/dev/mapper/$dst" ]; then
  959. if [ "$makeswap" = "yes" ]; then
  960. mkswap "/dev/mapper/$dst" 2>/dev/null >/dev/null
  961. fi
  962. if [ "$mke2fs" = "yes" ]; then
  963. if mke2fs "/dev/mapper/$dst" 2>/dev/null >/dev/null \
  964. && mdir=$(mktemp -d /tmp/mountXXXXXX); then
  965. mount "/dev/mapper/$dst" "$mdir" && chmod 1777 "$mdir"
  966. umount "$mdir"
  967. rmdir "$mdir"
  968. fi
  969. fi
  970. fi
  971. done < /etc/crypttab
  972. return $ret
  973. }
  974.  
  975. # A sed expression to filter out the files that is_ignored_file recognizes
  976. __sed_discard_ignored_files='/\(~\|\.bak\|\.orig\|\.rpmnew\|\.rpmorig\|\.rpmsave\)$/d'
  977.  
  978. #if we have privileges lets log to kmsg, otherwise to stderr
  979. if strstr "$(cat /proc/cmdline)" "rc.debug"; then
  980. [ -w /dev/kmsg ] && exec 30>/dev/kmsg && BASH_XTRACEFD=30
  981. set -x
  982. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement