磁盤存儲管理和文件系統管理 RAID

磁盤存儲管理 和文件系統管理 RAID

一、磁盤管理

1.1 磁盤分區

兩種分區方式:MBR,GPTnode

0磁道0扇區: 512byteslinux

446bytes:boot loadershell

64bytes:分區表,其中每16bytes標識一個分區vim

2bytes:55AAbash

1.2 管理分區

列出塊設備網絡

lsblk

建立分區命令app

fdisk   管理MBR分區
gdisk   管理GPT分區
parted  高級分區操做,

1.3 parted 命令

parted的操做是實時生效的,當心使用dom

格式:ide

parted [option]...[設備 [命令 [參數]...]...]

1.4 分區工具fdisk和gdisk

gdisk [device...]           類fdisk 的GPT分區工具
fdisk -l [-u] [device...]   查看分區
fdisk [device...]           管理MBR分區工具

子命令:工具

p       分區列表
t       更改分區類型
n       建立新分區
d       刪除分區
v       校驗分區
u       轉換單位
w       保存退出
q       不保存退出

查看內核是否已經識別新的分區

cat /proc/partations

非交互式建立分區

echo -e 'n\np\n\n\n+2G\nw\n' | fdisk /dev/sdc

1.5 文件系統管理

1.5.1 建立文件管理工具

  • mkfs命令:

    (1) mkfs.FS_TYPE /dev/DEVICE

    ​ ext4

    ​ xfs

    ​ btfs

    ​ vfat

    (2) mkfs -t FS_TYPE /dev/DEVICE

    ​ -L 'LABEL' 設定卷標

    • mke2fs:ext系列文件系統專用管理工具

經常使用選項:

-t {ext2|ext3|ext4|xfs}         指定文件系統類型
-b {1024|2048|4096}             指定塊大小
-L 'LABEL'                      設定卷標
-i              #爲數據空間中每第多少個字節建立一個inode;不小於塊大小
-N              #指定分區中建立多少個inode
-m              #默認5%,爲管理人員預留空間佔總空間的百分比
-o FEATURE[,...]    啓用指定特性
-o ^FEATURE         關閉指定特性
-j                  至關於 -t ext3

二、邏輯卷管理器

相關工具來自於lvm2 包

2.1 pv管理工具

#顯示pv信息
pvs:簡要pv信息顯示
pvdisplay

#建立pv
pvcreate /dev/DEVICE

#刪除pv
pvremove /dev/DEVICE

2.2 vg管理工具

#顯示卷組
vgs
vgdisplay

#建立卷組
vgcreate [-s #[KkmMgGtTpPeE]] volumeGroupName physicalDevicepath [PhysicalDevivePath...]

#管理卷組
vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevivePath...]
vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevivePath...]

#刪除卷組
先作pvmove
再作 vgremove

2.3 lv管理工具

#顯示邏輯卷
lvs
lvdisplay

#建立邏輯卷
lvcreate -l #[mMgGtT] -n Name VolumeGroup
lvcreate -l 60%VG -n mylv testvg

#刪除邏輯卷
lvremove /dev/VG_NAME/LV_NAME

2.4 在線擴展邏輯卷

lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
# ext格式
resize2fs /dev/VG_NAME/LV_NAME
# xfs格式
xfs_growfs MOUNTPOINT

lvresize -r -l +100%FREE /dev/VG_NAME/LV_NAME

三、RAID

3.1 總結RAID各個級別及其組合方式和性能的不一樣

RAID類型:
RAID 0、RAID 一、RAID 四、RAID 五、RAID 六、RAID 十、RAID 0一、RAID 50

RAID經常使用性能:
RAID 0:須要至少2塊硬盤,讀寫性能提高,可用空間爲n塊硬盤容量之和,無容錯能力。
RAID 1:至少須要2塊硬盤,硬盤數爲2的倍數,可用空間爲n/2容量的空間,讀性能提高,寫性能下降,有冗餘能力。
RAID 5:至少須要3塊硬盤,硬盤數爲3塊以上。可用空間爲(n-1),讀寫性能提高,有容錯能力,最多容許壞1塊盤。
RAID 6:至少須要4塊硬盤,硬盤數爲4塊以上,可用空間爲(n-2),讀寫性能提高,有容錯能力,最多容許壞2塊盤。
RAID 10:至少須要4塊硬盤,硬盤數爲4的偶數,可用空間爲n/2,讀寫性能提高,有容錯能力,每組鏡像最多隻能壞1塊盤。先組RAID 1再組 RAID 0。
RAID 01:至少4塊盤,硬盤數爲4的偶數,可用空間爲n/2,讀寫性能提高,有容錯能力。不一樣鏡像不能壞存儲相同內容的硬盤,先組RAID 0 再組 RAID 1。
RAID 50:至少須要6塊盤,硬盤數爲3的倍數,可用空間爲(n-3),讀寫性能提高,有容錯能力,每組鏡像容許壞1塊盤,最多容許壞2塊硬盤。

