@@ -25,8 +25,10 @@ import subprocess
25
25
import errno
26
26
from swsssdk import ConfigDBConnector
27
27
28
+ aboot_cfg_template = "/host/image-%s/kernel-cmdline"
28
29
grub_cfg = "/host/grub/grub.cfg"
29
30
kdump_cfg = "/etc/default/kdump-tools"
31
+ machine_cfg = "/host/machine.conf"
30
32
31
33
## Same as print(), but output to stderr instead of stdout
32
34
def print_err (* args ):
@@ -94,7 +96,7 @@ def get_next_image():
94
96
95
97
## Search for Current/Next SONiC image in grub configuration
96
98
#
97
- # @param lines Lines read from grub.cfg file
99
+ # @param lines Lines read from grub.cfg/cmdline file
98
100
# @param img String we are looking for ("loop=image...")
99
101
# @return Index in lines array wehere we found the string
100
102
def locate_image (lines , img ):
@@ -106,11 +108,11 @@ def locate_image(lines, img):
106
108
pass
107
109
return - 1
108
110
109
- ## Rewrite grub configuration file
111
+ ## Rewrite grub/cmdline configuration file
110
112
#
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 ):
114
116
fd = open (fname , "w" )
115
117
for x in lines :
116
118
fd .writelines (x + '\n ' )
@@ -254,50 +256,49 @@ def write_num_dumps(num_dumps):
254
256
print_err ("Error while writing KDUMP_NUM_DUMPS into %s" % kdump_cfg )
255
257
sys .exit (1 )
256
258
257
- ## Command: Enable kdump - Grub mode
259
+ ## Enable kdump
258
260
#
259
261
# @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 ):
262
264
263
- current_img = get_current_image ();
264
265
if verbose :
265
- print ("Current image=[%s]" % current_img )
266
+ print ("Enabling kdump for image=[%s]" % image )
266
267
try :
267
- lines = [line .rstrip ('\n ' ) for line in open (grub_cfg )]
268
+ lines = [line .rstrip ('\n ' ) for line in open (cmdline_file )]
268
269
except Exception as exception :
269
270
print_err (exception )
270
271
sys .exit (1 )
271
- current_img_index = locate_image (lines , "loop=image-" + current_img )
272
+ img_index = locate_image (lines , "loop=image-" + image )
272
273
if verbose :
273
- print ("Image index in grub.cfg =%d" % current_img_index )
274
+ print ("Image index in %s =%d" % ( cmdline_file , img_index ) )
274
275
275
276
changed = False
276
277
crash_kernel_in_cmdline = search_for_crash_kernel_in_cmdline ()
277
278
if verbose :
278
279
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 ])
280
281
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
284
285
changed = True
285
286
if verbose :
286
- print ("Added to grub.cfg : [ crashkernel=%s ]" % memory )
287
+ print ("Added to %s : [ crashkernel=%s ]" % ( cmdline_file , memory ) )
287
288
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 :
290
291
print ("kdump is already enabled" )
291
292
else :
292
293
changed = True
293
294
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 )
295
296
changed = True
296
297
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 ))
298
299
299
300
if changed :
300
- rewrite_grub_cfg (lines , grub_cfg )
301
+ rewrite_cfg (lines , cmdline_file )
301
302
302
303
write_use_kdump (1 )
303
304
@@ -306,8 +307,9 @@ def kdump_enable_grub(verbose, kdump_enabled, memory, num_dumps):
306
307
## Command: Enable kdump
307
308
#
308
309
# @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 ()):
311
313
312
314
kdump_enabled = get_kdump_administrative_mode ()
313
315
memory = get_kdump_memory ()
@@ -316,107 +318,51 @@ def cmd_kdump_enable(verbose):
316
318
print ("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled , memory , num_dumps ))
317
319
318
320
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 )
320
325
else :
321
326
print ("Feature not supported on this platform" )
322
- run_command ("config kdump disable" , use_shell = False );
323
327
return False
324
328
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
-
373
329
## Command: Enable kdump on Next image only
374
330
#
375
331
# @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
377
334
def cmd_kdump_config_next (verbose ):
335
+ return cmd_kdump_enable (verbose , image = get_next_image ())
378
336
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
391
338
#
392
339
# @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 ):
394
341
write_use_kdump (0 )
395
342
396
- current_img = get_current_image ();
397
343
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 )
401
347
402
348
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 :
405
351
print ("kdump is already disabled" )
406
352
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 , "" )
408
354
changed = True
409
355
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 ))
411
357
412
358
if changed :
413
- rewrite_grub_cfg (lines , grub_cfg )
414
-
359
+ rewrite_cfg (lines , cmdline_file )
415
360
416
361
## Command: Disable kdump
417
362
#
418
363
# @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 ()):
420
366
421
367
kdump_enabled = get_kdump_administrative_mode ()
422
368
memory = get_kdump_memory ()
@@ -425,8 +371,12 @@ def cmd_kdump_disable(verbose):
425
371
print ("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled , memory , num_dumps ))
426
372
427
373
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 )
429
378
else :
379
+ print ("Feature not supported on this platform" )
430
380
return False
431
381
432
382
## Command: Set / Get memory
0 commit comments