Linux基礎之磁盤管理 —— 只講乾貨,不吹牛!


這裏是一段防爬蟲文本,請讀者忽略。
本文原創首發於CSDN,作者IDYS
博客首頁:https://blog.csdn.net/weixin_41633902/
本文鏈接:https://blog.csdn.net/weixin_41633902/article/details/105784375


- 寫在開頭的話

  • 請記住:實踐是掌握知識的最快方法
  • 如果你只是懷着看看的態度去快速瀏覽文章,而不去認認真真的把文章裏面講的任何一個知識點去實踐一遍,那麼你永遠也掌握不了它
  • 生命不息,折騰不止!

- Linux磁盤管理

- Linux的設備

  • 塊設備:block,存取單位“塊”,磁盤

  • 字符設備,char,存取單位“字符“,鍵盤

  • 設備文件,關聯至一個設備驅動程序,進而能夠跟與之對應硬件設備進行通信

  • 設備號碼:

    • 主設備號:major number,標識設備類型
    • 次設備號:minor number,標識同一類型下的不同設備
當我們查看字符設備時,左數第5列裏面,左邊爲主設備號,右邊爲次設備號
[root@dayuanshuai ~]# ls -l /dev | head -5
總用量 0              (主設備號)  (次設備號)
crw-rw----. 1 root video    10, 175 9月  19 2019 agpgart
drwxr-xr-x. 2 root root         620 9月  19 2019 block
drwxr-xr-x. 2 root root          80 9月  19 2019 bsg
crw-------. 1 root root     10, 234 9月  19 2019 btrfs-control

- 硬盤接口類型

  • 並行(口) (接口速率)

    • ide:133MB/s
    • SCISI:640MB/s
  • 串行(接口速率)

    • SATA:6Gbps
    • SAS:6Gbps
    • USB:480MB/s
  • /dev/DEV_FILE

    • 磁盤設備的設備文件命名
  • IDE:/dev/hd

  • SCSI,SATA,SAS,USB:/dev/sd

    • 不同設備:a-z
      • /dev/sda,/dev/sdb
    • 同一設備上的不同分區:1,2,…
      • /dev/sda1,/dev/sda5

- 機械式硬盤:

  • track:磁道(同心圓)
  • cylinder:柱面
  • secotr:扇區
    • 521bytes
  • rpm:rotations per minute(每分鐘多少轉)
  • 如何分區:
    • 按照柱面分區
  • 0磁道0扇區:512bytes
    • MBR:Master Boot Record
      • 446bytes:boot loader
      • 64bytes:分區表
        • 16bytes:標識一個分區
      • 2bytes:55AA
      • 4個主分區:3個主分區+1擴展(N個邏輯分區)
        • 邏輯分區
    • MBR機制最大卷支持爲2TB,每個磁盤最多4個主分區
  • GPT能使用大於2T的硬盤而MBR不行,支持最大卷爲18EB
  • GPT可以支持無限個分區,而MBR最多支持4個主分區,超過只能通過邏輯分區
  • UEFI+GPT支持Secure BOOT

- 分區管理工具

  • fdisk: 對於一塊硬盤來講,最多隻能管理15個分區
  • fdisk -l查看硬盤分區信息
    • start 起始柱面
    • end 結束柱面
    • blocked 磁盤塊數
    • id 分區標識
    • system 應用在那個系統上的分區