3.2 破壞mbr表並修復

# 備份MBR分區表
[root@CentOS8 ~]#dd if=/dev/sda of=/data/mbr.img bs=1 count=64 skip=446
[root@CentOS8 ~]#scp /data/mbr.img 10.0.0.8:

# 破壞MBR分區表
[root@CentOS8 ~]#dd if=/dev/zero of=/dev/sda bs=1 count=64 seek=446

#重啓沒法進入系統
[root@CentOS8 ~]#reboot

#用光盤啓動,進入rescue mode,選擇1)Continue -->> 回車進入shell

#配置網絡
sh-4.4#ip addr add 10.0.0.18/24 dev ens33 
sh-4.4#scp 10.0.0.8:/root/mbr.img .

#恢復MBR分區表
sh-4.4#dd if=mbr.img of=/dev/sda bs=1 seek=446
sh-4.4#exit

3.3 磁盤管理實例

3.3.1 建立一個2G的文件系統,塊大小爲2048byte,預留1%可用空間,文件系統 ext4,卷標爲TEST,要求此分區開機後自動掛載至/test目錄,且默認有acl掛載選項

#建立2G分區
[root@CentOS8 ~]#fdisk -l
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 218105855   6291456    3G 82 Linux swap / Solaris
/dev/sda4       218105856 419430399 201324544   96G  5 Extended
/dev/sda5       218107904 322965503 104857600   50G 83 Linux
[root@CentOS8 ~]#fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (322967552-419430399, default 322967552): 
Last sector, +sectors or +size{K,M,G,T,P} (322967552-419430399, default 419430399): +2G

Created a new partition 6 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 218105855   6291456    3G 82 Linux swap / Solaris
/dev/sda4       218105856 419430399 201324544   96G  5 Extended
/dev/sda5       218107904 322965503 104857600   50G 83 Linux
/dev/sda6       322967552 327161855   4194304    2G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

[root@CentOS8 ~]#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  100G  0 part /
├─sda3   8:3    0    3G  0 part [SWAP]
├─sda4   8:4    0    1K  0 part 
├─sda5   8:5    0   50G  0 part /data
└─sda6   8:6    0    2G  0 part 
sr0     11:0    1  8.6G  0 rom  

#建立2G ext4文件系統
[root@CentOS8 ~]#mkfs.ext4 -b 2048 -L 'TEST' -m 1 /dev/sda6
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 1048576 2k blocks and 131072 inodes
Filesystem UUID: 344bd85b-cf28-4ace-aa25-a962f56a554f
Superblock backups stored on blocks: 
    16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@CentOS8 ~]#blkid | grep /dev/sda6
/dev/sda6: LABEL="TEST" UUID="344bd85b-cf28-4ace-aa25-a962f56a554f" BLOCK_SIZE="2048" TYPE="ext4" PARTUUID="3bce8aa0-06"

#建立text掛載點並添加掛載配置使開機自動掛載
[root@CentOS8 ~]#mkdir /test
[root@CentOS8 /]#vim /etc/fstab
UUID=344bd85b-cf28-4ace-aa25-a962f56a554f /test                   ext4    defaults        0 0  

#設置默認掛載acl選項
[root@CentOS8 ~]#tune2fs -o acl /dev/sda6
[root@CentOS8 /]#reboot
[root@CentOS8 ~]#df 
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda6        2031306    9236   1992908   1% /test
[root@CentOS8 ~]#tune2fs -l /dev/sda6
tune2fs 1.45.6 (20-Mar-2020)
Filesystem volume name:   TEST
Last mounted on:          <not available>
Filesystem UUID:          344bd85b-cf28-4ace-aa25-a962f56a554f
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent 64bit flex_bg sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
Filesystem flags:         signed_directory_hash 
Default mount options:    user_xattr acl            #分區掛載選項支持acl
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux

3.3.2 建立一個至少有兩個PV組成的大小爲20G的名爲testvg的VG;要求PE大小 爲16MB, 然後在卷組中建立大小爲5G的邏輯卷testlv;掛載至/users目錄

#建立兩個分區,/dev/sda7和/dev/sda8 大小均爲10G 且分區格式爲lvm
[root@CentOS8 ~]#fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 218105855   6291456    3G 82 Linux swap / Solaris
/dev/sda4       218105856 419430399 201324544   96G  5 Extended
/dev/sda5       218107904 322965503 104857600   50G 83 Linux
/dev/sda6       322967552 327161855   4194304    2G 83 Linux

Command (m for help): n
All primary partitions are in use.
Adding logical partition 7
First sector (327163904-419430399, default 327163904): 
Last sector, +sectors or +size{K,M,G,T,P} (327163904-419430399, default 419430399): +10G

Created a new partition 7 of type 'Linux' and of size 10 GiB.

