關于文件系統,我們在下面的博文中已有做簡單的介紹,外鏈網址已屏蔽
本篇博文我們學習的是文件系統中的tmpfs。
tmpfs是一種偽文件系統,它是從DRAM中創建出來的,
相比于磁盤而言,其具有更高的訪問效率。
如何創建一個tmpfs?
第一步先配置/etc/fstab,新增加一欄tmpfs的配置,
sh-3.2# cat /etc/fstab
# /etc/fstab: static file system information.
#
# ?? ? ?????????????????????? ?
/dev/root?????? /?????????????? auto??? noauto,rw,errors=remount-rw???? 0 0
none??????????? /proc?????????? proc??? defaults??????????????? 0 0
none??????????? /sys??????????? sysfs?? defaults??????????????? 0 0
none??????????? /dev/pts??????? devpts? noauto,gid=5,mode=620?? 0 0
none??????????? /tmp??????????? tmpfs?? defaults??????????????? 0 0
none??????????? /opt??????????? tmpfs?? defaults??????????????? 0 0
none??????????? /dev/shm??????? tmpfs?? noauto????????????????? 0 0
sh-3.2# mount
rootfs on / type rootfs (rw)
/dev/root on / type squashfs (ro,relatime)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
none on /tmp type tmpfs (rw,relatime)
none on /opt type tmpfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/mapper/dm-1 on /3rd type squashfs (ro,relatime)
ubi2:perm on /perm type ubifs (ro,relatime,bulk_read,no_chk_data_crc)
ubi1:3rd_rw on /3rd_rw type ubifs (rw,relatime,bulk_read,no_chk_data_crc)
第二步測試,為什么沒看到新增加的tmpfs呢?
sh-3.2# cat /etc/fstab
# /etc/fstab: static file system information.
#
# ?? ? ?????????????????????? ?
/dev/root?????? /?????????????? auto??? noauto,rw,errors=remount-rw???? 0 0
none??????????? /proc?????????? proc??? defaults??????????????? 0 0
none??????????? /sys??????????? sysfs?? defaults??????????????? 0 0
none??????????? /dev/pts??????? devpts? noauto,gid=5,mode=620?? 0 0
none??????????? /tmp??????????? tmpfs?? defaults??????????????? 0 0
none??????????? /opt??????????? tmpfs?? defaults??????????????? 0 0
none??????????? /dev/shm??????? tmpfs?? noauto????????????????? 0 0
none??? /tmp_fs???????? tmpfs?? defaults??????????????? 0 0
sh-3.2# mount
rootfs on / type rootfs (rw)
/dev/root on / type squashfs (ro,relatime)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
none on /tmp type tmpfs (rw,relatime)
none on /opt type tmpfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/mapper/dm-1 on /3rd type squashfs (ro,relatime)
ubi2:perm on /perm type ubifs (ro,relatime,bulk_read,no_chk_data_crc)
/dev/sda1 on /tmp/mnt/usb/sda1 type vfat (rw,noatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=utf8,shortname=mixed,errors=continue)
ubi1:3rd_rw on /3rd_rw type ubifs (rw,relatime,bulk_read,no_chk_data_crc)
通過檢查系統啟動時log發現,
mount: mount point /tmp_fs does not exist
原來是因為沒有mount point,所以導致tmpfs沒有掛載成功。
這個很容易理解,在mount一個文件系統之前必須指定一個目錄,然后將文件系統掛載到這個目錄下,
這個目錄就是mount point。
接下來我們在創建一個/tmp_fs的文件夾作為這個tmpfs的mount point,再次測試,
可以看到我們期待的文件系統/tmp_fs已經出來了。
sh-3.2# mount
rootfs on / type rootfs (rw)
/dev/root on / type squashfs (ro,relatime)
none on /proc type proc (rw,relatime)
none on /sys type sysfs (rw,relatime)
none on /tmp type tmpfs (rw,relatime)
none on /opt type tmpfs (rw,relatime)
none on /tmp_fs type tmpfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/mapper/dm-1 on /3rd type squashfs (ro,relatime)
ubi2:perm on /perm type ubifs (ro,relatime,bulk_read,no_chk_data_crc)
/dev/sda1 on /tmp/mnt/usb/sda1 type vfat (rw,noatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=utf8,shortname=mixed,errors=continue)
ubi1:3rd_rw on /3rd_rw type ubifs (rw,relatime,bulk_read,no_chk_data_crc)
sh-3.2# touch /tmp_fs/test
sh-3.2#
sh-3.2# ls /tmp_fs
test
上面介紹的是靜態配置文件系統的方法,我們也可以在系統啟動后動態的創建一個tmpfs類型的文件系統。
只要找到一個mount point,然后向tmpfs掛載到這個mount point上即可。
sh-3.2# mount -t tmpfs -o size=2m tmpfs /tmp_fs_2/
sh-3.2# touch /tmp_fs_2/test
sh-3.2# ls /tmp_fs_2/
test
sh-3.2# umount /tmp_fs_2/
sh-3.2# ls /tmp_fs_2/
如何umount一個文件系統?
可以直接使用umount命令來卸載一個文件系統。
sh-3.2# umount /tmp
umount: /tmp: device is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
但是有時候會出現文件系統無法卸載的情況。
初步分析可能是因為該文件系統中存在打開著的文件沒有被關閉造成的。
此時我們可以借助于lsof命令進行確認。
sh-3.2# ./lsof /tmp/
通過確認,果然有很多打開著的文件。
所以如果想要umount這個文件系統,必須將該文件系統中已打開的文件都關閉掉。