Skip to content

Commit 7105400

Browse files
authored
Add kdump support for Aboot platforms (sonic-net#824)
1 parent c5c5ffc commit 7105400

File tree

2 files changed

+63
-108
lines changed

2 files changed

+63
-108
lines changed

scripts/reboot

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
#!/bin/bash
22

3-
# Reboot immediately if we run the kdump capture kernel
4-
VMCORE_FILE=/proc/vmcore
5-
if [ -e $VMCORE_FILE -a -s $VMCORE_FILE ]; then
6-
debug "We have a /proc/vmcore, then we just kdump'ed"
7-
/sbin/reboot
8-
fi
9-
103
REBOOT_USER=$(logname)
114
REBOOT_TIME=$(date)
125
PLATFORM=$(sonic-cfggen -H -v DEVICE_METADATA.localhost.platform)
@@ -25,6 +18,18 @@ function debug()
2518
logger "$@"
2619
}
2720

21+
# Reboot immediately if we run the kdump capture kernel
22+
VMCORE_FILE=/proc/vmcore
23+
if [ -e $VMCORE_FILE -a -s $VMCORE_FILE ]; then
24+
debug "We have a /proc/vmcore, then we just kdump'ed"
25+
if [ -x ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT} ]; then
26+
VERBOSE=yes debug "Rebooting with platform ${PLATFORM} specific tool ..."
27+
exec ${DEVPATH}/${PLATFORM}/${PLAT_REBOOT}
28+
else
29+
/sbin/reboot
30+
fi
31+
fi
32+
2833
function stop_sonic_services()
2934
{
3035
if [[ x"$ASIC_TYPE" != x"mellanox" ]]; then

scripts/sonic-kdump-config

+51-101
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import subprocess
2525
import errno
2626
from swsssdk import ConfigDBConnector
2727

28+
aboot_cfg_template ="/host/image-%s/kernel-cmdline"
2829
grub_cfg = "/host/grub/grub.cfg"
2930
kdump_cfg = "/etc/default/kdump-tools"
31+
machine_cfg = "/host/machine.conf"
3032

3133
## Same as print(), but output to stderr instead of stdout
3234
def print_err(*args):
@@ -94,7 +96,7 @@ def get_next_image():
9496

9597
## Search for Current/Next SONiC image in grub configuration
9698
#
97-
# @param lines Lines read from grub.cfg file
99+
# @param lines Lines read from grub.cfg/cmdline file
98100
# @param img String we are looking for ("loop=image...")
99101
# @return Index in lines array wehere we found the string
100102
def locate_image(lines, img):
@@ -106,11 +108,11 @@ def locate_image(lines, img):
106108
pass
107109
return -1
108110

109-
## Rewrite grub configuration file
111+
## Rewrite grub/cmdline configuration file
110112
#
111-
# @param lines Lines read from grub.cfg file
112-
# @param fname Grub configuration file
113-
def rewrite_grub_cfg(lines, fname):
113+
# @param lines Lines read from grub/cmdline config file
114+
# @param fname Grub/cmdline configuration file
115+
def rewrite_cfg(lines, fname):
114116
fd = open(fname, "w")
115117
for x in lines:
116118
fd.writelines(x+'\n')
@@ -254,50 +256,49 @@ def write_num_dumps(num_dumps):
254256
print_err("Error while writing KDUMP_NUM_DUMPS into %s" % kdump_cfg)
255257
sys.exit(1)
256258

257-
## Command: Enable kdump - Grub mode
259+
## Enable kdump
258260
#
259261
# @param verbose If True, the function will display a few additinal information
260-
# @return True is the grub configuration has changed, and False if it has not
261-
def kdump_enable_grub(verbose, kdump_enabled, memory, num_dumps):
262+
# @return True if the grub/cmdline cfg has changed, and False if it has not
263+
def kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, cmdline_file):
262264

263-
current_img = get_current_image();
264265
if verbose:
265-
print("Current image=[%s]" % current_img)
266+
print("Enabling kdump for image=[%s]" % image)
266267
try:
267-
lines = [line.rstrip('\n') for line in open(grub_cfg)]
268+
lines = [line.rstrip('\n') for line in open(cmdline_file)]
268269
except Exception as exception:
269270
print_err(exception)
270271
sys.exit(1)
271-
current_img_index = locate_image(lines, "loop=image-"+current_img)
272+
img_index = locate_image(lines, "loop=image-"+image)
272273
if verbose:
273-
print("Image index in grub.cfg=%d" % current_img_index)
274+
print("Image index in %s=%d" % (cmdline_file, img_index))
274275

