Subversion Repositories configs

Rev

Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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