kindwolf.org Git repositories xavierg-snippets / master create-multiple-swap-files / mkswap
master

Tree @master (Download .tar.gz)

mkswap @masterraw · history · blame

#!/usr/bin/env bash
# Create and enable as many 1-GiB swap files as the kernel allows.
source determine-max-swapfiles-2.bash
echo "MAX_SWAPFILES: ${MAX_SWAPFILES}"

umask 0077
hostname=$(hostname -s)
((bs=1024**2))
((size=1*1024*bs))
((count=size/bs))
for num in $(seq 1 "${MAX_SWAPFILES}"); do
	printf -v num_padded "%02d" "${num}"
	file="/data/swap/${num_padded}"
	label="${hostname^^}SWAP${num_padded}"
	# 4-KiB swap partition header:
	truncate -s 4096 "${file}"
	# Fill it with zeroes:
	dd if=/dev/zero of="${file}" bs=4096 count=1 status=none
	# Add the desired swap size, filled with zeroes too:
	dd if=/dev/zero of="${file}" conv=notrunc oflag=append count="${count}" bs="${bs}" status=none
	# Format the swap partition:
	mkswap -L "${label}" "${file}"
	swapon "${file}"
done