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