-
Notifications
You must be signed in to change notification settings - Fork 27
/
CleanChroot.sh
executable file
·76 lines (65 loc) · 2.11 KB
/
CleanChroot.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# shellcheck disable=
#
# Do some file cleanup...
#
#########################
CHROOT=${AMIGENCHROOT:-/mnt/ec2-root}
CLOUDCFG="$CHROOT/etc/cloud/cloud.cfg"
JRNLCNF="$CHROOT/etc/systemd/journald.conf"
MAINTUSR=${MAINTUSR:-"maintuser"}
# Disable EPEL repos
chroot "${CHROOT}" yum-config-manager --disable "*epel*" > /dev/null
# Get rid of stale RPM data
chroot "${CHROOT}" yum clean --enablerepo=* -y packages
chroot "${CHROOT}" rm -rf /var/cache/yum
chroot "${CHROOT}" rm -rf /var/lib/yum
# Nuke any history data
cat /dev/null > "${CHROOT}/root/.bash_history"
# Clean up all the log files
# shellcheck disable=SC2044
for FILE in $(find "${CHROOT}/var/log" -type f)
do
cat /dev/null > "${FILE}"
done
# Enable persistent journal logging
if [[ $(grep -q ^Storage "${JRNLCNF}")$? -ne 0 ]]
then
echo 'Storage=persistent' >> "${JRNLCNF}"
install -d -m 0755 "${CHROOT}/var/log/journal"
chroot "${CHROOT}" systemd-tmpfiles --create --prefix /var/log/journal
fi
# Help prevent longer-lived systems running out of space on /boot
sed -i '/^installonly_limit=/s/[0-9][0-9]*$/2/' "${CHROOT}/etc/yum.conf"
# Set TZ to UTC
rm "${CHROOT}/etc/localtime"
cp "${CHROOT}/usr/share/zoneinfo/UTC" "${CHROOT}/etc/localtime"
# Create maintuser
CLINITUSR=$(grep -E "name: (maintuser|centos|ec2-user|cloud-user)" \
"${CLOUDCFG}" | awk '{print $2}')
if [ "${CLINITUSR}" = "" ]
then
echo "Cannot reset value of cloud-init default-user" > /dev/stderr
else
echo "Setting default cloud-init user to ${MAINTUSR}"
sed -i '/^system_info/,/^ ssh_svcname/d' "${CLOUDCFG}"
# shellcheck disable=SC1004
sed -i '/syntax=yaml/i\
system_info:\
default_user:\
name: '"${MAINTUSR}"'\
lock_passwd: true\
gecos: Local Maintenance User\
groups: [wheel, adm]\
sudo: ["ALL=(root) TYPE=sysadm_t ROLE=sysadm_r NOPASSWD:ALL"]\
shell: /bin/bash\
selinux_user: staff_u\
distro: rhel\
paths:\
cloud_dir: /var/lib/cloud\
templates_dir: /etc/cloud/templates\
ssh_svcname: sshd\
' "${CLOUDCFG}"
fi
# Update NS-Switch map-file for SEL-enabled environment
printf "%-12s %s\n" sudoers: files >> "${CHROOT}/etc/nsswitch.conf"