kexec-tools: bump version and add support for crashdump kernel
[openwrt/staging/wigyori.git] / package / boot / kexec-tools / files / kdump.init
1 #!/bin/sh /etc/rc.common
2
3 START=41
4 STOP=98
5
6 EXTRA_COMMANDS="status"
7 EXTRA_HELP=" status Print crashkernel status"
8
9 verify_kdump() {
10 local cfg="$1"
11 local enabled
12 local path
13 local save_vmcore
14 local save_dmesg
15
16 config_get_bool enabled "$cfg" enabled 1
17 config_get_bool save_dmesg "$cfg" save_dmesg 1
18 config_get_bool save_vmcore "$cfg" save_vmcore 0
19
20 [ "$enabled" -gt 0 ] || return 2
21
22 [ "$save_dmesg" -gt 0 ] || [ "$save_vmcore" -gt 0 ] || return 2
23
24 config_get path "$cfg" path "/"
25
26 [ -d "$path" ] || mkdir -p "$path" 2>/dev/null || return 1
27 }
28
29 run_kdump() {
30 local cfg="$1"
31 local enabled
32 local path
33 local save_vmcore
34 local save_dmesg
35
36 config_get_bool enabled "$cfg" enabled 1
37 [ "$enabled" -gt 0 ] || return
38
39 config_get_bool save_dmesg "$cfg" save_dmesg 1
40 config_get_bool save_vmcore "$cfg" save_vmcore 0
41 config_get path "$cfg" path "/"
42
43 timestamp=$(date "+%Y%m%dT%H%M%S")
44
45 if [ "$save_vmcore" -eq 1 ]; then
46 # would like 'sparse' but busybox doesn't support it
47 dd if=/proc/vmcore of="$path/vmcore-$timestamp" conv=fsync bs=1M
48 fi
49
50 if [ "$save_dmesg" -eq 1 ]; then
51 vmcore-dmesg /proc/vmcore > "$path/dmesg-$timestamp"
52 fi
53
54 sync
55 reboot -f
56 }
57
58 find_kernel() {
59 . /lib/functions.sh
60 local kernel
61
62 kernel="$BOOT_IMAGE"
63 if [ -r "$kernel" ]; then
64 echo $kernel
65 return 0
66 fi
67
68 kernel="$(find_mtd_part kernel)"
69 if [ -r "$kernel" ]; then
70 echo $kernel
71 return 0
72 fi
73
74 for voldir in /sys/class/ubi/ubi*_*; do
75 [ ! -e "$voldir" ] && continue
76 if [ "$(cat "${voldir}/name")" = "kernel" ]; then
77 kernel="/dev/$(basename "$voldir")"
78 echo $kernel
79 return 0
80 fi
81 done
82
83 return 1
84 }
85
86 load_crashkernel() {
87 local append_cmdline
88 local kernel
89
90 kernel="$(find_kernel)"
91 [ $? -gt 0 ] && return 1
92
93 case "$(uname -m)" in
94 i?86|x86_64)
95 grep -q "crashkernel=" /proc/cmdline || return 1
96 append_cmdline="1 irqpoll reset_devices maxcpus=1"
97 ;;
98 arm*)
99 append_cmdline="1 maxcpus=1 reset_devices"
100 ;;
101 esac
102 kexec -p "$kernel" --reuse-cmdline --append="$append_cmdline"
103 return $?
104 }
105
106 start() {
107 local retval
108
109 if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
110 return 1
111 fi
112
113 if [ -e /proc/vmcore ]; then
114 config_load kdump
115 config_foreach run_kdump kdump
116 else
117 config_load kdump
118 config_foreach verify_kdump kdump
119 retval=$?
120 [ $retval = 1 ] && return 1
121 [ $retval = 0 ] && load_crashkernel
122 return $?
123 fi
124 }
125
126 stop() {
127 [ "$(cat /sys/kernel/kexec_crash_loaded)" = "1" ] || return
128
129 if [ -e "$BOOT_IMAGE" ]; then
130 kexec -p -u "$BOOT_IMAGE"
131 fi
132 }
133
134 status() {
135 local retval kernel
136
137 if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
138 echo "crashdump not supported by kernel"
139 return
140 fi
141
142 if [ $(cat /sys/kernel/kexec_crash_size) -eq 0 ]; then
143 echo "memory for crashdump kernel not reserved!"
144 echo "check crashkernel= kernel cmdline parameter"
145 echo "(a reboot is required after installing kdump)"
146 return
147 fi
148
149 kernel="$(find_kernel)"
150 if [ $? -gt 0 ]; then
151 echo "cannot find kernel image"
152 return
153 else
154 echo "using kernel image $kernel"
155 fi
156
157 echo -n "kdump configuration is "
158 config_load kdump
159 retval=$?
160 if [ $retval = 0 ]; then
161 if [ "$(config_foreach echo kdump)" ]; then
162 config_foreach verify_kdump kdump
163 retval=$?
164 else
165 retval=1
166 fi
167 fi
168
169 if [ $retval = 0 ]; then
170 echo "valid"
171 elif [ $retval = 2 ]; then
172 echo "disabled"
173 else
174 echo "BROKEN"
175 fi
176
177 echo -n "kexec crash kernel "
178 if [ "$(cat /sys/kernel/kexec_crash_loaded)" = "0" ]; then
179 echo -n "not "
180 fi
181 echo "loaded"
182 }