Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions sonic_installer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def migrate_sonic_packages(bootloader, binary_image_version):
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")

packages_file = "packages.json"
packages_path = os.path.join(PACKAGE_MANAGER_DIR, packages_file)
Expand Down Expand Up @@ -382,8 +381,19 @@ def migrate_sonic_packages(bootloader, binary_image_version):
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)])
# Inject host DNS into chroot for sonic-package-manager to resolve hostnames.
chroot_resolv = os.path.join(new_image_mount, RESOLV_CONF_FILE)
if os.path.islink(chroot_resolv):
# Symlink: populate the target inside the chroot so the symlink resolves.
# Cannot cp over the symlink because the absolute target path escapes
# the overlay mount to the host filesystem ("are the same file" error).
resolv_target = os.readlink(chroot_resolv)
chroot_target = os.path.join(new_image_mount, resolv_target.lstrip("/"))
run_command_or_raise(["mkdir", "-p", os.path.dirname(chroot_target)])
run_command_or_raise(["cp", "-L", os.path.join("/", RESOLV_CONF_FILE), chroot_target])
else:
# Regular file: overwrite with host DNS content.
run_command_or_raise(["cp", os.path.join("/", RESOLV_CONF_FILE), chroot_resolv])

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",
Expand All @@ -395,7 +405,6 @@ def migrate_sonic_packages(bootloader, binary_image_version):
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
8 changes: 5 additions & 3 deletions tests/test_sonic_installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def test_install(run_command, run_command_or_raise, get_bootloader, swap, fs):
fs.create_dir(os.path.join(mounted_image_folder, "usr/lib/docker/docker.sh"))
fs.create_file("/var/run/docker.pid", contents="15")
fs.create_file("/proc/15/cmdline", contents="\x00".join(["dockerd"] + dockerd_opts))
# Simulate new image: /etc/resolv.conf is a dangling symlink (target doesn't exist in squashfs)
fs.create_symlink(f"{mounted_image_folder}/etc/resolv.conf", "/run/resolvconf/resolv.conf")

# Setup bootloader mock
mock_bootloader = Mock()
Expand Down Expand Up @@ -91,12 +93,12 @@ def rootfs_path_mock(path):
f"{mounted_image_folder}/var/lib/sonic-package-manager"]),
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"]),
# /etc/resolv.conf is a dangling symlink -> /run/resolvconf/resolv.conf, populate its target
call(["mkdir", "-p", f"{mounted_image_folder}/run/resolvconf"]),
call(["cp", "-L", "/etc/resolv.conf", f"{mounted_image_folder}/run/resolvconf/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"], capture=False),
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
Loading