[root@dayuanshuai ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00036416
		(Boot代表是否可以引導,即分區是否有內核)
   Device Boot      Start         End      Blocks   Id  System     (*表示是)
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         418     3145728   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             418        3917    28105728   83  Linux
  • fdisk DEVICE
    • 爲了實驗這個命令,我特意虛擬機上加了一塊新的硬盤
    • 但是當你插上新的硬盤時,主機是不會檢測到新的硬盤的,那麼如何讓主機檢測到你新插入的硬盤呢?

方法一:(簡單粗暴且無腦)

  • 直接重啓

方法二

  • 執行echo “- - -” > /sys/class/scsi_host/host0/scan 命令,然後fdisk -l查看是否檢測到,如果此時還沒有檢測到新插入的硬盤,那麼把前面host0依次改爲host1、host2,然後fdisk -l,以下是我的執行過程
[root@dayuanshuai ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
# 此時可以看到Linux並沒有成功檢測到新硬盤
[root@dayuanshuai ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00036416

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         418     3145728   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             418        3917    28105728   83  Linux
# 然後我依次改爲host1、host2,然後檢測成功
[root@dayuanshuai ~]# echo "- - -" > /sys/class/scsi_host/host1/scan  
[root@dayuanshuai ~]# echo "- - -" > /sys/class/scsi_host/host2/scan  
# 此時再來查看可以發現新的硬盤sdb檢測成功
[root@dayuanshuai ~]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00036416

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26         418     3145728   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             418        3917    28105728   83  Linux

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
  • 執行fdisk DEVICE後的菜單選項
    • m:顯示菜單和幫助信息
    • a:活動分區標記/引導分區
    • d:刪除分區
    • l:顯示分區類型(對應可以找到前面分區表信息ID所對應的文件系統類型)
    • n:新建分區
    • p:顯示分區信息
    • q:退出不保存
    • t:設置分區號碼(設置前面分區ID號碼)
    • v:進行分區檢查
    • w:保存修改
    • x:擴展應用,高級功能
# 以下是我的過程
[root@dayuanshuai ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xd406d040.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
         
# 執行p打印分區信息,因爲此時沒有分區,所以沒有顯示
Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd406d040

   Device Boot      Start         End      Blocks   Id  System
   
#執行n新建分區

Command (m for help): n

# 選擇分區類型:e代表擴展分區,p代表主分區
Command action
   e   extended
   p   primary partition (1-4)
p

# 選擇分區號碼
Partition number (1-4): 1  

# 選擇起始柱面,敲回車默認即可
First cylinder (1-261, default 1): 
Using default value 1

# 選擇結束柱面,此時你可以選擇分區大小和結束柱面兩種選項,我選擇 +10M 標識分區大小10M
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +10M  

# 打印分區
Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd406d040

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           2       16033+  83  Linux

# 設置分區的ID號碼(即是設置文件系統類型id號)
Command (m for help): t
# 選擇那個分區進行設置
Partition number (1-4): 2
# 更改爲什麼id號
Hex code (type L to list codes): a5
Changed system type of partition 2 to a5 (FreeBSD)

# 查看內核是否已經識別新的分區
[root@dayuanshuai ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0   31457280 sda
   8        1     204800 sda1
   8        2    3145728 sda2
   8        3   28105728 sda3
   # 此時可以看到新加入的磁盤已經被內核識別
   8       16    2097152 sdb
   8       17      16033 sdb1
   8       18      16065 sdb2
  • 查看內核是否已經識別新的分區

    • cat /proc/partitions
  • 通知內核重新讀取硬盤分區表

    • partx -a /dev/DEVICE
      • -n M:N
    • kpartx -a /dev/DEVICE
      • -f:force
    • CentOS 5:
      • partprobe [/dev/DEVICE]
  • 格式化分區,劃分文件系統

    • mkfs.ext4 /dev/DEVICE[NUM]
    • mkfs -t ext4 /dev/DEVICE[NUM]
  • 掛載分區

    • mount /dev/DEVICE[NUM] /mnt/tmp
# 格式化分區,劃分文件系統

[root@dayuanshuai ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系統標籤=
操作系統:Linux
塊大小=1024 (log=0)
分塊大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
4016 inodes, 16032 blocks
801 blocks (5.00%) reserved for the super user
第一個數據塊=1
Maximum filesystem blocks=16515072
2 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
        8193

正在寫入inode表: 完成                            
Creating journal (1024 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

# 分區掛載
[root@dayuanshuai ~]# mount /dev/sdb1 /mnt/tmp

# 查看磁盤使用情況
[root@dayuanshuai ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        27G  1.3G   24G   5% /
tmpfs           931M     0  931M   0% /dev/shm
/dev/sda1       190M   36M  145M  20% /boot
/dev/sdb1        15M  138K   14M   2% /mnt/tmp


- 寫在最後的話:

  • 無論每個知識點的難易程度如何,我都會盡力將它描繪得足夠細緻
  • 歡迎關注我的CSDN博客,IDYS’BLOG
  • 持續更新內容運維 | 網工 | 軟件技巧
  • 如果你有什麼疑問,或者是難題。歡迎評論或者私信我。你若留言,我必回覆!
  • 雖然我現在還很渺小,但我會做好每一篇內容。謝謝關注!

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章