Minecraft Wiki

除另有声明,转载时均必须注明出处若簡繁轉換出錯,請以遊戲內為準请勇于扩充与修正内容有兴趣逛逛我们的微博沟通交流,欢迎到社区专页需要协助,请在告示板留言

了解更多

Minecraft Wiki
Advertisement
Ic translate
此条目的(部分)内容需要翻译。

你可以帮助我们来翻译此条目,但请勿使用机器翻译

警告

因为这是Wiki的一个条目,可以在任何时间内被任何人编辑,因此建议你不要完全使用这个脚本,而是将其当作编写脚本的指导手册看待。

该教程向你大概描述什么是虚拟硬盘,怎样和 Minecraft 使用,以及如何设置一个虚拟硬盘服务器。

虚拟硬盘描述

传统上,文件和文件夹储存在硬盘驱动器,但以今天的标准来看,普通数据的传输速度(80MB/秒 到 200MB/秒之间)的提升空间还很大。虚拟硬盘是一个虚拟的文件系统(不像硬盘那样是硬件),会把文件数据完全实时储存在计算机内存条里。它们的普通数据传输速度(3000MB/秒 到 15000MB/秒之间)非常高,但会失去稳定性(重启电脑数据就会丢失)以及空间(受系统安装内存条的数量限制,包括交换空间)。然而,有许多实用工具可以每隔一段时间备份虚拟硬盘里面的数据,以及在关机前备份,然后在系统启动时加载上次保存的数据。

优点和缺点

优点

  • 非常高的传输速度(从数据到应用)
  • 非常低的寻道时间(在文件之间搜索)

缺点

  • 在系统重启时会清除虚拟硬盘的数据
  • 如果世界尺寸大于内存条可用空间则很难实现

为什么它对 Minecraft 服务器有意义

在一个 Minecraft 服务器,最大的一个瓶颈是相关的磁盘 I/O 操作(如区块管理)。通过向内存移动数据,访问时间会被大大缩减,数据传输速度也会大大提高,让区块加载和保存区块操作变得更快。因为一个 Minecraft 世界包含了许多区块文件,寻道时间没多大变化,因为重要的是整体速度。

基础 Minecraft 和虚拟硬盘安装

确保在开始前备份了你的文件!

GNU/Linux(简单方法)

2011年3月12日,在 Aimless Bits 的博客里,发布了把一个 Minecraft 服务器加载到虚拟硬盘的简单方法。它包含了修改过的 Wiki 脚本,以及一些小更改。本指南充实了这一过程,并在 Aimless Bits 的脚本里做了一些小更改。

这个快速指南假设你已为加载 Minecraft 设定了一个用户、Minecraft 目录和运行中的服务器。它还有助于熟悉 /etc/init.d/minecraft 启动脚本。

  • 第一步,在根目录里为虚拟硬盘创建一个目录。如“/home/username/minecraft_ramdisk”。
  • 要将其挂载为一个虚拟硬盘,只需要简单编辑你的 /etc/fstab/ 文件:
sudo nano /etc/fstab

然后添加这行,确保路径(用户名、目录名称等)是正确的。

tmpfs  /home/username/minecraft_ramdisk tmpfs  defaults,size=512m      0       0

虚拟硬盘的尺寸必须比 Minecraft 的世界目录还要大。确保你已经预留多了一些空间。

  • 重启你的计算机。虚拟硬盘现在会在你重启时加载,如果你希望立刻加载,输入
mount -t tmpfs none /home/username/minecraft_ramdisk -o size=512m

现在要做的是简单运行简单修改过的脚本,该脚本会加载硬盘上的文件到服务器里,将它们复制几次作为副本,以防止数据丢失。再次提醒,这个脚本是 Aimless Bits 脚本的修改版本。

如果你拥有 /etc/init.d/minecraft,删除它或者将其覆盖到这个脚本里。如果没有,新建一个文本文档,命名为“minecraft”,然后向它复制下列脚本内容。

#!/bin/bash
# /etc/init.d/minecraft
# version 0.6 2012-02-25 (YYYY-MM-DD)

### BEGIN INIT INFO
# Provides:   minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop:  $local_fs $remote_fs
# Should-Start:   $network
# Should-Stop:    $network
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:    Minecraft server
# Description:    Starts the minecraft server
### END INIT INFO

#Settings
JARFILE='craftbukkit-beta_1.4.6-R0.3.jar'
USERNAME="minecraft"
MCSTORE="/home/$USERNAME/minecraft"
MCPATH="/home/$USERNAME/minecraft_ramdisk"
CPU_COUNT=1
INVOCATION="java -Xmx2048M -Xms2048M -server -jar $JARFILE -o false"
BACKUPPATH="/home/$USERNAME/minecraft_backups/"
WORLD=Asgarde


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

mc_status() {
  ps aux |grep -F -v grep|grep -F -v SCREEN|grep -F --quiet $JARFILE
  return $?
}