Command (m for help): t
Partition number (1-7, default 7): 7   
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): n
All primary partitions are in use.
Adding logical partition 8
First sector (348137472-419430399, default 348137472): 
Last sector, +sectors or +size{K,M,G,T,P} (348137472-419430399, default 419430399): +10G

Created a new partition 8 of type 'Linux' and of size 10 GiB.

Command (m for help): t
Partition number (1-8, default 8): 8
Hex code (type L to list all codes): 8e

Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sda: 200 GiB, 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
Disklabel type: dos
Disk identifier: 0x3bce8aa0

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048   2099199   2097152    1G 83 Linux
/dev/sda2         2099200 211814399 209715200  100G 83 Linux
/dev/sda3       211814400 218105855   6291456    3G 82 Linux swap / Solaris
/dev/sda4       218105856 419430399 201324544   96G  5 Extended
/dev/sda5       218107904 322965503 104857600   50G 83 Linux
/dev/sda6       322967552 327161855   4194304    2G 83 Linux
/dev/sda7       327163904 348135423  20971520   10G 8e Linux LVM
/dev/sda8       348137472 369108991  20971520   10G 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Syncing disks.

#安裝lvm管理工具,查看工具包安裝文件
[root@CentOS8 ~]#yum -y install lvm2            
[root@CentOS8 ~]#rpm -ql lvm2

#建立物理卷PV
[root@CentOS8 ~]#pvcreate /dev/sda7
  Physical volume "/dev/sda7" successfully created.
[root@CentOS8 ~]#pvcreate /dev/sda8
  Physical volume "/dev/sda8" successfully created.
[root@CentOS8 ~]#pvs
  PV         VG Fmt  Attr PSize  PFree 
  /dev/sda7     lvm2 ---  10.00g 10.00g
  /dev/sda8     lvm2 ---  10.00g 10.00g

#建立卷組VG,且PE大小爲16MB

[root@CentOS8 ~]#vgcreate -s 16M testvg /dev/sda{7,8}
  Volume group "testvg" successfully created
[root@CentOS8 ~]#vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   0   0 wz--n- <19.97g <19.97g
[root@CentOS8 ~]#vgdisplay 
  --- Volume group ---
  VG Name               testvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <19.97 GiB
  PE Size               16.00 MiB
  Total PE              1278
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1278 / <19.97 GiB
  VG UUID               3peGQY-6UoH-3haX-rQCG-OeEY-jFcj-wKPFNu

#建立邏輯卷LV,大小爲5G的testlv
[root@CentOS8 ~]#lvcreate -n testlv -L 5G testvg
  Logical volume "testlv" created.
[root@CentOS8 ~]#lvs
  LV     VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv testvg -wi-a----- 5.00g                                                    
[root@CentOS8 ~]#lvdisplay 
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                c2bGkH-5o3O-pM60-Kvxd-VoxM-bzoz-Nlgr0S
  LV Write Access        read/write
  LV Creation host, time CentOS8.localdomain, 2021-03-26 06:21:59 +0800
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             320
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@CentOS8 ~]#vgs
  VG     #PV #LV #SN Attr   VSize   VFree  
  testvg   2   1   0 wz--n- <19.97g <14.97g

#建立ext4文件系統並自動掛載/users目錄
[root@CentOS8 ~]#mkfs.ext4 /dev/testvg/testlv
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: 2b540cd3-9f3a-4617-8940-1ae155b51474
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 

[root@CentOS8 ~]#blkid
/dev/sda7: UUID="F3GTs3-X70f-lifj-Cjbz-QCp5-tk7F-3Qz3q0" TYPE="LVM2_member" PARTUUID="3bce8aa0-07"
/dev/sda8: UUID="Qk2QT3-fUdm-w4QT-3TDO-BUbM-kZbv-PEPNC2" TYPE="LVM2_member" PARTUUID="3bce8aa0-08"
/dev/sr0: BLOCK_SIZE="2048" UUID="2020-11-18-21-39-52-00" LABEL="CentOS-8-3-2011-x86_64-dvd" TYPE="iso9660" PTUUID="6b8b4567" PTTYPE="dos"
/dev/mapper/testvg-testlv: UUID="2b540cd3-9f3a-4617-8940-1ae155b51474" BLOCK_SIZE="4096" TYPE="ext4"
[root@CentOS8 ~]#mkdir /users
[root@CentOS8 ~]#vim /etc/fstab
#最後一行,添加
UUID=2b540cd3-9f3a-4617-8940-1ae155b51474 /users                  ext4    defaults        0 0 

#手動掛載文件系統
[root@CentOS8 ~]#mount -a
[root@CentOS8 ~]#df -h
Filesystem                 Size  Used Avail Use% Mounted on
/dev/mapper/testvg-testlv  4.9G   20M  4.6G   1% /users
相關文章
相關標籤/搜索