Skip to content

Instantly share code, notes, and snippets.

@Marc-Bender
Last active July 19, 2022 12:26
Show Gist options
  • Save Marc-Bender/08cb9c4393e85f74b124fa5f2c83a9eb to your computer and use it in GitHub Desktop.
Save Marc-Bender/08cb9c4393e85f74b124fa5f2c83a9eb to your computer and use it in GitHub Desktop.
Shell script to make a RAM disk of a certain size on Linux systems. Designed to replace my previous C-Solution. This solution now sanitizes inputs and comes with a manpage
#! /bin/sh
if [ "$#" -ne 1 ]
then
echo Exactly one argument must be given
else
if df | grep '/mnt/ramdisk' > /dev/null
then
echo Already mounted a RAM-disk...
elif [ `grep 'MemFree' /proc/meminfo | grep -o [0-9]*` -lt `numfmt --from=iec $1 --to-unit=1024` ]
# /proc/meminfo contains lots of information on your memory including the amount of available free memory
# this information is put through grep to find that exact line that contains the free memory then grep is used again to
# remove everything but the numbers (leading to a number with no whitespaces and no unit). That is then compared with the requested RAM-disksize
# numfmt is used to convert what ever input format your size has to kB (1024 Bytes) so that the existing free memory is comparable with
# the requested size. The script errors out when less RAM is free that the RAM-disk should be in size because that would lead to
# an impossible combination that is oddly enough still executable. E.g. my system has 16G of RAM and I could create a RAM-disk of 20G
# using the command below and it would be created... This should not be possible.
then
echo Too big of a RAM-disk to fit into your available RAM. No RAM-disk was created.
echo The maximum size of a RAM-disk currently possible is `grep 'MemFree' /proc/meminfo | cut -d: -f2`
else
sudo mount -t tmpfs tmpfs /mnt/ramdisk/ -o size=$1 && echo RAM-disk of $1 created
fi
fi
.\" use with
.\" groff -man -Tascii makeRamdiskOfSize.1
.\" to preview or archive with gz and store in /usr/share/man/man1 for actual use...
.TH makeRamdiskOfSize 1 "March 2020" Linux "User Manuals"
.SH NAME
makeRamdiskOfSize \- Utility for creating a RAM-disk in /mnt/ramdisk with input sanitization
.SH SYNOPSIS
.B makeRamdiskOfSize
.I sizeInFSTABCompatibleFormat
.SH DESCRIPTION
.B makeRamdiskOfSize
is a wrapper for the command 'sudo mount -t tmpfs tmpfs /mnt/ramdisk/ -o size=sizeInFSTABCompatibleFormat'.
Input sanitation is provided in the sense that it will be checked if a RAM-disk is already created in this location, preventing
any cases of a RAM-disk being created inside of a RAM-disk. This would render the files that may be present in the RAM-disk that was created first unaccessible (while still being inside of the RAM and taking up space) until the later created RAM-disk is removed (say using 'umount /mnt/ramdisk'). Furthermore it is tested that the user does not try to create a RAM-disk that is larger in size than the currently free RAM. It is not checked if there are other RAM-disks created by different means with some size that just is not used up yet (say with an entry in the /etc/fstab). Therefor it is still possible that there is more total RAM-disk-size allowed on the system than is free memory (or even installed memory) available in the system.
.SH OPTIONS
.IP "sizeInFSTABCompatibleFormat"
This option must be given. It specifies the size of the RAM-disk. Any format that is understood in the context of /etc/fstab should be valid here. Known working are numbers with the M or G suffix. See the examples below.
.SH FILES
.I /usr/bin/makeRamdiskOfSize
.RS
This is the script that does the whole business. The location is arbitrary but should be in $PATH for convinience.
The latest version of that script should be available on https://gist.github.com/Marc-Bender
.SH DIAGNOSTICS
The following diagnostics may be echoed out to stdout:
Exactly one argument must be given
.RS
You either have to few or too many arguments given.
.RE
Too big of a RAM-disk to fit into your available RAM. No RAM-disk was created.
.RS
You have asked for a RAM-disk bigger than your currently free RAM. Your options are to either lower the requested size of the RAM-disk or to free up more RAM. (Installing more RAM is technically also an option but this is not considered here)
You will be given the amount of free RAM in Bytes along side this message so you know your limit should you consider lowering the requested size.
.RE
RAM-disk of <sizeInFSTABCompatibleFormat> created
.RS
The creation of the RAM-disk was successful and the RAM-disk should show up in the output of 'df'.
The argument given to the command will replace the <sizeInFSTABCompatibleFormat> in the output mentioned above.
.RE
.SH EXAMPLES
makeRamdiskOfSize 1G
.RS
This will create a RAM-disk of 1 Gigabyte in /mnt/ramdisk provided that this directory exists and provided that there is no RAM-disk mounted to this point already. But only in case there is 1 Gigabyte of free RAM avialable...
.RE
makeRamdiskOfSize 100M
.RS
This will create a RAM-disk of 100 Megabytes in /mnt/ramdisk. All the restrictions and conditions that are mentioned in the first example still apply.
.RE
.SH BUGS
None known to the author (but this does not have to mean anything)
.SH AUTHOR
Marc Bender <marc dot andre dot bender at hotmail dot com>
.SH "SEE ALSO"
.BR mount (8),
.BR umount (8),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment