Subversion Repositories configs

Rev

Rev 33 | Rev 95 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 - 1
#! /bin/sh
2
### BEGIN INIT INFO
3
# Provides: kdump
4
# Default-Start:  3 4 5
5
# Default-Stop: 0 1 6
33 - 6
# Require-Start: $network $local_fs $remote_fs
3 - 7
# Short-Description: start and stop kdump crash recovery service
8
# Description:  The kdump init script provides the support necessary for
9
#		loading a kdump kernel into memory at system bootup time,
10
#		and for copying away a vmcore at system panic time.
11
### END INIT INFO
12
#  Copyright 2005 Red Hat, Inc.
13
#
14
#  Author:  Jeff Moyer <jmoyer@redhat.com>
15
 
16
 
17
# Source function library.
18
. /etc/init.d/functions
19
 
20
KEXEC=/sbin/kexec
21
 
22
# Will be different for ia64, for example.  For now, that architecture isn't
23
# supported.  Code needs to be added here when we do.
24
BOOTDIR="/boot"
25
 
26
KDUMP_KERNELVER=""
27
KDUMP_COMMANDLINE=""
28
KDUMP_IDE_NOPROBE_COMMANDLINE=""
29
KEXEC_ARGS=""
30
KDUMP_CONFIG_FILE="/etc/kdump.conf"
31
MEM_RESERVED=""
32
MKDUMPRD_ARGS=""
33
CLUSTER_CONFIG_FILE="/etc/cluster/cluster.conf"
34
FENCE_KDUMP_CONFIG="/etc/sysconfig/fence_kdump"
35
SSH_KEY_LOCATION="/root/.ssh/kdump_id_rsa"
57 - 36
#kdump shall be the default dump mode
37
DEFAULT_DUMP_MODE="kdump"
3 - 38
 
39
LOGGER="/usr/bin/logger -p info -t kdump"
40
 
41
standard_kexec_args="-p"
42
 
43
if [ -f /etc/sysconfig/kdump ]; then
44
	. /etc/sysconfig/kdump
45
fi
46
 
47
function single_instance_lock()
48
{
49
	exec 9>/var/lock/kdump
50
	flock 9
51
}
52
 
57 - 53
determine_dump_mode()
54
{
55
	# Check if firmware-assisted dump is enabled
56
	# if yes, set the dump mode as fadump and source
57
	# fadump related functions
58
	fadump_enabled_sys_node="/sys/kernel/fadump_enabled"
59
	if [ -f $fadump_enabled_sys_node ]; then
60
		rc=`cat $fadump_enabled_sys_node`
61
		if [ $rc -eq 1 ]; then
62
			echo "kdump: dump mode is fadump"
63
			DEFAULT_DUMP_MODE="fadump"
64
			. /etc/init.d/fadump-functions
65
		fi
66
	fi
67
}
68
 
3 - 69
# remove_cmdline_param <kernel cmdline> <param1> [<param2>] ... [<paramN>]
70
# Remove a list of kernel parameters from a given kernel cmdline and print the result.
71
# For each "arg" in the removing params list, "arg" and "arg=xxx" will be removed if exists.
72
function remove_cmdline_param()
73
{
74
	local cmdline=$1
75
	shift
76
 
77
	for arg in $@; do
78
		cmdline=`echo $cmdline | \
79
			 sed -e "s/\b$arg=[^ ]*\b//g" \
80
			     -e "s/\b$arg\b//g"`
81
	done
82
	echo $cmdline
83
}
84
 
85
 
86
function in_xen_pv_guest()
87
{
88
	grep -q 'xen-percpu-virq  *timer0' /proc/interrupts
89
}
90
 
91
function in_xen_hvm_guest()
92
{
93
	grep -q "xen" /sys/hypervisor/type >& /dev/null && ! grep -q 'xen-percpu-virq  *timer0' /proc/interrupts
94
}
95
 
96
function check_xen_hvm_nopv_premkdumprd()
97
{
98
	if in_xen_hvm_guest; then
99
		if grep -q "xen_.*front" /proc/modules; then
100
			return 1
101
		fi
102
	fi
103
}
104
 
