Skip to content

Instantly share code, notes, and snippets.

@avinash-oza
Last active January 19, 2026 17:35
Show Gist options
  • Select an option

  • Save avinash-oza/9791c4edd78a03540dc69d6fbf21bd9c to your computer and use it in GitHub Desktop.

Select an option

Save avinash-oza/9791c4edd78a03540dc69d6fbf21bd9c to your computer and use it in GitHub Desktop.
How to boot from RAM on debian

Booting Debian from a RAM disk

This guide is adapted from http://reboot.pro/topic/14547-linux-load-your-root-partition-to-ram-and-boot-it/

What you need:

  • lots of RAM
  • Debian based distribution or any that supports booting from initramfs
  • mkinitramfs or a tool to build a new initramfs
  • some linux knowledge
  • no need to create an image
  • no need for Grub4Dos
  • no need for a "special driver"

Step 1: Choose a distribution thats supports booting from initramfs. (like ubuntu)

Step 2: Install to harddisk. Make sure you split it into multiple partitions (/, /boot, /home, swap, ...).

Step 3: Boot your new system, install updates, drivers if neccessary (this will improve performance), strip it down to the minimum. Every file will be loaded to RAM ! A fresh install uses about 2 GB auf harddisk-space.

Step 4: modify /etc/fstab :

cp /etc/fstab /etc/fstab.bak find the line specifing the root partition and change it in: none / tmpfs defaults 0 0* save

Step 5: edit the local script in your initramfs: cd /usr/share/initramfs-tools/scripts/* make a backup of /usr/share/initramfs-tools/scripts/local cp local local.bak* modify local, find this line:

# FIXME This has no error checking

# Mount root

mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} ${rootmnt}

change it to:

# FIXME This has no error checking

# Mount root

#mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} ${rootmnt}

mkdir /ramboottmp

mount ${roflag} -t ${FSTYPE} ${ROOTFLAGS} ${ROOT} /ramboottmp

mount -t tmpfs -o size=100% none ${rootmnt}

cd ${rootmnt}

cp -rfa /ramboottmp/* ${rootmnt}

umount /ramboottmp
  • save
  • execute, or rebuild initramfs mkinitramfs -o /boot/initrd.img-ramboot* replace modified local with original file cp -f local.bak localStep 6:
  • modify this file (needs a better solution) /boot/grub/grub.cfg* copy the first boot entry and replace the /initrd line with this: /initrd /boot/initrd.img-ramboot* label the new entry as RAMBOOT This will boot our generated initramfs instead the original one. Step 7:
  • reboot
  • choose standart boot (no ramdisk)
  • choose RAMBOOT and all your files on the root partition will be loaded to a tmpfs
@mav-
Copy link

mav- commented Dec 21, 2024

Can anyone confirm this is still valid in Debian 12 ?

Just tested it in Debian 12, still valid.

@smutbert
Copy link

As I am about to try this, may I ask if I got it right:

We modify /usr/share/initramfs-tools/scripts/local so that the initramfs will mount a tmpfs as / and copy the content of the original rootfs there. Then we restore the original script and any initramfs built after that won't be special any more.

So we do have to repeat Step 5 (and maybe 6) after every kernel update, don't we?

In Step 7 we boot with an unmodified initramfs. Will this even work since we modified the fstab to have a tmpfs as / in Step 4?

@h35jqh
Copy link

h35jqh commented Jan 19, 2026

Thank you for this @avinash-oza it has been extemely useful to me. My nas OS is now fully running in ram and I can spindown the drives when not needed without worry of OS activity spinning them up.

I also can confirm works great in Debian 11.
For those interested I have created a ram-sync.service to synchronize the ram disk to the source disk used on startup . In my case the source partition copied as part of the ramboot system is on a flash drive. To implement the service you need the root privilege and to create 2 files.

  1. the ram-sync.sh script to be placed in /usr/local/bin/ram-sync.sh note is uses rsync so that must be available in your system.

ram-sync.sh script below: alter locations to suit your installation.
It creates a persistent log of the file synced in a log in the persistent user root directory.
In my case /mnt/sdd1mnt/root/ram-sync.log

ed@tmdeb:/usr/local/bin$

cat ram-sync.sh

#!/bin/bash

#syncing from RAM overlay to persistent storage
SRC="/"
DEST="/mnt/sdd1mnt" # replace with you source of root file system or where you want backup on persistent storage

#Mount the persistent root if needed
if ! mountpoint -q "$DEST"; then
mount /dev/sdd1 "$DEST" || {
echo "Failed to mount $DEST"
exit 1
}
fi

echo "Starting sync from $SRC to $DEST..." >> /mnt/sdd1mnt/root/ram-sync.log
date >> /mnt/sdd1mnt/root/ram-sync.log

#replace rsync line with this line to only do dry run
#rsync --dry-run -auAX -v --inplace \

#Rsync (modify exclusions if needed)
rsync -auAX -v --inplace \
--exclude={"/dev/","/proc/","/sys/","/tmp/","/run/","/mnt/","/media/","/lost+found"} \
--exclude="/var/log/journal/
" \
"$SRC" "$DEST" >> /mnt/sdd1mnt/root/ram-sync.log

echo "Sync complete."

#Optionally unmount
umount "$DEST"

  1. the file to implement the ram-sync.service which will be activated at shutdown or halt or reboot. note the time to ram-sync will vary based on what files need syncing to conserve sync you will notice the rsync excludes some journal logs. For testing add --dry-run to the rsync command in the ram-sync.sh script and it will only show you what it would have synced but not perform any file transfers..

Add the ram-sync.service file to the /etc/systemd/system directory:

cat /etc/systemd/system/ram-sync.service

[Unit]
Description=Sync RAM filesystem to disk on shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
Requires=local-fs.target
Conflicts=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/local/bin/ram-sync.sh
RemainAfterExit=yes
TimeoutStopSec=300

[Install]
WantedBy=multi-user.target

**
3. now ensure the service is enabled:

sudo systemctl enable ram-sync.service
sudo systemctl start ram-sync.service

if all is well you should get a similar output if you do a sudo systemctl status ram-sync.service to that below:

sudo systemctl status ram-sync.service

● ram-sync.service - Sync RAM filesystem to disk on shutdown
Loaded: loaded (/etc/systemd/system/ram-sync.service; enabled; vendor preset: enabled)
Active: active (exited) since Tue 2026-01-13 16:10:38 EST; 5 days ago
Process: 248 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
Main PID: 248 (code=exited, status=0/SUCCESS)
CPU: 1ms

Warning: journal has been rotated since unit was started, output may be incomplete.

  1. I hope this is useful to others. I have never posted to git before and not sure how to add files correctly so just copied cats as text for file contents. Apologize in advance for any confusions this introduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment