文章目錄
- 前言
- 檔案型別
- 檔案系統
- 掛載硬碟
- 查硬碟磁區
- 查看當前掛載的檔案系統
- 格式化磁盤磁區
- 掛載新的磁盤磁區
- 普通掛載
- 以只讀方式掛載
- 只讀掛載改為讀寫模式
- 掛載光驅
- 掛載 windows 共享檔案
- 掛載 ios 檔案
- 計算機設備中的概念
- 扇區
- 塊、簇
- 頁
- 總結
前言
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 中常用的檔案系統有 FAT16、FAT32、NTFS、exFAT 等,而 Linux 中常用的檔案系統有 ext2、ext3、ext4、tmpfs、proc 等,這些檔案系統無需全部記住,在 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,后面的硬碟依次命名為 sdb、sdc 等,硬碟的第一個磁區后綴為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 是一種更新檔案訪問時間屬性的一種方式,此外還有 noatime 和 lazytime 型別,這一塊內容也比較多,此處就不展開了,有興趣可以轉到 《檔案系統中 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 倍
萬里山河都踏過 天下又入誰手 分分合合 不過幾十載春秋~
“你在教我做事啊?”
CSDN認證博客專家
Python
C/C++
分布式
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/264767.html
標籤:其他
