-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add build option to reduce final image size #16729
Add build option to reduce final image size #16729
Conversation
@StormLiangMS @lipxu I published this as a draft for now. |
|
||
def remove_docs(self): | ||
self._remove_root_paths([ | ||
'usr/share/doc', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe some actions can be enabled by default.
E.g. we clean up /usr/share/doc here:
sonic-buildimage/build_debian.sh
Line 748 in fe24c26
sudo LANG=C chroot $FILESYSTEM_ROOT bash -c 'rm -rf /usr/share/doc/* /usr/share/locale/* /var/lib/apt/lists/* /tmp/*' |
I think it's possible to remove other directories with docs/mans/licensses without any conditons.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these cleanup, especially the removal ones could definitely be added as part of the general build.
However this would have to be more than just here, since it would also need to apply to all docker images.
On top of that the removal of licenses could have some legal implication for public/redistributable builds such as the community ones.
This should however not be a problem for internal/private images.
Most of the savings of this change actually come from the hardlinking steps.
And for Aboot images the removal of other platforms and unneeded kernel modules and firmwares.
This mitigation, even if it merges in master, has 202305 as the main target for 4GB systems.
It therefore felt simpler to centralize the cleanup under a postprocessing script hidden behind a toggle instead of touching tens of files.
This script also has the advantage of cleaning up behind newly introduced code that forgot the cleanup steps.
Until there is some checks and tooling in place to prevent new code from contributing to bloat, things will slip and become a maintenance burden.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To add to your comment, the proper thing to do here would be to add an /etc/dpkg/dpkg.conf.d/01_sonic
conf instead of removing files like it's currently done in build_debian.sh
In this configuration you can specify things like path-exclude=/usr/share/doc
however that will not retroactively apply the previously installed packages.
In the case of docker, that means that the debian base image will retain these files no matter what.
If you decide to remove them as part of the build of a child Dockerfile, this is actually going to create whiteout files which consumes space instead of the final goal being to release space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of docker, that means that the debian base image will retain these files no matter what.
This is already done for docker containers:
RUN find /usr/share/doc -depth \( -type f -o -type l \) ! -name copyright | xargs rm || true |
COPY ["dpkg_01_drop", "/etc/dpkg/dpkg.cfg.d/01_drop"] |
So my suggestion was only about host system.
On top of that the removal of licenses could have some legal implication for public/redistributable builds such as the community ones.
We keep copyright files in docker containers but remove them from host system.
Probably we shouldn't remove these files from /usr/share/doc on host system.
There was my old PR #14417 to fix it. I can reopen it, but I'm still not sure should we keep these files or not.
|
Only considering compressed data vim is ~8x bigger than vim-tiny. Something I was thinking was to potentially add levels to the |
I agree. Then it's better to use hacks only when BUILD_REDUCE_IMAGE_SIZE is set.
I forgot about vim-runtime package. Then it makes sense. |
@Staphylo several questions.
|
Hi, @Staphylo , I picked this PR and enabled BUILD_REDUCE_IMAGE_SIZE, the test result seems OK for the private internal image. |
It is not possible to open gz compressed files directly with
It would impact any image built with this option, so
This is still ongoing On another note, I will have to refactor this PR a bit since the new staged build feature created a conflict #15924 |
19038a4
to
5ce9116
Compare
hi @Staphylo I think it would be good to get the vim back, it is very handy tool for production. And there is a merge conflict, could you help to take a look? |
hi @Staphylo for the hardlinks, one question, if someone modified one file in the container, does this change persistent after reboot? |
@StormLiangMS I will make the necessary changes and rebase my PR to address the conflict. Regarding the hardlinks, if someone makes a change in a container the original file (which might be hardlinked) will remain untouched. |
Add a build option to reduce the image size. The image reduction process is affecting the builds in 2 ways: - change some packages that are installed in the rootfs - apply a rootfs reduction script The script itself will perform a few steps: - remove file duplication by leveraging hardlinks - under /usr/share/sonic since the symlinks under the device folder are lost during the build. - under /var/lib/docker since the files there will only be mounted ro - remove some extra files (man, docs, licenses, ...) - some image specific space reduction (only for aboot images currently) The script can later be improved but for now it's reducing the rootfs size by ~30%.
5ce9116
to
746bbf1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ADO:25423859 |
Why I did it Running SONiC releases past 202012 has become really challenging on system with small storage devices (4GB). Some of these devices can also be limited by only having 4GB of RAM which complicates mitigations. The main contributor to these issues is the SONiC image growth. Being able to reduce it by some decent amount should allow these systems to run SONiC longer. It would also reduce some impacts related to space savings mitigations. Work item tracking Microsoft ADO (number only): How I did it Add a build option to reduce the image size. The image reduction process is affecting the builds in 2 ways: change some packages that are installed in the rootfs apply a rootfs reduction script The script itself will perform a few steps: remove file duplication by leveraging hardlinks under /usr/share/sonic since the symlinks under the device folder are lost during the build. under /var/lib/docker since the files there will only be mounted ro remove some extra files (man, docs, licenses, ...) some image specific space reduction (only for aboot images currently) The script can later be improved but for now it's reducing the rootfs size by ~30%. How to verify it Compare the size of an image with this option enabled and this option enabled. Expect the fully extracted content to be ~30% less. Which release branch to backport (provide reason below if selected) This is a backport of #16729 Description for the changelog Add build option to reduce final image size
enable reduce image size for slim image build sonic-net#16729 Signed-off-by: xuliping <xuliping@microsoft.com>
Why I did it
Running SONiC releases past 202012 has become really challenging on system with small storage devices (4GB).
Some of these devices can also be limited by only having 4GB of RAM which complicates mitigations.
The main contributor to these issues is the SONiC image growth.
Being able to reduce it by some decent amount should allow these systems to run SONiC longer.
It would also reduce some impacts related to space savings mitigations.
Work item tracking
How I did it
Add a build option to reduce the image size.
The image reduction process is affecting the builds in 2 ways:
The script itself will perform a few steps:
The script can later be improved but for now it's reducing the rootfs size by ~30%.
How to verify it
Compare the size of an image with this option enabled and this option enabled.
Expect the fully extracted content to be ~30% less.
Which release branch to backport (provide reason below if selected)
Description for the changelog
Add build option to reduce final image size