| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # ramdisk.sh |
| 3 | |
| 4 | # A "ramdisk" is a segment of system RAM memory |
| 5 | #+ which acts as if it were a filesystem. |
| 6 | # Its advantage is very fast access (read/write time). |
| 7 | # Disadvantages: volatility, loss of data on reboot or powerdown, |
| 8 | #+ less RAM available to system. |
| 9 | # |
| 10 | # Of what use is a ramdisk? |
| 11 | # Keeping a large dataset, such as a table or dictionary on ramdisk, |
| 12 | #+ speeds up data lookup, since memory access is much faster than disk access. |
| 13 | |
| 14 | |
| 15 | E_NON_ROOT_USER=70 # Must run as root. |
| 16 | ROOTUSER_NAME=root |
| 17 | |
| 18 | MOUNTPT=/mnt/ramdiskscip-shell filesystem . . `/mnt/ramdisk`. # Create with mkdir /mnt/ramdisk. |
| 19 | SIZE=2000 # 2K blocks (change as appropriate) |
| 20 | BLOCKSIZE=1024 # 1K (1024 byte) block size |
| 21 | DEVICE=/dev/ram0scip-shell filesystem . . `/dev/ram0`. # First ram device |
| 22 | |
| 23 | username=`idscip-shell system . . id. -nu` |
| 24 | if [scip-shell system . . `[`. "$username" != "$ROOTUSER_NAME" ] |
| 25 | then |
| 26 | echoscip-shell system . . echo. "Must be root to run \"`basenamescip-shell system . . basename. $0`\"." |
| 27 | exit $E_NON_ROOT_USER |
| 28 | fi |
| 29 | |
| 30 | if [scip-shell system . . `[`. ! -d "$MOUNTPT" ] # Test whether mount point already there, |
| 31 | then #+ so no error if this script is run |
| 32 | mkdirscip-shell system . . mkdir. $MOUNTPT #+ multiple times. |
| 33 | fi |
| 34 | |
| 35 | ############################################################################## |
| 36 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=$DEVICE count=$SIZE bs=$BLOCKSIZE # Zero out RAM device. |
| 37 | # Why is this necessary? |
| 38 | mke2fs $DEVICE # Create an ext2 filesystem on it. |
| 39 | mountscip-shell system . . mount. $DEVICE $MOUNTPT # Mount it. |
| 40 | chmodscip-shell system . . chmod. 777 $MOUNTPT # Enables ordinary user to access ramdisk. |
| 41 | # However, must be root to unmount it. |
| 42 | ############################################################################## |
| 43 | # Need to test whether above commands succeed. Could cause problems otherwise. |
| 44 | # Exercise: modify this script to make it safer. |
| 45 | |
| 46 | echoscip-shell system . . echo. "\"$MOUNTPT\" now available for use." |
| 47 | # The ramdisk is now accessible for storing files, even by an ordinary user. |
| 48 | |
| 49 | # Caution, the ramdisk is volatile, and its contents will disappear |
| 50 | #+ on reboot or power loss. |
| 51 | # Copy anything you want saved to a regular directory. |
| 52 | |
| 53 | # After reboot, run this script to again set up ramdisk. |
| 54 | # Remounting /mnt/ramdisk without the other steps will not work. |
| 55 | |
| 56 | # Suitably modified, this script can by invoked in /etc/rc.d/rc.local, |
| 57 | #+ to set up ramdisk automatically at bootup. |
| 58 | # That may be appropriate on, for example, a database server. |
| 59 | |
| 60 | exit 0 |