linux文件系统之tmpfs学习

本文详细介绍了tmpfs文件系统的工作原理、创建方法、动态与静态配置方式、如何查看使用状况以及如何umount文件系统。通过实例演示了在系统启动时创建tmpfs文件系统的过程,并解释了常见问题及解决办法。

关于文件系统,我们在下面的博文中已有做简单的介绍,
http://blog.csdn.net/boyxulin1986/article/details/12107113

本篇博文我们学习的是文件系统中的tmpfs。
tmpfs是一种伪文件系统,它是从DRAM中创建出来的,
相比于磁盘而言,其具有更高的访问效率。

如何创建一个tmpfs?
第一步先配置/etc/fstab,新增加一栏tmpfs的配置,
sh-# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>                       <dump>  <pass>
/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-# 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)

第二步测试,为什么没看到新增加的tmpfs呢?
sh-# cat /etc/fstab
# /etc/fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>                       <dump>  <pass>
/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-# 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/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)

通过检查系统启动时log发现,
mount: mount point /tmp_fs does not exist
原来是因为没有mount point,所以导致tmpfs没有挂载成功。
这个很容易理解,在mount一个文件系统之前必须指定一个目录,然后将文件系统挂载到这个目录下,
这个目录就是mount point。

接下来我们在创建一个/tmp_fs的文件夹作为这个tmpfs的mount point,再次测试,
可以看到我们期待的文件系统/tmp_fs已经出来了。
sh-# 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/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)

sh-# touch /tmp_fs/test
sh-#
sh-# ls /tmp_fs
test

上面介绍的是静态配置文件系统的方法,我们也可以在系统启动后动态的创建一个tmpfs类型的文件系统。
只要找到一个mount point,然后向tmpfs挂载到这个mount point上即可。
sh-# mount -t tmpfs -o size=2m tmpfs /tmp_fs_2/
sh-# touch /tmp_fs_2/test
sh-# ls /tmp_fs_2/
test
sh-# umount /tmp_fs_2/
sh-# ls /tmp_fs_2/

如何umount一个文件系统?
可以直接使用umount命令来卸载一个文件系统。
sh-# 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-# ./lsof /tmp/

通过确认,果然有很多打开着的文件。
所以如果想要umount这个文件系统,必须将该文件系统中已打开的文件都关闭掉。

如何查看文件系统的使用状况?
可以使用df命令来查看文件系统的状况,
sh-# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                41.0M     41.0M         0 100% /
none                    144.0M    384.0K    143.6M   0% /tmp
none                    144.0M         0    144.0M   0% /opt
none                    144.0M      4.0K    144.0M   0% /var/run
none                    144.0M         0    144.0M   0% /tmp_fs
/dev/sda1                 3.7G    771.3M      3.0G  20% /tmp/mnt/usb/sda1
tmpfs                     2.0M         0      2.0M   0% /tmp_fs_2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值