Bash script to create ramdisks

In a bid to get my CS:GO to load faster, I wrote a couple of bash scripts to create a ramdisk and remove it cleanly while keeping and renaming the ramdisk’d folder automagically.

Features:

  • Should work on any (Linux) System
  • Checks if you have enough ram to put stuff in
  • Allows for multiple ramdisks
  • Creates separate ramdisk for each file/folder
  • When you create a ramdisk, you can specify a buffer to allow for file growth/changes
  • Cleanup program to get stuff out of a ramdisk

Scripts:

ramdisk
#!/bin/bash

#Check if path is not specified, not a full path or help requested. Print help and exit
if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$(echo "$1" | cut -c 1)" != "/" ]
then
    echo "USAGE: ramdisk [PATH] [BUFFER]"
    echo ""
    echo "  PATH: The location of the file/folder. Must be a FULL PATH"
    echo "BUFFER: Specify number of Megabytes larger to make ramdisk (default 1)"
    echo ""
    echo "EXAMPLE: ramdisk /home/user/Documents/FILE.txt 120"
    echo "Will put FILE.txt into a ramdisk with a 120MB buffer size"
    exit 3
fi

#Check if buffer size specified
if [ -z "$2" ]
then
	#if not, set to 1MB
	BUFFER=1000
else
	BUFFER=$(( $2 * 1000 ))
fi

#Get size of file/folder in kilobytes
SIZE=$((`du -sk "$1" | cut -f1` + $BUFFER))

#Get available RAM in kilobytes
RAM=$(cat /proc/meminfo | grep MemAvailable | tr -dc '0-9')

#Get name of file/folder in path
NAME=$(basename "$1")

#Remove trailing "/" from path
FILEPATH="${1%/}"

#Is there enough RAM?
if [ "$SIZE" -gt "$RAM" ]
then
	echo "NOT ENOUGH RAM!"
	echo Avail"  |" $(( $RAM / 1000 ))MB
        echo Used"   |" $(( $SIZE / 1000 ))MB
	echo Needed" |" $(( $(($SIZE - $RAM)) / 1000 ))MB
	exit 1
else
	echo Creating ramdisk for \"$NAME\"
	echo At path \"$FILEPATH\"
	echo With buffer of $(($BUFFER / 1000))MB
	echo Avail" |" $(( $RAM / 1000 ))MB
	echo Used"  |" $(( $SIZE / 1000 ))MB
	echo Free"  |" $(( $(($RAM - $SIZE)) / 1000 ))MB
	read -p "Is this OK? [y/n] " yn
	case $yn in
		[Yy]* ) ;;
		[Nn]* ) exit 2;;
		* ) echo "Please answer yes or no.";;
	esac

fi

#Store path for ramdisk-cleanup
echo "$FILEPATH" >> ~/.ramdisk

#Create /ramdisk folder if it doesn't exist
if [ ! -d /ramdisk ]
then
	sudo mkdir /ramdisk
fi

#Create folder for the ramdisk
sudo mkdir /ramdisk/"$NAME"

#Create the ramdisk for real
sudo mount -t tmpfs -o size="$SIZE"K tmpfs /ramdisk/"$NAME"

#Make sure it's owned by the current user
sudo chown -R $USER /ramdisk

#Copy file/s to the ramdisk
echo "Copying..."
cp -a "$FILEPATH" /ramdisk/"$NAME"/

#Rename the original file/folder
mv "$FILEPATH" "$FILEPATH"_ORIG

#Create link to ramdisk with original name
ln -s /ramdisk/"$NAME"/"$NAME" "$FILEPATH"

echo "DONE"
ramdisk-cleanup
#!/bin/bash

#Check if there are any ramdisks to clean
if [ ! -f ~/.ramdisk ]; then
	echo Nothing to clean
	exit 1
fi

while true
do
	echo "Items available for cleanup:"

	#List names of Folders in ramdisk history
	while read p
	do
		basename "$(grep "$p" ~/.ramdisk)"
	done < ~/.ramdisk

	read -p "Which ramdisk to clean? " TARGET

	#Get full path from ramdisk history
	FILEPATH=$(grep "$TARGET" ~/.ramdisk)

	echo will clean \"$TARGET\" at path \"$FILEPATH\"
	read -p "Is this OK? [y/n] " yn
	case $yn in
		[Yy]* ) break;;
		[Nn]* ) ;;
		* ) echo "Please answer yes or no.";;
	esac
done

#Remove contents of ramdisk folder
sudo rm -rf /ramdisk/"$TARGET"/*

#Unmount ramdisk folder
sudo umount /ramdisk/"$TARGET"

#get rid of link
rm "$FILEPATH"

#rename original
mv "$FILEPATH"_ORIG "$FILEPATH"

#remove ramdisk from history
echo "$(grep -v "$FILEPATH" ~/.ramdisk)" > ~/.ramdisk

echo "DONE"

just copy these, chmod +x them and put somewhere in your $PATH (I keep them in ~/.local/bin) and enjoy faster load times!

Edit:
Here’s a github repo!

9 Likes

You sir are a hero. I had always wanted to know how to do this.

1 Like