mc_start() {
  if mc_status; then
    echo "Tried to start but $JARFILE was already running!"
  else
    echo "$JARFILE was not running... starting."
    if [ -d $MCSTORE/$WORLD.bak ]; then
      echo "last $WORLD.bak still exist, crashed warning! manual check required!!!"
      exit 1
    fi
    cd $MCPATH
    if [ ! -f "$MCPATH/$JARFILE" ]; then 
      echo "Ram drive empty...  prepping."
      as_user "cp -R $MCSTORE/* $MCPATH/"
    fi
    as_user "cd $MCPATH && screen -dmS minecraft $INVOCATION"
    sleep 7
    if mc_status; then
      echo "$JARFILE is now running."
    else
      echo "Could not start $JARFILE."
    fi
  fi
}

mc_saveoff() {
  if mc_status; then
    echo "$JARFILE is running... suspending saves"
    TO_SCREEN="screen -p 0 -S minecraft -X eval 'stuff "
    as_user "$TO_SCREEN \"say SERVER BACKUP STARTING. Server going readonly...\"\015'"
    as_user "$TO_SCREEN \"save-off\"\015'"
    as_user "$TO_SCREEN \"save-all\"\015'"
    sync
    sleep 10
  else
    echo "$JARFILE was not running. Not suspending saves."
  fi
}

mc_saveon() {
  if mc_status; then
    echo "$JARFILE is running... re-enabling saves"
    TO_SCREEN="screen -p 0 -S minecraft -X eval 'stuff "
    as_user "$TO_SCREEN \"save-on\"\015'"
    as_user "$TO_SCREEN \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
  else
    echo "$JARFILE was not running. Not resuming saves."
  fi
}

mc_stop() {
  if mc_status; then
    echo "$JARFILE is running... stopping."
    TO_SCREEN="screen -p 0 -S minecraft -X eval 'stuff "
    as_user "$TO_SCREEN \"say SERVER SHUTTING DOWN IN 5 SECONDS. Saving map...\"\015'"
    as_user "$TO_SCREEN \"save-all\"\015'"
    sleep 5
    as_user "$TO_SCREEN \"stop\"\015'"
    sleep 5
  else
    echo "$JARFILE was not running."
  fi

  if mc_status; then
    echo "$JARFILE could not be shut down... still running."
  else
    echo "$JARFILE is shut down."
  fi
}


mc_update() {
  if mc_status; then
    echo "$JARFILE 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\</a\> | 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/$JARFILE $MCPATH/minecraft_server.jar.update >/dev/null`
     then 
       echo "You are already running the latest version of $JARFILE."
     else
       as_user "mv $MCPATH/minecraft_server.jar.update $MCPATH/$JARFILE"
       echo "Minecraft successfully updated."
      fi
    else
      echo "Minecraft update could not be downloaded."
    fi
  fi
}

mc_backup() {
   echo "Backing up minecraft files"
   as_user "tar zcf $BACKUPPATH/MCBKUP_`date "+%Y.%m.%d-%H"`.tar.gz $MCSTORE"
   echo "Backup complete"
}

mc_disksaverun() {
  if mc_status; then
    echo "Saving ramdrive to disk."
    if [ ! -f $MCPATH/$JARFILE ]; then
      echo "Error.. Minecraft not in ram"
    else
      if [ -d $MCSTORE/$WORLD.bak ]; then
        echo "last $WORLD.bak still exist, crashed warning! manual check required!!!"
        exit 1
      fi
      if [ -d $MCSTORE/$WORLD ]; then
        as_user "mv $MCSTORE/$WORLD $MCSTORE/$WORLD.bak"
      fi

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

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

}

mc_disksavehalt() {
   echo "Saving ramdrive to disk."
   if [ ! -f $MCPATH/$JARFILE ]; then 
     echo "Error.. Minecraft not in ram"
   else
     if [ -d $MCSTORE/$WORLD.bak ]; then
        echo "last $WORLD.bak still exist, crashed warning! manual check required!!!"
        exit 1
     fi
     if [ -d $MCSTORE/$WORLD ]; then
       as_user "mv $MCSTORE/$WORLD $MCSTORE/$WORLD.bak"
     fi

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

     if [ -d $MCSTORE/$WORLD.bak ]; then
       as_user "rm -r $MCSTORE/$WORLD.bak"
     fi
   fi
}


#Start-Stop here
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 mc_status; then
      echo "$JARFILE is running."
    else
      echo "$JARFILE is not running."
    fi
    ;;
  *)
  echo "Usage: /etc/init.d/minecraft {start|stop|update|backup|status|restart|disksaverun}"
  exit 1
  ;;
esac
  • 把该脚本移动到你的 /etc/init.d/ directory,然后设置它为可执行:
mv /directory/wherefileis/filename /etc/init.d/minecraft
chmod a+x /etc/init.d/minecraft

