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