linux环境下的mount命令到底有什么玄机

前言

linux 环境下的 mount 命令可以挂载一个文件系统,这个命令目前是我所知命令中最陌生的一个,至今没有使用过,真的一次都没有挂载过,平时用的最多是的 Windwos 系统,完全不需要挂载,安装新硬盘自动就会出现盘符,稍微复杂点就是通过磁盘管理器来分区,或者使用魔术分区助手搞一点复杂的分区或格式化操作。

而 linux 系统下,无论是申请的开发机,还是购买的服务器,硬盘都是挂载好的,至今没有遇到需要手动挂载的情况,所以对这个命令并不是很熟悉,今天决定抽时间学一下,总结一下设备相关的知识,特别是磁盘相关的操作。

文件类型

“Everything is a file”(一切皆文件)是 linux 中的特点,所以在学习磁盘、设备之前先来看看文件类型,在 linux 并不通过后缀名来判断文件类型的,可以利用 ll (或 ls -l)命令可以列举指定目录下的内容,通过每一行的首个字母就可以判断出文件的大类型:

  • -rw-r-r-:-开头的都普通文件
  • brw-r-r-:b开头的是块设备文件
  • crw-r-r-:c开头的是字符设备文件
  • drw-r-r-:d开头的是目录文件
  • lrw-r-r-:l开头的是软链接文件,也叫符号链接
  • prw-r-r-:p开头的是管道文件
  • srw-r-r-:s开头的是socket文件

我们可以查一下CentOS系统设备目录 dev,这里的文件类型非常丰富,由于内容非常多,这里只展示一部分内容:

[root@VM-0-3-centos ~]# ll /dev
total 0
crw------- 1 root root     10, 235 Feb  9 11:13 autofs
drwxr-xr-x 2 root root         120 Feb  9 11:13 block
drwxr-xr-x 2 root root          60 Feb  9 11:13 bsg
crw------- 1 root root     10, 234 Feb  9 11:13 btrfs-control
drwxr-xr-x 3 root root          60 Feb  9 11:13 bus
lrwxrwxrwx 1 root root           3 Feb  9 11:13 cdrom -> sr0
drwxr-xr-x 2 root root        2560 Feb  9 11:13 char
crw------- 1 root root      5,   1 Feb  9 11:13 console
lrwxrwxrwx 1 root root          11 Feb  9 11:13 core -> /proc/kcore
drwxr-xr-x 3 root root          60 Feb  9 11:13 cpu
crw------- 1 root root     10,  61 Feb  9 11:13 cpu_dma_latency
crw------- 1 root root     10,  62 Feb  9 11:13 crash
drwxr-xr-x 6 root root         120 Feb  9 11:13 disk
drwxr-xr-x 2 root root          60 Feb  9 11:13 dri
crw-rw---- 1 root video    29,   0 Feb  9 11:13 fb0
lrwxrwxrwx 1 root root          13 Feb  9 11:13 fd -> /proc/self/fd
crw-rw-rw- 1 root root      1,   7 Feb  9 11:13 full
crw-rw-rw- 1 root root     10, 229 Feb  9 11:13 fuse
crw------- 1 root root     10, 228 Feb  9 11:13 hpet
drwxr-xr-x 2 root root           0 Feb  9 11:13 hugepages
crw------- 1 root root     10, 183 Feb  9 11:13 hwrng
lrwxrwxrwx 1 root root          25 Feb  9 11:13 initctl -> /run/systemd/initctl/fifo
drwxr-xr-x 3 root root         240 Feb  9 11:13 input
crw-r--r-- 1 root root      1,  11 Feb  9 11:13 kmsg
srw-rw-rw- 1 root root           0 Feb  9 11:13 log

其实还有一个更精准的,专门用来查询文件类型的命令,那就是 file 命令,下面可以测试一下,执行后会输出关于文件类型的描述:

[root@VM-0-3-centos ~]# ll
total 12
-rwxr-xr-x 1 root root   67 Feb  9 13:44 connecttendis.sh
drwxr-xr-x 2 root root 4096 Feb  9 13:44 tarlist
drwxr-xr-x 4 root root 4096 Feb  9 13:40 tendis
[root@VM-0-3-centos ~]# file connecttendis.sh
connecttendis.sh: Bourne-Again shell script, ASCII text executable
[root@VM-0-3-centos ~]# file tendis/
tendis/: directory

描述展示说明 connecttendis.sh 是一个 shell 脚本,而 tendis/ 是一个目录。

文件系统

说完文件类型还得说说文件系统,什么是文件系统,其实从名字就可以看出来,文件系统就是管文件的呗,不同的文件系统所支持的最大容量、对单个文件的大小限制、存取性能、是否可压缩、是否提供校验等都是不太一样的,当我们做系统或者烧制U盘、格式化硬盘时常常提到文件系统这个词。

在 Windows 中常用的文件系统有 FAT16FAT32NTFSexFAT 等,而 Linux 中常用的文件系统有 ext2ext3ext4tmpfsproc 等,这些文件系统无需全部记住,在 Linux 上可以通过查看 /proc/filesystems 文件得知当前系统都支持哪些文件系统。

[root@VM-0-3-centos ~]# cat /proc/filesystems
nodev   sysfs
nodev   rootfs
nodev   ramfs
nodev   bdev
nodev   proc
nodev   cgroup
nodev   cpuset
nodev   tmpfs
nodev   devtmpfs
nodev   debugfs
nodev   securityfs
nodev   sockfs
nodev   dax
nodev   bpf
nodev   pipefs
nodev   configfs
nodev   devpts
nodev   hugetlbfs
nodev   autofs
nodev   pstore
nodev   mqueue
        ext3
        ext2
        ext4
        iso9660
nodev   binfmt_misc

观察上述结果,第一列为空的文件系统需要挂载到块设备上才能访问其中的内容,之后才可以正常使用。

挂载硬盘

Windows 中的目录是分盘符的,每个分区可以指定一个盘符,每个盘符就是这个分区的根,可以通过各个盘符依次向下访问其中的内容。在 Linux 中是没有盘符概念的,只有一个根目录 /,只存在一棵完整的目录树,硬盘设备需要挂载到这棵目录树上才能被正常使用。

我们知道 /dev 目录下存放着几乎所有的设备文件,从中就可以找到硬盘设备:

[root@VM-0-3-centos ~]# ll /dev/ | grep disk
drwxr-xr-x 6 root root         120 Feb  9 11:13 disk
brw-rw---- 1 root disk      7,   0 Feb  9 11:13 loop0
crw-rw---- 1 root disk     10, 237 Feb  9 11:13 loop-control
brw-rw---- 1 root disk    253,   0 Feb  9 11:13 vda
brw-rw---- 1 root disk    253,   1 Feb  9 11:13 vda1

从命令结果可以看出,这台计算机只有一块普通硬盘 vda,硬盘只有一个分区 vda1,这是目前一种通用的命名方式,hd 是指IDE接口的硬盘,sd 是指SATA接口的硬盘、vd 指 virtio 磁盘,现在的内核一般都会把硬盘,移动硬盘,U盘等识别为sdX的形式,第一块硬盘使用 a 作为后缀,例如 sda,后面的硬盘依次命名为 sdbsdc 等,硬盘的第一个分区后缀为1,后面分区号依次递增。

查硬盘分区

硬盘作为一种设备可以在 /dev 目录下查询,但是可以通过 fdisk 做更细致的查询

[root@VM-0-3-centos dev]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d64b4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

通过 fdisk -l 命令查询出的结果与 /dev 目录下查到的信息一致

查看当前挂载的文件系统

查看当前挂载的所有文件系统只需要一个 mount 命令就够了,也可以加 -t tmpfs 参数查看指定类型

[root@VM-0-3-centos ~]# mount
sysfs on /sys type sysfs (rw,relatime)
proc on /proc type proc (rw,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,size=930496k,nr_inodes=232624,mode=755)
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev)
devpts on /dev/pts type devpts (rw,relatime,gid=5,mode=620,ptmxmode=000)
tmpfs on /run type tmpfs (rw,nosuid,nodev,mode=755)
tmpfs on /sys/fs/cgroup type tmpfs (ro,nosuid,nodev,noexec,mode=755)
...
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,nosuid,nodev,noexec,relatime,freezer)
configfs on /sys/kernel/config type configfs (rw,relatime)
/dev/vda1 on / type ext4 (rw,noatime,data=ordered)
debugfs on /sys/kernel/debug type debugfs (rw,relatime)
hugetlbfs on /dev/hugepages type hugetlbfs (rw,relatime)
mqueue on /dev/mqueue type mqueue (rw,relatime)
tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,size=188204k,mode=700)
binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)

输出每一行格式一致,均包含6列内容,通过空格来分割,具体形式为:

<块设备或者其他文件系统> on <挂载目录> type <文件系统类型> <(与文件系统内容相关的选项)>

以 ‘/dev/vda1 on / type ext4 (rw,noatime,data=ordered)’ 这一行为例,就表示 /dev/vda 这个硬盘的第一个分区挂载到了一个 类型为 ext4 的文件系统上,挂载点是根目录 /,括号中的 rw 表示可读写,data=ordered 表示有序,relatime 是一种更新文件访问时间属性的一种方式,此外还有 noatimelazytime 类型,这一块内容也比较多,此处就不展开了,有兴趣可以转到 《文件系统中 atime,lazytime,relatime 详聊》 这篇文章中了解一下。

格式化磁盘分区

使用 mkfs 可以格式化一个磁盘分区,格式化的同时可以修改文件系统的类型

[root@VM-0-3-centos ~]# mkfs -t ext4 /dev/sda2

挂载新的磁盘分区

使用 mount 命令可以将新的磁盘分区挂载到目录树上

普通挂载

[root@VM-0-3-centos ~]# mkdir -p /mnt/data
[root@VM-0-3-centos ~]# mount /dev/sda2 /mnt/data

