This document describes how to safely reduce the size of an LXC container before migrating it to Incus or copying it to a remote server. It provides commands to identify large directories, remove unnecessary data, and clean up legacy components.
1. Identify Large Directories
Run these commands inside the container:
du -xhd1 /
du -xhd1 /var
du -xhd1 /var/lib
du -xhd1 /usr
Use ncdu for an interactive view:
apt install ncdu
ncdu /
2. Clean System Logs
Check journal usage
journalctl --disk-usage
Reduce journal to 200M
journalctl --vacuum-size=200M
Re-check:
journalctl --disk-usage
du -sh /var/log/journal
3. Clean APT Cache
apt clean
rm -rf /var/cache/apt/archives/partial/*
du -sh /var/cache/apt
Optional autoremove:
apt autoremove --purge
4. Remove Unused MySQL Databases
List databases
mysql -e "SHOW DATABASES;"
Drop legacy databases
Example:
mysql -e "DROP DATABASE kafrep;"
mysql -e "DROP DATABASE 3dcp;"
mysql -e "DROP DATABASE b3dcp;"
Optional: purge old binary logs
mysql -e "PURGE BINARY LOGS BEFORE NOW() - INTERVAL 7 DAY;"
Check size again:
du -sh /var/lib/mysql
5. Remove Legacy MySQL Backups
Show matching backups
find /var/backups/automysqlbackup -type f \( \
-name 'kafrep*' -o \
-name '3dcp*' -o \
-name 'b3dcp*' \
\) -print
Delete matching backups
find /var/backups/automysqlbackup -type f \( \
-name 'kafrep*' -o \
-name '3dcp*' -o \
-name 'b3dcp*' \
\) -print -delete
If directories exist:
find /var/backups/automysqlbackup -type d \( \
-name 'kafrep*' -o \
-name '3dcp*' -o \
-name 'b3dcp*' \
\) -print -exec rm -rf {} +
6. Slimming /usr
Analyse /usr/share
du -xhd1 /usr/share
The largest components are typically:
/usr/share/locale/usr/share/doc/usr/share/man/usr/share/icons/usr/share/fonts/usr/share/nodejs(optional)
Install localepurge (cleanup locales)
apt install localepurge
localepurge
Select languages to keep, e.g.: de, de_AT, en, en_US.
Remove documentation
rm -rf /usr/share/doc/*
Remove manpages
rm -rf /usr/share/man/*
Remove icons and themes (headless)
rm -rf /usr/share/icons/*
rm -rf /usr/share/themes/*
Remove fonts (headless)
rm -rf /usr/share/fonts/*
Remove NodeJS (optional)
Check usage:
which node
node --version
dpkg -l | grep node
If unused:
apt purge nodejs
rm -rf /usr/share/nodejs/*
7. Clean Temporary Files and Rotated Logs
One-liner to clean all rotated logs in all subdirectories of /var/log
find /var/log -type f -name "*.gz" -print -delete
find /var/log -type f -regex ".*\.[0-9]+" -print -delete
rm -rf /tmp/*
rm -f /var/log/*.gz /var/log/*.[0-9]
Clean rotated Apache logs
find /var/log/apache2 -type f -name "*.gz" -print -delete
find /var/log/apache2 -type f -regex ".*\.[0-9]+" -print -delete
Clean rotated NGINX logs
find /var/log/nginx -type f -name "*.gz" -print -delete
find /var/log/nginx -type f -regex ".*\.[0-9]+" -print -delete
Clean rotated APT logs
find /var/log/apt -type f -name "*.gz" -print -delete
Clean rotated unattended-upgrades logs
find /var/log/unattended-upgrades -type f -name "*.gz" -print -delete
If Exim4 is unused:
rm -f /var/log/exim4/*.[0-9]*
rm -rf /tmp/*
rm -f /var/log/*.gz /var/log/*.[0-9]
8. Verify Final Size
du -sh /usr
Optional:
df -h
9. Ready for Incus Migration
Once cleaned, publish or copy the container:
Example: publish to image
incus publish CTNAME --alias slimimage
Copy image to remote server
incus remote add TARGETSERVER
incus copy slimimage TARGETSERVER: --mode=push
End of document.
- Log in to post comments