Skip to content

Instantly share code, notes, and snippets.

@Pusnow
Last active March 19, 2025 03:56
Show Gist options
  • Save Pusnow/c113dfa5a3d2a030c0c5e33f75f4a50e to your computer and use it in GitHub Desktop.
Save Pusnow/c113dfa5a3d2a030c0c5e33f75f4a50e to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
# disable swap
sudo sed -i '/swap/d' /etc/fstab
sudo swapoff -a
# uninstall packages
export DEBIAN_FRONTEND=noninteractive
TO_UNINSTALL=(
unattended-upgrades
apport
udisks2
snapd
ubuntu-report
popularity-contest
lxd-installer
lxd-agent-loader
multipath-tools
ubuntu-release-upgrader-core
packagekit
apparmor
sosreport
byobu
vim
)
for pacakge in ${TO_UNINSTALL[@]}; do
DEBIAN_FRONTEND=noninteractive sudo apt autoremove -y $pacakge
done
# install some package
sudo apt install -y bash-completion language-pack-en
exit
# enable manpages (borrwoed from unminimize)
if [ -f /etc/dpkg/dpkg.cfg.d/excludes ] || [ -f /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp ]; then
echo "Re-enabling installation of all documentation in dpkg..."
if [ -f /etc/dpkg/dpkg.cfg.d/excludes ]; then
mv /etc/dpkg/dpkg.cfg.d/excludes /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp
fi
echo "Updating package list and upgrading packages..."
apt-get update
# apt-get upgrade asks for confirmation before upgrading packages to let the user stop here
apt-get upgrade -y
echo "Restoring system documentation..."
echo "Reinstalling packages with files in /usr/share/man/ ..."
# Reinstallation takes place in two steps because a single dpkg --verified
# command generates very long parameter list for "xargs dpkg -S" and may go
# over ARG_MAX. Since many packages have man pages the second download
# handles a much smaller amount of packages.
dpkg -S /usr/share/man/ | sed 's|, |\n|g;s|: [^:]*$||' | DEBIAN_FRONTEND=noninteractive xargs --no-run-if-empty apt-get install --reinstall -y
echo "Reinstalling packages with system documentation in /usr/share/doc/ .."
# This step processes the packages which still have missing documentation
dpkg --verify --verify-format rpm | awk '$2 ~ /\/usr\/share\/doc/ {print $2}' | sed 's|/[^/]*$||' | sort | uniq |
xargs --no-run-if-empty dpkg -S | sed 's|, |\n|g;s|: [^:]*$||' | uniq | DEBIAN_FRONTEND=noninteractive xargs --no-run-if-empty apt-get install --reinstall -y
echo "Restoring system translations..."
# This step processes the packages which still have missing translations
dpkg --verify --verify-format rpm | awk '$2 ~ /\/usr\/share\/locale/ {print $2}' | sed 's|/[^/]*$||' | sort | uniq |
xargs --no-run-if-empty dpkg -S | sed 's|, |\n|g;s|: [^:]*$||' | uniq | DEBIAN_FRONTEND=noninteractive xargs --no-run-if-empty apt-get install --reinstall -y
if dpkg --verify --verify-format rpm | awk '$2 ~ /\/usr\/share\/doc/ {exit 1}'; then
echo "Documentation has been restored successfully."
rm /etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp
else
echo "There are still files missing from /usr/share/doc/:"
dpkg --verify --verify-format rpm | awk '$2 ~ /\/usr\/share\/doc/ {print " " $2}'
echo "You may want to try running this script again or you can remove"
echo "/etc/dpkg/dpkg.cfg.d/excludes.dpkg-tmp and restore the files manually."
fi
fi
if [ "$(dpkg-divert --truename /usr/bin/man)" = "/usr/bin/man.REAL" ]; then
# Remove diverted man binary
rm -f /usr/bin/man
dpkg-divert --quiet --remove --rename /usr/bin/man
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment