Creates and mounts a proper RAMdisk of custom size. (tmpfs IS NOT A REAL RAMDISK AND CAN SWAP!)
wget -q https://kps.makz.net/run/ramdisk.sh
#!/bin/bash
##
# v2.0
##
_SIZE=1 # Gigabyte
##
##
_force=0
_info=0
while getopts "fhirs:" opt; do
case "${opt}" in
f)
_force=1
;;
h)
echo "If you call this script without any arguments it'll attempt to create a 1GB RAMDisk."
echo ""
echo " -h"
echo " Show this help."
echo " -i"
echo " Show RAMDisk status."
echo " -r"
echo " Unmount and remove RAMDisk."
echo " Be warned that this happens forcefully and will kill any application still accessing /srv/ramdisk!"
echo " -s <number>"
echo " Create ext4 RAMDisk of <number> Gigabyte size at /srv/ramdisk."
exit 100
;;
i)
if $(mountpoint -q /srv/ramdisk)
then
df -h /srv/ramdisk
exit $?
else
echo "No RAMDisk at /srv/ramdisk"
exit 1
fi
;;
r)
echo "(Forcefully) Removing RAMDisk..."
fuser -km /srv/ramdisk
umount /srv/ramdisk
blockdev --flushbufs /dev/ram0
modprobe -r brd
exit $?
;;
s)
_SIZE=${OPTARG}
;;
\?)
exit 1;
;;
esac
done
##
_SIZE_KB=$((${_SIZE} * 1024 * 1024))
if [ ! $(modinfo brd >/dev/null 2>/dev/null && ! modprobe -n --first-time brd 2>/dev/null && echo 0 || echo 1) = 0 ]
then
echo "INFO: Ramdisk Kernel Module not loaded. Loading now..."
modprobe brd rd_nr=1 max_part=1 rd_size=${_SIZE_KB}
sleep 1
echo "Done loading Ramdisk Kernel Module."
echo "Creating Ramdisk with size of ${_SIZE}GB now..."
if [ ! -d "/srv/ramdisk" ]
then
mkdir /srv/ramdisk
chattr +i /srv/ramdisk
fi
dd if=/dev/zero of=/dev/ram0 bs=1024 count=${_SIZE}M status=progress
mke2fs -t ext4 -O extents -vm0 /dev/ram0 ${_SIZE}G -L 'RAMDISK'
mount /dev/ram0 /srv/ramdisk
chmod a+rwx /srv/ramdisk
echo "Done creating ${_SIZE}GB Ramdisk at /srv/ramdisk"
elif [ -d "/srv/ramdisk/lost+found" ]
then
echo "RAMDisk /srv/ramdisk already exists. Exiting."
else
echo "Something went wrong."
fi