275276
changed = False
276277
crash_kernel_in_cmdline = search_for_crash_kernel_in_cmdline()
277278
if verbose:
278279
print("crash_kernel_in_cmdline=[%s]" % crash_kernel_in_cmdline)
279-
curr_crash_kernel_mem = search_for_crash_kernel(lines[current_img_index])
280+
crash_kernel_mem = search_for_crash_kernel(lines[img_index])
280281
if verbose:
281-
print("curr_crash_kernel_mem=[%s]" % curr_crash_kernel_mem)
282-
if curr_crash_kernel_mem == None:
283-
lines[current_img_index] += " crashkernel=%s" % memory
282+
print("crash_kernel_mem=[%s]" % crash_kernel_mem)
283+
if crash_kernel_mem == None:
284+
lines[img_index] += " crashkernel=%s" % memory
284285
changed = True
285286
if verbose:
286-
print("Added to grub.cfg: [ crashkernel=%s ]" % memory)
287+
print("Added to %s: [ crashkernel=%s ]" % (cmdline_file, memory))
287288
else:
288-
if curr_crash_kernel_mem == memory:
289-
if curr_crash_kernel_mem == crash_kernel_in_cmdline:
289+
if crash_kernel_mem == memory:
290+
if crash_kernel_mem == crash_kernel_in_cmdline:
290291
print("kdump is already enabled")
291292
else:
292293
changed = True
293294
else:
294-
lines[current_img_index] = lines[current_img_index].replace(curr_crash_kernel_mem, memory)
295+
lines[img_index] = lines[img_index].replace(crash_kernel_mem, memory)
295296
changed = True
296297
if verbose:
297-
print("Replace [%s] with [%s] in grub.cfg" % (curr_crash_kernel_mem, memory))
298+
print("Replace [%s] with [%s] in %s" % (crash_kernel_mem, memory, cmdline_file))
298299

299300
if changed:
300-
rewrite_grub_cfg(lines, grub_cfg)
301+
rewrite_cfg(lines, cmdline_file)
301302

302303
write_use_kdump(1)
303304

@@ -306,8 +307,9 @@ def kdump_enable_grub(verbose, kdump_enabled, memory, num_dumps):
306307
## Command: Enable kdump
307308
#
308309
# @param verbose If True, the function will display a few additinal information
309-
# @return True is the grub configuration has changed, and False if it has not
310-
def cmd_kdump_enable(verbose):
310+
# @param image The image on which kdump settings are changed
311+
# @return True if the grub/cmdline cfg has changed, and False if it has not
312+
def cmd_kdump_enable(verbose, image=get_current_image()):
311313

312314
kdump_enabled = get_kdump_administrative_mode()
313315
memory = get_kdump_memory()
@@ -316,107 +318,51 @@ def cmd_kdump_enable(verbose):
316318
print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps))
317319

318320
if os.path.exists(grub_cfg):
319-
return kdump_enable_grub(verbose, kdump_enabled, memory, num_dumps)
321+
return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg)
322+
elif open(machine_cfg, 'r').read().find('aboot_platform') >= 0:
323+
aboot_cfg = aboot_cfg_template % image
324+
return kdump_enable(verbose, kdump_enabled, memory, num_dumps, image, aboot_cfg)
320325
else:
321326
print("Feature not supported on this platform")
322-
run_command("config kdump disable", use_shell=False);
323327
return False
324328

