Last active 1705433115

VM template cleanup

cleanup.sh Raw
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
7major_version="`sed 's/^.\+ release \([.0-9]\+\).*/\1/' /etc/redhat-release | awk -F. '{print $1}'`";
8
9# make sure we use dnf on EL 8+
10if [ "$major_version" -ge 8 ]; then
11 pkg_cmd="dnf"
12else
13 pkg_cmd="yum"
14fi
15
16
17echo "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
20if [ "$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)
25else
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
31fi
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
35echo "Removing extra firmware packages"
36$pkg_cmd -y remove linux-firmware
37
38echo "clean all package cache information"
39$pkg_cmd -y clean all --enablerepo=\*;
40
41# Clean up network interface persistence
42rm -f /etc/udev/rules.d/70-persistent-net.rules;
43mkdir -p /etc/udev/rules.d/70-persistent-net.rules;
44rm -f /lib/udev/rules.d/75-persistent-net-generator.rules;
45rm -rf /dev/.udev/;
46
47for 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
52done
53
54
55echo "truncate any logs that have built up during the install"
56find /var/log -type f -exec truncate --size=0 {} \;
57
58echo "remove the install log"
59rm -f /root/anaconda-ks.cfg /root/original-ks.cfg
60
61echo "remove the contents of /tmp and /var/tmp"
62rm -rf /tmp/* /var/tmp/*
63
64echo "Clear the history so our install commands aren't there"
65rm -f /root/.wget-hsts