当前位置: 首页 > news >正文

linux mount命令学习

我们在下面这篇博文中已经有笼统的学习了文件系统的一些相关知识,
http://blog.csdn.net/boyxulin1986/article/details/12107113

本篇我们主要是用来学习如何去挂载一个磁盘文件系统,以U盘为例进行说明和分析。

1. 先查看下mount U盘之前系统上已经挂载了哪些文件系统,
sh-# cat /proc/mounts
rootfs / rootfs rw 0 0
/dev/root / squashfs ro,relatime 0 0
none /proc proc rw,relatime 0 0
none /sys sysfs rw,relatime 0 0
none /tmp tmpfs rw,relatime 0 0
none /opt tmpfs rw,relatime 0 0
none /tmp_fs tmpfs rw,relatime 0 0
none /proc/bus/usb usbfs rw,relatime 0 0

2. 接下来我们要知道挂载的U盘名是什么,这只U盘是什么类型的文件系统,
sh-# fdisk -l

Disk /dev/sda: 4002 MB, 4002910208 bytes
32 heads, 63 sectors/track, 3878 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 3878 3908992+ b Win95 FAT32

3. 查看当前系统中支持哪些类型的文件系统,
sh-# cat /proc/filesystems
nodev sysfs
nodev rootfs
nodev bdev
nodev proc
nodev tmpfs
nodev debugfs
nodev sockfs
nodev usbfs
nodev pipefs
nodev anon_inodefs
nodev devpts
ext2
nodev ramfs
vfat
nodev mqueue
nodev mtd_inodefs
nodev ubifs
ntfs

4. 挂载一个文件系统必须有一个mount point,所以这里暂时将这只U盘挂载到/tmp_fs这个目录下。
sh-# touch /tmp_fs/test.txt
sh-# ls -l /tmp_fs/test.txt
-rw-r--r-- 1 root root 0 Jan 1 00:07 /tmp_fs/test.txt

5. 真正要mount这个U盘了,
sh-# mount -t vfat /dev/sda1 /tmp_fs

6. 检查是否有挂载成功,
sh-# cat /proc/mounts
rootfs / rootfs rw 0 0
/dev/root / squashfs ro,relatime 0 0
none /proc proc rw,relatime 0 0
none /sys sysfs rw,relatime 0 0
none /tmp tmpfs rw,relatime 0 0
none /opt tmpfs rw,relatime 0 0
none /tmp_fs tmpfs rw,relatime 0 0
none /proc/bus/usb usbfs rw,relatime 0 0
/dev/sda1 /tmp_fs vfat rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro 0 0
接下来我们再访问看看啦,
sh-# ls /tmp_fs/
TF1014VIZUSMTKO0-420.VOB gdb
tcpdump lsof
真的可以访问U盘里面的文件耶,说明mount成功了。

7. umount这个文件系统,可以看到原先的文件系统/tmp_fs又恢复出来了,
sh-# umount /mnt/usb/sda1/
sh-# ls /tmp_fs
test.txt
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)

以上说明的是如何mount一个FAT32类型文件系统的U盘。
接下来,我们要将U盘format成NTFS类型的文件系统,再试试看能不能正常挂载。
mount失败了,要如何才能mount成功呢?
sh-# busybox fdisk -l

Disk /dev/sda: 4002 MB, 4002910208 bytes
32 heads, 63 sectors/track, 3878 cylinders
Units = cylinders of 2016 * 512 = 1032192 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 3878 3908992+ 7 HPFS/NTFS

sh-# mount -t ntfs /dev/sda1 /tmp_fs/
mount: wrong fs type, bad option, bad superblock on /dev/sda1,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so


有时候需要将文件系统从ro改成rw或者从rw更改成ro,我们以U盘进行实验。

1. 通过mount命令查看/dev/sda1为rw的文件系统,
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 /var/run 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)

2. 测试下是否可以写入/dev/sda1,
sh-# touch /tmp/mnt/usb/sda1/test.txt
sh-# ls -l /tmp/mnt/usb/sda1/test.txt
-rwxrwxrwx 1 root root 0 Jan 1 00:13 /tmp/mnt/usb/sda1/test.txt

3. 将/dev/sda1这个device重新挂载成ro的文件系统,
sh-# mount -o remount,ro /tmp/mnt/usb/sda1/

4. 再次使用mount命令进行查看,你会发现/dev/sda1已经变成ro了。
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 /var/run type tmpfs (rw,relatime)
none on /proc/bus/usb type usbfs (rw,relatime)
/dev/sda1 on /tmp/mnt/usb/sda1 type vfat (ro,noatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=cp437,iocharset=utf8,shortname=mixed,errors=continue)

5. 通过实验来验证一下是不是真的有效,
sh-# touch /tmp/mnt/usb/sda1/test.txt
touch: cannot touch `/tmp/mnt/usb/sda1/test.txt': Read-only file system

当然如果你的文件系统在忙比如有打开着的文件,那么remount就会失败。
sh-# mount -o remount,ro /tmp/
mount: /tmp is busy
sh-# echo $?
32

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))

如果设备处于busy状态,而你却执意要umount这个设备。
可以使用lsof定位哪些进程打开了文件,然后再将这个进程杀死,
再去umount应该就能成功卸载这个设备了。

待确认问题:
linux mount ntfs文件系统?

相关文章:

  • TCP头分析+面试题
  • Maven--多模块依赖实例解析(五)
  • Python解决codeforces ---- 1
  • HDU 2493 Timer 数学(二分+积分)
  • linux printk函数学习
  • HDU 3262 Seat taking up is tough (模拟搜索)
  • 2014各大网络公司校招笔试算法题(收集并更新中)
  • erlang mnesia 数据库查询
  • HDU 3264 Open-air shopping malls (计算几何-圆相交面积)
  • 2014Microsoft 校招笔试真题(找工作的虾米们赶紧做题晒答案喽)
  • 黑马程序员_IO流基本操作(Writer,Reader)
  • aptana 插件离线下载方式
  • Eclipse安装aptana 插件的方法
  • VC写的双人版俄罗斯方块
  • 2014百度校招笔试题之动态链接库静态链接库详解
  • Git学习与使用心得(1)—— 初始化
  • Java编程基础24——递归练习
  • jdbc就是这么简单
  • jquery cookie
  • leetcode讲解--894. All Possible Full Binary Trees
  • oschina
  • PyCharm搭建GO开发环境(GO语言学习第1课)
  • UMLCHINA 首席专家潘加宇鼎力推荐
  • Zepto.js源码学习之二
  • 程序员该如何有效的找工作?
  • 回顾 Swift 多平台移植进度 #2
  • 蓝海存储开关机注意事项总结
  • 前端自动化解决方案
  • 悄悄地说一个bug
  • 我感觉这是史上最牛的防sql注入方法类
  • 小程序 setData 学问多
  • 鱼骨图 - 如何绘制?
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • #define,static,const,三种常量的区别
  • #NOIP 2014# day.2 T2 寻找道路
  • #微信小程序:微信小程序常见的配置传旨
  • (10)STL算法之搜索(二) 二分查找
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (二)springcloud实战之config配置中心
  • (剑指Offer)面试题34:丑数
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (三)终结任务
  • (十一)图像的罗伯特梯度锐化
  • (循环依赖问题)学习spring的第九天
  • ..回顾17,展望18
  • .NET : 在VS2008中计算代码度量值
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET Core跨平台微服务学习资源
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .net 无限分类
  • .NetCore 如何动态路由
  • .NET国产化改造探索(三)、银河麒麟安装.NET 8环境
  • .py文件应该怎样打开?
  • @Autowired 与@Resource的区别