Site icon eKiwi-Blog.de

Setting up and using a RAM disc in Linux

Script for setting up a RAM disc in Linux.

In contrast to Windows, a RAM disc can be set up quite easily in Linux using board tools. Below you will find a script that will do this for you.

Video

Setting up a RAM disc in Linux

We copy the code (below) into a new file, e.g. with the name “ramdisk.sh”. We then make the file executable.

Size and configuration

We can define the size and mount point for integration at the beginning of the file.

Start of the script

Ideally, we start the script via a terminal. We can now use the menu to mount the RAM disc, remove it from the system or check the status.

The code

Alternatively, you can also find the script as a ZIP file here:

ramdisk.sh

#!/bin/bash

# Define variables
MOUNT_POINT="/mnt/ramdisk"
SIZE="1G" # You can adjust this size (e.g., 512M, 2G)

# Function to create and mount tmpfs RAM disk
create_ramdisk() {
    if [ ! -d "$MOUNT_POINT" ]; then
        echo "Creating mount point at $MOUNT_POINT..."
        sudo mkdir -p "$MOUNT_POINT"
    fi

    echo "Mounting tmpfs at $MOUNT_POINT with size $SIZE..."
    sudo mount -t tmpfs -o size=$SIZE tmpfs $MOUNT_POINT

    echo "RAM disk mounted at $MOUNT_POINT with size $SIZE"
}

# Function to check the RAM disk
check_ramdisk() {
    echo "Checking mounted RAM disks..."
    df -h | grep "$MOUNT_POINT"
}

# Function to unmount and clean up the RAM disk
unmount_ramdisk() {
    echo "Unmounting RAM disk at $MOUNT_POINT..."
    sudo umount $MOUNT_POINT

    if [ $? -eq 0 ]; then
        echo "Successfully unmounted $MOUNT_POINT"
    else
        echo "Failed to unmount $MOUNT_POINT"
    fi
}

# Main menu
echo "Choose an option:"
echo "1. create and mount RAM disk"
echo "2. check RAM disk"
echo "3. unmount RAM disk"
echo "4. exit"

read -p "Enter your choice [1-4]: " choice

case $choice in
    1)
        create_ramdisk
        ;;
    2)
        check_ramdisk
        ;;
    3)
        unmount_ramdisk
        ;;
    4)
        echo "Exiting "
        ;;
    *)
        echo "Invalid option!"
        ;;
esac
Exit mobile version