11#! /bin/bash
2-
3- # DigitalOcean Marketplace Image Validation Tool
4- # © 2021 DigitalOcean LLC.
5- # This code is licensed under Apache 2.0 license (see LICENSE.md for details)
6-
72set -o errexit
83
9- # Ensure /tmp exists and has the proper permissions before
10- # checking for security updates
11- # https://github.com/digitalocean/marketplace-partners/issues/94
4+ # Ensure /tmp exists and has proper permissions
125if [[ ! -d /tmp ]]; then
136 mkdir /tmp
147fi
158chmod 1777 /tmp
169
17- if [ -n " $( command -v yum) " ]; then
18- yum update -y
19- yum clean all
20- elif [ -n " $( command -v apt-get) " ]; then
21- # Cleanup more packages
22- apt-get -y remove --purge \
23- automake \
24- autoconf \
25- autotools-dev \
26- cmake-data \
27- cpp-9 \
28- cpp-10 \
29- gcc-9 \
30- gcc-10 \
31- git \
32- git-man \
33- ansible \
34- libicu-dev \
35- libcgal-dev \
36- libgcc-9-dev \
37- ansible
38-
10+ # Update system
11+ if [ -n " $( command -v apt-get) " ]; then
3912 # Remove ansible PPA directly (software-properties-common may not be installed)
4013 rm -f /etc/apt/sources.list.d/ansible-ubuntu-ansible-* .list \
4114 /etc/apt/sources.list.d/ansible-ubuntu-ansible-* .sources 2> /dev/null || true
4215
43- source /etc/os-release
44-
45- # Protect critical runtime packages from autoremove
46- apt-mark manual libevent-2.1-7t64
47-
48- # Ensure cloud-init and openssh-server are installed
49- # They may have been removed as dependencies during package cleanup
50- apt-get -y install --no-install-recommends cloud-init openssh-server
51-
52- # Ensure cloud-init and SSH services are enabled (may not be re-enabled on reinstall)
53- # systemctl enable can fail silently in chroot - create symlinks manually
54- mkdir -p /etc/systemd/system/cloud-init.target.wants
55- mkdir -p /etc/systemd/system/multi-user.target.wants
56- ln -sf /usr/lib/systemd/system/cloud-init-local.service /etc/systemd/system/cloud-init.target.wants/ || true
57- ln -sf /usr/lib/systemd/system/cloud-init.service /etc/systemd/system/cloud-init.target.wants/ || true
58- ln -sf /usr/lib/systemd/system/cloud-config.service /etc/systemd/system/cloud-init.target.wants/ || true
59- ln -sf /usr/lib/systemd/system/cloud-final.service /etc/systemd/system/cloud-init.target.wants/ || true
60- ln -sf /usr/lib/systemd/system/cloud-init.target /etc/systemd/system/multi-user.target.wants/ || true
61- ln -sf /usr/lib/systemd/system/ssh.service /etc/systemd/system/multi-user.target.wants/ || true
62- echo " Created cloud-init and SSH service symlinks"
63-
64- # Protect SSH and cloud-init dependencies from autoremove
65- # Without these, the AMI won't be accessible via SSH after boot
66- apt-mark manual openssh-server cloud-init python3-systemd python3-jinja2 \
67- python3-yaml python3-oauthlib python3-configobj python3-requests \
68- python3-urllib3 python3-certifi python3-chardet python3-idna || true
69-
70- apt-get -y autoremove
71- apt-get -y autoclean
72-
7316 apt-get -y update
7417 apt-get -y upgrade
18+ apt-get -y autoremove
19+ apt-get -y autoclean
7520fi
21+
22+ # Clean temp files
7623rm -rf /tmp/* /var/tmp/*
24+
25+ # Clear history
7726history -c
7827cat /dev/null > /root/.bash_history
7928unset HISTFILE
29+
30+ # Clean logs
8031find /var/log -mtime -1 -type f -exec truncate -s 0 {} \;
8132rm -rf /var/log/* .gz /var/log/* .[0-9] /var/log/* -????????
33+
34+ # Clean cloud-init for fresh start
8235rm -rf /var/lib/cloud/instances/*
36+
37+ # Remove SSH keys (cloud-init regenerates on boot)
8338rm -f /root/.ssh/authorized_keys /etc/ssh/* key*
8439touch /etc/ssh/revoked_keys
8540chmod 600 /etc/ssh/revoked_keys
8641
87- # Securely erase the unused portion of the filesystem
42+ # Securely erase unused disk space
8843GREEN=' \033[0;32m'
8944NC=' \033[0m'
9045printf " \n${GREEN} Writing zeros to the remaining disk space to securely
@@ -95,11 +50,12 @@ The secure erase will complete successfully when you see:${NC}
9550Beginning secure erase now\n"
9651
9752dd if=/dev/zero of=/zerofile &
98- PID=$!
99- while [ -d /proc/$PID ]
100- do
101- printf " ."
102- sleep 5
103- done
53+ PID=$!
54+ while [ -d /proc/$PID ]; do
55+ printf " ."
56+ sleep 5
57+ done
10458sync; rm /zerofile; sync
105- cat /dev/null > /var/log/lastlog; cat /dev/null > /var/log/wtmp
59+
60+ cat /dev/null > /var/log/lastlog
61+ cat /dev/null > /var/log/wtmp
0 commit comments