Raid 獨立磁碟冗餘陣列

2023-06-18 18:00:20

Raid儲存的實現

概念:

RAID ( Redundant Array of Independent Disks )即獨立磁碟冗餘陣列,簡稱為「磁碟陣列」,其實就是用多個獨立的磁碟組成在一起形成一個大的磁碟系統,從而實現比單塊磁碟更好的儲存效能和更高的可靠性。

種類:

Raid 0:至少需要兩塊硬碟,磁碟越多,讀寫速度越快,沒有冗餘。

Raid 1:只能用兩塊硬碟,兩塊硬碟的資料互為映象(寫慢,讀快),一塊磁碟冗餘。

Raid 5:至少需要3塊硬碟,一塊磁碟冗餘。它是最通行的設定方式。具有奇偶校驗的資料恢復功能的資料存貯方式。奇偶校驗資料塊分佈於陣列裡的各個硬碟中。

Raid 6:至少需要4塊硬碟,2塊磁碟冗餘,硬碟的總數大於等於4即可。

Raid 10:至少需要4塊硬碟,冗餘一半的硬碟數量,但是硬碟的總數必須是大於或等於4的偶數(相當於每兩塊硬碟做一個Raid0,然後把各個Raid0做成一個Raid1)。

Raid 50:至少需要6塊硬碟,磁碟的冗餘相當於每三個硬碟做了一個Raid5,然後,每個Raid5又組合成了Raid0(Raid5中有一個硬碟冗餘,即使壞了也不會破壞Raid0),所以,Raid50的磁碟的 冗餘硬碟數量=磁碟總數÷3 。

Raid 60:至少需要8塊硬碟

raid 0的建立

RAID0 是一種非常簡單的的方式,它將多塊磁碟組合在一起形成一個大容量的儲存。當我們要寫資料的時候,會將資料分為N份,以獨立的方式實現N塊磁碟的讀寫,那麼這N份資料會同時並行的寫到磁碟中,因此執行效能非常的高。RAID0 的讀寫效能理論上是單塊磁碟的N倍(僅限理論,因為實際中磁碟的定址時間也是效能佔用的大頭)

但RAID0的問題是,它並不提供資料校驗或冗餘備份,因此一旦某塊磁碟損壞了,資料就直接丟失,無法恢復了。因此RAID0就不可能用於高要求的業務中,但可以用在對可靠性要求不高,對讀寫效能要求高的場景中。

