Skip to content

Commit

Permalink
Sonic image build optimization
Browse files Browse the repository at this point in the history
The current SONIC build infrastructure always prepares the rootfs for final image generation.
Because of rootfs preparation, final image generation takes long regardless of SONiC changes.

The rootfs preparation consists of two parts.
	Debian packages installation
		Bootstrap preparation
		General packages installation, such as curl, vim, sudo, python3, etc
	Sonic packages installation
		Packages that are built and installed from the sonic repo.
		Docker images that are built and installed from the sonic repo

The build time can be optimized by generating the Debian packages as a base image and
it can be run in parallel with the other targets, before the final image.
	Benefits:
		- High hit rate, for fewer dependencies.
		- Reduce the cache size.
		- Improve the concurrency when the cache is not hit, the step has small dependencies and can be run with any other steps.

Other enhancements:
	- The docker load is also optimized by running in parallel.
	- Added support for gipz compression
	- Added tmpfs support for rootfs and final image generation.
  • Loading branch information
Kalimuthu-Velappan committed May 23, 2022
1 parent 3928996 commit 0e5c319
Show file tree
Hide file tree
Showing 17 changed files with 1,230 additions and 550 deletions.
21 changes: 16 additions & 5 deletions Makefile.cache
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,14 @@ define LOAD_FROM_CACHE
# Load the cache if cache is enabled and cache file is present in the cache
# Update the cache_loaded variable
$(if $(and $(CACHE_FILE_SELECT),$(filter $(RCACHE_OPTIONS),$(SONIC_DPKG_CACHE_METHOD))),
$(if $(LOAD_DRV_DEB), $($(1)_CACHE_USER) tar -C $($(1)_BASE_PATH) -mxzvf $(CACHE_FILE_SELECT) 1>> $($(1)_DST_PATH)/$(1).log ,echo );
echo "File $(CACHE_FILE_SELECT) is loaded from cache into $($(1)_BASE_PATH)" >> $($(1)_DST_PATH)/$(1).log
$(if $(or $(LOAD_DRV_DEB), $($(1)_FORCE_COPY) ), \
$(if $($(1)_LOAD_HANDLER), \
$(call $($(1)_LOAD_HANDLER), $(CACHE_FILE_SELECT), $($(1)_DST_PATH)/$(1) ), \
$($(1)_CACHE_USER) tar -C $($(1)_BASE_PATH) -mxzvf $(CACHE_FILE_SELECT) 1>> $($(1)_DST_PATH)/$(1).log \
) \
,echo \
);
echo "File $(CACHE_FILE_SELECT) is loaded into $($(1)_DST_PATH)" >> $($(1)_DST_PATH)/$(1).log
$(eval $(1)_CACHE_LOADED := Yes)
$(if $(call CHECK_WCACHE_ENABLED,$(1)), $(shell touch $(CACHE_FILE_SELECT)))
echo "[ CACHE::LOADED ] $($(1)_CACHE_DIR)/$($(1)_MOD_CACHE_FILE)" >> $($(1)_DST_PATH)/$(1).log
Expand Down Expand Up @@ -293,9 +299,14 @@ define SAVE_INTO_CACHE
echo "Target $(1) dependencies are modifed - global save cache skipped" >> $($(1)_DST_PATH)/$(1).log
$(eval $(1)_CACHE_DIR := $(SONIC_DPKG_LOCAL_CACHE_DIR))
)
$($(1)_CACHE_USER) tar -C $($(1)_BASE_PATH) -mczvf $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE) $(2) $(addprefix $($(1)_DST_PATH)/,$($(1)_DERIVED_DEBS) $($(1)_EXTRA_DEBS) ) \
1>>$($(1)_DST_PATH)/$(1).log
sudo chmod 777 $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE)

# Call user handler if exists
$(eval $(1)_SAVE_FILES := $(2) $(addprefix $($(1)_DST_PATH)/,$($(1)_DERIVED_DEBS) $($(1)_EXTRA_DEBS) ) )
$(if $($(1)_SAVE_HANDLER),
$(call $($(1)_SAVE_HANDLER), $($(1)_SAVE_FILES), $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE) ),
$($(1)_CACHE_USER) tar -C $($(1)_BASE_PATH) -mczvf $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE) $($(1)_SAVE_FILES) 1>>$($(1)_DST_PATH)/$(1).log \
)
chmod 777 $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE) &>/dev/null || sudo chmod 777 $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE)

echo "File $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE) saved in cache " >> $($(1)_DST_PATH)/$(1).log
echo "[ CACHE::SAVED ] $($(1)_CACHE_DIR)/$(MOD_CACHE_FILE)" >> $($(1)_DST_PATH)/$(1).log
Expand Down
335 changes: 63 additions & 272 deletions build_debian.sh

Large diffs are not rendered by default.

Loading

0 comments on commit 0e5c319

Please sign in to comment.