105
function save_kernel_logs()
106
{
107
	local _path=$1
108
 
109
	mkdir -p $_path
110
 
111
	if [ ! -f /sbin/vmcore-dmesg ];then
112
		$LOGGER "Skipping saving vmcore-dmesg.txt. File /sbin/vmcore-dmesg is not present"
113
		return;
114
	fi
115
 
116
	echo "kdump: saving vmcore-dmesg.txt to $_path"
117
	$LOGGER "saving vmcore-dmesg.txt to $_path"
118
	/sbin/vmcore-dmesg /proc/vmcore > $_path/vmcore-dmesg-incomplete.txt
119
	if [ $? == 0 ]; then
120
		mv $_path/vmcore-dmesg-incomplete.txt $_path/vmcore-dmesg.txt
121
		echo "kdump: saved vmcore-dmesg.txt to $_path"
122
		$LOGGER "saved vmcore-dmesg.txt to $_path"
123
	else
124
		echo "kdump: failed to save vmcore-dmesg.txt to $_path"
125
		$LOGGER "failed to save vmcore-dmesg.txt to $_path"
126
	fi
127
 
128
}
129
 
6 - 130
#
131
# This function returns the "initial apicid" of the
132
# boot cpu (cpu 0) if present.
133
#
134
function get_bootcpu_initial_apicid()
135
{
136
    awk '                                                       \
137
	BEGIN { CPU = "-1"; }                                   \
138
	$1=="processor" && $2==":"      { CPU = $NF; }          \
139
	CPU=="0" && /initial apicid/    { print $NF; }          \
140
	'                                                       \
141
	/proc/cpuinfo
142
}
143
 
144
#
145
# This function appends argument "$2=$3" to string ($1) if not already present.
146
#
147
function append_cmdline()
148
{
149
    local cmdline=$1
150
    local newstr=${cmdline/$2/""}
151
 
152
    # unchanged str implies argument wasn't there
153
    if [ "$cmdline" == "$newstr" ]; then
154
        cmdline="${cmdline} ${2}=${3}"
155
    fi
156
 
157
    echo $cmdline
158
}
159
 
160
# This function performs a series of edits on the command line
161
function prepare_cmdline()
162
{
163
	local cmdline;
164
	if [ -z "$KDUMP_COMMANDLINE" ]; then
165
		cmdline=`cat /proc/cmdline`
166
	else
167
		cmdline=${KDUMP_COMMANDLINE}
168
	fi
169
	cmdline=`remove_cmdline_param "$cmdline" crashkernel mem hugepages hugepagesz`
170
	cmdline="${cmdline} ${KDUMP_COMMANDLINE_APPEND}"
171
	avoid_cdrom_drive
172
	KDUMP_COMMANDLINE="${KDUMP_COMMANDLINE} ${KDUMP_IDE_NOPROBE_COMMANDLINE}"
173
 
174
	local id=`get_bootcpu_initial_apicid`
175
	if [ ! -z ${id} ] ; then
176
		cmdline=`append_cmdline "${cmdline}" disable_cpu_apicid ${id}`
177
	fi
178
 
179
	echo $cmdline
180
}
181
 
3 - 182
function save_core()
183
{
184
	local kdump_path
185
	kdump_path=`grep ^path $KDUMP_CONFIG_FILE | cut -d' '  -f2-`
186
	if [ -z "$kdump_path" ]; then
187
		coredir="/var/crash/127.0.0.1-`date +"%Y-%m-%d-%H:%M"`"
188
	else
189
		coredir="${kdump_path}/127.0.0.1-`date +"%Y-%m-%d-%H:%M"`"
190
	fi
191
 
192
	mkdir -p $coredir
193
	save_kernel_logs "${coredir}"
194
	/usr/sbin/makedumpfile -c --message-level 1 -d 31 /proc/vmcore $coredir/vmcore-incomplete
195
	if [ $? == 0 ]; then
196
		mv $coredir/vmcore-incomplete $coredir/vmcore
197
		$LOGGER "saved a vmcore to $coredir"
198
	else
199
		$LOGGER "failed to save a vmcore to $coredir"
200
	fi
201
}
202
 