注: 该脚本没有写上在该网站的其他 minecraft 初始化脚本的命令行选项! 因此重写了该脚本,加入了一些命令代码,因此虚拟硬盘服务器也可以使用命令之类去同步而不用使用其他插件来进行计划:http://pastebin.com/4ynwL2js 。如果需要命令行选项,可以使用这个。

你就快完成了!这个脚本表现的和原始的启动脚本差不多,只有一点不同的就是它把数据加载到虚拟硬盘里。你也可以修改脚本来使用 rsync 而不是 cp。

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

如果你想做其他东西,例如远程复制,但这对性能没什么影响,除非你的世界很大。

  • 请勿跳过这个步骤!你需呀添加一个 crontab 条目来保存你的世界。因为某些原因,你要接着往下看,但是如果你不完成这个步骤,那么你在虚拟硬盘里的数据就会丢失。该脚本有2个磁盘保存函数,disksavehalt 和 disksaverun。 Disksavehalt 指定 screen 会话正在关闭还是正在备份,不会关闭地图保存。不要在 crontab 里调用这个函数,而是使用 disksave。做这个:
sudo crontab -e

然后添加下面2行:

*/5 * * * * /etc/init.d/minecraft disksaverun
20 */6 * * * /etc/init.d/minecraft backup

数字代表每隔多少分钟就保存世界,如果你喜欢一个强健的安装、有力的供给备份以及工作,就减少这个数字。这个间隔最低只能为5分钟。

其他行运行 Minecraft 会每隔6小时备份一次,并且固定在:20时进行备份。不要跳过备份!这是最后一次警告!

但愿这能够对管理员有帮助,祝你好运!

GNU/Linux (alternative)

On most GNU/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

~/minecraft_server/
world/
minecraft_server.jar
server.log
server.properties
...

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)

#!/bin/sh

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

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

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/<your_username>/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
#!/bin/sh
#remake the paths
mkdir /dev/shm/minecraft
mkdir /dev/shm/minecraft/world

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

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

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 Ramdisk utility like Dataram RAMDisk (freeware version available) to create a RAM disk and place the server files on it. Dataram RAMDisk has the option to automatically save an image every time it shuts down and also every few minutes.

Before you begin:

  • At least 4G of RAM on your machine is ideal
  • Enabling "Save Disk Image on Shutdown" will impact your Windows shutdown times where you leave RAMDisk running on shutdown, and similarly startup times for "Load Disk Image on Startup". This is not a problem if you manually start and stop the server only when needed.
  • Search the Internet for some ways to save your RAMDisk in case it does not work.
  • FAT16 is generally faster than FAT32 on RAM disks, however FAT16 formatting is not available for partitions over 2048MB
  • REMEMBER: Always have a backup! If your computer crashes, any data on the RAM disk that has not been backed-up/copied to your hard drive will be lost!
  • Make sure that you allocate more than enough memory for the RAM disk than that of the size of your 'Minecraft Server' folder - remember that the Minecraft world data can increase by a lot!
  • ...and on the other hand, don't leave too little RAM remaining for the running of Windows and the server itself.

Setting up your RAM disk (Simple usage)

  1. Download and install Dataram RAMDisk
  2. Set your disk size (Setting the maximum is not recommended)
  3. If the disk size you set was 2048MB or less, choose 'FAT16 Partition', otherwise choose 'FAT32 Partition' (Advanced users may wish to select 'unformatted' and format the disk themselves)
  4. Go under the Load/Save tab and select all three RAMDisk saving methods ("AutoSave", "Save Disk Image on Shutdown" and "Load Disk Image on Startup")
  5. Start your RAMDisk - Click 'Start RAMDisk'
  6. Now go to 'My Computer' and you should see a new disk
  7. Open it and copy all your Minecraft Server files in it.
  8. Start your server per usual, now from the RAM disk you have just created - you are now up and running!

Your RAM disk will now automatically save upon shutdown, and will be restored (with data intact) on startup. Depending on the size you set, you will notice longer boot/shutdown times. Also, you may wish to adjust the AutoSave interval.

If you did not select "Save Disk Image on Shutdown", make sure continue reading especially!!

You need to follow these procedures every time you shutdown the computer to avoid data loss!

Stopping the RAM disk manually (Before shutting down computer)
  1. Stop your Minecraft server if it is running
  2. Open the Dataram RAMDisk configuration Utility (again)
  3. Please enable "Load Disk Image on Startup" if not already under the Load/save tab
  4. Click 'Save disk image now'
  5. Click 'Stop RAMDisk'
(When you wish to start the server next time, just start it like you did the first time. Only do this IF you ticked 'Load Disk Image at Startup')
Alternative:
  1. Stop your server
  2. Copy all the files in the RAMDisk to a backup folder in a hard disk
  3. Click 'Stop RAMDisk' on the Dataram RAMDisk configuration Utility
(To start it again, start your RAMDisk like you did the first time and copy all the server files into the RAMDisk, then start your server)

Mac OS X

Type this to 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.

Advertisement