Supercharging Firefox: Moving Profiles to RAM on MX Linux
Supercharging Firefox: Moving Profiles to RAM on MX Linux
Running a web browser from a RAM-disk significantly improves performance and reduces disk I/O, which is particularly beneficial for SSD longevity and system responsiveness. Inspired by the methods detailed at youtux.org, I have developed a set of scripts tailored specifically for MX Linux 23/25 (SysVinit) to automate the installation and removal of this setup.
---
The Concept
Modern browsers like Firefox perform constant small read/write operations to their profile folder, including databases, cache, and session data. By moving the profile to /dev/shm—a temporary filesystem in RAM—and using a symlink, these operations happen at memory speeds.
To prevent data loss, this implementation includes:
- Sync at Boot: Restores the profile from the physical disk to RAM.
- Sync at Shutdown: Saves the current RAM state back to the physical disk.
- Periodic Backup: A cron job runs every 30 minutes to ensure changes are saved even if the system crashes.
---
Implementation for MX Linux (SysVinit)
The solution consists of three parts: an installer, an uninstaller, and desktop entries for easy GUI management via pkexec.
#### 1. The Installation Script (install-ff-ram.sh)
This script detects the active Firefox profile, creates a backup on the physical disk, and moves the working files to RAM. It also sets up a SysVinit script in /etc/init.d/ and a crontab entry for the user.
#### 2. The Uninstaller (uninstall-ff-ram.sh)
If you need to revert to a standard disk-based setup, the uninstaller performs a final sync, removes the symlink, restores the backup directory to its original location, and cleans up the SysVinit and cron entries.
#### 3. Desktop Integration (ff-ram-desktop.sh)
To make this accessible, this script generates two .desktop files. These appear in the System/Settings menu, allowing you to toggle the RAM-disk setup without touching the terminal.
---
Key Advantages
- Speed: Instantaneous page loading from cache and faster database queries.
- Privacy: Browsing artifacts remain in volatile memory.
- Disk Health: Minimizes wear on SSDs by redirecting constant writes to RAM.
Precautions
Because /dev/shm is volatile, any data not synced to the physical disk will be lost upon a power failure. The 30-minute cron job mitigates this risk, but it is important to ensure the firefox-ram service is allowed to run through the full shutdown sequence to perform the final save operation.
---
Script Source Code
#### File: /usr/local/bin/install-ff-ram.sh
#!/bin/bash
# --- CONFIGURATION ---
if [ "$EUID" -ne 0 ]; then echo "Please run as root (sudo)"; exit 1; fi
REAL_USER=${SUDO_USER:-$(logname)}
REAL_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
MOZ_PATH="$REAL_HOME/.config/mozilla/firefox"
RAM_PATH="/dev/shm/firefox-profile-$REAL_USER"
# 1. Find Profile
PROFILE_PATH=$(find "$MOZ_PATH" -maxdepth 1 -type d -name "*.default-release" | head -n 1)
if [ -z "$PROFILE_PATH" ]; then
RELATIVE_PATH=$(grep -m 1 '^Path=' "$MOZ_PATH/profiles.ini" | cut -d= -f2)
PROFILE_PATH="$MOZ_PATH/$RELATIVE_PATH"
fi
PROFILE_NAME=$(basename "$PROFILE_PATH")
BACKUP_PATH="$MOZ_PATH/$PROFILE_NAME.backup"
# --- SAFETY CHECK ---
if [ -L "$PROFILE_PATH" ] || [ -d "$BACKUP_PATH" ]; then
echo "----------------------------------------------------------------"
echo "CHECK: Firefox RAM-disk profile seems to be already installed."
echo "Reason: Link exists at $PROFILE_PATH or Backup exists at $BACKUP_PATH"
echo "Aborting to prevent data loss."
echo "----------------------------------------------------------------"
exit 0
fi
if [ ! -d "$PROFILE_PATH" ]; then
echo "Error: Profile path $PROFILE_PATH not found."
exit 1
fi
echo "Stopping Firefox for $REAL_USER..."
pkill -u "$REAL_USER" firefox
sleep 2
# 2. Create the helper script
cat <<EOF > /usr/local/bin/firefox-ram-sync
#!/bin/bash
case "\$1" in
restore)
mkdir -p $RAM_PATH
chown $REAL_USER:$REAL_USER $RAM_PATH
# Sync from Backup (SSD) to RAM
sudo -u $REAL_USER rsync -avX $BACKUP_PATH/ $RAM_PATH/
;;
save)
# Sync from RAM to Backup (SSD)
sudo -u $REAL_USER rsync -avX --delete $RAM_PATH/ $BACKUP_PATH/
;;
esac
EOF
chmod +x /usr/local/bin/firefox-ram-sync
# 3. Create the Init Script
cat <<EOF > /etc/init.d/firefox-ram
#!/bin/sh
### BEGIN INIT INFO
# Provides: firefox-ram
# Required-Start: \$local_fs \$remote_fs
# Required-Stop: \$local_fs \$remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Sync Firefox profile to RAM
### END INIT INFO
case "\$1" in
start) /usr/local/bin/firefox-ram-sync restore ;;
stop) /usr/local/bin/firefox-ram-sync save ;;
*) echo "Usage: /etc/init.d/firefox-ram {start|stop}"; exit 1 ;;
esac
EOF
chmod +x /etc/init.d/firefox-ram
update-rc.d firefox-ram defaults
# 4. Initial Migration
echo "Moving profile to backup and creating symlink..."
mkdir -p $RAM_PATH
cp -a "$PROFILE_PATH/." "$RAM_PATH/"
mv "$PROFILE_PATH" "$BACKUP_PATH"
ln -s "$RAM_PATH" "$PROFILE_PATH"
chown -h $REAL_USER:$REAL_USER "$PROFILE_PATH"
chown -R $REAL_USER:$REAL_USER $RAM_PATH
chown -R $REAL_USER:$REAL_USER $BACKUP_PATH
# 5. Add Cron Job
CRON_JOB="*/30 * * * * /usr/local/bin/firefox-ram-sync save"
(crontab -u $REAL_USER -l 2>/dev/null | grep -Fv "/usr/local/bin/firefox-ram-sync" ; echo "$CRON_JOB") | crontab -u $REAL_USER -
#### File: /usr/local/bin/uninstall-ff-ram.sh
#!/bin/bash
if [ "$EUID" -ne 0 ]; then echo "Please run as root (sudo)"; exit 1; fi
REAL_USER=${SUDO_USER:-$(logname)}
REAL_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
MOZ_PATH="$REAL_HOME/.config/mozilla/firefox"
PROFILE_PATH=$(find "$MOZ_PATH" -maxdepth 1 -type l -name "*.default-release" | head -n 1)
if [ -z "$PROFILE_PATH" ]; then
for d in "$MOZ_PATH"/*; do
if [ -L "$d" ]; then PROFILE_PATH="$d"; break; fi
done
fi
BACKUP_PATH="$PROFILE_PATH.backup"
if [ ! -L "$PROFILE_PATH" ]; then
echo "Aborting: No symlink found."
exit 1
fi
echo "Stopping Firefox..."
pkill -u "$REAL_USER" firefox
sleep 2
if [ -f "/usr/local/bin/firefox-ram-sync" ]; then
/usr/local/bin/firefox-ram-sync save
fi
rm "$PROFILE_PATH"
mv "$BACKUP_PATH" "$PROFILE_PATH"
if [ -f "/etc/init.d/firefox-ram" ]; then
update-rc.d -f firefox-ram remove
rm /etc/init.d/firefox-ram
fi
[ -f "/usr/local/bin/firefox-ram-sync" ] && rm /usr/local/bin/firefox-ram-sync
rm -rf "/dev/shm/firefox-profile-$REAL_USER"
crontab -u "$REAL_USER" -l 2>/dev/null | grep -v "firefox-ram-sync save" | crontab -u "$REAL_USER" -
#### Desktop Entry Configuration
cat <<EOF > /usr/share/applications/install-ff-ram.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=Install Firefox to RAM
Comment=Move Firefox profile to RAM for speed
Exec=pkexec /usr/local/bin/install-ff-ram.sh
Icon=browser-download
Terminal=true
Categories=System;Settings;
EOF
cat <<EOF > /usr/share/applications/uninstall-ff-ram.desktop
[Desktop Entry]
Version=1.0
Type=Application
Name=Uninstall Firefox RAM-disk
Comment=Move Firefox profile back to physical disk
Exec=pkexec /usr/local/bin/uninstall-ff-ram.sh
Icon=edit-delete
Terminal=true
Categories=System;Settings;
EOF