以只读方式挂载

[root@VM-0-3-centos ~]# mount -o ro /dev/sda2 /mnt/data

只读挂载改为读写模式

[root@VM-0-3-centos ~]# mount /mnt/data -o rw,remount

挂载光驱

挂载光驱的方式与挂载硬盘是一样的,只不过光驱是单独的设备,对应着不同的文件

[root@VM-0-3-centos ~]# mount /dev/cdrom /media/cdrom

挂载 windows 共享文件

挂载 windows 共享文件时需要用到 windows 计算机的IP、用户名和密码

[root@VM-0-3-centos ~]# mkdir -p /mnt/share
[root@VM-0-3-centos ~]# mount -t cifs -o username=admin,password=94741 //10.2.49.172/share /mnt/share

挂载 ios 文件

直接挂载 iso 文件就不需要光驱了,使用起来更加方便,先模拟创建一个 iso 文件

[root@VM-0-3-centos ~]# genisoimage -o test.iso tendis/
I: -input-charset not specified, using utf-8 (detected in locale settings)
Using TENDI000.;1 for  tendis/bin/tendisplus (tendisplus_static)
  2.77% done, estimate finish Sat Feb 27 23:03:08 2021
  5.53% done, estimate finish Sat Feb 27 23:03:08 2021
  8.30% done, estimate finish Sat Feb 27 23:03:08 2021
...
 49.78% done, estimate finish Sat Feb 27 23:03:10 2021
...
 99.55% done, estimate finish Sat Feb 27 23:03:11 2021
Total translation table size: 0
Total rockridge attributes bytes: 0
Total directory bytes: 6144
Path table size(bytes): 50
Max brk space used 1a000
180825 extents written (353 MB)

把 iso 直接挂载到目录就可以了,之后就可以通过 /mnt/iso 目录访问 test.iso 文件中的内容了

[root@VM-0-3-centos ~]# mkdir -p /mnt/iso
[root@VM-0-3-centos ~]# mount test.iso /mnt/iso
mount: /dev/loop0 is write-protected, mounting read-only

计算机设备中的概念

计算机科学中的概念有很多,常常记不清或者记混它们的内容,比如扇区、块、簇、页等,每次看完过后就忘记了,在此简单总结一下。

扇区

硬盘的读写以扇区为基本单位,属于物理层面的概念,操作系统是不直接与扇区交互的,可以通过 fdisk -l 查看扇区大小

[root@VM-0-3-centos dev]# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000d64b4

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048   104857566    52427759+  83  Linux

其中 Sector size,就表示扇区大小,从结果来看还分逻辑扇区和物理扇区,不过本例中为均为 512 bytes。

块、簇

块和簇其实是一种东西,通常 linux 系统叫做块,而 windows 系统叫做簇,它是文件系统读写数据的最小单位,每个磁盘块可以包括相邻的 2、4、8、16、32 或 64 个扇区,是操作系统所使用的逻辑概念,可以通过命令 stat /boot 来查看

[root@VM-0-3-centos mnt]# stat /boot
  File: ‘/boot’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd01h/64769d    Inode: 18          Links: 5
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-08 11:20:35.109000000 +0800
Modify: 2021-02-09 11:14:21.799238058 +0800
Change: 2021-02-09 11:14:21.799238058 +0800
 Birth: -

结果中 IO Block 就是磁盘块大小 4096 Bytes,也就是 4K,这样将相邻扇区合并为块来存取数据是为了更高效地、更好地管理磁盘空间。操作系统规定一个磁盘块中只能放置一个文件,这就会造成空间的浪费,那就意味着大多数情况会出现文件所占用的磁盘空间大于文件大小的情况。

这是一个内存相关的概念,是内存的最小存储单位。它大小通常为磁盘块大小的 2 n 2^n 2n 倍,可以通过命令 getconf PAGE_SIZE 来获取页的大小

[root@VM-0-3-centos mnt]# getconf PAGE_SIZE
4096

总结

  • mount 是 linux 系统中非常重要的一个命令,但是我感觉用的比较少
  • 系统新挂载的硬盘关机后会被卸载,可以将挂载信息添加到 /etc/fstab 文件实现开机后自动挂载
  • 可以把多个设备挂载到同一个目录,默认后面挂载的内容会隐藏前面挂载的内容,卸载后面的挂载,前面挂载的内容还会出现
  • 可以把同一个设备挂载到不同的目录,并在挂载时可以指定不同的权限,这样在不同的目录中看到的是同样的内容,但是权限不同
  • 扇区是硬盘的读写的基本单位,属于物理概念
  • 块/簇是操作系统读写数据的基本单位,属于逻辑概念
  • 页是内存的最小存储单位,通常为磁盘块大小的 2 n 2^n 2n

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

万里山河都踏过 天下又入谁手 分分合合 不过几十载春秋~

“你在教我做事啊?”

猜你喜欢

转载自blog.csdn.net/shihengzhen101/article/details/114199056
今日推荐