Last active
June 17, 2021 19:57
-
-
Save ezwiefel/70662a9289dd10f116f157b9bd049bb9 to your computer and use it in GitHub Desktop.
AML Compute Instance - Enable Swap Space setup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copyright (c) 2021 Microsoft | |
# | |
# This software is released under the MIT License. | |
# https://opensource.org/licenses/MIT | |
# ============================================ | |
# AZURE ML COMPUTE INSTANCE - ENABLE SWAP FILE | |
# ============================================ | |
# This startup script is intended for Azure ML Compute Instances | |
# The script will update the /etc/waagent.conf file to include the swap file settings. | |
# See https://docs.microsoft.com/en-us/troubleshoot/azure/virtual-machines/oom-swap-file-linux-vm | |
# for more details. | |
# In choosing the swap size, many different recommendations exist. Since this is intended for | |
# Azure ML Compute Instance - which is intended for very RAM intensive data manipulation tasks | |
# we'll use pretty aggressive recommendations - following the "hibernation" guidelines here: | |
# https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/storage_administration_guide/ch-swapspace#tb-recommended-system-swap-space | |
echo "=================" | |
echo "Create Swap Space" | |
echo "=================" | |
echo | |
RAM_MB=`free -m | awk '/Mem:/ {print $2}'` | |
SWAP_MB=`free -m | awk '/Swap:/ {print $2}'` | |
echo "Current:" | |
echo " RAM (MB) : " $RAM_MB | |
echo " Swap (MB): " $SWAP_MB | |
echo | |
# First, determine if the swap space is already set, if it is, then skip | |
# this step | |
# NOTE: This logic could be updated to check if the alloted swap size follows the recommended sizing | |
# But for our purposes, we'll just exit the script if the swap file exists | |
if [ $SWAP_MB -ne 0 ]; then | |
echo " Swap Already Created - exiting script" | |
exit 0 | |
fi | |
echo "Future:" | |
# Calculate the future swap size | |
# Since expr can't multiply float values, we'll | |
# multiple these by 10, and then divide total by 10 at the end | |
if [ $RAM_MB -le 2048 ]; then | |
# If RAM 2 GB or less, then swap = 3x RAM | |
SWAP_RATIO=30 | |
elif [ $RAM_MB -le 8192 ]; then | |
# If RAM 2 GB - 8 GB, then swap = 2x RAM | |
SWAP_RATIO=20 | |
elif [ $RAM_MB -le 65536 ]; then | |
# If RAM 8 GB - 64 GB, then swap = 1.5x RAM | |
SWAP_RATIO=15 | |
else | |
# Swap == RAM | |
SWAP_RATIO=10 | |
fi | |
SWAP_TIMES_TEN=`expr $RAM_MB \* $SWAP_RATIO` | |
NEW_SWAP_MB=`expr $SWAP_TIMES_TEN / 10` | |
echo " Swap (MB): " $NEW_SWAP_MB | |
# Set values in waagent.conf | |
echo | |
echo "Actions:" | |
echo " Creating backup of config file - '/etc/waagent.conf.bak'" | |
echo " Updated '/etc/waagent.conf'" | |
sed -i.bak -e 's/ResourceDisk.Format=.*/ResourceDisk.Format=y/' \ | |
-e 's/#* *ResourceDisk.EnableSwap=.*/ResourceDisk.EnableSwap=y/' \ | |
-e "s/#* *ResourceDisk.SwapSizeMB=.*/ResourceDisk.SwapSizeMB=$NEW_SWAP_MB/" /etc/waagent.conf | |
shutdown -r 1 #reboot in 1 minute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think there needs to be a form of 'chmod +w /etc/waagent.conf' for this to run without any other intervention. Once I did that manually, it ran great.