Ramdisk

From ArchWiki
Revision as of 14:58, 4 December 2012 by Kynikos.bot (Talk | contribs) (wikify some external links, use https for archlinux.org)

Jump to: navigation, search

A ramdisk is a portion of RAM utilised as a disk. Many distributions use /dev/ram for this, however Arch does not have a /dev/ram so we must use /etc/fstab to create ramdisks. The most important thing to remember about a ramdisk is that it is stored in RAM and thus volatile. Anything stored on a ramdisk will be lost if the computer completely seizes up, or loses power. Therefore, it is necessary to save the contents of your ramdisk to the harddrive if you want to save them.

Why use one?

As a ramdisk is stored in RAM, it is much faster than a conventional filesystem. So, if you need to manipulate files at high speed, a ramdisk could be the best solution. Popular choices include /tmp, /var/cache/pacman, or /var/lib/pacman, although I'm sure you could think of many others.

How to create a ramdisk

To create a ramdisk, you must treat it as any other mounted filesystem. You must first pick and create a location for it to reside on, and then add it to your /etc/fstab file as follows:

none     /path/to/location     ramfs  defaults   0     0

If you do not have a lot of spare ram, it is recommended to use 'tmpfs' instead of 'ramfs', as tmpfs uses Swap when the available RAM starts to run out, whereas ramfs does not. Of course, dipping into swap loses the performance benefits of RAM, so it's a little pointless unless you have a lot of spare RAM.

Example usage

The files that have been edited are /etc/fstab, /etc/rc.local, and /etc/rc.local.shutdown to store /tmp, /var/cache/pacman, and /var/lib/pacman in RAM, and sync them to disk before shutting down. Portions of the files not relevant to the topic at hand have been removed to save space

/etc/fstab:

none        /tmp         ramfs   defaults              0 0
none        /mnt/ramdisk ramfs   defaults              0 0

/etc/rc.local:

chmod 777 /tmp
touch /etc/ramdisk.sh

/bin/cat - >> /etc/ramdisk.sh << EOT
#!/bin/sh

cd /var/ && /bin/tar cf abs.tar abs/
cd /var/cache/ && /bin/tar cf pacman.tar pacman/
cd /var/lib/ && /bin/tar cf pacman.tar pacman/

/bin/mkdir /mnt/ramdisk/var/
/bin/mkdir /mnt/ramdisk/var/cache/
/bin/mkdir /mnt/ramdisk/var/lib/

/bin/mv /var/abs /mnt/ramdisk/var && /bin/ln -s /mnt/ramdisk/var/abs /var/abs
/bin/mv /var/cache/pacman /mnt/ramdisk/var/cache &&  /bin/ln -s /mnt/ramdisk/var/lib/pacman /var/lib/pacman
/bin/mv /var/lib/pacman /mnt/ramdisk/var/lib && /bin/ln -s /mnt/ramdisk/var/cache/pacman /var/cache/pacman

/bin/ln -s /tmp /mnt/ramdisk/tmp
/bin/chmod 777 /mnt/ramdisk/tmp
EOT

/etc/ramdisk.sh &

/etc/rc.local.shutdown:

echo "Saving contents of ramdisk to harddrive"
rm /var/abs
rm /var/cache/pacman
rm /var/lib/pacman
mv /mnt/ramdisk/var/abs /var
mv /mnt/ramdisk/var/cache/pacman /var/cache
mv /mnt/ramdisk/var/lib/pacman /var/lib

Useful links