@@ -17,12 +17,14 @@ See the License for the specific language governing permissions and
17
17
limitations under the License.
18
18
'''
19
19
20
- import sys
21
20
import argparse
22
- import shlex
21
+ import json
23
22
import os
23
+ import shlex
24
+ import sys
25
+ import syslog
24
26
import subprocess
25
- import json
27
+
26
28
from swsscommon .swsscommon import ConfigDBConnector
27
29
28
30
aboot_cfg_template = "/host/image-%s/kernel-cmdline"
@@ -437,7 +439,7 @@ def get_kdump_config_json(config_param):
437
439
# @param verbose If True, the function will display a few additinal information
438
440
# @param image The image on which kdump settings are changed
439
441
# @return True if the grub/cmdline cfg has changed, and False if it has not
440
- def cmd_kdump_enable (verbose , image = get_current_image () ):
442
+ def cmd_kdump_enable (verbose , image ):
441
443
442
444
kdump_enabled = get_kdump_administrative_mode ()
443
445
memory = get_kdump_memory ()
@@ -460,75 +462,76 @@ def cmd_kdump_enable(verbose, image=get_current_image()):
460
462
# @param image The image on which kdump settings are changed
461
463
# @return True if the grub/cmdline cfg has changed, and False if it has not
462
464
def cmd_kdump_config_next (verbose ):
463
- return cmd_kdump_enable (verbose , image = get_next_image ())
465
+ image = get_next_image ()
466
+ return cmd_kdump_enable (verbose , image )
464
467
465
- ## Disable kdump
466
- #
467
- # @param verbose If True, the function will display a few additional information
468
- # @return True if the grub/cmdline cfg has changed, and False if it has not
469
- def kdump_disable (verbose , kdump_enabled , memory , num_dumps , image , cmdline_file ):
468
+
469
+ def kdump_disable (verbose , image , booter_config_file_path ):
470
+ """Unloads Kdump kernel and remove parameter `crashkernel=*` from configuration file of
471
+ kernel boot loader.
472
+
473
+ Args:
474
+ image: A string represents SONiC image version.
475
+ booter_config_file_path: A string represents path of kernel boot loader configuration file.
476
+
477
+ Returns:
478
+ changes: If kernel booter configuration file was changed, returns True; Otherwise, returns
479
+ False.
480
+ """
470
481
write_use_kdump (0 )
471
482
472
483
if verbose :
473
484
print ("Disabling kdump for image=[%s]\n " % image )
474
- lines = [line .rstrip ('\n ' ) for line in open (cmdline_file )]
485
+
486
+ try :
487
+ with open (booter_config_file_path ) as config_file_handler :
488
+ lines = [line .rstrip ('\n ' ) for line in config_file_handler ]
489
+ except OSError as sys_err :
490
+ syslog .syslog (syslog .LOG_ERR , "Failed to open kernel booter configuration file: '{}', Error is: '{}'!"
491
+ .format (booter_config_file_path , sys_err ))
492
+ return False
493
+
475
494
img_index = locate_image (lines , "loop=image-" + image )
476
495
477
496
changed = False
497
+
478
498
crash_kernel_mem = search_for_crash_kernel (lines [img_index ])
479
499
if crash_kernel_mem is None :
480
500
print ("kdump is already disabled" )
481
501
else :
482
502
lines [img_index ] = lines [img_index ].replace ("crashkernel=" + crash_kernel_mem , "" )
483
503
changed = True
484
504
if verbose :
485
- print ("Removed [%s] in %s" % ("crashkernel=" + crash_kernel_mem , cmdline_file ))
505
+ print ("Removed [%s] in %s" % ("crashkernel=" + crash_kernel_mem , booter_config_file_path ))
486
506
487
507
if changed :
488
- rewrite_cfg (lines , cmdline_file )
508
+ rewrite_cfg (lines , booter_config_file_path )
489
509
490
510
if not os .path .exists ('/etc/sonic/config_db.json' ):
491
511
print_err ("Startup configuration not found, Kdump configuration is not saved" )
492
512
return False
493
513
494
- current_img = get_current_image ();
495
- if verbose :
496
- print ("Current image=[%s]\n " % current_img )
497
- lines = [line .rstrip ('\n ' ) for line in open (grub_cfg )]
498
- current_img_index = locate_image (lines , "loop=image-" + current_img )
499
-
500
- changed = False
501
- curr_crash_kernel_mem = search_for_crash_kernel (lines [current_img_index ])
502
- if curr_crash_kernel_mem is None :
503
- print ("Kdump is already disabled" )
504
- else :
505
- lines [current_img_index ] = lines [current_img_index ].replace ("crashkernel=" + curr_crash_kernel_mem , "" )
506
- changed = True
507
- if verbose :
508
- print ("Removed [%s] in grub.cfg" % ("crashkernel=" + curr_crash_kernel_mem ))
509
-
510
- if changed :
511
- rewrite_grub_cfg (lines , grub_cfg )
512
-
513
514
return changed
514
515
516
+
515
517
## Command: Disable kdump
516
518
#
517
519
# @param verbose If True, the function will display a few additional information
518
- # @param image The image on which kdump settings are changed
519
- def cmd_kdump_disable (verbose , image = get_current_image ()):
520
+ def cmd_kdump_disable (verbose ):
520
521
522
+ image = get_current_image ()
521
523
kdump_enabled = get_kdump_administrative_mode ()
522
524
memory = get_kdump_memory ()
523
525
num_dumps = get_kdump_num_dumps ()
526
+
524
527
if verbose :
525
528
print ("configDB: kdump_enabled=%d memory=[%s] num_nums=%d" % (kdump_enabled , memory , num_dumps ))
526
529
527
530
if os .path .exists (grub_cfg ):
528
- return kdump_disable (verbose , kdump_enabled , memory , num_dumps , image , grub_cfg )
531
+ return kdump_disable (verbose , image , grub_cfg )
529
532
elif open (machine_cfg , 'r' ).read ().find ('aboot_platform' ) >= 0 :
530
533
aboot_cfg = aboot_cfg_template % image
531
- return kdump_disable (verbose , kdump_enabled , memory , num_dumps , image , aboot_cfg )
534
+ return kdump_disable (verbose , image , aboot_cfg )
532
535
else :
533
536
print ("Feature not supported on this platform" )
534
537
return False
@@ -549,7 +552,8 @@ def cmd_kdump_memory(verbose, memory):
549
552
memory_in_db = get_kdump_memory ()
550
553
memory_in_json = get_kdump_config_json ("memory" )
551
554
if memory != crash_kernel_in_cmdline or memory != memory_in_db or memory != memory_in_json :
552
- cmd_kdump_enable (verbose )
555
+ image = get_current_image ()
556
+ cmd_kdump_enable (verbose , image )
553
557
print ("Kdump updated memory will be only operational after the system reboots" )
554
558
else :
555
559
num_dumps = get_kdump_num_dumps ()
@@ -635,7 +639,8 @@ def main():
635
639
changed = False
636
640
try :
637
641
if options .enable :
638
- changed = cmd_kdump_enable (options .verbose )
642
+ image = get_current_image ()
643
+ changed = cmd_kdump_enable (options .verbose , image )
639
644
elif options .config_next :
640
645
changed = cmd_kdump_config_next (options .verbose )
641
646
elif options .disable :
0 commit comments