sonic-buildimage/files/initramfs-tools/varlog
Saikrishna Arcot 5617b1ae3e
Image disk space reduction (#10172)
# Why I did it

Reduce the disk space taken up during bootup and runtime.

# How I did it

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 <sarcot@microsoft.com>

* Don't create var-log.ext4 if we're storing logs in memory

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>

* Run tune2fs on the device containing /host to not reserve any blocks for just the root user

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
2022-03-15 18:12:49 -07:00

55 lines
1.4 KiB
Bash

#!/bin/sh -e
PREREQS=""
prereqs() { echo "$PREREQS"; }
case $1 in
prereqs)
prereqs
exit 0
;;
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}')
if [ $cur_varlog_size == $((1024*1024*$varlog_size)) ]; then
exit 0
else
rm -rf ${rootmnt}/host/disk-img
fi
fi
# create varlog disk
case "${ROOT}" in
ubi*)
# sys_fallocate is NOT supported over UBIFS
mkdir -p ${rootmnt}/host/disk-img && ${rootmnt}/usr/bin/truncate -s "$varlog_size"M ${rootmnt}/host/disk-img/var-log.ext4 && mkfs.ext4 -q -F ${rootmnt}/host/disk-img/var-log.ext4
;;
*)
mkdir -p ${rootmnt}/host/disk-img && ${rootmnt}/usr/bin/fallocate -l "$varlog_size"M ${rootmnt}/host/disk-img/var-log.ext4 && mkfs.ext4 -q -F ${rootmnt}/host/disk-img/var-log.ext4
;;
esac