57 - 203
function check_kdump_config()
3 - 204
{
205
	local modified_files=""
206
	local force_rebuild=0
57 - 207
	MKDUMPRD="/sbin/mkdumprd -d -f $MKDUMPRD_ARGS"
3 - 208
 
209
	force_rebuild=`grep ^force_rebuild $KDUMP_CONFIG_FILE | cut -d' '  -f2`
210
	if [ -n "$force_rebuild" ] && [ "$force_rebuild" -ne 0 ]
211
	then
212
                modified_files="force_rebuild"
213
	fi
214
 
215
	if [ -z "$KDUMP_KERNELVER" ]; then
216
		local running_kernel=`uname -r`
217
 
218
		kdump_kver=`echo $running_kernel | sed 's/smp//g'`
219
	else
220
		kdump_kver=$KDUMP_KERNELVER
221
	fi
222
 
223
	kdump_kernel="${KDUMP_BOOTDIR}/${KDUMP_IMG}-${kdump_kver}${KDUMP_IMG_EXT}"
224
	kdump_initrd="${KDUMP_BOOTDIR}/initrd-${kdump_kver}kdump.img"
225
 
226
	if [ ! -f $kdump_kernel ]; then
227
		echo -n "No kdump kernel image found."; warning; echo
228
		echo "Tried to locate ${kdump_kernel}"
229
		return 0
230
	fi
231
 
232
	if [ ! -f $kdump_initrd ]; then
233
		echo  -n "No kdump initial ramdisk found."; warning; echo
234
		if ! check_xen_hvm_nopv_premkdumprd; then
235
			echo "hvm guest with pv drivers is not supported."
236
			exit 1
237
		fi
238
 
239
		echo "Rebuilding $kdump_initrd"
240
		$MKDUMPRD $kdump_initrd $kdump_kver
241
		if [ $? != 0 ]; then
242
			echo "Failed to run mkdumprd"
243
			$LOGGER "mkdumprd: failed to make kdump initrd"
57 - 244
			rm -f $kdump_initrd
3 - 245
			exit 1
246
		fi
247
		return 0
248
	fi
249
 
250
	if [ -z "$modified_files" ]
251
	then
252
		#check to see if config file or kdump post has been modified
253
		#since last build of the image file
254
		image_time=`stat -c "%Y" $kdump_initrd`
255
		EXTRA_FILES=`grep ^kdump_post $KDUMP_CONFIG_FILE | cut -d\  -f2`
256
		CHECK_FILE=`grep ^kdump_pre $KDUMP_CONFIG_FILE | cut -d\  -f2`
257
		EXTRA_FILES="$EXTRA_FILES $CHECK_FILE"
258
		CHECK_FILE=`grep ^extra_modules $KDUMP_CONFIG_FILE | cut -d\  -f2-`
259
		EXTRA_FILES="$EXTRA_FILES $CHECK_FILE"
260
		CHECK_FILE=`grep ^extra_bins $KDUMP_CONFIG_FILE | cut -d\  -f2-`
261
		EXTRA_FILES="$EXTRA_FILES $CHECK_FILE"
262
		FORCE_REBUILD=`grep ^extra_modules $KDUMP_CONFIG_FILE`
263
		files="$KDUMP_CONFIG_FILE $kdump_kernel $EXTRA_FILES"
264
 
8 - 265
		# changes in CLUSTER_CONFIG_FILE should be monitored only if fence_kdump
266
		# is not configured using fence_kdump_nodes option in /etc/kdump.conf
267
		# and fence_kdump is configured in Pacemaker cluster
268
		if ! grep -q ^fence_kdump_nodes /etc/kdump.conf \
269
			&& [ -f "$CLUSTER_CONFIG_FILE" ] \
270
			&& grep -q fence_kdump "$CLUSTER_CONFIG_FILE"
3 - 271
		then
272
		    files="$files $CLUSTER_CONFIG_FILE"
273
		    if [ -f "$FENCE_KDUMP_CONFIG" ]; then
274
			files="$files $FENCE_KDUMP_CONFIG"
275
		    fi
276
		fi
277
 
278
		for file in $files; do
279
			time_stamp=0
280
			if [ -f "$file" ]; then
281
				time_stamp=`stat -c "%Y" $file`
282
			else
283
				modified_files="$modified_files $file"
284
				continue
285
			fi
286
			if [ "$time_stamp" -gt "$image_time" ]; then
287
				modified_files="$modified_files $file"
288
			fi
289
		done
290
	fi
291
 
292
        if [ -n "$FORCE_REBUILD" -a "$modified_files"!=" " ]
293
        then
294
                modified_files="force_rebuild"
295
        fi
296
 
297
        if [ -n "$modified_files" -a "$modified_files"!=" " ]; then
298
                if [ "$modified_files" != "force_rebuild" ]
299
                then
300
                        echo "Detected change(s) the following file(s):"
301
                        echo -n "  "; echo "$modified_files" | sed 's/\s/\n  /g'
302
                fi
303
 
304
		if ! check_xen_hvm_nopv_premkdumprd; then
305
			echo "hvm guest with pv drivers is not supported."
306
			exit 1
307
		fi
308
 
309
                echo "Rebuilding $kdump_initrd"
310
		$MKDUMPRD $kdump_initrd $kdump_kver
311
                if [ $? != 0 ]; then
312
                        echo "Failed to run mkdumprd"
313
                        $LOGGER "mkdumprd: failed to make kdump initrd"
57 - 314
			rm -f $kdump_initrd
3 - 315
                        return 1
316
                fi
317
        fi
318
 
319
	#double check the xen_*front modules are not included for xen hvm
320
	if in_xen_hvm_guest; then
321
		if $(lsinitrd $kdump_initrd|grep -q "xen_.*front.ko"); then
322
			echo "Found xen pv drivers in kdump initrd"
323
			exit 1
324
		fi
325
	fi
326
        return 0
327
}
328
 
