|
6 | 6 | import re |
7 | 7 | import time |
8 | 8 | from typing import List, Tuple |
| 9 | +import configparser |
9 | 10 |
|
10 | 11 | import appdirs |
11 | 12 | from commonwealth.utils.commands import run_command, save_file, locate_file, load_file |
@@ -442,6 +443,56 @@ def fix_wpa_service() -> bool: |
442 | 443 | return True |
443 | 444 |
|
444 | 445 |
|
| 446 | +def configure_network_manager() -> bool: |
| 447 | + """ |
| 448 | + Ensures NetworkManager.conf has [main] section with dns=none set if dns is not already configured |
| 449 | + """ |
| 450 | + logger.info("Configuring NetworkManager DNS settings...") |
| 451 | + file_path = "/etc/NetworkManager/NetworkManager.conf" |
| 452 | + |
| 453 | + config = configparser.ConfigParser() |
| 454 | + |
| 455 | + # Try to read existing file |
| 456 | + result = run_command(f"test -f {file_path} && cat {file_path}", check=False) |
| 457 | + if result.returncode != 0: |
| 458 | + # File doesn't exist, create with template |
| 459 | + content = """[main] |
| 460 | +plugins=ifupdown,keyfile |
| 461 | +dns=none |
| 462 | +
|
| 463 | +[ifupdown] |
| 464 | +managed=false |
| 465 | +
|
| 466 | +[device] |
| 467 | +wifi.scan-rand-mac-address=no |
| 468 | +""" |
| 469 | + run_command(f"echo '{content}' | sudo tee {file_path}", check=False) |
| 470 | + return True |
| 471 | + |
| 472 | + config.read_string(result.stdout) |
| 473 | + |
| 474 | + # Check if we need to make changes |
| 475 | + if "main" in config.sections() and "dns" in config["main"]: |
| 476 | + return False |
| 477 | + |
| 478 | + # Add our settings if needed |
| 479 | + if "main" not in config: |
| 480 | + config.add_section("main") |
| 481 | + if "dns" not in config["main"]: |
| 482 | + config["main"]["dns"] = "none" |
| 483 | + |
| 484 | + # Write back if changes were made |
| 485 | + content = "" |
| 486 | + for section in config.sections(): |
| 487 | + content += f"[{section}]\n" |
| 488 | + for key, value in config[section].items(): |
| 489 | + content += f"{key}={value}\n" |
| 490 | + content += "\n" |
| 491 | + |
| 492 | + run_command(f"echo '{content}' | sudo tee {file_path}", check=False) |
| 493 | + return True |
| 494 | + |
| 495 | + |
445 | 496 | def main() -> int: |
446 | 497 | start = time.time() |
447 | 498 | # check if boot_loop_detector exists |
@@ -492,7 +543,7 @@ def main() -> int: |
492 | 543 | ] |
493 | 544 | ) |
494 | 545 | if host_os == HostOs.Bookworm: |
495 | | - patches_to_apply.extend([("wpa", fix_wpa_service)]) |
| 546 | + patches_to_apply.extend([("wpa", fix_wpa_service), ("networkmanager", configure_network_manager)]) |
496 | 547 |
|
497 | 548 | logger.info("The following patches will be applied if needed:") |
498 | 549 | for name, patch in patches_to_apply: |
|
0 commit comments