forked from plus3it/amigen7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CleanChroot.sh
executable file
·69 lines (60 loc) · 1.77 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
#!/bin/bash
# shellcheck disable=
#
# Do some file cleanup...
#
#########################
CHROOT=${CHROOT:-/mnt/ec2-root}
CLOUDCFG="$CHROOT/etc/cloud/cloud.cfg"
JRNLCNF="$CHROOT/etc/systemd/journald.conf"
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
# 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: maintuser\
lock_passwd: true\
gecos: Local Maintenance User\
groups: [wheel, adm]\
sudo: ["ALL=(root) NOPASSWD:ALL"]\
shell: /bin/bash\
distro: rhel\
paths:\
cloud_dir: /var/lib/cloud\
templates_dir: /etc/cloud/templates\
ssh_svcname: sshd\
' "${CLOUDCFG}"
fi