Step 0: 簡介
Step 1: 系統預設的 RAM DISK
Step 2: 修改 ramdisk 大小
Step 3: Format the ramdisk
Step 4: mount RAM Disk
Step 5: 使用 ramdisk
Step 0: 簡介
RAM Disk 就是將系統上一塊 RAM 把它當成是 Disk 來存取.相較於一般的 Disk 他的資料在下次開機後就不能保存了.
Step 1: 系統預設的 RAM DISK
系統在一開始就已經存在 16 個 RAM DISK, 從 ram0 – ram15 大小為 16M,一開始他並不占用任何系統上 RAM 的空間空間,直到我們掛載他為止.
[root @benjr ~]# ll /dev/ram* lrwxrwxrwx 1 root root 4 Jun 12 00:31 /dev/ram -> ram1 brw-rw—- 1 root disk 1, 0 Jan 30 2003 /dev/ram0 brw-rw—- 1 root disk 1, 1 Jan 30 2003 /dev/ram1 brw-rw—- 1 root disk 1, 10 Jan 30 2003 /dev/ram10 brw-rw—- 1 root disk 1, 11 Jan 30 2003 /dev/ram11 brw-rw—- 1 root disk 1, 12 Jan 30 2003 /dev/ram12 brw-rw—- 1 root disk 1, 13 Jan 30 2003 /dev/ram13 brw-rw—- 1 root disk 1, 14 Jan 30 2003 /dev/ram14 brw-rw—- 1 root disk 1, 15 Jan 30 2003 /dev/ram15 brw-rw—- 1 root disk 1, 16 Jan 30 2003 /dev/ram16 brw-rw—- 1 root disk 1, 17 Jan 30 2003 /dev/ram17 brw-rw—- 1 root disk 1, 18 Jan 30 2003 /dev/ram18 brw-rw—- 1 root disk 1, 19 Jan 30 2003 /dev/ram19 brw-rw—- 1 root disk 1, 2 Jan 30 2003 /dev/ram2 brw-rw—- 1 root disk 1, 3 Jan 30 2003 /dev/ram3 brw-rw—- 1 root disk 1, 4 Jan 30 2003 /dev/ram4 brw-rw—- 1 root disk 1, 5 Jan 30 2003 /dev/ram5 brw-rw—- 1 root disk 1, 6 Jan 30 2003 /dev/ram6 brw-rw—- 1 root disk 1, 7 Jan 30 2003 /dev/ram7 brw-rw—- 1 root disk 1, 8 Jan 30 2003 /dev/ram8 brw-rw—- 1 root disk 1, 9 Jan 30 2003 /dev/ram9 lrwxrwxrwx 1 root root 4 Jun 12 00:31 /dev/ramdisk -> ram0 [root@benjr ~]# dmesg | grep -i ramdisk RAMDISK driver initialized: 16 RAM disks of 16000K size 1024 blocksize
如果我們需要更大的空間此時才要修改一下大小的參數.
Step 2: 修改 ramdisk 大小
要修改 ramdisk 大小的方法很簡單,只要傳送我們想要的大小參數給 kernel(ramdisk_size=xxxxx) 就可以了,我們也可以直接修改 grub.conf 在每次開機都可以生效.
[root @benjr ~]# vi /etc/grub.conf # grub.conf generated by anaconda # # Note that you do not have to rerun grub after making changes to this file # NOTICE: You have a /boot partition. This means that # all kernel and initrd paths are relative to /boot/, eg. # root (hd0,0) # kernel /vmlinuz-version ro root=/dev/hda5 # initrd /initrd-version.img #boot=/dev/hda default=0 timeout=10 splashimage=(hd0,0)/grub/splash.xpm.gz title Red Hat Linux (2.4.20-20.9) root (hd0,0) kernel /vmlinuz-2.4.20-20.9 ro root=LABEL=/ hdc=ide-scsiramdisk_size=32000 initrd /initrd-2.4.20-20.9.img
重新開機就會生效了.
[root@benjr ~]# dmesg | grep -i ramdisk RAMDISK driver initialized: 16 RAM disks of 32000K size 1024 blocksize RAMDISK:
Step 3: Format the ramdisk
因為是 RAM Disk 的關係所以不需要 Format 成 ext3(Journaling file system)我們只需 ext2 的格式就好.選一個你需要的 ram0 ~ ram15,我這裡使用的是 /dev/ram0
[root@benjr ~]# mkfs.ext2 /dev/ram0 mke2fs 1.32 (09-Nov-2002) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) 4000 inodes, …….略…………….. Writing inode tables: done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 22 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override.
Step 4: mount RAM Disk
[root@benjr ~]# mkdir /mnt/ramdisk [root@benjr ~]# mount /dev/ram0 /mnt/ramdisk [root@benjr ~]# df -h | grep ram0 /dev/ram0 31M 386K 29M 2% /mnt/ramdisk
我們一樣可以用 tune2fs 來看一些詳細的資料.
[root@benjr ~]# tune2fs -l /dev/ram0 tune2fs 1.32 (09-Nov-2002) Filesystem volume name: none Last mounted on: not available Filesystem UUID: fbb80e9a-8e7c-4bd4-b3d9-37c29813a5f5 Filesystem magic …….略……………..
Step 5: 使用 ramdisk
接下來我們就可以像是用硬碟一樣使用這個空間.不需要時直接 umount 就可以了.
[root@benjr ~]# umount -v /mnt/rd /dev/ram0
RAM Disk 的資料會一直保留起來,直到你下次開機前系統都不會動到這一塊的空間.參考資料
http://www.linuxfocus.org/English/November1999/article124.htmlhttp://www.vanemery.com/Linux/Ramdisk/ramdisk.html
One thought on “Linux – Ramdisk”