Skip to content

Commit f1086ee

Browse files
authored
[sonic_installer]Add --skip-platform-check option for sonic_installer when image mismatch (#1791)
What I did Add --skip-platform-check option for sonic_installer; How I did it Add --skip-platform-check option for sonic_installer when image ASIC mismatch; Split verify_binary_image to verify_secureboot_image and verify_image_platform to handle image verification more accurately. How to verify it Installing a bin file which differs the running platform's ASIC will fail. Previous command output (if the output of a command-line utility has changed) sudo sonic-installer install --help Usage: sonic-installer install [OPTIONS] URL Install image from local binary or URL Options: -y, --yes -f, --force_install Force installation of an image of a type which differs from that of the current running image --skip_migration Do not migrate current configuration to the newly installed image --skip-package-migration Do not migrate current packages to the newly installed image --help Show this message and exit. New command output (if the output of a command-line utility has changed) Options: -y, --yes -f, --force, --skip-secure-check Force installation of an image of a non- secure type than secure running image --skip-platform-check Force installation of an image of a type which is not of the same platform --skip_migration Do not migrate current configuration to the newly installed image --skip-package-migration Do not migrate current packages to the newly installed image --help Show this message and exit.
1 parent c007d65 commit f1086ee

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed

sonic_installer/bootloader/aboot.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ def get_binary_image_version(self, image_path):
163163
return None
164164
return IMAGE_PREFIX + version.strip()
165165

166-
def verify_binary_image(self, image_path):
166+
def verify_image_platform(self, image_path):
167+
return os.path.isfile(image_path)
168+
169+
def verify_secureboot_image(self, image_path):
167170
try:
168171
subprocess.check_call(['/usr/bin/unzip', '-tq', image_path])
169172
return self._verify_secureboot_image(image_path)

sonic_installer/bootloader/bootloader.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ def get_binary_image_version(self, image_path):
4949
"""returns the version of the image"""
5050
raise NotImplementedError
5151

52-
def verify_binary_image(self, image_path):
53-
"""verify that the image is supported by the bootloader"""
52+
def verify_image_platform(self, image_path):
53+
"""verify that the image is of the same platform than running platform"""
54+
raise NotImplementedError
55+
56+
def verify_secureboot_image(self, image_path):
57+
"""verify that the image is secure running image"""
5458
raise NotImplementedError
5559

5660
def verify_next_image(self):

sonic_installer/bootloader/grub.py

+34
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88

99
import click
1010

11+
from sonic_py_common import device_info
1112
from ..common import (
1213
HOST_PATH,
1314
IMAGE_DIR_PREFIX,
1415
IMAGE_PREFIX,
1516
run_command,
1617
)
1718
from .onie import OnieInstallerBootloader
19+
from .onie import default_sigpipe
20+
21+
MACHINE_CONF = "installer/machine.conf"
1822

1923
class GrubBootloader(OnieInstallerBootloader):
2024

@@ -81,6 +85,36 @@ def remove_image(self, image):
8185
run_command('grub-set-default --boot-directory=' + HOST_PATH + ' 0')
8286
click.echo('Image removed')
8387

88+
def verify_image_platform(self, image_path):
89+
if not os.path.isfile(image_path):
90+
return False
91+
92+
# Get running platform's ASIC
93+
try:
94+
version_info = device_info.get_sonic_version_info()
95+
if version_info:
96+
asic_type = version_info['asic_type']
97+
else:
98+
asic_type = None
99+
except (KeyError, TypeError) as e:
100+
click.echo("Caught an exception: " + str(e))
101+
102+
# Get installing image's ASIC
103+
p1 = subprocess.Popen(["sed", "-e", "1,/^exit_marker$/d", image_path], stdout=subprocess.PIPE, preexec_fn=default_sigpipe)
104+
p2 = subprocess.Popen(["tar", "xf", "-", MACHINE_CONF, "-O"], stdin=p1.stdout, stdout=subprocess.PIPE, preexec_fn=default_sigpipe)
105+
p3 = subprocess.Popen(["sed", "-n", r"s/^machine=\(.*\)/\1/p"], stdin=p2.stdout, stdout=subprocess.PIPE, preexec_fn=default_sigpipe, text=True)
106+
107+
stdout = p3.communicate()[0]
108+
image_asic = stdout.rstrip('\n')
109+
110+
# Return false if machine is not found or unexpected issue occur
111+
if not image_asic:
112+
return False
113+
114+
if asic_type == image_asic:
115+
return True
116+
return False
117+
84118
@classmethod
85119
def detect(cls):
86120
return os.path.isfile(os.path.join(HOST_PATH, 'grub/grub.cfg'))

sonic_installer/bootloader/onie.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ def get_binary_image_version(self, image_path):
4444

4545
return IMAGE_PREFIX + version_num
4646

47-
def verify_binary_image(self, image_path):
47+
def verify_secureboot_image(self, image_path):
4848
return os.path.isfile(image_path)

sonic_installer/bootloader/uboot.py

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ def remove_image(self, image):
7777
subprocess.call(['rm','-rf', HOST_PATH + '/' + image_dir])
7878
click.echo('Done')
7979

80+
def verify_image_platform(self, image_path):
81+
return os.path.isfile(image_path)
82+
8083
@classmethod
8184
def detect(cls):
8285
arch = platform.machine()

sonic_installer/main.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,10 @@ def sonic_installer():
480480
@sonic_installer.command('install')
481481
@click.option('-y', '--yes', is_flag=True, callback=abort_if_false,
482482
expose_value=False, prompt='New image will be installed, continue?')
483-
@click.option('-f', '--force', is_flag=True,
484-
help="Force installation of an image of a type which differs from that of the current running image")
483+
@click.option('-f', '--force', '--skip-secure-check', is_flag=True,
484+
help="Force installation of an image of a non-secure type than secure running image")
485+
@click.option('--skip-platform-check', is_flag=True,
486+
help="Force installation of an image of a type which is not of the same platform")
485487
@click.option('--skip_migration', is_flag=True,
486488
help="Do not migrate current configuration to the newly installed image")
487489
@click.option('--skip-package-migration', is_flag=True,
@@ -500,7 +502,7 @@ def sonic_installer():
500502
cls=clicommon.MutuallyExclusiveOption, mutually_exclusive=['skip_setup_swap'],
501503
callback=validate_positive_int)
502504
@click.argument('url')
503-
def install(url, force, skip_migration=False, skip_package_migration=False,
505+
def install(url, force, skip_platform_check=False, skip_migration=False, skip_package_migration=False,
504506
skip_setup_swap=False, swap_mem_size=None, total_mem_threshold=None, available_mem_threshold=None):
505507
""" Install image from local binary or URL"""
506508
bootloader = get_bootloader()
@@ -530,10 +532,17 @@ def install(url, force, skip_migration=False, skip_package_migration=False,
530532
echo_and_log('Error: Failed to set image as default', LOG_ERR)
531533
raise click.Abort()
532534
else:
533-
# Verify that the binary image is of the same type as the running image
534-
if not bootloader.verify_binary_image(image_path) and not force:
535+
# Verify not installing non-secure image in a secure running image
536+
if not bootloader.verify_secureboot_image(image_path) and not force:
535537
echo_and_log("Image file '{}' is of a different type than running image.\n".format(url) +
536-
"If you are sure you want to install this image, use -f|--force.\n" +
538+
"If you are sure you want to install this image, use -f|--force|--skip-secure-check.\n" +
539+
"Aborting...", LOG_ERR)
540+
raise click.Abort()
541+
542+
# Verify that the binary image is of the same platform type as running platform
543+
if not bootloader.verify_image_platform(image_path) and not skip_platform_check:
544+
echo_and_log("Image file '{}' is of a different platform type than running platform.\n".format(url) +
545+
"If you are sure you want to install this image, use --skip-platform-check.\n" +
537546
"Aborting...", LOG_ERR)
538547
raise click.Abort()
539548

0 commit comments

Comments
 (0)