From 500aa2a7edfaea7b0927c316f0dd955c9ca74701 Mon Sep 17 00:00:00 2001 From: Stepan Blyschak Date: Thu, 22 Dec 2022 11:54:01 +0200 Subject: [PATCH] [sonic_installer] use /etc/resolv.conf from the host when migrating packages Signed-off-by: Stepan Blyschak --- sonic_installer/main.py | 19 +++++++++++++------ tests/test_sonic_installer.py | 3 +++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/sonic_installer/main.py b/sonic_installer/main.py index 9b75db6add..ce1c15866d 100644 --- a/sonic_installer/main.py +++ b/sonic_installer/main.py @@ -318,13 +318,15 @@ def get_docker_opts(): def migrate_sonic_packages(bootloader, binary_image_version): """ Migrate SONiC packages to new SONiC image. """ + TMP_DIR = "tmp" SONIC_PACKAGE_MANAGER = "sonic-package-manager" PACKAGE_MANAGER_DIR = "/var/lib/sonic-package-manager/" DOCKER_CTL_SCRIPT = "/usr/lib/docker/docker.sh" DOCKERD_SOCK = "docker.sock" VAR_RUN_PATH = "/var/run/" + RESOLV_CONF_FILE = os.path.join("etc", "resolv.conf") + RESOLV_CONF_BACKUP_FILE = os.path.join("/", TMP_DIR, "resolv.conf.backup") - tmp_dir = "tmp" packages_file = "packages.json" packages_path = os.path.join(PACKAGE_MANAGER_DIR, packages_file) sonic_version = re.sub(IMAGE_PREFIX, '', binary_image_version, 1) @@ -332,10 +334,10 @@ def migrate_sonic_packages(bootloader, binary_image_version): new_image_upper_dir = os.path.join(new_image_dir, UPPERDIR_NAME) new_image_work_dir = os.path.join(new_image_dir, WORKDIR_NAME) new_image_docker_dir = os.path.join(new_image_dir, DOCKERDIR_NAME) - new_image_mount = os.path.join("/", tmp_dir, "image-{0}-fs".format(sonic_version)) + new_image_mount = os.path.join("/", TMP_DIR, "image-{0}-fs".format(sonic_version)) new_image_docker_mount = os.path.join(new_image_mount, "var", "lib", "docker") docker_default_config = os.path.join(new_image_mount, "etc", "default", "docker") - docker_default_config_backup = os.path.join(new_image_mount, tmp_dir, "docker_config_backup") + docker_default_config_backup = os.path.join(new_image_mount, TMP_DIR, "docker_config_backup") if not os.path.isdir(new_image_docker_dir): # NOTE: This codepath can be reached if the installation process did not @@ -370,21 +372,26 @@ def migrate_sonic_packages(bootloader, binary_image_version): run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "start"]) docker_started = True - run_command_or_raise(["cp", packages_path, os.path.join(new_image_mount, tmp_dir, packages_file)]) + run_command_or_raise(["cp", packages_path, os.path.join(new_image_mount, TMP_DIR, packages_file)]) run_command_or_raise(["touch", os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)]) run_command_or_raise(["mount", "--bind", os.path.join(VAR_RUN_PATH, DOCKERD_SOCK), os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)]) + + run_command_or_raise(["cp", os.path.join(new_image_mount, RESOLV_CONF_FILE), RESOLV_CONF_BACKUP_FILE]) + run_command_or_raise(["cp", os.path.join("/", RESOLV_CONF_FILE), os.path.join(new_image_mount, RESOLV_CONF_FILE)]) + run_command_or_raise(["chroot", new_image_mount, "sh", "-c", "command -v {}".format(SONIC_PACKAGE_MANAGER)]) run_command_or_raise(["chroot", new_image_mount, SONIC_PACKAGE_MANAGER, "migrate", - os.path.join("/", tmp_dir, packages_file), - "--dockerd-socket", os.path.join("/", tmp_dir, DOCKERD_SOCK), + os.path.join("/", TMP_DIR, packages_file), + "--dockerd-socket", os.path.join("/", TMP_DIR, DOCKERD_SOCK), "-y"]) finally: if docker_started: run_command_or_raise(["chroot", new_image_mount, DOCKER_CTL_SCRIPT, "stop"], raise_exception=False) if os.path.exists(docker_default_config_backup): run_command_or_raise(["mv", docker_default_config_backup, docker_default_config], raise_exception=False); + run_command_or_raise(["cp", RESOLV_CONF_BACKUP_FILE, os.path.join(new_image_mount, RESOLV_CONF_FILE)], raise_exception=False) umount(new_image_mount, recursive=True, read_only=False, remove_dir=False, raise_exception=False) umount(new_image_mount, raise_exception=False) diff --git a/tests/test_sonic_installer.py b/tests/test_sonic_installer.py index c004bba9dc..c445dfb6e3 100644 --- a/tests/test_sonic_installer.py +++ b/tests/test_sonic_installer.py @@ -73,9 +73,12 @@ def rootfs_path_mock(path): call(["cp", "/var/lib/sonic-package-manager/packages.json", f"{mounted_image_folder}/tmp/packages.json"]), call(["touch", f"{mounted_image_folder}/tmp/docker.sock"]), call(["mount", "--bind", "/var/run/docker.sock", f"{mounted_image_folder}/tmp/docker.sock"]), + call(["cp", f"{mounted_image_folder}/etc/resolv.conf", "/tmp/resolv.conf.backup"]), + call(["cp", "/etc/resolv.conf", f"{mounted_image_folder}/etc/resolv.conf"]), call(["chroot", mounted_image_folder, "sh", "-c", "command -v sonic-package-manager"]), call(["chroot", mounted_image_folder, "sonic-package-manager", "migrate", "/tmp/packages.json", "--dockerd-socket", "/tmp/docker.sock", "-y"]), call(["chroot", mounted_image_folder, "/usr/lib/docker/docker.sh", "stop"], raise_exception=False), + call(["cp", "/tmp/resolv.conf.backup", f"{mounted_image_folder}/etc/resolv.conf"], raise_exception=False), call(["umount", "-f", "-R", mounted_image_folder], raise_exception=False), call(["umount", "-r", "-f", mounted_image_folder], raise_exception=False), call(["rm", "-rf", mounted_image_folder], raise_exception=False),