Skip to content

Commit 1f3e35e

Browse files
authored
Support Ubuntu 24.04 server in KVM (#17883)
What is the motivation for this PR? When running with Ubuntu 24.04 host, many tasks from a sonic-mgmt container may fail. One reason is that a newer version of Python being used in Ubuntu 24.04 that doesn't support some of the packages that ansible modules attempt to import. Such as imp module is removed in Python 3.11, need to use importlib, and PR #16039 did some replacement but still need to replace in somewhere else. Another reason is that some ansible modules must be running in a python3 venv for scecurity reason. Also there are some access issues need to fix. How did you do it? Try to use import importlib instead of importing imp Install python3-venv in Ubuntu 24.04 server Update libvirt qemu configuration in Ubuntu 24.04 server * fix * remove --------- Co-authored-by: xwjiang2021 <[email protected]>
1 parent 9f2a35f commit 1f3e35e

File tree

6 files changed

+62
-13
lines changed

6 files changed

+62
-13
lines changed

ansible/devutil/conn_graph_helper.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
import os
22
import inspect
33
import sys
4-
import imp
4+
try:
5+
import importlib.util
6+
import importlib.machinery
7+
use_importlib = True
8+
except ImportError:
9+
import imp
10+
use_importlib = False
511

612
CONN_GRAPH_LOG = "/tmp/conn_graph_debug.txt"
713

814

15+
def load_source(modname, filename):
16+
if use_importlib:
17+
loader = importlib.machinery.SourceFileLoader(modname, filename)
18+
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
19+
module = importlib.util.module_from_spec(spec)
20+
# The module is always executed and not cached in sys.modules.
21+
# Uncomment the following line to cache the module.
22+
# sys.modules[module.__name__] = module
23+
loader.exec_module(module)
24+
else:
25+
# For Python 2.x compatibility
26+
module = imp.load_source(modname, filename)
27+
return module
28+
29+
930
def get_conn_graph_facts(hostnames):
1031
"""
1132
@summary: Load conn_graph_facts from conn_graph_facts.xml
@@ -18,7 +39,7 @@ def get_conn_graph_facts(hostnames):
1839
if ansible_path not in sys.path:
1940
sys.path.append(ansible_path)
2041

21-
utils = imp.load_source('conn_graph_utils', os.path.join(
42+
utils = load_source('conn_graph_utils', os.path.join(
2243
ansible_path, 'library/conn_graph_facts.py'))
2344
utils.LAB_GRAPHFILE_PATH = os.path.join(
2445
ansible_path, utils.LAB_GRAPHFILE_PATH)

ansible/health_checker.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env python3
2-
3-
import imp
42
import os
53
import logging
64
import sys
@@ -13,6 +11,7 @@
1311
except ImportError:
1412
# ToDo: Support running without Ansible
1513
has_ansible = False
14+
from devutil.conn_graph_helper import load_source
1615

1716
ANSIBLE_DIR = os.path.abspath(os.path.dirname(__file__))
1817
SONIC_MGMT_DIR = os.path.dirname(ANSIBLE_DIR)
@@ -35,7 +34,7 @@
3534

3635
def get_testbeds_dict():
3736
"""Return a dictionary containing mapping from dut hostname to testbed name."""
38-
testbed = imp.load_source('testbed', os.path.join(
37+
testbed = load_source('testbed', os.path.join(
3938
SONIC_MGMT_DIR, 'tests/common/testbed.py'))
4039
testbeds_dict = testbed.TestbedInfo(TESTBED_FILE).testbed_topo
4140
return testbeds_dict

ansible/recover_server.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
import argparse
1111
import collections
1212
import datetime
13-
import imp
1413
import logging
1514
import os
1615
import subprocess
1716
import sys
1817
import tempfile
1918
import threading
2019
import time
21-
20+
from devutil.conn_graph_helper import load_source
2221
from tabulate import tabulate
2322
# Add tests path to syspath
2423
sys.path.append('../')
@@ -40,7 +39,7 @@
4039

4140
def parse_testbed(testbedfile, testbed_servers):
4241
"""Return a dictionary containing mapping from server name to testbeds."""
43-
testbed = imp.load_source('testbed', os.path.join(
42+
testbed = load_source('testbed', os.path.join(
4443
SONIC_MGMT_DIR, 'tests/common/testbed.py'))
4544
testbeds = {server_name: list() for server_name in testbed_servers}
4645
for tbname, tb in testbed.TestbedInfo(testbedfile).testbed_topo.items():
@@ -258,7 +257,7 @@ def _join_all(threads):
258257
break
259258
time.sleep(5)
260259

261-
utilities = imp.load_source('utilities', os.path.join(
260+
utilities = load_source('utilities', os.path.join(
262261
SONIC_MGMT_DIR, 'tests/common/utilities.py'))
263262

264263
curr_date = datetime.datetime.today().strftime('%Y-%m-%d_%H-%M-%S')

ansible/restart_nightly_ptf.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
import argparse
44
import logging
5-
import imp
65
import os
76
import recover_server
87
import sys
98
import collections
109
import datetime
1110
import time
1211
import tempfile
13-
12+
from devutil.conn_graph_helper import load_source
1413
# Add tests path to syspath
1514
sys.path.append('../')
1615

@@ -69,7 +68,7 @@ def __call__(self):
6968

7069
def parse_testbed(testbedfile, servers):
7170
"""Return a dictionary containing mapping from server name to nightly testbeds that need restart-ptf."""
72-
testbed = imp.load_source('testbed', os.path.join(
71+
testbed = load_source('testbed', os.path.join(
7372
SONIC_MGMT_DIR, 'tests/common/testbed.py'))
7473
all_testbeds = testbed.TestbedInfo(testbedfile).testbed_topo
7574
nightly_dir = os.path.join(SONIC_MGMT_DIR, ".azure-pipelines", "nightly")
@@ -117,7 +116,7 @@ def _join_all(threads):
117116
break
118117
time.sleep(5)
119118

120-
utilities = imp.load_source('utilities', os.path.join(
119+
utilities = load_source('utilities', os.path.join(
121120
SONIC_MGMT_DIR, 'tests/common/utilities.py'))
122121

123122
curr_date = datetime.datetime.today().strftime('%Y-%m-%d_%H-%M-%S')

ansible/roles/vm_set/tasks/docker.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@
106106

107107
- name: Update python3 packages
108108
block:
109+
- name: Ensure python3-venv is installed
110+
apt:
111+
name: python3-venv
112+
state: present
113+
update_cache: yes
114+
become: yes
109115
- name: Install python packages
110116
pip: name=docker version=7.1.0 state=forcereinstall virtualenv=/tmp/sonic-mgmt-virtualenv virtualenv_site_packages=true virtualenv_command="python3 -m venv"
111117
become: yes

ansible/roles/vm_set/tasks/main.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,31 @@
160160
become: yes
161161
when: host_distribution_version.stdout >= "20.04"
162162

163+
- name: Update libvirt qemu configuration
164+
block:
165+
- name: Set user to root in qemu.conf
166+
lineinfile:
167+
path: /etc/libvirt/qemu.conf
168+
regexp: '^#?user\s*=.*'
169+
line: 'user = "root"'
170+
state: present
171+
backrefs: yes
172+
become: yes
173+
- name: Set group to root in qemu.conf
174+
lineinfile:
175+
path: /etc/libvirt/qemu.conf
176+
regexp: '^#?group\s*=.*'
177+
line: 'group = "root"'
178+
state: present
179+
backrefs: yes
180+
become: yes
181+
- name: Restart libvirtd to apply qemu.conf changes
182+
service:
183+
name: libvirtd
184+
state: restarted
185+
become: yes
186+
when: host_distribution_version.stdout >= "24.04"
187+
163188
- name: Install br_netfilter kernel module
164189
become: yes
165190
modprobe: name=br_netfilter state=present

0 commit comments

Comments
 (0)