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