linux 磁碟管理詳細介紹
阿新 • • 發佈:2020-08-03
linux磁碟介紹
什麼是磁碟 什麼是軟盤 什麼是硬碟 磁碟的基本結構 碟片 磁軌 扇區 512B 柱面 磁頭 磁碟的相關概念 磁碟的介面: IDE SCSI # 淘汰了 SATA III SAS #企業級 SSD( SATA III PCIE ) M2 超極本的介面標準 MSATA 機械硬碟 HDD 固態硬碟 SSD 容量大價格低 容量小价格貴 抗擊打能力弱 抗擊打能力強 讀寫速度慢 讀寫速度快 資料損壞恢復易 資料恢復難 壽命一直使用 使用期限有一定限制
1)磁碟的命令方式
磁碟的命名方式:
/dev/sda1 # 第一塊物理硬碟的第一個分割槽
/dev/sdb3 # 第二塊物理硬碟的第三個分割槽
/dev/vdc5 # 第三塊虛擬硬碟的第5個分割槽
sd # 物理硬碟
vd # 虛擬硬碟
2)磁碟的相關單位和術語
磁碟的相關單位和術語 尺寸: 2.5英寸 3.5英寸 資料大小: bit B KB MB GB TB PB EB 單位換算: 1024 1B=8b 轉速: RPM 轉/每分鐘 5400 7200 10k 15k IOPS 衡量磁碟讀寫 IO的單位 每秒的輸入輸出 兩個分割槽表: MBR 適用於2TB 只能有4個主分割槽 擴充套件分割槽(邏輯分割槽 ) 3個主分割槽 + 1個擴充套件分割槽( 邏輯分割槽...... ) MBR的位置: 0磁頭0磁軌1扇區 512位元組 446位元組 MBR引導記錄 64位元組4個主分割槽 2位元組分割槽結束識別符號 GPT 適用於2TB 128個主分割槽 分割槽工具: fdisk # 適用於2TB的分割槽 gdisk # 適用於2TB以上
fdisk分割槽
# 檢視系統磁碟和分割槽情況 [root@localhost /mnt]# ll /dev/sd* brw-rw---- 1 root disk 8, 0 Aug 3 10:43 /dev/sda brw-rw---- 1 root disk 8, 1 Aug 3 10:43 /dev/sda1 brw-rw---- 1 root disk 8, 2 Aug 3 11:39 /dev/sda2 brw-rw---- 1 root disk 8, 3 Aug 3 10:43 /dev/sda3 brw-rw---- 1 root disk 8, 16 Aug 3 10:43 /dev/sdb brw-rw---- 1 root disk 8, 17 Aug 3 10:43 /dev/sdb1 brw-rw---- 1 root disk 8, 32 Aug 3 10:45 /dev/sdc brw-rw---- 1 root disk 8, 33 Aug 3 10:45 /dev/sdc1 # 檢視磁碟分割槽結構詳細資訊 [root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 20G 0 disk ├─sda1 8:1 0 500M 0 part /boot ├─sda2 8:2 0 1G 0 part [SWAP] └─sda3 8:3 0 18.5G 0 part / sdb 8:16 0 20G 0 disk └─sdb1 8:17 0 10G 0 part sdc 8:32 0 3T 0 disk └─sdc1 8:33 0 3T 0 part sr0 11:0 1 4.3G 0 rom # 詳細檢視fdisk分割槽格式的分割槽資訊 [root@localhost ~]# fdisk -l /dev/sda Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000ac22f Device Boot Start End Blocks Id System /dev/sda1 * 2048 1026047 512000 83 Linux /dev/sda2 1026048 3123199 1048576 82 Linux swap / Solaris /dev/sda3 3123200 41943039 19409920 83 Linux # 進行fdisk分割槽 建立分割槽 [root@localhost ~]# fdisk /dev/sdb # 針對sdb磁碟進行fdisk分割槽 Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n # n建立新分割槽 Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p # P建立主分割槽 Partition number (2-4, default 2): 2 # 2主分割槽的編號 First sector (20973568-41943039, default 20973568): # 從哪個扇區位置開始 Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): # 給多大的空間 Using default value 41943039 Partition 2 of type Linux and of size 10 GiB is set # 分割槽成功 Command (m for help): w # 儲存退出 The partition table has been altered! Calling ioctl() to re-read partition table. # 提示重新載入一下分割槽 Syncing disks. # 分割槽建立好了需要格式化一下 [root@localhost ~]# mkfs.xfs /dev/sdb2 meta-data=/dev/sdb2 isize=512 agcount=4, agsize=655296 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=2621184, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 # 格式化好後就可以使用了 掛載目錄 [root@localhost ~]# mkdir /fisk_sdb2 # 建立掛載目錄 [root@localhost ~]# mount /dev/sdb2 /fisk_sdb2/ # 掛載 [root@localhost ~]# ll /fisk_sdb2 total 0 [root@localhost ~]# df -h # 檢視磁碟掛載 Filesystem Size Used Avail Use% Mounted on /dev/sda3 19G 1.4G 18G 8% / devtmpfs 900M 0 900M 0% /dev tmpfs 910M 0 910M 0% /dev/shm tmpfs 910M 9.5M 901M 2% /run tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/sda1 497M 130M 367M 27% /boot tmpfs 182M 0 182M 0% /run/user/0 /dev/sdb2 10G 33M 10G 1% /fisk_sdb2 # 測試掛載是否可用 [root@localhost ~]# dd if=/dev/zero of=/fisk_sdb2/data bs=100M count=10 10+0 records in 10+0 records out 1048576000 bytes (1.0 GB) copied, 2.83371 s, 370 MB/s [root@localhost ~]# ll /fisk_sdb2/data -rw-r--r-- 1 root root 1048576000 Aug 3 16:20 /fisk_sdb2/data # 實現永久掛載開機自動掛載 # 檢視磁碟uuid號 [root@localhost ~]# blkid /dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/sda1: UUID="4b4a640f-2389-47bb-8318-fe69554236d5" TYPE="xfs" /dev/sda2: UUID="2439ad1b-794a-47c3-9715-94c2e55215af" TYPE="swap" /dev/sda3: UUID="8647091b-fad2-423c-898b-91fbdb48b908" TYPE="xfs" /dev/sdb1: UUID="07e38a19-75bf-49b9-8719-e445f7e94598" TYPE="xfs" /dev/sdb2: UUID="16d22521-821d-4055-a60e-f26b60d27d0a" TYPE="xfs" /dev/sdc1: PARTLABEL="Linux filesystem" PARTUUID="b0b79b87-d0ca-46a5-b9be-fb627fed6f83" # 通過uuid進行永久掛載掛載 [root@localhost ~]# vim /etc/fstab [root@localhost ~]# vim /etc/fstab # # /etc/fstab # Created by anaconda on Sun Jul 5 00:10:31 2020 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=8647091b-fad2-423c-898b-91fbdb48b908 / xfs defaults 0 0 UUID=4b4a640f-2389-47bb-8318-fe69554236d5 /boot xfs defaults 0 0 UUID=2439ad1b-794a-47c3-9715-94c2e55215af swap swap defaults 0 0 UUID=16d22521-821d-4055-a60e-f26b60d27d0a /fisk_sdb2 xfs defaults 0 0 [root@localhost ~]# mount -a # 重新載入掛載點目錄 # 配置掛載檔案說明 #以空白字元為分隔符,分為6列: 第一列: 掛載的裝置 掛載的地址 網路裝置地址 第二列: 掛載點 掛載的目錄 空目錄 第三列: 檔案系統的型別 xfs 7預設的 ext4 6預設的 nfs gfs 第四列: 掛載的引數 defaults 第五列: 是否允許dump進行備份 0 # 不備份 1 # 每天備份 2 # 不定期備份 第六列: 是否通過fsck命令檢查磁碟 0 # 不檢查 1 # 檢查 檢查順序 先檢查 / 2 # 檢查順序
gdisk分割槽
# 安裝gdisk掛載工具
[root@qls ~]# yum install -y gdisk
# 檢視磁碟分割槽層級詳細資訊
[root@qls ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 97.5G 0 part /
sdb 8:16 0 100G 0 disk
├─sdb1 8:17 0 10G 0 part /fdisk01
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 20G 0 part
sdc 8:32 0 3T 0 disk
sr0 11:0 1 4.3G 0 rom
# 檢視gdisk分割槽格式的分割槽資訊
[root@qls ~]# gdisk -l /dev/sdc
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2BF39D49-315B-42C3-9C9C-C5A98D1EBB71
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 6442450877 sectors (3.0 TiB)
Number Start (sector) End (sector) Size Code Name
# 建立分割槽
[root@qls ~]# gdisk /dev/sdc
GPT fdisk (gdisk) version 0.8.10
Partition table scan:
MBR: not present
BSD: not present
APM: not present
GPT: not present
Creating new GPT entries.
Command (? for help): ?
b back up GPT data to a file
c change a partition's name
d delete a partition #刪除分割槽
i show detailed information on a partition
l list known partition types #顯示分割槽型別
n add a new partition #建立一個新的分割槽
o create a new empty GUID partition table (GPT)
p print the partition table #列印分割槽表
q quit without saving changes #不儲存退出
r recovery and transformation options (experts only)
s sort partitions
t change a partition's type code
v verify disk
w write table to disk and exit #儲存退出
x extra functionality (experts only)
? print this menu #幫助
Command (? for help):
Command (? for help): n #建立分割槽
Partition number (1-128, default 1): #分割槽編號
First sector (34-6442450910, default = 2048) or {+-}size{KMGTP}: #扇區起始位置
Last sector (2048-6442450910, default = 6442450910) or {+-}size{KMGTP}: #扇區結束位置
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): #選擇分割槽型別
Changed type of partition to 'Linux filesystem'
Command (? for help): p #列印分割槽表資訊
Disk /dev/sdc: 6442450944 sectors, 3.0 TiB
Logical sector size: 512 bytes
Disk identifier (GUID): 2E58E6D9-4971-4B2F-A455-16D3A5115002
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 6442450910
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)
Number Start (sector) End (sector) Size Code Name
1 2048 6442450910 3.0 TiB 8300 Linux filesystem
Command (? for help):
Command (? for help): w #儲存退出
Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!
Do you want to proceed? (Y/N): y #是否確認
OK; writing new GUID partition table (GPT) to /dev/sdc.
The operation has completed successfully.
# 格式化,建立檔案系統
[root@qls ~]# mkfs.xfs /dev/sdc1
meta-data=/dev/sdc1 isize=512 agcount=4, agsize=201326527 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=805306107, imaxpct=5
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=393215, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
# 建立掛載點,進行掛載
[root@qls ~]# mkdir /gdisk01
[root@qls ~]# mount /dev/sdc1 /gdisk01
[root@qls ~]# df -h | grep '/gdisk01'
/dev/sdc1 3.0T 33M 3.0T 1% /gdisk01
[root@qls ~]# cp /fdisk01/test.log /gdisk01/
[root@qls ~]# df -h | grep '/gdisk01'
/dev/sdc1 3.0T 1.1G 3.0T 1% /gdisk01
[root@qls ~]#
# 永久掛載
[root@qls ~]# tail -1 /etc/fstab
/dev/sdc1 /gdisk01 xfs defaults 0 0
[root@qls ~]# mount -a
[root@qls ~]#
磁碟的掛載方式
在Linux系統中,所有的裝置必須掛載才能使用
mount # 掛載的命令
選項:
-t # 指定檔案系統 網路協議 nfs
-o # 指定掛載的引數
-a # 重新掛載/etc/fstab檔案中的掛載列表
umount # 解除安裝
選項:
-l # 強制解除安裝
[root@qls fdisk01]# umount /fdisk01 # 不能在掛載的目錄中進行解除安裝
[root@qls fdisk01]# umount -l /dev/sdb1 # 強制解除安裝掛載
# 通過uuid掛載
[root@qls ~]# mount UUID="0668e9f1-7d37-42eb-9e79-980f6352c342" /fdisk01/
# 以空白字元為分隔符,分為6列:
第一列: 掛載的裝置 掛載的地址 網路裝置地址
第二列: 掛載點 掛載的目錄 空目錄
第三列: 檔案系統的型別 xfs 7預設的 ext4 6預設的 nfs gfs
第四列: 掛載的引數 defaults
第五列: 是否允許dump進行備份
0 # 不備份
1 # 每天備份
2 # 不定期備份
第六列: 是否通過fsck命令檢查磁碟
0 # 不檢查
1 # 檢查 檢查順序 先檢查 /
2 # 檢查順序
虛擬記憶體
# pratprobe告訴作業系統核心已經發生變化
Out of memory
OOM # 記憶體溢位 記憶體不足
# free檢視記憶體使用情況
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1863224 85432 622216 9708 1155576 1593040
Swap: 1048572 0 1048572
虛擬記憶體
# 新增一個swap分割槽
# 建立一個1G的分割槽
# 建立一個fdisk格式分割槽
[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x368ba5c5
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199): +1G
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# 把新建立的格式分割槽是設定成swap檔案
[root@localhost ~]# mkswap /dev/sdb1
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=97d9b776-5dbd-4068-9446-8d5e60b07a7c
# 把swap檔案交給記憶體使用
[root@localhost ~]# swapon /dev/sdb1
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1863224 129728 1588624 9688 144872 1569524
Swap: 2097144 0 2097144
# 取消swap檔案
[root@localhost ~]# swapoff /dev/sdb1
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 126M 1.5G 9.5M 141M 1.5G
Swap: 1.0G 0B 1.0
# 禁用swap
[root@localhost ~]# swapoff -a
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 125M 1.5G 9.5M 141M 1.5G
Swap: 0B 0B 0B
# 啟用swap
[root@localhost ~]# swapon -a
[root@localhost ~]# free -h
total used free shared buff/cache available
Mem: 1.8G 125M 1.5G 9.5M 141M 1.5G
Swap: 1.0G 0B 1.0G
# 生成一個大檔案 作為swap
[root@localhost ~]# dd if=/dev/zero of=/root/swap.txt bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 17.0654 s, 61.4 MB/s
[root@localhost ~]# ll
total 1024000
-rw-r--r-- 1 root root 1048576000 Aug 3 19:27 swap.txt
# 把檔案設定成swap檔案
[root@localhost ~]# mkswap ./swap.txt
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=f80dcef6-db90-4cba-acc1-629c5e0a08f8
# 檔案建議改成600許可權
[root@localhost ~]# chmod 600 ./swap.txt
# 啟用該檔案作為swap
[root@localhost ~]# swapon /root/swap.txt
[root@localhost ~]# free
total used free shared buff/cache available
Mem: 1863224 130112 534972 9688 1198140 1545012
Swap: 2072568 0 2072568
raid基本介紹
磁碟陣列技術
做磁碟陣列的好處:
1. 獲得更高的容量
2. 獲得更高的效能
3. 讓資料更安全 冗餘
如何做RAID:
軟體實現 軟RAID 不推薦 mdadm
硬體RAID RAID卡實現的 推薦
做RAID什麼時候做:
在裝系統之前做
RAID的級別:
級別 別名 硬碟數量 容量 效能 容錯性
RAID0 條帶卷 至少兩塊 N*單塊 讀寫最快 0
RAID1 映象卷 只能兩塊 損失一半 寫慢 讀一般 50%
RAID5 校驗卷 至少3塊 浪費一塊 讀寫一般 N分之一 只能壞一塊
RAID10 映象陣列條帶卷 至少4塊 浪費一半 讀寫很快 50%
LVM概述
邏輯卷管理
PV #物理卷 硬碟 分割槽
VG #卷組 多個硬碟 多個分割槽組成
LV #邏輯卷 分割槽 邏輯分割槽
PE #基本單元 4MB 一個LV只能分配65534個PE
磁碟故障解決
# 磁碟大檔案佔了空間
[root@qls ~]# echo "123" >> /opt/file.txt
-bash: echo: write error: No space left on device # 磁碟空間不足
# 檢查磁碟空間發現被佔滿
[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 6.4G 92G 7% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
/dev/sdb4 2.0G 2.0G 20K 100% /opt
# 找到這個大檔案 確認好之後在刪 一級一級往下找
[root@qls ~]# du -sh /opt/* | grep 'G'
2.0G /opt/test.log
# 磁碟被許多小檔案佔滿了導致ionde號不夠了
[root@qls ~]# echo "123" >>/opt/test.txt
-bash: /opt/test.txt: No space left on device
[root@qls ~]#
# 檢查空間磁碟
[root@qls ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 98G 6.4G 92G 7% /
devtmpfs 980M 0 980M 0% /dev
tmpfs 991M 0 991M 0% /dev/shm
tmpfs 991M 9.6M 981M 1% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sda1 497M 120M 378M 25% /boot
tmpfs 199M 0 199M 0% /run/user/0
/dev/sdb1 10G 1.1G 9.0G 11% /fdisk01
/dev/sdb4 2.0G 582M 1.5G 29% /opt
# 檢視都是小檔案
[root@qls ~]# df -i
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 51123712 46093 51077619 1% /
devtmpfs 250786 417 250369 1% /dev
tmpfs 253511 1 253510 1% /dev/shm
tmpfs 253511 774 252737 1% /run
tmpfs 253511 16 253495 1% /sys/fs/cgroup
/dev/sda1 256000 326 255674 1% /boot
tmpfs 253511 1 253510 1% /run/user/0
/dev/sdb1 5242880 4 5242876 1% /fdisk01
/dev/sdb4 1048576 1048576 0 100% /opt
# 都是目錄佔了大多空間
[root@qls ~]# find /opt/ -type d -size +2M | xargs ls -lhd
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug 3 12:27 /opt/test/test05
# 找到這些小檔案目錄
[root@qls ~]# find /opt/ -type d -size +1M | xargs ls -lhd
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data01
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data02
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data03
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data04
drwxr-xr-x 2 root root 1.2M Aug 3 12:25 /opt/test/data05
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test01
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test02
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test03
drwxr-xr-x 2 root root 3.8M Aug 3 12:27 /opt/test/test04
drwxr-xr-x 2 root root 3.7M Aug 3 12:27 /opt/test/test05
[root@qls ~]# ll /opt/test/data01 | wc -l
50001
#找到之後,確認在刪除
[root@qls ~]# find /opt/test/data01 -type f -delete
[root@qls ~]# rm -f /opt/test/test01/*
-bash: /usr/bin/rm: Argument list too long
[root@qls ~]# ll /opt/test/test01/ | wc -l
150001