Skip to content

Commit

Permalink
Added example boilerplate
Browse files Browse the repository at this point in the history
A user solving #16 can add their code on line 36.
  • Loading branch information
soyfrien authored May 16, 2022
1 parent 3ebd0c0 commit 48afb12
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions usr/sbin/makeswap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ err() {
exit 1
}


#######################################
# Converts a string to a form that looks like an integer of bytes
# See https://github.com/soyfrien/makeswap-on-azure.service/issues/16
# Arguments:
# human_readable_size
# Returns:
# bytestring
#######################################
human_to_bytestring() {
local temp="$1"

# Begin Github magic.
# 🪄🧟‍♀️🧟🧟‍♂️🧙🏽‍♀️🧙🏽🧙🏽‍♂️🤹🏻‍♀️🤹🏿🤹🏼‍♂️🧌🪄
# End Github magic.

local bytestring="$temp"
return bytestring
}

#######################################
# Allocates disk space, creates swap file and enables it
# Globals:
Expand Down Expand Up @@ -54,6 +74,16 @@ commit_swap() {
if [ "$base" != "G" ] && [ "$base" != "g" ] && [ "$base" != "M" ] && [ "$base" != "m" ] && [ "$base" != "K" ] && [ "$base" != "k" ]; then
swap_size=$(echo "$swap_size" | gawk '{print int($1)}')
fi

# Get disk space as bytes
FREE_DISK_SPACE=$(df | grep /mnt | gawk '{print int($4)}')

swap_size=$(human_to_bytestring "$swap_size")

# If this is true it exits returning 1
if [[ "$swap_size" < "$FREE_DISK_SPACE" ]]; then
err "Not enough free disk space."
fi

fallocate -l "$swap_size" "$SWAP_FILE"
if (($? == 0)); then
Expand Down

0 comments on commit 48afb12

Please sign in to comment.