新增4塊硬碟,每塊50G

  • 顯示

  • 命令檢視

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   200G  0 disk 
    ├─sda1            8:1    0     1G  0 part /boot
    └─sda2            8:2    0   199G  0 part 
      ├─centos-root 253:0    0    50G  0 lvm  /
      ├─centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm  
    sdb               8:16   0    50G  0 disk 
    sdc               8:32   0    50G  0 disk 
    sdd               8:48   0    50G  0 disk 
    sde               8:64   0    50G  0 disk 
    sr0              11:0    1   4.2G  0 rom  
    
  • 下載mdadm命令

    #檢索
    [root@localhost ~]# yum provides *bin/mdadm
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.cqu.edu.cn
     * extras: mirrors.cqu.edu.cn
     * updates: mirrors.cqu.edu.cn
    base                                                                                                          | 3.6 kB  00:00:00     
    extras                                                                                                        | 2.9 kB  00:00:00     
    updates                                                                                                       | 2.9 kB  00:00:00     
    updates/7/x86_64/primary_db                                                                                   |  20 MB  00:00:07     
    mdadm-4.1-6.el7.x86_64 : The mdadm program controls Linux md devices (software RAID arrays)
    Repo        : base
    Matched from:
    Filename    : /usr/sbin/mdadm
    
    
    
    updates/7/x86_64/filelists_db                                                                                 |  11 MB  00:00:04     
    mdadm-4.1-7.el7_9.x86_64 : The mdadm program controls Linux md devices (software RAID arrays)
    Repo        : updates
    Matched from:
    Filename    : /usr/sbin/mdadm
    
    
    
    mdadm-4.1-8.el7_9.x86_64 : The mdadm program controls Linux md devices (software RAID arrays)
    Repo        : updates
    Matched from:
    Filename    : /usr/sbin/mdadm
    
    
    
    mdadm-4.1-9.el7_9.x86_64 : The mdadm program controls Linux md devices (software RAID arrays)
    Repo        : updates
    Matched from:
    Filename    : /usr/sbin/mdadm
    
    #安裝
    [root@localhost ~]# yum -y install mdadm
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base: mirrors.cqu.edu.cn
     * extras: mirrors.cqu.edu.cn
     * updates: mirrors.cqu.edu.cn
    Resolving Dependencies
    --> Running transaction check
    ---> Package mdadm.x86_64 0:4.1-9.el7_9 will be installed
    --> Processing Dependency: libreport-filesystem for package: mdadm-4.1-9.el7_9.x86_64
    --> Running transaction check
    ......
    
  • 用兩塊磁碟建立raid 0

    [root@localhost ~]# mdadm --create /dev/md0 --level 0 --raid-device 2 /dev/sd{b,c}
    mdadm: Fail to create md0 when using /sys/module/md_mod/parameters/new_array, fallback to creation via node
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md0 started.
    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    sda               8:0    0   200G  0 disk  
    ├─sda1            8:1    0     1G  0 part  /boot
    └─sda2            8:2    0   199G  0 part  
      ├─centos-root 253:0    0    50G  0 lvm   /
      ├─centos-swap 253:1    0   7.9G  0 lvm   [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm   
    sdb               8:16   0    50G  0 disk  
    └─md0             9:0    0   100G  0 raid0 
    sdc               8:32   0    50G  0 disk  
    └─md0             9:0    0   100G  0 raid0 
    sdd               8:48   0    50G  0 disk  
    sde               8:64   0    50G  0 disk  
    sr0              11:0    1   4.2G  0 rom   
    
  • 格式化md0,並掛載

    #格式化
    [root@localhost ~]# mkfs.xfs /dev/md0 
    meta-data=/dev/md0               isize=512    agcount=16, agsize=1637248 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=26195968, imaxpct=25
             =                       sunit=128    swidth=256 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=12792, version=2
             =                       sectsz=512   sunit=8 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    #掛載
    [root@localhost ~]# mkdir /raid0
    [root@localhost ~]# mount /dev/md0 /raid0
    [root@localhost ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G   14G   37G  28% /
    devtmpfs                devtmpfs  3.8G     0  3.8G   0% /dev
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
    tmpfs                   tmpfs     3.9G   12M  3.8G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  142M  873M  14% /boot
    tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
    /dev/md0                xfs       100G   33M  100G   1% /raid0
    
  • 新增到開機自啟動組態檔裡面

    [root@localhost ~]# blkid /dev/md0
    /dev/md0: UUID="cc930653-5ab7-43a0-87e4-6906c3d1bd98" TYPE="xfs" 
    [root@localhost ~]# vi /etc/fstab
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID=cc930653-5ab7-43a0-87e4-6906c3d1bd98    /raid0     xfs    defaults  0 0
    

raid 1的建立

RAID1 是磁碟陣列中單位成本最高的一種方式。因為它的原理是在往磁碟寫資料的時候,將同一份資料無差別的寫兩份到磁碟,分別寫到工作磁碟和映象磁碟,那麼它的實際空間使用率只有50%了,兩塊磁碟當做一塊用,這是一種比較昂貴的方案。

RAID1其實與RAID0效果剛好相反。RAID1 這種寫雙份的做法,就給資料做了一個冗餘備份。這樣的話,任何一塊磁碟損壞了,都可以再基於另外一塊磁碟去恢復資料,資料的可靠性非常強,但效能就沒那麼好了。

  • 兩塊磁碟建立raid 1

    [root@localhost ~]# mdadm --create /dev/md1 --level 1 --raid-device 2 /dev/sd{e,f}
    mdadm: Note: this array has metadata at the start and
        may not be suitable as a boot device.  If you plan to
        store '/boot' on this device please ensure that
        your boot-loader understands md/v1.x metadata, or use
        --metadata=0.90
    mdadm: cannot open /dev/sdf: No such file or directory
    [root@localhost ~]# mdadm --create /dev/md1 --level 1 --raid-device 2 /dev/sd{d,e}
    mdadm: Note: this array has metadata at the start and
        may not be suitable as a boot device.  If you plan to
        store '/boot' on this device please ensure that
        your boot-loader understands md/v1.x metadata, or use
        --metadata=0.90
    Continue creating array? y
    mdadm: Fail to create md1 when using /sys/module/md_mod/parameters/new_array, fallback to creation via node
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md1 started.
    
  • 檢視,並格式化掛載

    #檢視
    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    sda               8:0    0   200G  0 disk  
    ├─sda1            8:1    0     1G  0 part  /boot
    └─sda2            8:2    0   199G  0 part  
      ├─centos-root 253:0    0    50G  0 lvm   /
      ├─centos-swap 253:1    0   7.9G  0 lvm   [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm   
    sdb               8:16   0    50G  0 disk  
    └─md0             9:0    0   100G  0 raid0 
    sdc               8:32   0    50G  0 disk  
    └─md0             9:0    0   100G  0 raid0 
    sdd               8:48   0    50G  0 disk  
    └─md1             9:1    0    50G  0 raid1 
    sde               8:64   0    50G  0 disk  
    └─md1             9:1    0    50G  0 raid1 
    sr0              11:0    1   4.2G  0 rom   
    
    #格式化
    [root@localhost ~]# mkfs.xfs /dev/md1
    meta-data=/dev/md1               isize=512    agcount=4, agsize=3274688 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=13098752, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=6395, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    #掛載
    [root@localhost ~]# mkdir /raid1
    [root@localhost ~]# mount /dev/md1 /raid1
    [root@localhost ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G   14G   37G  28% /
    devtmpfs                devtmpfs  3.8G     0  3.8G   0% /dev
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
    tmpfs                   tmpfs     3.9G   12M  3.8G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  142M  873M  14% /boot
    tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
    /dev/md1                xfs        50G   33M   50G   1% /raid1
    
  • 新增到開機自啟動組態檔裡

    [root@localhost ~]# umount /raid1
    [root@localhost ~]# blkid /dev/md1
    /dev/md1: UUID="401222a9-5c23-47cb-936a-aa03101cbb1a" TYPE="xfs" 
    [root@localhost ~]# vi /etc/fstab
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID=401222a9-5c23-47cb-936a-aa03101cbb1a     /raid1    xfs   defaults   0 0
    

raid 5的建立

RAID 5是目前用的最多的一種方式。因為 RAID5 是一種將 儲存效能、資料安全、儲存成本 兼顧的一種方案。RAID5模式中,不再需要用單獨的磁碟寫校驗碼了。它把校驗碼資訊分佈到各個磁碟上。例如,總共有N塊磁碟,那麼會將要寫入的資料分成N份,並行的寫入到N塊磁碟中,同時還將資料的校驗碼資訊也寫入到這N塊磁碟中(資料與對應的校驗碼資訊必須得分開儲存在不同的磁碟上)。一旦某一塊磁碟損壞了,就可以用剩下的資料和對應的奇偶校驗碼資訊去恢復損壞的資料。

RAID5校驗位演演算法原理:P = D1 xor D2 xor D3 … xor Dn (D1,D2,D3 … Dn為資料塊,P為校驗,xor為互斥或運算)

RAID5的方式,最少需要三塊磁碟來組建磁碟陣列,允許最多同時壞一塊磁碟。如果有兩塊磁碟同時損壞了,那資料就無法恢復了。

新增3塊硬碟,每塊20G

  • 顯示

  • 檢視

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   200G  0 disk 
    ├─sda1            8:1    0     1G  0 part /boot
    └─sda2            8:2    0   199G  0 part 
      ├─centos-root 253:0    0    50G  0 lvm  /
      ├─centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm  
    sdb               8:16   0    20G  0 disk 
    sdc               8:32   0    20G  0 disk 
    sdd               8:48   0    20G  0 disk 
    
  • 建立raid 5

    [root@localhost ~]# mdadm --create /dev/md5 --raid-device 3 /dev/sd{b,c,d}
    mdadm: a RAID level is needed to create an array.
    [root@localhost ~]# mdadm --create /dev/md5 --level 5 --raid-device 3 /dev/sd{b,c,d}
    mdadm: Fail to create md5 when using /sys/module/md_mod/parameters/new_array, fallback to creation via node
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md5 started.
    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
    sda               8:0    0   200G  0 disk  
    ├─sda1            8:1    0     1G  0 part  /boot
    └─sda2            8:2    0   199G  0 part  
      ├─centos-root 253:0    0    50G  0 lvm   /
      ├─centos-swap 253:1    0   7.9G  0 lvm   [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm   
    sdb               8:16   0    20G  0 disk  
    └─md5             9:5    0    40G  0 raid5 
    sdc               8:32   0    20G  0 disk  
    └─md5             9:5    0    40G  0 raid5 
    sdd               8:48   0    20G  0 disk  
    └─md5             9:5    0    40G  0 raid5 
    
  • 格式化掛載

    #格式化
    [root@localhost ~]# mkfs.xfs /dev/md5
    meta-data=/dev/md5               isize=512    agcount=16, agsize=654720 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=10475520, imaxpct=25
             =                       sunit=128    swidth=256 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=5120, version=2
             =                       sectsz=512   sunit=8 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    #掛載
    [root@localhost ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G   14G   37G  28% /
    devtmpfs                devtmpfs  3.8G     0  3.8G   0% /dev
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
    tmpfs                   tmpfs     3.9G   12M  3.8G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  142M  873M  14% /boot
    tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
    /dev/md5                xfs        40G   33M   40G   1% /raid5
    
  • 新增到開機自啟動組態檔裡

    [root@localhost ~]# blkid /dev/md5
    /dev/md5: UUID="0d4231a6-6c05-4c89-a489-671cf062c966" TYPE="xfs" 
    [root@localhost ~]# vi /etc/fstab
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID=0d4231a6-6c05-4c89-a489-671cf062c966    /raid5   xfs   defaults 0 0
    

raid 10的建立

RAID10兼備了RAID1和RAID0的有優點。首先基於RAID1模式將磁碟分為2份,當要寫入資料的時候,將所有的資料在兩份磁碟上同時寫入,相當於寫了雙份資料,起到了資料保障的作用。且在每一份磁碟上又會基於RAID0技術講資料分為N份並行的讀寫,這樣也保障了資料的效率。

但也可以看出RAID10模式是有一半的磁碟空間用於儲存冗餘資料的,浪費的很嚴重,因此用的也不是很多。

新增4塊硬碟,每塊100G

  • 顯示

  • 檢視

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   200G  0 disk 
    ├─sda1            8:1    0     1G  0 part /boot
    └─sda2            8:2    0   199G  0 part 
      ├─centos-root 253:0    0    50G  0 lvm  /
      ├─centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm  
    sdb               8:16   0   100G  0 disk 
    sdc               8:32   0   100G  0 disk 
    sdd               8:48   0   100G  0 disk 
    sde               8:64   0   100G  0 disk 
    sr0              11:0    1   4.2G  0 rom  
    
  • 建立raid 10

    [root@localhost ~]# mdadm --create /dev/md10 --level 10 --raid-device 4 /dev/sd{b,c,d,e}
    mdadm: Fail to create md10 when using /sys/module/md_mod/parameters/new_array, fallback to creation via node
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md10 started.
    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE   MOUNTPOINT
    sda               8:0    0   200G  0 disk   
    ├─sda1            8:1    0     1G  0 part   /boot
    └─sda2            8:2    0   199G  0 part   
      ├─centos-root 253:0    0    50G  0 lvm    /
      ├─centos-swap 253:1    0   7.9G  0 lvm    [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm    
    sdb               8:16   0   100G  0 disk   
    └─md10            9:10   0 199.9G  0 raid10 
    sdc               8:32   0   100G  0 disk   
    └─md10            9:10   0 199.9G  0 raid10 
    sdd               8:48   0   100G  0 disk   
    └─md10            9:10   0 199.9G  0 raid10 
    sde               8:64   0   100G  0 disk   
    └─md10            9:10   0 199.9G  0 raid10 
    sr0              11:0    1   4.2G  0 rom   
    
  • 磁碟格式化並掛載

    #格式化
    [root@localhost ~]# mkfs.xfs /dev/md10
    meta-data=/dev/md10              isize=512    agcount=16, agsize=3274624 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=52393984, imaxpct=25
             =                       sunit=128    swidth=256 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=25584, version=2
             =                       sectsz=512   sunit=8 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    #掛載
    [root@localhost ~]# mkdir /raid10
    [root@localhost ~]# mount /dev/md10 /raid10
    [root@localhost ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G   14G   37G  28% /
    devtmpfs                devtmpfs  3.8G     0  3.8G   0% /dev
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
    tmpfs                   tmpfs     3.9G   12M  3.8G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  142M  873M  14% /boot
    tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
    /dev/md10               xfs       200G   33M  200G   1% /raid10
    
  • 新增到開機自啟動組態檔裡

    [root@localhost ~]# blkid /dev/md10
    /dev/md10: UUID="c57bbd24-efc0-4521-a4d7-a870f012c430" TYPE="xfs" 
    [root@localhost ~]# vi /etc/fstab
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID=c57bbd24-efc0-4521-a4d7-a870f012c430   /raid10    xfs   defaults  0 0
    

磁碟分割區,格式化,掛載

分割區

新增一塊2T磁碟

檢視所有磁碟情況

  • 命令:

    [root@localhost ~]# fdisk -l
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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 /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x00097353
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200   419430399   208665600   8e  Linux LVM
    
    Disk /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 8455 MB, 8455716864 bytes, 16515072 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 /dev/mapper/centos-home: 151.5 GB, 151523426304 bytes, 295944192 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
    

分割區開始

  • 內容為1個primary分割區,1個exetended分割區,3個logical分割區

    #建立主分割區
    Command (m for help): p
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0xb65bdaed
    
       Device Boot      Start         End      Blocks   Id  System
    
    Command (m for help): n
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)
       e   extended
    Select (default p): 
    Using default response p
    Partition number (1-4, default 1): 
    First sector (2048-4194303999, default 2048): 
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999): +10G
    Partition 1 of type Linux and of size 10 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0xb65bdaed
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    
    #建立擴充套件分割區
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 0 extended, 3 free)
       e   extended
    Select (default p): e
    Partition number (2-4, default 2): 
    First sector (20973568-4194303999, default 20973568): 
    Using default value 20973568
    Last sector, +sectors or +size{K,M,G} (20973568-4194303999, default 4194303999): 
    Using default value 4194303999
    Partition 2 of type Extended and of size 2 TiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0xb65bdaed
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    /dev/sdb2        20973568  4194303999  2086665216    5  Extended
    
    #建立3個邏輯分割區
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    Adding logical partition 5
    First sector (20975616-4194303999, default 20975616): 
    Using default value 20975616
    Last sector, +sectors or +size{K,M,G} (20975616-4194303999, default 4194303999): +20G
    Partition 5 of type Linux and of size 20 GiB is set
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    Adding logical partition 6
    First sector (62920704-4194303999, default 62920704): 
    Using default value 62920704
    Last sector, +sectors or +size{K,M,G} (62920704-4194303999, default 4194303999): +30G
    Partition 6 of type Linux and of size 30 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x10285311
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    /dev/sdb2        20973568  4194303999  2086665216    5  Extended
    /dev/sdb5        20975616    62918655    20971520   83  Linux
    /dev/sdb6        62920704   125835263    31457280   83  Linux
    
    Command (m for help): n
    Partition type:
       p   primary (1 primary, 1 extended, 2 free)
       l   logical (numbered from 5)
    Select (default p): l
    Adding logical partition 7
    First sector (125837312-4194303999, default 125837312): 
    Using default value 125837312
    Last sector, +sectors or +size{K,M,G} (125837312-4194303999, default 4194303999): +50G
    Partition 7 of type Linux and of size 50 GiB is set
    
    Command (m for help): p
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0x10285311
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    /dev/sdb2        20973568  4194303999  2086665216    5  Extended
    /dev/sdb5        20975616    62918655    20971520   83  Linux
    /dev/sdb6        62920704   125835263    31457280   83  Linux
    /dev/sdb7       125837312   230694911    52428800   83  Linux
    

分割區結束儲存退出

  • 命令:

    #w儲存退出
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    #重新整理磁碟
    [root@localhost ~]# partprobe 
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    
  • 檢視

    [root@localhost ~]# lsblk
    NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
    sda               8:0    0   200G  0 disk 
    ├─sda1            8:1    0     1G  0 part /boot
    └─sda2            8:2    0   199G  0 part 
      ├─centos-root 253:0    0    50G  0 lvm  /
      ├─centos-swap 253:1    0   7.9G  0 lvm  [SWAP]
      └─centos-home 253:2    0 141.1G  0 lvm  
    sdb               8:16   0     2T  0 disk 
    ├─sdb1            8:17   0    10G  0 part 
    ├─sdb2            8:18   0     1K  0 part 
    ├─sdb5            8:21   0    20G  0 part 
    ├─sdb6            8:22   0    30G  0 part 
    └─sdb7            8:23   0    50G  0 part 
    sr0              11:0    1   4.2G  0 rom  
    
    [root@localhost ~]# fdisk -l
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0xe4f035bc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    /dev/sdb2        20973568  4194303999  2086665216    5  Extended
    /dev/sdb5        20975616    62918655    20971520   83  Linux
    /dev/sdb6        62920704   125835263    31457280   83  Linux
    /dev/sdb7       125837312   230694911    52428800   83  Linux
    

格式化與掛載

取分割區的磁碟進行格式化

  • 命令:

    #設定/dev/sdb6的檔案格式為xfs
    [root@localhost ~]# mkfs.xfs /dev/sdb6
    meta-data=/dev/sdb6              isize=512    agcount=4, agsize=1966080 blks
             =                       sectsz=512   attr=2, projid32bit=1
             =                       crc=1        finobt=0, sparse=0
    data     =                       bsize=4096   blocks=7864320, imaxpct=25
             =                       sunit=0      swidth=0 blks
    naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
    log      =internal log           bsize=4096   blocks=3840, version=2
             =                       sectsz=512   sunit=0 blks, lazy-count=1
    realtime =none                   extsz=4096   blocks=0, rtextents=0
    
    #將sdb6掛載到mnt資料夾
    [root@localhost ~]# mount /dev/sdb6 /mnt
    [root@localhost ~]# df -hT
    Filesystem              Type      Size  Used Avail Use% Mounted on
    /dev/mapper/centos-root xfs        50G   14G   37G  28% /
    devtmpfs                devtmpfs  3.8G     0  3.8G   0% /dev
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /dev/shm
    tmpfs                   tmpfs     3.9G   12M  3.8G   1% /run
    tmpfs                   tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1               xfs      1014M  142M  873M  14% /boot
    tmpfs                   tmpfs     781M     0  781M   0% /run/user/0
    /dev/sdb6               xfs        30G   33M   30G   1% /mnt
    
    #設定開機自啟動掛載
    [root@localhost ~]# blkid /dev/sdb6
    /dev/sdb6: UUID="57eb7bba-cc7a-4f6c-99bf-bccb067d266f" TYPE="xfs" 
    [root@localhost ~]# vi /etc/fstab 
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID="57eb7bba-cc7a-4f6c-99bf-bccb067d266f"  /mnt  xfs  defaults 0 0
    

利用磁碟建立swap分割區

  • 更改磁碟id為swap

    #更改/dev/sdb7的id為82
    [root@localhost ~]# fdisk /dev/sdb
    Command (m for help): t
    Partition number (1,2,5-7, default 7): 7
    Hex code (type L to list all codes): 82
    Changed type of partition 'Linux' to 'Linux swap / Solaris'
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@localhost ~]# partprobe 
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    [root@localhost ~]# fdisk -l
    
    Disk /dev/sdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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: 0xe4f035bc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    20973567    10485760   83  Linux
    /dev/sdb2        20973568  4194303999  2086665216    5  Extended
    /dev/sdb5        20975616    62918655    20971520   83  Linux
    /dev/sdb6        62920704   125835263    31457280   83  Linux
    /dev/sdb7       125837312   230694911    52428800   82  Linux swap / Solaris
    
  • 格式化

    [root@localhost ~]# mkswap /dev/sdb7
    Setting up swapspace version 1, size = 52428796 KiB
    no label, UUID=a48e154b-4da3-4771-9e09-8a357aa6049b
    
  • 掛載到swap分割區

    #檢視swap分割區有7G
    [root@localhost ~]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           7.6G        181M        7.2G         11M        295M        7.2G
    Swap:          7.9G          0B        7.9G
    
    #新增/dev/sdb7後
    [root@localhost ~]# swapon /dev/sdb7
    [root@localhost ~]# free -h
                  total        used        free      shared  buff/cache   available
    Mem:           7.6G        221M        7.1G         11M        295M        7.1G
    Swap:           57G          0B         57G
    
  • 實現自動掛載

    [root@localhost ~]# blkid /dev/sdb7
    /dev/sdb7: UUID="6c77c8f6-9b57-4def-9885-d92a45b7abe4" TYPE="swap" 
    [root@localhost ~]# vi /etc/fstab 
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=60defebd-6b85-43da-b5ce-1977bac72048 /boot                   xfs     defaults        0 0
    #/dev/mapper/centos-home /home                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    UUID="6c77c8f6-9b57-4def-9885-d92a45b7abe4"   swap  swap   defaults 0 0