linux文件系统学习,linux文件系统之tmpfs学习

关于文件系统,我们在下面的博文中已有做简单的介绍,外链网址已屏蔽

本篇博文我们学习的是文件系统中的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这个文件系统,必须将该文件系统中已打开的文件都关闭掉。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值