#!/bin/bash # Luke's all singing, all dancing, Gentoo Portage @world update script # ====== EDIT VARIABLES BELOW HERE ====== # Portage temporary directory to be passed to "rm -r", if you would like it cleared afterwards (e.g. "/tmp/portage/*") PORTAGE_TEMP="/var/tmp/portage/*" # Services that are safe to restart if they have deleted files open (e.g. "apache2 sshd vixie-cron") SAFE_SERVICES="cupsd lvmetad ntpd smartd sshd tor syslog-ng vixie-cron webmin" # ====== EDIT VARIABLES ABOVE HERE ====== # Handle arguments while test $# -gt 0 do case "$1" in --skip-sync) export SKIP_SYNC="true" # In case we don't to --sync the Portage tree ;; --skip-layman-sync) export SKIP_LAYMAN_SYNC="true" # In case we don't to --sync-all Layman ;; --skip-news) export SKIP_NEWS="true" # In case we don't to read new news before continuing ;; --skip-update) export SKIP_UPDATE="true" # In case we don't want to do the actual "emerge --update" ;; --skip-depclean) export SKIP_DEPCLEAN="true" # In case we don't want to do an "emerge --depclean" ;; --skip-preserved-rebuild) export SKIP_PRESERVED_REBUILD="true" # In case we don't want to do an "emerge @preserved-rebuild" ;; --*) echo "bad argument '$1'" ; exit 1 ;; *) echo "unexpected argument '$1'" ; exit 1 ;; esac shift done # Sync the Portage tree, unless skipped if [ -z ${SKIP_SYNC+x} ]; then echo 'emerge --sync' emerge --sync || exit 1 fi # Sync Layman repos, unless skipped if [ -z ${SKIP_LAYMAN_SYNC+x} ]; then echo 'layman -S' # Only try to run layman if the command seems to exist if command -v layman > /dev/null; then layman -S || exit 1 fi fi # Check to see if there's any news, unless skipped if [ -z ${SKIP_NEWS+x} ] && [ $(eselect news count new) -ne "0" ]; then echo "IMPORTANT: at least 1 news item needs reading" echo "Use eselect news read to view new items" exit 1 fi # Do a @world update, unless skipped if [ -z ${SKIP_UPDATE+x} ]; then echo 'emerge --keep-going -av --update --newuse --deep --with-bdeps=y @world' emerge --keep-going -av --update --newuse --deep --with-bdeps=y @world || exit 1 fi # Update -9999 packages since they aren't included in a @world update pkgs9999=$(find /var/db/pkg/ -mindepth 2 -maxdepth 2 -name '*-9999') if [ "$pkgs9999" != "" ] ; then echo "emerge -av $(find /var/db/pkg/ -mindepth 2 -maxdepth 2 -name '*-9999' | tr '\n' ' ' | sed 's|/var/db/pkg/|=|g')" emerge -av $(find /var/db/pkg/ -mindepth 2 -maxdepth 2 -name '*-9999' | tr '\n' ' ' | sed 's|/var/db/pkg/|=|g') fi # Remove unnecessary dependencies, unless skipped if [ -z ${SKIP_DEPCLEAN+x} ]; then echo 'emerge -av --depclean' emerge -av --depclean || exit 1 fi # Rebuild any packages known to be built against old/missing packages, unless skipped if [ -z ${SKIP_PRESERVED_REBUILD+x} ]; then echo 'emerge -av --keep-going @preserved-rebuild' emerge -av --keep-going @preserved-rebuild || exit 1 fi # Search for any remaining packages built against old/missing packages echo 'revdep-rebuild -p' revdep-rebuild -- -av || exit 1 # Restart any defined safe-to-restart services that have open but deleted files DELETED=$(lsof -n | grep "DEL\|deleted") for service in ${SAFE_SERVICES[@]}; do echo "$DELETED" | grep -Eq "^$service\s+[0-9]" if [ "$?" -eq 0 ]; then echo "/etc/init.d/$service restart" /etc/init.d/"$service" restart || exit 1 fi done # Print any remaining processes that have open but deleted files echo 'lsof -n | grep "DEL\|deleted" | less' echo "$(echo -e 'These things might need manually restarting afterwards due to having open files:\n' && lsof -n | grep DEL)" | less || exit 1 # Clean up /etc/portage/package.* echo 'portpeek -arq' portpeek -arq || exit 1 # Remove distfiles not owned by any installed package echo 'eclean-dist --destructive' eclean-dist --destructive || exit 1 # Clear Portage's temporary directory, as defined above echo 'rm -r '"$PORTAGE_TEMP" #rm -r "$PORTAGE_TEMP" || exit 1 echo "Would do rm -r $PORTAGE_TEMP here, do not have the balls yet due to rm -ring a variable" || exit 1 # Print disk space echo 'df -H' df -H || exit 1 # Print an SSH warning echo -e '\nAll done! If sshd was restarted, now would be a good time to verify you can SSH in to this machine...'