09550471565b36a0245c750de3f830f690eae119
[openwrt/staging/wigyori.git] / package / boot / kexec-tools / files / kdump.init
1 #!/bin/sh /etc/rc.common
2
3 START=41
4 STOP=90
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 echo -n "Saving vmcore (this may take a while)..."
47 # would like 'sparse' but busybox doesn't support it
48 dd if=/proc/vmcore of="$path/vmcore-$timestamp" conv=fsync bs=1M
49 echo " done"
50 fi
51
52 if [ "$save_dmesg" -eq 1 ]; then
53 vmcore-dmesg /proc/vmcore > "$path/dmesg-$timestamp"
54 fi
55
56 sync
57 reboot -f
58 }
59
60 find_kernel() {
61 . /lib/functions.sh
62 local kernel
63
64 kernel="$BOOT_IMAGE"
65 if [ -r "$kernel" ]; then
66 echo $kernel
67 return 0
68 fi
69
70 kernel="$(find_mtd_part kernel)"
71 if [ -r "$kernel" ]; then
72 echo $kernel
73 return 0
74 fi
75
76 for voldir in /sys/class/ubi/ubi*_*; do
77 [ ! -e "$voldir" ] && continue
78 if [ "$(cat "${voldir}/name")" = "kernel" ]; then
79 kernel="/dev/$(basename "$voldir")"
80 echo $kernel
81 return 0
82 fi
83 done
84
85 return 1
86 }
87
88 load_crashkernel() {
89 local append_cmdline
90 local kernel
91
92 kernel="$(find_kernel)"
93 [ $? -gt 0 ] && return 1
94
95 case "$(uname -m)" in
96 i?86|x86_64)
97 grep -q "crashkernel=" /proc/cmdline || return 1
98 append_cmdline="1 irqpoll reset_devices maxcpus=1"
99 ;;
100 arm*)
101 append_cmdline="1 maxcpus=1 reset_devices"
102 ;;
103 esac
104 kexec -p "$kernel" --reuse-cmdline --append="$append_cmdline"
105 return $?
106 }
107
108 start() {
109 local retval
110
111 if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
112 return 1
113 fi
114
115 if [ -e /proc/vmcore ]; then
116 config_load kdump
117 config_foreach run_kdump kdump
118 else
119 config_load kdump
120 config_foreach verify_kdump kdump
121 retval=$?
122 [ $retval = 1 ] && return 1
123 [ $retval = 0 ] && load_crashkernel
124 return $?
125 fi
126 }
127
128 stop() {
129 [ "$(cat /sys/kernel/kexec_crash_loaded)" = "1" ] || return
130
131 if [ -e "$BOOT_IMAGE" ]; then
132 kexec -p -u "$BOOT_IMAGE"
133 fi
134 }
135
136 status() {
137 local retval kernel
138
139 if [ ! -e /sys/kernel/kexec_crash_loaded ]; then
140 echo "crashdump not supported by kernel"
141 return
142 fi
143
144 if [ $(cat /sys/kernel/kexec_crash_size) -eq 0 ]; then
145 echo "memory for crashdump kernel not reserved!"
146 echo "check crashkernel= kernel cmdline parameter"
147 echo "(a reboot is required after installing kdump)"
148 return
149 fi
150
151 kernel="$(find_kernel)"
152 if [ $? -gt 0 ]; then
153 echo "cannot find kernel image"
154 return
155 else
156 echo "using kernel image $kernel"
157 fi
158
159 echo -n "kdump configuration is "
160 config_load kdump
161 retval=$?
162 if [ $retval = 0 ]; then
163 if [ "$(config_foreach echo kdump)" ]; then
164 config_foreach verify_kdump kdump
165 retval=$?
166 else
167 retval=1
168 fi
169 fi
170
171 if [ $retval = 0 ]; then
172 echo "valid"
173 elif [ $retval = 2 ]; then
174 echo "disabled"
175 else
176 echo "BROKEN"
177 fi
178
179 echo -n "kexec crash kernel "
180 if [ "$(cat /sys/kernel/kexec_crash_loaded)" = "0" ]; then
181 echo -n "not "
182 fi
183 echo "loaded"
184 }