From e9db38594dcd00a2a824aecceac78baced52eff5 Mon Sep 17 00:00:00 2001 From: Saikrishna Arcot Date: Tue, 29 Mar 2022 10:11:28 -0700 Subject: [PATCH] Image disk space reduction (#10172) (#10371) Reduce the disk space taken up during bootup and runtime. 1. Remove python package cache from the base image and from the containers. 2. During bootup, if logs are to be stored in memory, then don't create the `var-log.ext4` file just to delete it later during bootup. 3. For the partition containing `/host`, don't reserve any blocks for just the root user. This just makes sure all disk space is available for all users, if needed during upgrades (for example). * Remove pip2 and pip3 caches from some containers Only containers which appeared to have a significant pip cache size are included here. Signed-off-by: Saikrishna Arcot * Don't create var-log.ext4 if we're storing logs in memory Signed-off-by: Saikrishna Arcot * Run tune2fs on the device containing /host to not reserve any blocks for just the root user Signed-off-by: Saikrishna Arcot (cherry picked from commit 5617b1ae3efc6cee52b875651135ada67898ebf0) --- dockers/docker-base-buster/Dockerfile.j2 | 2 +- dockers/docker-config-engine-buster/Dockerfile.j2 | 2 +- dockers/docker-orchagent/Dockerfile.j2 | 2 +- dockers/docker-sonic-mgmt-framework/Dockerfile.j2 | 2 +- files/Aboot/boot0.j2 | 7 ++++++- files/initramfs-tools/varlog | 10 ++++++++++ installer/arm64/install.sh | 5 +++++ installer/armhf/install.sh | 5 +++++ installer/x86_64/install.sh | 11 +++++++++++ 9 files changed, 41 insertions(+), 5 deletions(-) diff --git a/dockers/docker-base-buster/Dockerfile.j2 b/dockers/docker-base-buster/Dockerfile.j2 index e16a0f677b..52c784e9a7 100644 --- a/dockers/docker-base-buster/Dockerfile.j2 +++ b/dockers/docker-base-buster/Dockerfile.j2 @@ -121,7 +121,7 @@ RUN apt-get -y purge \ RUN apt-get clean -y && \ apt-get autoclean -y && \ apt-get autoremove -y && \ - rm -rf /var/lib/apt/lists/* /tmp/* + rm -rf /var/lib/apt/lists/* /tmp/* ~/.cache/ COPY ["etc/rsyslog.conf", "/etc/rsyslog.conf"] COPY ["etc/rsyslog.d/*", "/etc/rsyslog.d/"] diff --git a/dockers/docker-config-engine-buster/Dockerfile.j2 b/dockers/docker-config-engine-buster/Dockerfile.j2 index 3022546a06..1e7a64bfcd 100644 --- a/dockers/docker-config-engine-buster/Dockerfile.j2 +++ b/dockers/docker-config-engine-buster/Dockerfile.j2 @@ -49,4 +49,4 @@ RUN apt-get purge -y \ apt-get clean -y && \ apt-get autoclean -y && \ apt-get autoremove -y && \ - rm -rf /debs /python-wheels + rm -rf /debs /python-wheels ~/.cache diff --git a/dockers/docker-orchagent/Dockerfile.j2 b/dockers/docker-orchagent/Dockerfile.j2 index 70dc98c34c..ef6273ca17 100755 --- a/dockers/docker-orchagent/Dockerfile.j2 +++ b/dockers/docker-orchagent/Dockerfile.j2 @@ -63,7 +63,7 @@ RUN apt-get purge -y \ apt-get clean -y && \ apt-get autoclean -y && \ apt-get autoremove -y && \ - rm -rf /debs + rm -rf /debs ~/.cache COPY ["files/arp_update", "/usr/bin"] COPY ["arp_update.conf", "files/arp_update_vars.j2", "/usr/share/sonic/templates/"] diff --git a/dockers/docker-sonic-mgmt-framework/Dockerfile.j2 b/dockers/docker-sonic-mgmt-framework/Dockerfile.j2 index dca399f23f..048e3b59bf 100644 --- a/dockers/docker-sonic-mgmt-framework/Dockerfile.j2 +++ b/dockers/docker-sonic-mgmt-framework/Dockerfile.j2 @@ -51,6 +51,6 @@ RUN apt-get purge -y python-dev RUN apt-get remove -y g++ python3-dev RUN apt-get clean -y; apt-get autoclean -y; apt-get autoremove -y -RUN rm -rf /debs +RUN rm -rf /debs ~/.cache ENTRYPOINT ["/usr/local/bin/supervisord"] diff --git a/files/Aboot/boot0.j2 b/files/Aboot/boot0.j2 index 0433e32891..747a658b1e 100644 --- a/files/Aboot/boot0.j2 +++ b/files/Aboot/boot0.j2 @@ -351,7 +351,12 @@ extract_image() { unzip -oq "$swipath" -x boot0 "$dockerfs" -d "$image_path" ## detect rootfs type - rootfs_type=`grep " $target_path " /proc/mounts | cut -d' ' -f3` + local mountstr="$(grep " $target_path " /proc/mounts)" + local rootdev="$(echo $mountstr | cut -f1 -d' ')" + rootfs_type="$(echo $mountstr | cut -d' ' -f3)" + + ## Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $rootdev info "Extracting $dockerfs from swi" ## Unpacking dockerfs delayed diff --git a/files/initramfs-tools/varlog b/files/initramfs-tools/varlog index e8063e41a5..980b245234 100644 --- a/files/initramfs-tools/varlog +++ b/files/initramfs-tools/varlog @@ -11,17 +11,27 @@ case $1 in ;; esac +logs_inram=false + # Extract kernel parameters set -- $(cat /proc/cmdline) for x in "$@"; do case "$x" in varlog_size=*) varlog_size="${x#varlog_size=}" + ;; + logs_inram=on) + logs_inram=true + ;; esac done [ -z "$varlog_size" ] && exit 0 +# If logs are being stored in memory, then don't bother +# creating the log file just to have it deleted afterwards. +$logs_inram && exit 0 + # exit when the var_log.ext4 exists and the size matches if [ -e "${rootmnt}/host/disk-img/var-log.ext4" ]; then cur_varlog_size=$(ls -l ${rootmnt}/host/disk-img/var-log.ext4 | awk '{print $5}') diff --git a/installer/arm64/install.sh b/installer/arm64/install.sh index c1e20b62bf..7d77f43f25 100755 --- a/installer/arm64/install.sh +++ b/installer/arm64/install.sh @@ -117,6 +117,11 @@ elif [ "$install_env" = "sonic" ]; then rm -rf $f fi done + + demo_dev=$(findmnt -n -o SOURCE --target /host) + + # Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $demo_dev fi # Create target directory or clean it up if exists diff --git a/installer/armhf/install.sh b/installer/armhf/install.sh index c85170ff25..84917552ec 100755 --- a/installer/armhf/install.sh +++ b/installer/armhf/install.sh @@ -117,6 +117,11 @@ elif [ "$install_env" = "sonic" ]; then rm -rf $f fi done + + demo_dev=$(findmnt -n -o SOURCE --target /host) + + # Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $demo_dev fi # Create target directory or clean it up if exists diff --git a/installer/x86_64/install.sh b/installer/x86_64/install.sh index a75e70f1f6..1adec3bae2 100755 --- a/installer/x86_64/install.sh +++ b/installer/x86_64/install.sh @@ -434,6 +434,9 @@ if [ "$install_env" = "onie" ]; then # Make filesystem mkfs.ext4 -L $demo_volume_label $demo_dev + # Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $demo_dev + # Mount demo filesystem demo_mnt=$(${onie_bin} mktemp -d) || { echo "Error: Unable to create file system mount point" @@ -466,12 +469,20 @@ elif [ "$install_env" = "sonic" ]; then rm -rf $f fi done + + demo_dev=$(findmnt -n -o SOURCE --target /host) + + # Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $demo_dev else demo_mnt="build_raw_image_mnt" demo_dev=$cur_wd/"%%OUTPUT_RAW_IMAGE%%" mkfs.ext4 -L $demo_volume_label $demo_dev + # Don't reserve any blocks just for root + tune2fs -m 0 -r 0 $demo_dev + echo "Mounting $demo_dev on $demo_mnt..." mkdir $demo_mnt mount -t auto -o loop $demo_dev $demo_mnt