57 - 329
function check_config()
330
{
331
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
332
		check_fadump_config
333
	else
334
		check_kdump_config
335
	fi
336
 
337
	return $?
338
}
339
 
3 - 340
# This function check iomem and determines if we have more than
341
# 4GB of ram available. Returns 1 if we do, 0 if we dont
342
function need_64bit_headers()
343
{
344
    return `tail -n 1 /proc/iomem | awk '{ split ($1, r, "-"); \
345
    print (strtonum("0x" r[2]) > strtonum("0xffffffff")); }'`
346
}
347
 
348
function avoid_cdrom_drive()
349
{
350
	local DRIVE=""
351
	local MEDIA=""
352
	local IDE_DRIVES=(`echo hd{a,b,c,d}`)
353
	local COUNTER="0"
354
 
355
	for DRIVE in ${IDE_DRIVES[@]}
356
	do
357
		if ! $(echo "$KDUMP_COMMANDLINE" |grep -q "$DRIVE=");then
358
			if [ -f /proc/ide/$DRIVE/media ];then
359
				MEDIA=$(cat /proc/ide/$DRIVE/media)
360
				if [ x"$MEDIA" == x"cdrom" ]; then
361
					KDUMP_IDE_NOPROBE_COMMANDLINE="$KDUMP_IDE_NOPROBE_COMMANDLINE $DRIVE=cdrom"
362
					COUNTER=$(($COUNTER+1))
363
				fi
364
			fi
365
		else
366
			KDUMP_IDE_NOPROBE_COMMANDLINE="$KDUMP_IDE_NOPROBE_COMMANDLINE $DRIVE=noprobe"
367
		fi
368
	done
369
	# We don't find cdrom drive.
370
	if [ $COUNTER -eq 0 ]; then
371
		KDUMP_IDE_NOPROBE_COMMANDLINE=""
372
	fi
373
}
374
 
375
function check_kernel_parameter()
376
{
377
	if [ -z "$KDUMP_COMMANDLINE" ]
378
	then
379
		KDUMP_COMMANDLINE=`cat /proc/cmdline`
380
	fi
381
 
382
	MEM_RESERVED=`cat /sys/kernel/kexec_crash_size`
383
 
384
	if [ $MEM_RESERVED -eq 0 ]
385
	then
386
		return 1
387
	else
388
		return 0
389
	fi
390
}
391
 
