Linux/Systemd/SwapEphemeralDrive
- Create and enable swap file on Azure ephemeral drive
shell script /root/azure-swapfile.sh
# Azure Swap File Script # Version: 1.0 # Created: 2025-11-13 # Purpose: Automatically creates and activates swap file on Azure ephemeral disk (/mnt) # Hardcoded settings SWAP_SIZE="1G" SWAP_FILE="/mnt/swapfile" WAIT_SECS=30 set -euo pipefail # Exit on error, undefined vars, pipe failures # Function to cleanup on failure cleanup() { if [[ -f "$SWAP_FILE" ]] && ! swapon --show | grep -q "$SWAP_FILE"; then echo "Cleaning up incomplete swap file..." rm -f "$SWAP_FILE" fi } trap cleanup ERR # Wait for /mnt to be mounted echo "Waiting for /mnt to mount..." for ((i = 1; i <= WAIT_SECS; i++)); do if mountpoint -q /mnt; then echo "/mnt is mounted." break fi sleep 1 done if ! mountpoint -q /mnt; then echo "Error: /mnt not mounted after ${WAIT_SECS}s. Exiting." exit 1 fi # Convert size to bytes for comparison SWAP_SIZE_BYTES=$(numfmt --from=iec "$SWAP_SIZE") # Check if swap file exists and has correct size RECREATE_SWAP=false if [[ ! -f "$SWAP_FILE" ]]; then echo "Swap file does not exist." RECREATE_SWAP=true else CURRENT_SIZE=$(stat -f%z "$SWAP_FILE" 2>/dev/null || stat -c%s "$SWAP_FILE" 2>/dev/null) if [[ "$CURRENT_SIZE" != "$SWAP_SIZE_BYTES" ]]; then echo "Swap file size mismatch. Current: $CURRENT_SIZE bytes, Expected: $SWAP_SIZE_BYTES bytes." RECREATE_SWAP=true fi fi if [[ "$RECREATE_SWAP" == "true" ]]; then echo "Creating swap file: $SWAP_FILE ($SWAP_SIZE)" # Remove existing file if present [[ -f "$SWAP_FILE" ]] && rm -f "$SWAP_FILE" # Try fallocate first, fall back to dd if it fails if ! fallocate -l "$SWAP_SIZE" "$SWAP_FILE" 2>/dev/null; then echo "fallocate failed, using dd as fallback..." dd if=/dev/zero of="$SWAP_FILE" bs=1M count=$(numfmt --from=iec --to-unit=1048576 "$SWAP_SIZE") status=progress fi chmod 600 "$SWAP_FILE" mkswap "$SWAP_FILE" echo "Swap file created and formatted." else echo "Swap file exists with correct size; skipping creation." fi # Activate swap (idempotent: safe if already on) if ! swapon --show | grep -q "$SWAP_FILE"; then swapon "$SWAP_FILE" echo "Swap activated: $SWAP_FILE" else echo "Swap already active: $SWAP_FILE" fi # Display current swap status echo "Current swap status:" swapon --showsystemd unit file /etc/systemd/system/azure-swapfile.service
# Azure Swap File Service # Version: 1.0 # Created: 2025-11-13 # Purpose: Automatically creates and activates swap file on Azure ephemeral disk (/mnt) # Compatible with: Ubuntu/Debian on Azure VMs with ephemeral storage [Unit] Description=Activate Swap File on Azure Ephemeral Disk (/mnt) Documentation=https://learn.microsoft.com/en-us/azure/virtual-machines/linux/swap After=local-fs.target RequiresMountsFor=/mnt DefaultDependencies=yes [Service] Type=oneshot ExecStart=/root/azure-swapfile.sh RemainAfterExit=yes StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target
enable the systemd unit file
sudo systemctl daemon-reload sudo systemctl enable azure-swapfile.service sudo systemctl start azure-swapfile.service sudo systemctl status azure-swapfile.service
