chmod +x upgrade_ubuntu.sh
sudo ./upgrade_ubuntu.sh --dist-upgrade
sudo reboot
sudo ./upgrade_ubuntu.sh --release-upgrade
cat /var/log/dist-upgrade/main.log cat /var/log/dist-upgrade/apt.log
| #!/bin/bash | |
| # ============================================================================= | |
| # Azure Ubuntu VM Upgrade Script | |
| # Purpose: Update packages or upgrade the OS release on an Azure Ubuntu VM. | |
| # Usage: | |
| # sudo bash azure_ubuntu_upgrade.sh [--release-upgrade [22.04|24.04]] [--dist-upgrade] | |
| # Without options, performs a safe apt upgrade only. | |
| # ============================================================================= | |
| set -euo pipefail | |
| # Configuration | |
| AZURE_AGENT_PKG="walinuxagent" | |
| CLOUD_INIT_PKG="cloud-init" | |
| UPDATE_MANAGER_PKG="update-manager-core" | |
| # Colors for output | |
| RED='\033[0;31m' | |
| GREEN='\033[0;32m' | |
| YELLOW='\033[1;33m' | |
| NC='\033[0m' | |
| # ----------------------------------------------------------------------------- | |
| # Helper functions | |
| # ----------------------------------------------------------------------------- | |
| log_info() { echo -e "${GREEN}[INFO]${NC} $*"; } | |
| log_warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } | |
| log_error() { echo -e "${RED}[ERROR]${NC} $*" >&2; exit 1; } | |
| check_root() { | |
| if [[ $EUID -ne 0 ]]; then | |
| log_error "This script must be run as root (use sudo)." | |
| fi | |
| } | |
| is_azure_vm() { | |
| # Check if running on Azure by looking for the Azure agent | |
| systemctl is-active --quiet walinuxagent 2>/dev/null || dpkg -l walinuxagent &>/dev/null | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # Package update (safe) | |
| # ----------------------------------------------------------------------------- | |
| perform_package_update() { | |
| log_info "Updating package lists..." | |
| apt update -y | |
| log_info "Upgrading all packages..." | |
| apt upgrade -y | |
| # Upgrade Azure-specific packages explicitly | |
| if is_azure_vm; then | |
| log_info "Azure VM detected – ensuring Azure agent and cloud‑init are up to date..." | |
| apt install --only-upgrade -y "$AZURE_AGENT_PKG" "$CLOUD_INIT_PKG" || true | |
| fi | |
| # Optionally clean up | |
| apt autoremove -y | |
| apt autoclean | |
| log_info "Package update completed." | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # Distribution upgrade (apt dist-upgrade) | |
| # ----------------------------------------------------------------------------- | |
| perform_dist_upgrade() { | |
| log_info "Performing full distribution upgrade (apt dist-upgrade)..." | |
| apt update -y | |
| apt dist-upgrade -y | |
| apt autoremove -y | |
| apt autoclean | |
| log_info "Distribution upgrade completed. Please reboot if kernel was updated." | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # OS release upgrade (e.g., 20.04 to 22.04) | |
| # ----------------------------------------------------------------------------- | |
| perform_release_upgrade() { | |
| # Install update-manager-core if missing | |
| if ! dpkg -l "$UPDATE_MANAGER_PKG" &>/dev/null; then | |
| log_info "Installing $UPDATE_MANAGER_PKG..." | |
| apt install -y "$UPDATE_MANAGER_PKG" | |
| fi | |
| log_info "Ensuring current system is fully updated before release upgrade..." | |
| perform_package_update | |
| log_info "Upgrading to the next available LTS release (non‑interactive)..." | |
| export DEBIAN_FRONTEND=noninteractive | |
| echo "libc6 libraries/restart-without-asking boolean true" | debconf-set-selections | |
| # Use -d to allow upgrade before the first point release | |
| do-release-upgrade -f DistUpgradeViewNonInteractive --allow-third-party -m server | |
| log_warn "Release upgrade completed. A system reboot is strongly recommended." | |
| } | |
| # ----------------------------------------------------------------------------- | |
| # Main script | |
| # ----------------------------------------------------------------------------- | |
| check_root | |
| RELEASE_UPGRADE=false | |
| TARGET_VERSION="" | |
| DIST_UPGRADE=false | |
| # Parse arguments | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| --release-upgrade) | |
| RELEASE_UPGRADE=true | |
| if [[ -n "${2:-}" && "$2" != --* ]]; then | |
| TARGET_VERSION="$2" | |
| shift | |
| fi | |
| ;; | |
| --dist-upgrade) | |
| DIST_UPGRADE=true | |
| ;; | |
| *) | |
| log_error "Unknown option: $1" | |
| ;; | |
| esac | |
| shift | |
| done | |
| # Pre‑flight check: is this an Azure VM? | |
| if is_azure_vm; then | |
| log_info "Running on an Azure VM." | |
| else | |
| log_warn "Azure agent not detected – proceeding, but Azure‑specific steps may be skipped." | |
| fi | |
| # Execute requested operations | |
| if $RELEASE_UPGRADE; then | |
| log_warn "======================================================================" | |
| log_warn " WARNING: You are about to perform a major OS release upgrade." | |
| log_warn " Ensure you have a recent snapshot/backup of the VM's OS disk." | |
| log_warn "======================================================================" | |
| read -rp "Type 'yes' to continue: " confirm | |
| if [[ "$confirm" != "yes" ]]; then | |
| log_info "Aborted by user." | |
| exit 0 | |
| fi | |
| perform_release_upgrade "$TARGET_VERSION" | |
| elif $DIST_UPGRADE; then | |
| perform_dist_upgrade | |
| else | |
| perform_package_update | |
| fi | |
| log_info "Script completed successfully." |