cleanup.sh
· 2.0 KiB · Bash
Raw
#!/bin/sh -eux
## VM cleanup script for generating templates
##
## Download from https://gist.githubusercontent.com/0ranki/a2fb607c41a4112c410befc9be004646/raw/e7f2ce00636170416c2f65b63196358320166624/cleanup.sh
major_version="`sed 's/^.\+ release \([.0-9]\+\).*/\1/' /etc/redhat-release | awk -F. '{print $1}'`";
# make sure we use dnf on EL 8+
if [ "$major_version" -ge 8 ]; then
pkg_cmd="dnf"
else
pkg_cmd="yum"
fi
echo "Remove development and kernel source packages"
$pkg_cmd -y remove gcc cpp gc kernel-devel kernel-headers glibc-devel elfutils-libelf-devel glibc-headers kernel-devel kernel-headers
if [ "$major_version" -ge 8 ]; then
echo "remove orphaned packages"
dnf -y autoremove
echo "Remove previous kernels that preserved for rollbacks"
dnf -y remove -y $(dnf repoquery --installonly --latest-limit=-1 -q)
else
echo "Remove previous kernels that preserved for rollbacks"
if ! command -v package-cleanup >/dev/null 2>&1; then
yum -y install yum-utils
fi
package-cleanup --oldkernels --count=1 -y
fi
# Avoid ~200 meg firmware package we don't need
# this cannot be done in the KS file so we do it here
echo "Removing extra firmware packages"
$pkg_cmd -y remove linux-firmware
echo "clean all package cache information"
$pkg_cmd -y clean all --enablerepo=\*;
# Clean up network interface persistence
rm -f /etc/udev/rules.d/70-persistent-net.rules;
mkdir -p /etc/udev/rules.d/70-persistent-net.rules;
rm -f /lib/udev/rules.d/75-persistent-net-generator.rules;
rm -rf /dev/.udev/;
for ndev in `ls -1 /etc/sysconfig/network-scripts/ifcfg-*`; do
if [ "`basename $ndev`" != "ifcfg-lo" ]; then
sed -i '/^HWADDR/d' "$ndev";
sed -i '/^UUID/d' "$ndev";
fi
done
echo "truncate any logs that have built up during the install"
find /var/log -type f -exec truncate --size=0 {} \;
echo "remove the install log"
rm -f /root/anaconda-ks.cfg /root/original-ks.cfg
echo "remove the contents of /tmp and /var/tmp"
rm -rf /tmp/* /var/tmp/*
echo "Clear the history so our install commands aren't there"
rm -f /root/.wget-hsts
1 | #!/bin/sh -eux |
2 | |
3 | ## VM cleanup script for generating templates |
4 | ## |
5 | ## Download from https://gist.githubusercontent.com/0ranki/a2fb607c41a4112c410befc9be004646/raw/e7f2ce00636170416c2f65b63196358320166624/cleanup.sh |
6 | |
7 | major_version="`sed 's/^.\+ release \([.0-9]\+\).*/\1/' /etc/redhat-release | awk -F. '{print $1}'`"; |
8 | |
9 | # make sure we use dnf on EL 8+ |
10 | if [ "$major_version" -ge 8 ]; then |
11 | pkg_cmd="dnf" |
12 | else |
13 | pkg_cmd="yum" |
14 | fi |
15 | |
16 | |
17 | echo "Remove development and kernel source packages" |
18 | $pkg_cmd -y remove gcc cpp gc kernel-devel kernel-headers glibc-devel elfutils-libelf-devel glibc-headers kernel-devel kernel-headers |
19 | |
20 | if [ "$major_version" -ge 8 ]; then |
21 | echo "remove orphaned packages" |
22 | dnf -y autoremove |
23 | echo "Remove previous kernels that preserved for rollbacks" |
24 | dnf -y remove -y $(dnf repoquery --installonly --latest-limit=-1 -q) |
25 | else |
26 | echo "Remove previous kernels that preserved for rollbacks" |
27 | if ! command -v package-cleanup >/dev/null 2>&1; then |
28 | yum -y install yum-utils |
29 | fi |
30 | package-cleanup --oldkernels --count=1 -y |
31 | fi |
32 | |
33 | # Avoid ~200 meg firmware package we don't need |
34 | # this cannot be done in the KS file so we do it here |
35 | echo "Removing extra firmware packages" |
36 | $pkg_cmd -y remove linux-firmware |
37 | |
38 | echo "clean all package cache information" |
39 | $pkg_cmd -y clean all --enablerepo=\*; |
40 | |
41 | # Clean up network interface persistence |
42 | rm -f /etc/udev/rules.d/70-persistent-net.rules; |
43 | mkdir -p /etc/udev/rules.d/70-persistent-net.rules; |
44 | rm -f /lib/udev/rules.d/75-persistent-net-generator.rules; |
45 | rm -rf /dev/.udev/; |
46 | |
47 | for ndev in `ls -1 /etc/sysconfig/network-scripts/ifcfg-*`; do |
48 | if [ "`basename $ndev`" != "ifcfg-lo" ]; then |
49 | sed -i '/^HWADDR/d' "$ndev"; |
50 | sed -i '/^UUID/d' "$ndev"; |
51 | fi |
52 | done |
53 | |
54 | |
55 | echo "truncate any logs that have built up during the install" |
56 | find /var/log -type f -exec truncate --size=0 {} \; |
57 | |
58 | echo "remove the install log" |
59 | rm -f /root/anaconda-ks.cfg /root/original-ks.cfg |
60 | |
61 | echo "remove the contents of /tmp and /var/tmp" |
62 | rm -rf /tmp/* /var/tmp/* |
63 | |
64 | echo "Clear the history so our install commands aren't there" |
65 | rm -f /root/.wget-hsts |