Skip to content

Commit

Permalink
[sonic_installer] use /etc/resolv.conf from the host when migrating p…
Browse files Browse the repository at this point in the history
…ackages (sonic-net#2573)

- What I did
SONiC package migration has been failing due to the lack of DNS configuration for registries domain names.
I used /etc/resolv.conf from host OS when migrating.

- How I did it
Copy /etc/resolv.conf into new image filesystem during migration, then, restore it back.

- How to verify it
Run sonic-installer install.

Signed-off-by: Stepan Blyschak <[email protected]>
  • Loading branch information
stepanblyschak authored Jan 2, 2023
1 parent a09461d commit fd4d9c0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 13 additions & 6 deletions sonic_installer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,24 +318,26 @@ 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)
new_image_dir = bootloader.get_image_path(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
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions tests/test_sonic_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit fd4d9c0

Please sign in to comment.