Skip to content

Commit a09889e

Browse files
authored
[action] [PR:237] [kdump-config] Update kdump default config with pre-defined value in /proc/cmdline (installer.conf) (#315)
#### Why I did it Modify the hostcfgd to handle the kdump enabled to the CONFIG_DB when kdump is enabled by default in the platform installer.conf . Related PR for 202405 is: #236 ### How I did it Added a new method init_kdump_config_from_cmdline() in the KdumpCfg class to check the /proc/cmdline. If key "crashkernel=" exists in /proc/cmdline when system is up, this means that the kdump is enabled. init_kdump_config_from_cmdline() will update the kdump_defaults and CONFIG_DB when if the current config data are not matched the pre-defined data in the /proc/cmdline. This happens during a new image upgrade with the crashkernel is defined the platform installer.conf. ### How to verify it 1) install an image with predefine kdump enabled in the grub.conf. 2) execute the "config load_minigraph" - check it should not any error in the syslog - After the hostcfgd is running, execute "show kdump config" should show kdump is enabled. Output as below ``` admin@sonic:~$ show kdump config Kdump administrative mode: Enabled Kdump operational mode: Ready Kdump memory reservation: 8G-:1G Maximum number of Kdump files: 3 ``` 3) save config and reboot the system 4) show command should show the kdump is enabled and also there should not be any kdump/hostcfgd error log in syslog ### Which release branch to backport (provide reason below if selected) - [ ] 201811 - [ ] 201911 - [ ] 202006 - [ ] 202012 - [ ] 202106 - [ ] 202111 - [ ] 202205 - [ ] 202211 - [ ] 202305 - [x] 202405
1 parent 7341ba6 commit a09889e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

scripts/hostcfgd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,40 @@ class KdumpCfg(object):
11371137
"memory": "0M-2G:256M,2G-4G:320M,4G-8G:384M,8G-:448M",
11381138
"num_dumps": "3" }
11391139

1140+
# check if kdump is enabled by default with grub config
1141+
self.update_config_from_proc_cmdline = False
1142+
self.init_kdump_config_from_cmdline()
1143+
1144+
def init_kdump_config_from_cmdline(self):
1145+
# Update the kdump_defaults with value found /proc/cmdline if
1146+
# key "crashkernel=" is pre-defined in and /proc/cmdline (grub.conf)
1147+
if os.environ.get("HOSTCFGD_UNIT_TESTING") == "2":
1148+
modules_path = os.path.join(os.path.dirname(__file__), "..")
1149+
tests_path = os.path.join(modules_path, "tests")
1150+
cmdline = os.path.join(tests_path, "proc","cmdline")
1151+
else:
1152+
cmdline = "/proc/cmdline"
1153+
1154+
expected_str = ' crashkernel='
1155+
if os.path.exists(cmdline):
1156+
lines= [line.rstrip('\n') for line in open(cmdline)]
1157+
p = lines[0].find(expected_str)
1158+
if p == -1:
1159+
return
1160+
kdump_config = self.config_db.get_entry("KDUMP", "config")
1161+
syslog.syslog(syslog.LOG_INFO, "kdump enabled is found in /proc/cmdline")
1162+
self.kdump_defaults["enabled"] = "true"
1163+
next_space = lines[0].find(" ", p+1)
1164+
if next_space == -1:
1165+
memory = lines[0][p+len(expected_str):]
1166+
else:
1167+
memory = lines[0][p+len(expected_str):next_space]
1168+
self.kdump_defaults["memory"] = memory
1169+
if not kdump_config or kdump_config.get("enabled") != self.kdump_defaults["enabled"] or kdump_config.get("memory") != self.kdump_defaults["memory"]:
1170+
self.update_config_from_proc_cmdline = True
1171+
syslog.syslog(syslog.LOG_INFO, "Update KDUMP config entry with data from /proc/cmdline")
1172+
self.config_db.mod_entry("KDUMP", "config", self.kdump_defaults)
1173+
11401174
def load(self, kdump_table):
11411175
"""
11421176
Set the KDUMP table in CFG DB to kdump_defaults if not set by the user
@@ -1155,6 +1189,10 @@ class KdumpCfg(object):
11551189

11561190
def kdump_update(self, key, data):
11571191
syslog.syslog(syslog.LOG_INFO, "Kdump global configuration update")
1192+
if self.update_config_from_proc_cmdline and os.environ.get("HOSTCFGD_UNIT_TESTING") != "2":
1193+
self.update_config_from_proc_cmdline = False
1194+
syslog.syslog(syslog.LOG_INFO, "Kdump is enabled by default with /proc/cmdline. Skip the first update")
1195+
return
11581196
if key == "config":
11591197
# Admin mode
11601198
kdump_enabled = self.kdump_defaults["enabled"]

tests/hostcfgd/hostcfgd_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,31 @@ def test_kdump_load(self):
236236

237237
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)
238238

239+
def test_kdump_event_with_proc_cmdline(self):
240+
os.environ["HOSTCFGD_UNIT_TESTING"] = "2"
241+
MockConfigDb.set_config_db(HOSTCFG_DAEMON_CFG_DB)
242+
daemon = hostcfgd.HostConfigDaemon()
243+
default=daemon.kdumpCfg.kdump_defaults
244+
daemon.kdumpCfg.load(default)
245+
daemon.register_callbacks()
246+
MockConfigDb.event_queue = [('KDUMP', 'config')]
247+
with mock.patch('hostcfgd.subprocess') as mocked_subprocess:
248+
popen_mock = mock.Mock()
249+
attrs = {'communicate.return_value': ('output', 'error')}
250+
popen_mock.configure_mock(**attrs)
251+
mocked_subprocess.Popen.return_value = popen_mock
252+
try:
253+
daemon.start()
254+
except TimeoutError:
255+
pass
256+
expected = [
257+
call(['sonic-kdump-config', '--enable']),
258+
call(['sonic-kdump-config', '--num_dumps', '3']),
259+
call(['sonic-kdump-config', '--memory', '8G-:1G'])
260+
]
261+
mocked_subprocess.check_call.assert_has_calls(expected, any_order=True)
262+
os.environ["HOSTCFGD_UNIT_TESTING"] = ""
263+
239264
def test_devicemeta_event(self):
240265
"""
241266
Test handling DEVICE_METADATA events.

tests/proc/cmdline

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BOOT_IMAGE=/image-202405.0-dirty-20250403.162657/boot/vmlinuz-6.1.0-22-2-amd64 root=UUID=bba2ec4f-9141-4069-a41e-230ecac4a5fb rw console=tty0 console=ttyS0,115200n8 quiet processor.max_cstate=1 amd_idle.max_cstate=0 sonic_fips=1 net.ifnames=0 biosdevname=0 loop=image-202405.0-dirty-20250403.162657/fs.squashfs loopfstype=squashfs systemd.unified_cgroup_hierarchy=0 apparmor=1 security=apparmor varlog_size=4096 usbcore.autosuspend=-1 amd_iommu=off pci=resource_alignment=48@00:03.1 crashkernel=8G-:1G

0 commit comments

Comments
 (0)