325-
## Command: Enable kdump on Next image only - Grub mode
326-
#
327-
# @param verbose If True, the function will display a few additional information
328-
# @return True is the grub configuration has changed, and False if it has not
329-
def kdump_config_next_grub(verbose, kdump_enabled, memory, num_dumps):
330-
next_img = get_next_image();
331-
if verbose:
332-
print("Next image=[%s]" % next_img)
333-
try:
334-
lines = [line.rstrip('\n') for line in open(grub_cfg)]
335-
except Exception as exception:
336-
print_err(exception)
337-
sys.exit(1)
338-
next_img_index = locate_image(lines, "loop=image-"+next_img)
339-
if verbose:
340-
print("Image index in grub.cfg=%d" % next_img_index)
341-
342-
changed = False
343-
crash_kernel_in_cmdline = search_for_crash_kernel_in_cmdline()
344-
if verbose:
345-
print("crash_kernel_in_cmdline=[%s]" % crash_kernel_in_cmdline)
346-
curr_crash_kernel_mem = search_for_crash_kernel(lines[next_img_index])
347-
if verbose:
348-
print("curr_crash_kernel_mem=[%s]" % curr_crash_kernel_mem)
349-
if curr_crash_kernel_mem == None:
350-
lines[next_img_index] += " crashkernel=%s" % memory
351-
changed = True
352-
if verbose:
353-
print("Added to grub.cfg: [ crashkernel=%s ]" % memory)
354-
else:
355-
if curr_crash_kernel_mem == memory:
356-
if curr_crash_kernel_mem == crash_kernel_in_cmdline:
357-
print("kdump is already enabled")
358-
else:
359-
changed = True
360-
else:
361-
lines[next_img_index] = lines[next_img_index].replace(curr_crash_kernel_mem, memory)
362-
changed = True
363-
if verbose:
364-
print("Replace [%s] with [%s] in grub.cfg" % (curr_crash_kernel_mem, memory))
365-
366-
if changed:
367-
rewrite_grub_cfg(lines, grub_cfg)
368-
369-
write_use_kdump(1)
370-
371-
return changed
372-
373329
## Command: Enable kdump on Next image only
374330
#
375331
# @param verbose If True, the function will display a few additional information
376-
# @return True is the grub configuration has changed, and False if it has not
332+
# @param image The image on which kdump settings are changed
333+
# @return True if the grub/cmdline cfg has changed, and False if it has not
377334
def cmd_kdump_config_next(verbose):
335+
return cmd_kdump_enable(verbose, image=get_next_image())
378336

379-
kdump_enabled = get_kdump_administrative_mode()
380-
memory = get_kdump_memory()
381-
num_dumps = get_kdump_num_dumps()
382-
if verbose:
383-
print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps))
384-
385-
if os.path.exists(grub_cfg):
386-
return kdump_config_next_grub(verbose, kdump_enabled, memory, num_dumps)
387-
else:
388-
return False
389-
390-
## Command: Disable kdump - Grub mode
337+
## Disable kdump
391338
#
392339
# @param verbose If True, the function will display a few additional information
393-
def kdump_disable_grub(verbose, kdump_enabled, memory, num_dumps):
340+
def kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, cmdline_file):
394341
write_use_kdump(0)
395342

396-
current_img = get_current_image();
397343
if verbose:
398-
print("Current image=[%s]\n" % current_img)
399-
lines = [line.rstrip('\n') for line in open(grub_cfg)]
400-
current_img_index = locate_image(lines, "loop=image-"+current_img)
344+
print("Disabling kdump for image=[%s]\n" % image)
345+
lines = [line.rstrip('\n') for line in open(cmdline_file)]
346+
img_index = locate_image(lines, "loop=image-"+image)
401347

402348
changed = False
403-
curr_crash_kernel_mem = search_for_crash_kernel(lines[current_img_index])
404-
if curr_crash_kernel_mem == None:
349+
crash_kernel_mem = search_for_crash_kernel(lines[img_index])
350+
if crash_kernel_mem == None:
405351
print("kdump is already disabled")
406352
else:
407-
lines[current_img_index] = lines[current_img_index].replace("crashkernel="+curr_crash_kernel_mem, "")
353+
lines[img_index] = lines[img_index].replace("crashkernel="+crash_kernel_mem, "")
408354
changed = True
409355
if verbose:
410-
print("Removed [%s] in grub.cfg" % ("crashkernel="+curr_crash_kernel_mem))
356+
print("Removed [%s] in %s" % ("crashkernel="+crash_kernel_mem, cmdline_file))
411357

412358
if changed:
413-
rewrite_grub_cfg(lines, grub_cfg)
414-
359+
rewrite_cfg(lines, cmdline_file)
415360

416361
## Command: Disable kdump
417362
#
418363
# @param verbose If True, the function will display a few additional information
419-
def cmd_kdump_disable(verbose):
364+
# @param image The image on which kdump settings are changed
365+
def cmd_kdump_disable(verbose, image=get_current_image()):
420366

421367
kdump_enabled = get_kdump_administrative_mode()
422368
memory = get_kdump_memory()
@@ -425,8 +371,12 @@ def cmd_kdump_disable(verbose):
425371
print("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled, memory, num_dumps))
426372

427373
if os.path.exists(grub_cfg):
428-
return kdump_disable_grub(verbose, kdump_enabled, memory, num_dumps)
374+
return kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, grub_cfg)
375+
elif open(machine_cfg, 'r').read().find('aboot_platform') >= 0:
376+
aboot_cfg = aboot_cfg_template % image
377+
return kdump_disable(verbose, kdump_enabled, memory, num_dumps, image, aboot_cfg)
429378
else:
379+
print("Feature not supported on this platform")
430380
return False
431381

432382
## Command: Set / Get memory

0 commit comments

Comments
 (0)