Tutorials/Ramdisk enabled server

This tutorial is intended to give you a basic understanding of what a ramdisk is, what use it is for Minecraft and how to make a Minecraft server use a ramdisk.

=Ramdisk Introduction= Conventionally, files and directories are stored on hard disk drives which, by today's standards, offer a lot of space at mediocre data transfer rates (between 80 MB/s - 250 MB/s). Ramdisks are virtual file systems (unlike HDDs which are hardware) that live completely inside the computer's RAM. They offer significantly higher data transfer rates (1300 MB/s - 3200 MB/s) at the cost of volatility (= data will be lost after restarting the computer) and space (limited by the amount of RAM installed on the system, including swap space).

=Advantages and Disadvantages=

Advantages

 * Very high transfer speed (data to application)
 * Very low seek time (searching between and in files)

Disadvantages

 * Ramdisks will be cleared when a system restarts
 * Unfeasible if the world size exceeds the available RAM

=Why it makes sense for Minecraft servers= In a Minecraft server, one of the strongest bottlenecks are disk I/O related operations (e.g. chunk management). By moving the data into the RAM, access times will be near instant and data transfer rates will be significantly faster, making chunk loading and saving much faster operations. Since a Minecraft world currently consists of very many chunk files, seek time is equally, if not more, important for overall speed.

=Basic Minecraft and ramdisk setup= '''Make sure to back up your files before starting! '''

Linux (easy way)
A simple way to load a minecraft server into a ramdisk was posted on the Aimless Bits blog on March 12, 2011. It involves modifying the server startup script available on the wiki and making some minor changes to fstab. This guide fleshes out the process and makes some minor changes to Aimless Bits' script.

This quick guide assumes you have a user for loading minecraft, a minecraft directory and a server running. It also helps to be familiar with the /etc/init.d/minecraft startup script.

sudo nano /etc/fstab Then add this line, making sure that the path is correct (username, dir name etc) tmpfs /home/username/minecraft_ramdisk tmpfs  defaults,size=512m      0       0 The size of the ramdisk MUST be larger than the minecraft directory world. Make sure that you give yourself some overhead. mount -t tmpfs none /home/username/minecraft_ramdisk -o size=512m
 * Firstly, start by creating a directory for the ramdisk in your home directory, i.e. "/home/username/minecraft_ramdisk".
 * To mount it as a ramdisk, simply edit your /etc/fstab/ file:
 * Restart your computer. The ramdisk will now be loaded every time you restart. If you wish to load immediately, type

It's now a matter of simply running a modified script that loads the files on the drive onto the server, copies them back on a timely basis to prevent data loss, and does backups. Again, this is a modified version of the script found at Aimless Bits.

If you have /etc/init.d/minecraft, delete it or overwrite it with this script. If you don't, make a new text file, call it minecraft, and copy this script into it.


 * 1) !/bin/bash
 * 2) /etc/init.d/minecraft
 * 3) version 0.5 2011-09-24 (YYYY-MM-DD)


 * 1) BEGIN INIT INFO
 * 2) Provides:   minecraft
 * 3) Required-Start: $local_fs $remote_fs
 * 4) Required-Stop:  $local_fs $remote_fs
 * 5) Should-Start:   $network
 * 6) Should-Stop:    $network
 * 7) Default-Start:  2 3 4 5
 * 8) Default-Stop:   0 1 6
 * 9) Short-Description:    Minecraft server
 * 10) Description:    Starts the minecraft server
 * 11) END INIT INFO

SERVICE='minecraft_server.jar' USERNAME="username" MCSTORE='/home/username/minecraft' MCPATH='/home/username/minecraft_ramdisk' CPU_COUNT=1 INVOCATION="java -Xmx2048M -Xms2048M -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalPacing -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -jar minecraft_server.jar nogui" BACKUPPATH='/home/username/minecraft_backups/'
 * 1) Settings

ME=`whoami` as_user { if [ "$ME" == "$USERNAME" ] ; then bash -c "$1" else su - $USERNAME -c "$1" fi }

mc_start { if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "Tried to start but $SERVICE was already running!" else echo "$SERVICE was not running... starting." cd $MCPATH if [ ! -f "$MCPATH/minecraft_server.jar" ] then echo "Ram drive empty... prepping." as_user "cp -R $MCSTORE/* $MCPATH/" fi   as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION" sleep 7 if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is now running." else echo "Could not start $SERVICE." fi fi }