57 - 392
# Load the kdump kernel specified in /etc/sysconfig/kdump
3 - 393
# If none is specified, try to load a kdump kernel with the same version
394
# as the currently running kernel.
395
function load_kdump()
396
{
397
	ARCH=`uname -m`
398
 
399
	# Get the approx amount of ram the kernel is using in Kb
400
	KMEMINUSE=`awk '/Slab:.*/ {print $2}' /proc/meminfo`
401
	# Convert the reserved ram amount to Kb
402
	MEM_RESERVED=`dc -e"$MEM_RESERVED 1024 / p"`
403
 
404
	# Take 70% of the reserved value rounding up to the nearest integer
405
	MEM_RESERVED=`dc -e"$MEM_RESERVED .7 * 10 * 10 / p"`
406
 
407
	#On x86, we are using nr_cpus=1, so the following check is not necessary.
408
	if [ "$ARCH" != "i686" -a "$ARCH" != "i386" -a "$ARCH" != "x86_64" ]
409
	then
410
		#Check if the KMEMINUSE is greater than MEM_RESERVED
411
		# This indicates that the currently runnign kernel is using
412
		# 70% of the amount of memory that we have reserved for kdump
413
		# we should issue a warning here indicating that the user may
414
		# want to increase the amount of reserved ram on the system
415
		if [ $KMEMINUSE -gt $MEM_RESERVED ]
416
		then
417
			echo -n "Your running kernel is using more than 70% of the amount of space you reserved for kdump, you should consider increasing your crashkernel reservation"
418
			warning
419
			echo
420
		fi
421
	fi
422
 
423
	if [ "$ARCH" == "i686" -o "$ARCH" == "i386" ]
424
	then
425
 
426
		need_64bit_headers
427
		if [ $? == 1 ]
428
		then
429
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf32-core-headers`
430
			if [ -n "$FOUND_ELF_ARGS" ]
431
			then
432
				echo -n "Warning: elf32-core-headers overrides correct elf64 setting"
433
				warning
434
				echo
435
			else
436
				KEXEC_ARGS="$KEXEC_ARGS --elf64-core-headers"
437
			fi
438
		else
439
			FOUND_ELF_ARGS=`echo $KEXEC_ARGS | grep elf64-core-headers`
440
			if [ -z "$FOUND_ELF_ARGS" ]
441
			then
442
				KEXEC_ARGS="$KEXEC_ARGS --elf32-core-headers"
443
			fi
444
		fi
445
	fi
446
 
447
	if [ -f /sys/firmware/efi/systab ]
448
	then
449
		if grep -q '^ACPI20=' /sys/firmware/efi/systab
450
		then
451
			acpi_addr=$(awk -F'=' '/^ACPI20=/ {print $2}' /sys/firmware/efi/systab)
452
		else
453
			acpi_addr=$(awk -F'=' '/^ACPI=/ {print $2}' /sys/firmware/efi/systab)
454
		fi
455
		KDUMP_COMMANDLINE="$KDUMP_COMMANDLINE noefi acpi_rsdp=$acpi_addr"
456
	fi
457
 
458
	if echo "$KDUMP_COMMANDLINE_APPEND" | grep -q nr_cpus;
459
	then
460
		ver=`uname -r`
461
		maj=`echo $ver | cut -d'-' -f1`
462
		min=`echo $ver | cut -d'-' -f2`
463
		min=${min%%.*}
464
		if [ "$maj" = "2.6.32" ] && [ $min -lt 171 ]
465
		then
466
			echo "Your kernel is old, please use maxcpus=1 instead of nr_cpus=1"
467
			return 1
468
		fi
469
	fi
470
 
6 - 471
	KDUMP_COMMANDLINE=`prepare_cmdline`
472
 
3 - 473
	if ! grep -q /sys/kernel/debug /proc/mounts;
474
	then
475
		mount -t debugfs debug /sys/kernel/debug
476
		MNTDEBUG=/sys/kernel/debug
477
	fi
6 - 478
 
3 - 479
	$KEXEC $KEXEC_ARGS $standard_kexec_args \
480
		--command-line="$KDUMP_COMMANDLINE" \
481
		--initrd=$kdump_initrd $kdump_kernel 2>/dev/null
482
	if [ $? == 0 ]; then
483
		umount $MNTDEBUG 2>/dev/null
484
		$LOGGER "kexec: loaded kdump kernel"
485
		return 0
486
	else
487
		umount $MNTDEBUG 2>/dev/null
488
		$LOGGER "kexec: failed to load kdump kernel"
489
		return 1
490
	fi
491
}
492
 
493
function propagate_ssh_key()
494
{
495
	while read config_opt config_val; do
496
		case "$config_opt" in
497
		sshkey)
498
			SSH_KEY_LOCATION="$config_val"
499
			;;
500
		*)
501
			;;
502
		esac
503
	done < $KDUMP_CONFIG_FILE
504
 
505
	local KEYFILE=$SSH_KEY_LOCATION
506
	local errmsg="Failed to propagate ssh key"
507
 
508
	#make sure they've configured kdump.conf for ssh dumps
509
	local SSH_TARGET=`awk '/^\ *net.*@.*$/ {print $0}' $KDUMP_CONFIG_FILE`
510
	[ -z "$SSH_TARGET" ] && SSH_TARGET=`awk '/^\ *ssh.*@.*$/ {print $0}' $KDUMP_CONFIG_FILE`
511
	if [ -z "$SSH_TARGET" ]; then
512
		echo "No ssh config specified in $KDUMP_CONFIG_FILE.  Can't propagate"
513
		$LOGGER "$errmsg, no ssh config specified in $KDUMP_CONFIG_FILE"
514
		exit 1
515
	fi
516
 
517
	#Check to see if we already created key, if not, create it.
518
	if [ -f $KEYFILE ]; then
519
		echo "Using existing keys..."
520
	else
521
		echo -n "Generating new ssh keys... "
522
		/usr/bin/ssh-keygen -t rsa -f $KEYFILE -N "" 2>&1 > /dev/null
523
		echo "done."
524
	fi
525
 
526
	#now find the target ssh user and server to contact.
527
	SSH_USER=`echo $SSH_TARGET | cut -d\  -f2 | cut -d@ -f1`
528
	SSH_SERVER=`echo $SSH_TARGET | sed -e's/\(.*@\)\(.*$\)/\2/'`
529
 
530
	#now send the found key to the found server
531
	ssh-copy-id -i $KEYFILE $SSH_USER@$SSH_SERVER &>/dev/null
532
	RET=$?
533
	if [ $RET == 0 ]; then
534
		echo $KEYFILE has been added to ~$SSH_USER/.ssh/authorized_keys on $SSH_SERVER
535
		$LOGGER "propagated ssh key (ssh server: $SSH_SERVER)"
536
		return 0
537
	else
538
		echo $KEYFILE failed in transfer to $SSH_SERVER
539
		$LOGGER "$errmsg, unable to transfer $KEYFILE to $SSH_SERVER"
540
		exit 1
541
	fi
542
 
543
}
544
 
545
function status()
546
{
57 - 547
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
548
		check_current_fadump_status
549
		return $?
550
	fi
551
 
3 - 552
	if [ ! -e /sys/kernel/kexec_crash_loaded ]
553
	then
554
		return 2
555
	fi
556
 
557
	if in_xen_pv_guest; then
558
		return 2
559
	elif in_xen_hvm_guest && ! grep -q -e xen_emul_unplug=never -e xen_emul_unplug=unnecessary /proc/cmdline; then
560
		echo 'kdump only supported on xen hvm guests booted with xen_emul_unplug=never or xen_emul_unplug=unnecessary'
561
		return 2
562
	fi
563
 
564
	rc=`cat /sys/kernel/kexec_crash_loaded`
565
	if [ $rc == 1 ]; then
566
		return 0
567
	else
568
		return 1
569
	fi
570
}
571
 
572
function save_raw()
573
{
574
	local raw_part=$(awk '$1 ~ /^raw$/ { print $2; }' $KDUMP_CONFIG_FILE)
575
	local kdump_dir
576
	if [ "$raw_part" ]; then
577
		[ -b "$raw_part" ] || {
578
			echo "raw partition $raw_part not found"
579
			return 1
580
		}
581
		kdump_dir=`grep ^path $KDUMP_CONFIG_FILE | cut -d' '  -f2-`
582
		if [ -z "${kdump_dir}" ]; then
583
			coredir="/var/crash/`date +"%Y-%m-%d-%H:%M"`"
584
		else
585
			coredir="${kdump_dir}/`date +"%Y-%m-%d-%H:%M"`"
586
		fi
587
		mkdir -p "$coredir"
588
		[ -d "$coredir" ] || {
589
			echo "failed to create $coredir"
590
			return 1
591
		}
592
		if makedumpfile -R $coredir/vmcore <$raw_part >/dev/null 2>&1; then
593
			# dump found
594
			echo "Dump saved to $coredir/vmcore"
595
			# wipe makedumpfile header
596
			dd if=/dev/zero of=$raw_part bs=1b count=1 2>/dev/null
597
		else
598
			rm -rf "$coredir"
599
		fi
600
	fi
601
	return 0
602
}
603
 
604
get_save_path() {
605
	local _save_path=$(grep "^path" /etc/kdump.conf|awk '{print $2}')
606
	if [ -z "$_save_path" ]; then
607
		_save_path="/var/crash"
608
	fi
609
 
610
	echo $_save_path
611
}
612
 
613
is_dump_target_configured() {
614
    local _target
615
 
57 - 616
    _target=$(egrep "^ext[234]|^xfs|^btrfs|^raw|^ssh|^nfs|^nfs4|^net" /etc/kdump.conf)
3 - 617
 
618
     [ -n "$_target" ]
619
}
620
 
621
local_fs_dump_target()
622
{
623
	local _target
624
 
57 - 625
	_target=$(egrep "^ext[234]|^xfs|^btrfs" /etc/kdump.conf)
3 - 626
	if [ $? -eq 0 ]; then
627
		echo $_target|awk '{print $2}'
628
	fi
629
}
630
 
631
path_to_be_relabeled() {
632
	local _path _target _mnt="/" _rmnt
633
 
634
	if is_dump_target_configured; then
635
		_target=$(local_fs_dump_target)
636
		if [[ -n "$_target" ]]; then
637
			_mnt=$(findmnt -k -f -n -r -o TARGET $_target)
638
			if [ -z "$_mnt" ]; then
639
				return
640
			fi
641
		else
642
			return
643
		fi
644
	fi
645
 
646
	_path=$(get_save_path)
647
	# if $_path is masked by other mount, we will not relabel it.
648
	# one exception is ! is_dump_target_configure && "$_rmnt" != "$_mnt"
649
	# for this exception see mkdumprd code about [ -z "$USING_METHOD" ]
650
	_rmnt=$(df $_mnt/$_path 2>/dev/null | tail -1 | awk '{ print $NF }')
651
	if [[ "$_rmnt" == "$_mnt" ]] || ! is_dump_target_configured; then
652
		echo $_mnt/$_path
653
	fi
654
}
655
 
656
selinux_relabel()
657
{
658
	local _path _i _attr
659
 
660
	_path=$(path_to_be_relabeled)
661
	if [ -z "$_path" ] || ! [ -d "$_path" ] ; then
662
		return
663
	fi
664
 
665
	for _i in $(find $_path); do
666
		_attr=$(getfattr -m "security.selinux" $_i 2>/dev/null)
667
		if [ -z "$_attr" ]; then
668
			restorecon $_i;
669
		fi
670
	done
671
}
672
 
57 - 673
start_dump()
674
{
675
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
676
		start_fadump
677
	else
678
		load_kdump
679
	fi
3 - 680
 
57 - 681
	return $?
682
}
683
 
684
 
3 - 685
# Notes about xen support:
686
# pv guests are not supported
687
# hvm guests are supported only when you ensure below items:
688
# 1. Boot guests with either xen_emul_unplug=never or
689
#    xen_emul_unplug=unnecessary.
690
# 2. While recreating kdump initrd xen_netfront and xen_blkfront modules
691
#    are not loaded
692
function start()
693
{
694
	if sestatus 2>/dev/null | grep -q "SELinux status.*enabled"; then
695
		selinux_relabel
696
	fi
697
	save_raw
698
	if [ $? -ne 0 ]; then
699
		echo -n "Starting kdump:"; failure; echo
700
		$LOGGER "failed to start up"
701
		return 1
702
	fi
703
	status
704
	rc=$?
705
	if [ $rc == 2 ]; then
706
		echo -n "Kdump is not supported on this kernel"; failure; echo
707
		return 1;
708
	else
709
		if [ $rc == 0 ]; then
710
			echo -n "Kdump already running"; success; echo
711
			return 0
712
		fi
713
	fi
714
 
57 - 715
	# Kernel parameter check is not required for fadump as 'fadump=on'
716
	# should have already been passed if dump mode is fadump
717
	if [ $DEFAULT_DUMP_MODE != "fadump" ]; then
718
		check_kernel_parameter
719
		if [ $? != 0 ]; then
720
			echo -n "Starting kdump:"; failure; echo
721
			$LOGGER "No crashkernel parameter specified for running kernel"
722
			return 1
723
		fi
3 - 724
	fi
725
 
726
	check_config
727
	if [ $? != 0 ]; then
728
		echo -n "Starting kdump:"; failure; echo
729
		$LOGGER "failed to start up, config file incorrect"
730
		return 1
731
	fi
57 - 732
 
733
	start_dump
3 - 734
	if [ $? != 0 ]; then
735
		echo -n "Starting kdump:"; failure; echo
736
		$LOGGER "failed to start up"
737
		return 1
738
	fi
739
 
740
	echo -n "Starting kdump:"; success; echo
741
	$LOGGER "started up"
742
}
743
 
57 - 744
stop_kdump()
745
{
746
	$KEXEC -p -u 2>/dev/null
747
	if [ $? != 0 ]; then
748
		$LOGGER "kexec: failed to unload kdump kernel"
749
		return 1
750
	fi
751
 
752
	$LOGGER "kexec: unloaded kdump kernel"
753
	return 0
754
}
755
 
3 - 756
function stop()
757
{
57 - 758
	if [ $DEFAULT_DUMP_MODE == "fadump" ]; then
759
		stop_fadump
3 - 760
	else
57 - 761
		stop_kdump
762
	fi
763
 
764
	if [ $? != 0 ]; then
3 - 765
		echo -n "Stopping kdump:"; failure; echo
766
		$LOGGER "failed to stop"
767
		return 1
768
	fi
57 - 769
 
770
	echo -n "Stopping kdump:"; success; echo
771
	$LOGGER "stopped"
772
	return 0
3 - 773
}
774
 
775
# Other kdump init instances will block in queue, until this one exits.
776
single_instance_lock
777
 
57 - 778
# Determine if the dump mode is kdump or fadump
779
determine_dump_mode
780
 
3 - 781
case "$1" in
782
  start)
57 - 783
	if [ $DEFAULT_DUMP_MODE == "fadump" ] && is_fadump_save_path
784
	then
785
		# fadump: second boot after crash, save vmcore
786
		save_fadump_core
787
		if [ $? -eq 0 ]; then
788
			reboot
789
		else
790
			save_fadump_core_on_error
791
			handle_fadump_default_action
792
		fi
793
	elif [ -s /proc/vmcore ]; then
3 - 794
		save_core
795
		reboot
796
	else
797
		start
798
	fi
799
	;;
800
  stop)
801
	stop
802
	;;
803
  status)
804
	EXIT_CODE=0
805
	status
806
	case "$?" in
807
	0)
808
		echo "Kdump is operational"
809
		EXIT_CODE=0
810
		;;
811
	1)
812
		echo "Kdump is not operational"
813
		EXIT_CODE=3
814
		;;
815
	2)
816
		echo "Kdump is unsupported on this kernel"
817
		EXIT_CODE=3
818
		;;
819
	esac
820
	exit $EXIT_CODE
821
	;;
822
  restart)
823
	stop
824
	start
825
	;;
826
  condrestart)
827
        EXIT_CODE=1
828
        status
829
        case "$?" in
830
        0)
831
                stop
832
                start
833
                EXIT_CODE=0
834
        ;;
835
        esac
836
        exit $EXIT_CODE
837
	;;
838
  propagate)
839
	propagate_ssh_key
840
	;;
841
  *)
842
	echo $"Usage: $0 {start|stop|status|restart|propagate}"
843
	exit 1
844
esac
845
 
846
exit $?