Difference between revisions of "Ramdisk"

From ArchWiki
Jump to: navigation, search
m
(Marked out of date: mentions /etc/rc.local and Arch Linux does not use initscripts anymore.)
(23 intermediate revisions by 14 users not shown)
Line 1: Line 1:
[[Category:File systems (English)]]
+
[[Category:File systems]]
==What is a ramdisk?==
+
[[de:Ramdisk]]
 
+
[[es:Ramdisk]]
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.
+
[[fr:ramfs]]
 +
[[ja:Ramdisk]]
 +
{{Out of date|Mentions /etc/rc.local and Arch Linux does not use initscripts anymore.}}
 +
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 [[Fstab | /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?==
 
==Why use one?==
  
As a ramdisk is stored in RAM, it is much faster than a conventional harddrive. So, if you need to manipulate files at high speed, a ramdisk could be the best solution. Possible uses are for /tmp, /var/cache/pacman, or /var/lib/pacman, although I'm sure you could think of many others.
+
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, [[Firefox Ramdisk|Firefox profile]], although I'm sure you could think of many others.
  
 
==How to create a ramdisk==
 
==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:
+
To create a ramdisk, you treat it like any other filesystem, using any arbitrary word for the device file (here "none"):
  
  none     /path/to/location     ramfs    0    0
+
  mount -t ramfs none /path/to/location
  
If you don't 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 doesn't. 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.
+
For a more permanent solution, you may 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==
 
==Example usage==
  
Here is an example of the current (as of this writing) set up on my computer. I have edited /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. I have removed portions of the files not relevant to the topic at hand to save space
+
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:'''
 
'''/etc/fstab:'''
Line 28: Line 35:
 
  touch /etc/ramdisk.sh
 
  touch /etc/ramdisk.sh
 
   
 
   
  echo "#!/bin/dash" >> /etc/ramdisk.sh
+
  /bin/cat - >> /etc/ramdisk.sh << EOT
 +
#!/bin/sh
 
   
 
   
  echo "cd /var/ && tar cf abs.tar abs/" >> /etc/ramdisk.sh
+
  cd /var/ && /bin/tar cf abs.tar abs/
  echo "cd /var/cache/ && tar cf pacman.tar pacman/" >> /etc/ramdisk.sh
+
  cd /var/cache/ && /bin/tar cf pacman.tar pacman/
  echo "cd /var/lib/ && tar cf pacman.tar pacman/" >> /etc/ramdisk.sh
+
  cd /var/lib/ && /bin/tar cf pacman.tar pacman/
 
   
 
   
  echo "mkdir /mnt/ramdisk/var/" >> /etc/ramdisk.sh
+
  /bin/mkdir /mnt/ramdisk/var/
echo "mkdir /mnt/ramdisk/var/cache/" >> /etc/ramdisk.sh
+
/bin/mkdir /mnt/ramdisk/var/cache/
echo "mkdir /mnt/ramdisk/var/lib/" >> /etc/ramdisk.sh
+
/bin/mkdir /mnt/ramdisk/var/lib/
 
   
 
   
  echo "mv /var/abs /mnt/ramdisk/var" >> /etc/ramdisk.sh
+
  /bin/mv /var/abs /mnt/ramdisk/var && /bin/ln -s /mnt/ramdisk/var/abs /var/abs
  echo "mv /var/cache/pacman /mnt/ramdisk/var/cache" >> /etc/ramdisk.sh
+
  /bin/mv /var/cache/pacman /mnt/ramdisk/var/cache &&  /bin/ln -s /mnt/ramdisk/var/lib/pacman /var/lib/pacman
  echo "mv /var/lib/pacman /mnt/ramdisk/var/lib" >> /etc/ramdisk.sh
+
  /bin/mv /var/lib/pacman /mnt/ramdisk/var/lib && /bin/ln -s /mnt/ramdisk/var/cache/pacman /var/cache/pacman
 
   
 
   
  echo "ln -s /mnt/ramdisk/var/abs /var/abs" >> /etc/ramdisk.sh
+
  /bin/ln -s /tmp /mnt/ramdisk/tmp
echo "ln -s /mnt/ramdisk/var/cache/pacman /var/cache/pacman" >> /etc/ramdisk.sh
+
  /bin/chmod 777 /mnt/ramdisk/tmp
  echo "ln -s /mnt/ramdisk/var/lib/pacman /var/lib/pacman" >> /etc/ramdisk.sh
+
EOT
echo "ln -s /tmp /mnt/ramdisk/tmp" >> /etc/ramdisk.sh
+
 
   
 
   
echo "chmod 777 /mnt/ramdisk/tmp" >> /etc/ramdisk.sh
 
echo "rm /etc/ramdisk.sh" >> /etc/ramdisk.sh
 
 
chmod +x /etc/ramdisk.sh
 
 
  /etc/ramdisk.sh &
 
  /etc/ramdisk.sh &
  
Line 61: Line 64:
 
  mv /mnt/ramdisk/var/cache/pacman /var/cache
 
  mv /mnt/ramdisk/var/cache/pacman /var/cache
 
  mv /mnt/ramdisk/var/lib/pacman /var/lib
 
  mv /mnt/ramdisk/var/lib/pacman /var/lib
 +
 +
==Useful links==
 +
* [https://bbs.archlinux.org/viewtopic.php?id=50893 How to mount Ramdisk]
 +
* [https://bbs.archlinux.org/viewtopic.php?pid=326269 Ramdrive setup]
 +
* [http://www.kernel.org/doc/Documentation/filesystems/tmpfs.txt Official tmpfs documentation]
 +
* [http://www.kernel.org/doc/Documentation/filesystems/ramfs-rootfs-initramfs.txt Official ramfs documentation]

Revision as of 00:13, 5 September 2013

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: Mentions /etc/rc.local and Arch Linux does not use initscripts anymore. (Discuss in Talk:Ramdisk#)

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, Firefox profile, although I'm sure you could think of many others.

How to create a ramdisk

To create a ramdisk, you treat it like any other filesystem, using any arbitrary word for the device file (here "none"):

mount -t ramfs none /path/to/location

For a more permanent solution, you may 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