Skip to content

Commit 640b2ca

Browse files
fix: prevent 'no space left on device' errors with enhanced cleanup script
1 parent a028d72 commit 640b2ca

File tree

1 file changed

+57
-14
lines changed

1 file changed

+57
-14
lines changed

packer-scripts/cleanup

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,51 @@ set -o errexit
44
set -o xtrace
55
shopt -s nullglob
66

7-
# clear the APT cache and delete everything from /var/cache/apt/archives/
8-
# removed /etc/ssh/ssh_host_* \ - this probably caused issues with ssh connection for some reason..
97
apt clean
108

9+
echo "Cleaning APT..."
10+
apt-get autoremove -y || true
11+
apt-get clean || true
12+
13+
echo "Cleaning Node.js and Yarn..."
14+
if command -v yarn &>/dev/null; then
15+
yarn cache clean || true
16+
fi
17+
18+
if command -v npm &>/dev/null; then
19+
npm cache clean --force || true
20+
fi
21+
22+
echo "Cleaning Yarn directories..."
23+
rm -rf /tmp/.yarn-* || true
24+
rm -rf /root/.npm /root/.yarn || true
25+
rm -rf /home/*/.npm /home/*/.yarn || true
26+
rm -rf /usr/local/share/.cache/yarn || true
27+
rm -rf /usr/local/yarn-*/cache || true
28+
find /usr/local/yarn-* -type d -name "node_modules" -exec rm -rf {} + 2>/dev/null || true
29+
30+
if command -v docker &>/dev/null; then
31+
echo "Cleaning Docker..."
32+
docker system prune -af || true
33+
fi
34+
35+
if command -v pip3 &>/dev/null; then
36+
echo "Cleaning pip..."
37+
pip3 cache purge || true
38+
fi
39+
40+
if command -v apt &>/dev/null; then
41+
echo "Removing old kernels..."
42+
dpkg -l 'linux-*' | awk '/^ii/{ print $2}' | grep -v -e "$(uname -r | sed -r 's/-[a-z]+//')" | grep -E '(image|headers)' | xargs apt-get -y purge || true
43+
fi
44+
45+
if command -v snap &>/dev/null; then
46+
echo "Cleaning snap..."
47+
snap list --all | awk '/disabled/{print $1, $3}' | while read -r snapname revision; do
48+
snap remove "$snapname" --revision="$revision" || true
49+
done
50+
fi
51+
1152
rm -rf \
1253
/etc/apparmor* \
1354
/etc/profile.d/jdk.csh \
@@ -22,7 +63,12 @@ rm -rf \
2263
/var/log/installer \
2364
/var/tmp/* \
2465
VBoxGuestAdditions_*.iso \
25-
VBoxGuestAdditions_*.iso.?
66+
VBoxGuestAdditions_*.iso.? \
67+
/var/cache/* \
68+
/usr/share/doc/* \
69+
/usr/share/man/*
70+
71+
find /usr/share/locale -mindepth 1 -maxdepth 1 -type d -not -name 'en' -not -name 'en_US' -exec rm -rf {} \; || true
2672

2773
if [[ $CLEANUP_APT_LISTS ]]; then
2874
rm -rf /var/lib/apt/lists/* || echo "Suppressing exit $?"
@@ -36,21 +82,21 @@ if [[ -d /home/travis ]]; then
3682
rm -f /home/travis/linux.iso /home/travis/shutdown.sh
3783
fi
3884

85+
echo "Cleaning logs..."
3986
find /var/log -type f | while read -r f; do
4087
dd if=/dev/null of="${f}"
4188
done
4289

43-
#
44-
# Disable motd
45-
# in /etc/default/motd-news set ENABLED=0
46-
#
90+
echo "Removing large files..."
91+
find / -type f -size +100M -not -path "/proc/*" -not -path "/sys/*" -not -path "/dev/*" -not -path "/run/*" -print -delete 2>/dev/null || true
92+
93+
journalctl --vacuum-time=1d || true
94+
journalctl --vacuum-size=10M || true
95+
4796
if test -f /etc/default/motd-news >&/dev/null; then
4897
sed -i 's|ENABLED=1|ENABLED=0|' /etc/default/motd-news
4998
fi
5099

51-
#
52-
# Disable services to speed up boot time
53-
#
54100
if command -v systemctl >&/dev/null; then
55101
systemctl --all --type service | grep -q lxd-containers.service && systemctl disable lxd-containers.service
56102
systemctl --all --type service | grep -q accounts-daemon.service && systemctl mask accounts-daemon.service
@@ -64,12 +110,9 @@ if command -v systemctl >&/dev/null; then
64110
systemctl --all --type service | grep -q motd-news.service && systemctl disable motd-news.service
65111
fi
66112

67-
#
68-
# Disable package update on boot
69-
#
70113
if test -f /etc/cloud/cloud.cfg >&/dev/null; then
71114
sed -i '/package-update-upgrade-install/d' /etc/cloud/cloud.cfg
72115
fi
73116

74117
echo "PubkeyAcceptedKeyTypes +ssh-rsa" >> /etc/ssh/sshd_config
75-
echo "HostKeyAlgorithms +ssh-rsa" >> /etc/ssh/sshd_config
118+
echo "HostKeyAlgorithms +ssh-rsa" >> /etc/ssh/sshd_config

0 commit comments

Comments
 (0)