mc_saveoff { if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is running... suspending saves" as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP STARTING. Server going readonly...\"\015'" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'" sync sleep 10 else echo "$SERVICE was not running. Not suspending saves." fi }

mc_saveon { if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is running... re-enabling saves" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'" as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'" else echo "$SERVICE was not running. Not resuming saves." fi }

mc_stop { if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is running... stopping." as_user "screen -p 0 -S minecraft -X eval 'stuff \"say SERVER SHUTTING DOWN IN 5 SECONDS. Saving map...\"\015'" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'" sleep 5 as_user "screen -p 0 -S minecraft -X eval 'stuff \"stop\"\015'" sleep 5 else echo "$SERVICE was not running." fi       if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE could not be shut down... still running." else echo "$SERVICE is shut down." fi }

mc_update { if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is running! Will not start update." else MC_SERVER_URL=http://minecraft.net/`wget -q -O - http://www.minecraft.net/download.jsp | grep minecraft_server.jar\ | cut -d \" -f 2`   as_user "cd $MCPATH && wget -q -O $MCPATH/minecraft_server.jar.update $MC_SERVER_URL"    if [ -f $MCPATH/minecraft_server.jar.update ]    then      if `diff $MCPATH/minecraft_server.jar $MCPATH/minecraft_server.jar.update >/dev/null`        then           echo "You are already running the latest version of $SERVICE."        else          as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/minecraft_server.jar"          echo "Minecraft successfully updated."      fi    else      echo "Minecraft update could not be downloaded."    fi  fi }

mc_backup { echo "Backing up minecraft files" if [ -f $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`.tar.gz ] then for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 do      if [ -f $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`-$i.tar.gz ] then continue else as_user "cd $MCSTORE && tar zcf $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`-$i.tar.gz ." break fi    done else as_user "cd $MCSTORE && tar zcf $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d"`.tar.gz ." fi  echo "Backup complete" }

mc_disksaverun {

if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "Saving ramdrive to disk." if [ ! -f $MCPATH/minecraft_server.jar ] then echo "Error.. Minecraft not in ram" else if [ -d $MCSTORE/world2 ] then as_user "rm -r $MCSTORE/world2" fi    if [ -d $MCSTORE/world ] then as_user "mv $MCSTORE/world $MCSTORE/world2" fi

as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-off\"\015'" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-all\"\015'" as_user "cp -R $MCPATH/* $MCSTORE/" as_user "screen -p 0 -S minecraft -X eval 'stuff \"save-on\"\015'"

if [ -d $MCSTORE/world2 ] then as_user "rm -r $MCSTORE/world2" fi  fi  else echo "Service is not running" fi

}

mc_disksavehalt { echo "Saving ramdrive to disk." if [ ! -f $MCPATH/minecraft_server.jar ] then echo "Error.. Minecraft not in ram" else if [ -d $MCSTORE/world2 ] then as_user "rm -r $MCSTORE/world2" fi    if [ -d $MCSTORE/world ] then as_user "mv $MCSTORE/world $MCSTORE/world2" fi

echo "Saving, screen session closed" as_user "cp -R $MCPATH/* $MCSTORE/"

if [ -d $MCSTORE/world2 ] then as_user "rm -r $MCSTORE/world2" fi  fi

}

case "$1" in start)    mc_start    ;;  stop) mc_stop mc_disksavehalt ;; restart)    mc_stop    mc_disksavehalt    mc_start    ;;  update) mc_stop mc_backup mc_update mc_start ;; backup)    mc_disksaverun    mc_saveoff    mc_backup    mc_saveon    ;;  disksavehalt) mc_disksavehalt ;; disksaverun)    mc_disksaverun    ;;  status) if ps ax | grep -v grep | grep -v -i SCREEN | grep $SERVICE > /dev/null then echo "$SERVICE is running." else echo "$SERVICE is not running." fi   ;;
 * 1) Start-Stop here

*) echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|status|restart|disksaverun}"  exit 1  ;; esac

exit 0

mv /directory/wherefileis/filename /etc/init.d/minecraft chmod a+x /etc/init.d/minecraft
 * Move this script into your /etc/init.d/ directory, and make it executable:

You're almost done! This script behaves exactly like the standard startup script, only that it takes care of loading and maintaining the ramdisk. You can also modify the script to use rsync instead of cp

"rsync -r -t $MCPATH/ $MCSTORE/"

in case you want to do other things, such as remote copying, but performance differences are probably neglible unless you have very big worlds.

sudo crontab -e Then add the line: 20 */6 * * * /etc/init.d/minecraft backup
 * DO NOT SKIP THIS STEP. You need to add a crontab entry to save your world. See below for specific reasons, but you run the risk of losing data if you don't do this. This script has two disk save functions, disksavehalt and disksaverun. Disksavehalt assumes the screen session is closing or backing up, and thus does not disable level saving. Do NOT call this function in crontab. Use disksave run instead. To do this
 * /5 * * * * /etc/init.d/minecraft disksaverun

The number represents how often in minutes should you save the world. If you feel like you have a robust setup, power supply backups and the whole shebang, run this less frequently. Otherwise, stick to 5 minutes at the least!

The other line runs minecraft backup every 6 hours, at :20. Don't skimp on backups! You've been warned!

Hope this helps all those would be admins out there. Good luck!

Linux (alternative)
On most Linux distributions there is already a ramdisk set up (usually mounted to /dev/shm (shared memory)) which defaults to using at most half of your total installed RAM. If there is not one already set up, resources on how to do it are widely available on the Internet.

It is possible to move anything into the ramdisk, but here I will focus on just moving the map into it and leaving the server files on the conventional drive.

Given the following basic server directory "minecraft_server/", inside a user's home directory, containing the world "world" and all other required files

We will want to move "world/" into the shared memory. Because of the volatility of ramdisks, we will also want to create a new folder into which an automated script will periodically save the current snapshot of the world, called (for example) "world_storage" by copying the current world to a new name

$ cd ~/minecraft_server/ $ cp -r world/ world_storage/

Now with the old world in a safe location, we can go ahead and move the world into the ram-disk

$ mkdir /dev/shm/minecraft $ mv world/ /dev/shm/minecraft

By now, the world is loaded into the RAM, but the Minecraft server doesn't see it in its directory anymore, causing it to recreate it when started. To stop it from doing that, we have to create a symbolic link to the world in the ramdisk by running

$ ln -s /dev/shm/minecraft/world/.

This will create a link to "/dev/shm/minecraft/world/" called "world/" in the server's directory, which the server will use like the actual world folder, but now inside the RAM.

Now we need to take care of the volatility of the ramdisk, by periodically saving the world from the RAM into "world_storage/". I'm going to use cron for scheduling and rsync for synching here.

First, we need a script that can be called by cron (it doesn't have to be a script, you could call rsync directly from the cron command line, but this allows for easy customizing later on)


 * 1) !/bin/sh

VOLATILE="/home/$USER/minecraft_server/world/" PERMANENT="/home/$USER/minecraft_server/world_storage/"

rsync -r -t -v "$VOLATILE" "$PERMANENT"
 * 1) TODO: Check if both directories actually exist, skipped here for clearness

And then we need to make this script execute every few minutes (I'll use 5 minutes here, you can test out what works best for you)

$ crontab -e

You will be put into an editor (more precisely: the editor in your "EDITOR" environment variable) for editing your user cron table. Add the following line:


 * /5 * * * * bash /home//minecraft_server/save_world.sh &>/dev/null

Now if your server restarts you will need to recreate the world folder (/dev/shm/minecraft) then (/dev/shm/minecraft/world) in the shared memory because the /dev/shm/ empties after restart,. You can do this by making another similar shell script.

So make a shell script file like before: exec 1>/tmp/backup_world.log 2>&1 #sends the output to this file mkdir /dev/shm/minecraft mkdir /dev/shm/minecraft/world
 * 1) !/bin/sh
 * 2) remake the paths

VOLATILE="/home/$USER/minecraft_server/world/" PERMANENT="/home/$USER/minecraft_server/world_storage/"

rsync -r -t -v "$PERMANENT" "$VOLATILE"
 * 1) TODO: Check if both directories actually exist, skipped here for clearness
 * 2) reversed the order

Everytime you restart you need to run this script to remount the Ramdisk. Do not add this to the crontab. You can add this to the start up if you figure it out.

Windows
Use a tool like Dataram RAMDisk to create a RAM disk and put the server on it. This free tool has options to automatically save the image every time it shuts down and also every few minutes. Just remember to format the new "drive" once it's created.

Mac OS X
Type this create your RAM disk on Mac OS:

diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://1165430`

It's only one command line to write, quite fast and efficient. :)

If you've followed these instructions, your RAM Disk will be available in /Volumes/ramdisk. After that, proceed as if you were on Linux, using Terminal and your favorite text editor.

Tutoriels/Sauvegarder vos donnés de cartes sur un RAM-disque