| 论坛注册| 加入收藏 | 设为首页| RSS
Google
您当前的位置:首页 > Linux频道 > Linux开发区 > SHELL

linux下创建虚拟磁盘的shell脚本

时间:2007-02-20 11:50:43  来源:Linux联盟收集整理  作者:
#!/bin/bashGaYLinux联盟
# ramdisk.shGaYLinux联盟
# A "ramdisk" is a segment of system RAM memoryGaYLinux联盟
#+ that acts as if it were a filesystem.GaYLinux联盟
# Its advantage is very fast access (read/write time).GaYLinux联盟
# Disadvantages: volatility, loss of data on reboot or powerdown.GaYLinux联盟
# less RAM available to system.GaYLinux联盟
#GaYLinux联盟
# What good is a ramdisk?GaYLinux联盟
# Keeping a large dataset, such as a table or dictionary on ramdiskGaYLinux联盟
#+ speeds up data lookup, since memory access is much faster than disk access.
E_NON_ROOT_USER=70 # Must run as root.GaYLinux联盟
ROOTUSER_NAME=rootGaYLinux联盟
MOUNTPT=/mnt/ramdiskGaYLinux联盟
SIZE=2000 # 2K blocks (change as appropriate)GaYLinux联盟
BLOCKSIZE=1024 # 1K (1024 byte) block sizeGaYLinux联盟
DEVICE=/dev/ram0 # First ram deviceGaYLinux联盟
username=`id -nu`
if [ "$username" != "$ROOTUSER_NAME" ]GaYLinux联盟
thenGaYLinux联盟
  echo "Must be root to run \"`basename $0`\"."GaYLinux联盟
  exit $E_NON_ROOT_USERGaYLinux联盟
fi
if [ ! -d "$MOUNTPT" ] # Test whether mount point already there,GaYLinux联盟
then #+ so no error if this script is runGaYLinux联盟
  mkdir $MOUNTPT #+ multiple times.GaYLinux联盟
fi
dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM device.GaYLinux联盟
mke2fs $DEVICE # Create an ext2 filesystem on it.GaYLinux联盟
mount $DEVICE $MOUNTPT # Mount it.GaYLinux联盟
chmod 777 $MOUNTPT # So ordinary user can access ramdisk.
# However, must be root to unmount it.GaYLinux联盟
echo "\"$MOUNTPT\" now available for use."GaYLinux联盟
# The ramdisk is now accessible for storing files, even by an ordinary user.GaYLinux联盟
# Caution, the ramdisk is volatile, and its contents will disappearGaYLinux联盟
#+ on reboot or power loss.GaYLinux联盟
# Copy anything you want saved to a regular directory.GaYLinux联盟
# After reboot, run this script again to set up ramdisk.GaYLinux联盟
# Remounting /mnt/ramdisk without the other steps will not work.GaYLinux联盟
exit 0
来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
相关文章
栏目更新
栏目热门