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文件系统?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值