ramdisk.py is a Python script designed to simplify the creation and removal of RAM disks on Linux systems. It supports both traditional tmpfs RAM disks and compressed ZRAM RAM disks, providing a command-line interface to manage a RAM disk located at /ramdisk.
- Dual RAM Disk Types:
tmpfsRAM Disk: Create a standard RAM disk stored in volatile system memory.ZRAMRAM Disk: Create a compressed RAM disk using ZRAM, potentially saving memory while still offering RAM-like speeds.
- User-Specified Size: Easily create a RAM disk with a defined size (e.g., "4G" for 4 Gigabytes, "512M" for 512 Megabytes, "100K" for 100 Kilobytes).
- Simple Removal: Cleanly unmounts and removes the RAM disk, automatically detecting if it's
tmpfsorZRAMfor appropriate cleanup. - Fixed Mount Point: Operates on a predefined mount point:
/ramdisk. - Directory Management: Automatically creates the
/ramdiskdirectory if it doesn't exist during creation and removes it during the removal process. - Sudo Requirement: Enforces execution with
sudoprivileges, as mounting and unmounting filesystems are restricted operations. - Prerequisite Checks: For ZRAM, checks for necessary utilities like
zramctlandmkfs.ext2. - Input Validation: Includes basic validation for the size argument format.
- Status Messages: Provides informative messages about its operations and any errors encountered.
The script operates based on the command-line arguments provided.
- Sudo Check: Verifies if the script is run with
sudoprivileges. Exits if not. - Argument Parsing: Determines if a
tmpfsorZRAMdisk is requested (via the optional--zramflag) and validates the size argument. - Mount Point Check: Ensures
/ramdiskis not already a mount point. - Directory Creation: If
/ramdiskdoes not exist, it's created.
- Mount
tmpfs: Executesmount -t tmpfs -o size=<size_str> tmpfs /ramdisk.
- Prerequisite Check: Verifies
zramctlandmkfs.ext2(or the configuredZRAM_FS_TYPEtool) are available. - Load ZRAM Module: Ensures the
zramkernel module is loaded usingmodprobe zram. - Configure ZRAM Device:
- Uses
zramctl --find --size <size_str> --algorithm <ZRAM_COMP_ALGORITHM>to find an available ZRAM device (e.g.,/dev/zram0), set its disk size, and specify the compression algorithm (default:lz4).
- Uses
- Format ZRAM Device: Formats the allocated ZRAM device with the specified filesystem (default:
ext2) usingmkfs.<ZRAM_FS_TYPE> /dev/zramX. - Mount ZRAM Device: Mounts the formatted ZRAM device to
/ramdisk.
- Sudo Check: Verifies
sudoprivileges. - Mount Information Retrieval: If
/ramdiskis mounted, usesfindmnt(with a fallback to parsing/proc/mounts) to determine the source device and filesystem type. - Unmount Operation: Unmounts
/ramdiskusingumount /ramdisk. - ZRAM Device Reset (if applicable): If the source device was a ZRAM device (e.g.,
/dev/zramX), it resets the device usingzramctl --reset /dev/zramX. This frees up the ZRAM device. - Directory Removal: Attempts to remove the
/ramdiskdirectory if it exists and is empty.
-
Save the Script: Save the Python code into a file named
ramdisk.py. -
Make it Executable (Optional but Recommended): Open your terminal and run:
chmod +x ramdisk.py
-
Dependencies:
- Python 3.x
- Standard Linux command-line utilities (
mount,umount,mkdir,modprobe). - For
tmpfs(usually pre-installed): No special dependencies beyond standard utilities. - For
ZRAM:util-linux: Provides thezramctlutility.- Debian/Ubuntu:
sudo apt install util-linux - Fedora:
sudo dnf install util-linux
- Debian/Ubuntu:
e2fsprogs(or tools for your chosenZRAM_FS_TYPE): Providesmkfs.ext2.- Debian/Ubuntu:
sudo apt install e2fsprogs - Fedora:
sudo dnf install e2fsprogs
- Debian/Ubuntu:
- The
zramkernel module must be available and loadable.
Note: All commands must be run with sudo.
-
Create a 4GB
tmpfsRAM Disk:sudo python3 ramdisk.py 4G
If executable:
sudo ./ramdisk.py 4G -
Create a 1GB
ZRAMRAM Disk (compressed):sudo python3 ramdisk.py 1G --zram
If executable:
sudo ./ramdisk.py 1G --zram -
Create a 512MB
tmpfsRAM Disk:sudo python3 ramdisk.py 512M
-
Verify RAM Disk Creation:
- For both types:
df -h /ramdisk mount | grep /ramdisk - Additionally for ZRAM:
zramctl # Check /proc/swaps if you configured it as swap, though this script uses it as a block device for a filesystem
- For both types:
-
Remove the RAM Disk (works for both
tmpfsandZRAM):sudo python3 ramdisk.py remove
If executable:
sudo ./ramdisk.py remove
- Sudo Privileges: Essential for all operations.
- Linux Specific: Relies on Linux-specific tools and kernel features (
tmpfs,zram,mount,zramctl). - Data Volatility:
tmpfs: Data is stored in RAM and is lost on unmount or reboot.ZRAM: Data is stored in a compressed form in RAM. It is also lost on unmount (which includes ZRAM device reset) or reboot.
- Mount Point: Uses the hardcoded
/ramdiskmount point. - ZRAM Specifics:
- Compression: ZRAM uses compression (default
lz4in the script). The actual memory used will be less than the specified disk size, depending on data compressibility. - Performance: ZRAM involves a CPU overhead for compression/decompression. For highly compressible data, it can effectively increase available RAM for the disk.
lz4is generally fast. - Kernel Module: The
zramkernel module must be available. - Filesystem on ZRAM: The script formats the ZRAM device with
ext2by default. This is a lightweight filesystem suitable for temporary use.
- Compression: ZRAM uses compression (default
- Error Handling: The script provides basic error messages. If
umountfails due to "target is busy," uselsof /ramdiskorfuser -vm /ramdiskto find and stop processes using the RAM disk.
This script is released under the MIT License.
MIT License
Copyright (c) 2025 Cameron Walker - me@cameronwalker.nz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.