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

#!/bin/bash

# ramdisk.sh

# A "ramdisk" is a segment of system RAM memory

#+ that acts as if it were a filesystem.

# Its advantage is very fast access (read/write time).

# Disadvantages: volatility, loss of data on reboot or

powerdown.

# less RAM available to system.

#

# What good is a ramdisk?

# Keeping a large dataset, such as a table or dictionary on

ramdisk

#+ speeds up data lookup, since memory access is much faster than

disk access.

E_NON_ROOT_USER=70 # Must run as root.

ROOTUSER_NAME=root

MOUNTPT=/mnt/ramdisk

SIZE=2000 # 2K blocks (change as appropriate)

BLOCKSIZE=1024 # 1K (1024 byte) block size

DEVICE=/dev/ram0 # First ram device

username=`id -nu`

if [ "$username" != "$ROOTUSER_NAME" ]

then

echo "Must be root to run \"`basename

$0`\"."

exit $E_NON_ROOT_USER

fi

if [ ! -d "$MOUNTPT" ] # Test whether mount point already

there,

then #+ so no error if this script is run

mkdir $MOUNTPT #+ multiple times.

fi

dd if=/dev/zero of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM

device.

mke2fs $DEVICE # Create an ext2 filesystem on it.

mount $DEVICE $MOUNTPT # Mount it.

chmod 777 $MOUNTPT # So ordinary user can access ramdisk.

# However, must be root to unmount it.

echo "\"$MOUNTPT\" now available for use."

# The ramdisk is now accessible for storing files, even by an

ordinary user.

# Caution, the ramdisk is volatile, and its contents will

disappear

#+ on reboot or power loss.

# Copy anything you want saved to a regular directory.

# After reboot, run this script again to set up ramdisk.

# Remounting /mnt/ramdisk without the other steps will not

work.

exit 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值