Skip to content

Commit 8fa076d

Browse files
authored
sonic-installer: enhance next image detection for Aboot (sonic-net#3433)
The Aboot bootloader relies of the SWI= keyword argument in the boot-config file to know which image to boot. This value is also used by sonic-installer to figure to extract the next image that will be executed. The current code has an issue as it only expects the next image to match the installation path of a SONiC image but not anything else. This means that `SWI=flash:sonic-aboot-broadcom.swi` is not valid and can therefore be a problem when trying to install a new image via cold reboot. Additionally a missing or empty boot-config would generate a python backtrace instead of gracefully recovering from this state.
1 parent a7897d1 commit 8fa076d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

sonic_installer/bootloader/aboot.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class AbootBootloader(Bootloader):
7171

7272
def _boot_config_read(self, path=BOOT_CONFIG_PATH):
7373
config = collections.OrderedDict()
74+
if not os.path.exists(path):
75+
return config
7476
with open(path) as f:
7577
for line in f.readlines():
7678
line = line.strip()
@@ -112,7 +114,10 @@ def get_installed_images(self):
112114

113115
def get_next_image(self):
114116
config = self._boot_config_read()
115-
match = re.search(r"flash:/*(\S+)/", config['SWI'])
117+
swi = config.get('SWI', '')
118+
match = re.search(r"flash:/*(\S+)/", swi)
119+
if not match:
120+
return swi.split(':', 1)[-1]
116121
return match.group(1).replace(IMAGE_DIR_PREFIX, IMAGE_PREFIX, 1)
117122

118123
def set_default_image(self, image):

tests/installer_bootloader_aboot_test.py

+17-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
# Constants
1010
image_dir = f'{aboot.IMAGE_DIR_PREFIX}expeliarmus-{aboot.IMAGE_DIR_PREFIX}abcde'
11+
image_chainloader = f'{image_dir}/.sonic-boot.swi'
1112
exp_image = f'{aboot.IMAGE_PREFIX}expeliarmus-{aboot.IMAGE_DIR_PREFIX}abcde'
1213
image_dirs = [image_dir]
1314

@@ -45,15 +46,27 @@ def test_get_installed_images():
4546
assert bootloader.get_installed_images() == [exp_image]
4647

4748

48-
@patch("sonic_installer.bootloader.aboot.re.search")
49-
def test_get_next_image(re_search_patch):
49+
def test_get_next_image():
5050
bootloader = aboot.AbootBootloader()
51-
bootloader._boot_config_read = Mock(return_value={'SWI': None})
51+
52+
# Test missing boot-config
53+
bootloader._boot_config_read()
54+
55+
# Test missing SWI value
56+
bootloader._boot_config_read = Mock(return_value={})
57+
assert bootloader.get_next_image() == ''
5258

5359
# Test convertion image dir to image name
54-
re_search_patch().group = Mock(return_value=image_dir)
60+
swi = f'flash:{image_chainloader}'
61+
bootloader._boot_config_read = Mock(return_value={'SWI': swi})
5562
assert bootloader.get_next_image() == exp_image
5663

64+
# Test some other image
65+
next_image = 'EOS.swi'
66+
bootloader._boot_config_read = Mock(return_value={'SWI': f'flash:{next_image}'})
67+
assert bootloader.get_next_image() == next_image
68+
69+
5770
def test_install_image():
5871
image_path = 'sonic'
5972
env = os.environ.copy()

0 commit comments

Comments
 (0)