Last active
December 11, 2018 07:33
-
-
Save taavituisk/52daffcca5fe14e8d3c8 to your computer and use it in GitHub Desktop.
Make ephemeral swap and tmp.
This file contains 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 | |
# mk-eph.sh | |
# Creates a lvm volume over all ephemeral devices. | |
# Swap related operations | |
function SwapEnabler() { | |
local swapdev='/dev/ephemeral/swap' | |
local swapon="$(swapon -s | wc -l)" | |
# See if some swap is enabled already. | |
if (( ${swapon} > 1 )); then | |
echo 'Swap already enabled' | |
return 0 | |
fi | |
if [[ -e '/dev/ephemeral/swap' ]]; then | |
echo 'Enabling swap on vg group: ephemeral' | |
mkswap -L 'ephemeral' "${swapdev}" | |
swapon -L 'ephemeral' | |
else | |
echo 'vg group "ephemeral" not yet enabled. skipping swap.' | |
return 1 | |
fi | |
echo 'swap done' | |
} | |
# Trying to enable swap as the first thing. | |
SwapEnabler | |
# Will quit if /dev/ephemeral directory is already present. | |
if [[ -d '/dev/ephemeral' ]]; then | |
echo 'VG group at /dev/ephemeral already present, quitting' | |
exit 0 | |
fi | |
# Initializing variables | |
device_count=0 | |
# Getting the device list from aws metadata service.. | |
edevices=$(curl -s http://169.254.169.254/latest/meta-data/block-device-mapping/ | grep ephemeral) | |
# Generating a usable list of all ephemeral devices. | |
for device in ${edevices}; do | |
edev=$(curl -s http://169.254.169.254/latest/meta-data/block-device-mapping/${device}) | |
localdev="/dev/${edev/sd/xvd}" | |
# Skip if the local device is not found | |
if [[ ! -b "${localdev}" ]]; then | |
continue | |
fi | |
# Doing the umount just in case | |
umount ${localdev} | |
# This script must be reboot proof, not adding already added volumes. | |
pvd=$(pvdisplay -s ${localdev} 2>&1); ret=${?} | |
if [[ ${ret} != 0 ]]; then | |
ephemeral_devices="${ephemeral_devices} ${localdev}" | |
(( device_count++ )) | |
else | |
echo "device ${localdev} already initialized" | |
fi | |
done | |
# No point in continuing if there are no ephemeral devices. | |
if (( ${device_count} < 1 )); then | |
echo "No ephemeral drives found, quitting." | |
exit 0 | |
fi | |
# See if we can use striping or not | |
if (( ${device_count} > 1 )); then | |
lvprefix="lvcreate -i ${device_count}" | |
else | |
lvprefix="lvcreate" | |
fi | |
# Use LVM striping | |
memtotal=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}') | |
# Creating the pv and vg groups. | |
pvcreate $ephemeral_devices | |
vgcreate ephemeral $ephemeral_devices | |
# We need to check if we have enough space for tmp and swap. | |
vgtotal=$(vgdisplay -c ephemeral | awk -F ':' '{print $12}') | |
vgtmp=$(( ${vgtotal} - ${memtotal} )) | |
# See if we have more than 2GB available after enabling swap. | |
if (( ${vgtmp} < 2465792 )); then | |
echo "Taking ${memtotal}kB for swap would leave only ${vgtmp}kB for tmp. Only swap enabled." | |
${lvprefix} -n swap -l 100%FREE ephemeral | |
else | |
${lvprefix} -n swap -L ${memtotal}K ephemeral | |
${lvprefix} -n tmp -l 100%FREE ephemeral | |
fi | |
# Trying to enable swap as the last thing too. | |
SwapEnabler | |
# Done | |
echo "lvm volume done with devices: ${ephemeral_devices}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment