Ramdisk Service for Systemd

Ramdisk service for systemd that stores the content during shutdown.

The  RAM memory is a lot faster than any SSD disk. This means you want to have the browser or game cache in RAM, don’t you? When you shut down you don’t want your cache to disappear, do you? If you use Linux and Systemd, the new and excellent init replacing the System V init or others, this is a way to do it.

The reading speed of this disk on my system is 1.5 GB/s when measured with hdparm.

Create ramdisk.service with the following contents:

[Unit]
Description=Ramdisk
After=syslog.target
Requires=local-fs.target
After=local-fs.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/root/openramdisk
ExecStop=/root/closeramdisk

[Install]
WantedBy=graphical.target

Save it in /etc/systemd/system.

Create createramdisk with the following content. Save it in /root.

#!/bin/bash
mkdir /var/lib/ramdisk
dd if=/dev/zero of=/var/lib/ramdisk/ramdisk.xfs bs=1M count=1024 status=none
mkfs.xfs -q /var/lib/ramdisk/ramdisk.xfs

Create openramdisk with the following content. Save it in /root.

#!/bin/bash
rsync -a /var/lib/ramdisk/ramdisk.xfs /tmp/ramdisk.xfs
mkdir -p /tmp/ramdisk
mount -o loop /tmp/ramdisk.xfs /tmp/ramdisk
chmod 0777 /tmp/ramdisk

Create closeramdisk with the following content. Save it in /root.

#!/bin/bash
xfs_freeze -f /tmp/ramdisk
rsync -a /tmp/ramdisk.xfs /var/lib/ramdisk/ramdisk.xfs
xfs_freeze -u /tmp/ramdisk
umount -ld /tmp/ramdisk
# /tmp/ramdisk.xfs and /tmp/ramdisk are not deleted
# deleting them might cause problems with open connections and unfinished transactions.

Create deleteramdisk with the following content. Save it in /root.

#!/bin/bash
rm -rf /var/lib/ramdisk /tmp/ramdisk.xfs /tmp/ramdisk

Run these commands:

cd /root
sudo chmod +x createramdisk openramdisk closeramdisk deleteramdisk
sudo ./createramdisk
sudo systemctl enable ramdisk.service
sudo systemctl start ramdisk.service

1 thought on “Ramdisk Service for Systemd

  1. Comment from the #archlinux chat:

    (04:39:37 PM) gtmanfred: though if you made an actual.mount point, you could also make a .service that was before thast mount point got unmounted that could save and restore
    (04:39:49 PM) gtmanfred: i would make a .mount unit
    (04:40:04 PM) gtmanfred: and a .service that just does the save and restore (without that script)

Leave a comment