软 RAID 组建 LVM 可扩展磁盘

后知后觉 暂无评论

因考虑到方便扩容,因此使用逻辑卷功能,又需要考虑冗余,因此使用软 RAID 配合逻辑卷功能。

磁盘阵列 (RAID, Redundant Arrays of Independent Disks),有“独立磁盘构成的具有冗余能力的阵列”之意。

磁盘阵列

磁盘阵列是由很多块独立的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘提供数据所产生加成效果提升整个磁盘系统效能。利用这项技术,将数据切割成许多区段,分别存放在各个硬盘上。

部分磁盘阵列还能利用同位检查技术,当磁盘阵列中任意一个硬盘故障时,仍可读出数据,在数据重构时,将数据经计算后重新置入新硬盘中。

常用的 RAID 有:

逻辑卷管理 (LVM, Logical Volume Manager),系统分区时,一个常见的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

磁盘阵列

磁盘格式化

先将磁盘格式化准备就绪

# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.33.1).
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.
Created a new DOS disklabel with disk identifier 0x42eb8a7b.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): g
Created a new GPT disklabel (GUID: 03D1E124-F474-2844-9CBD-538D8AA68081).

Command (m for help): n
Partition number (1-128, default 1): 
First sector (2048-104857260, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-104857260, default 104857260): 

Created a new partition 1 of type 'Linux filesystem' and of size 50 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

以此类推,将 sdb, sdc, sdd, sde 格式化。

检查磁盘状态

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   20G  0 disk 
├─sda1   8:1    0  512M  0 part /boot/efi
├─sda2   8:2    0  3.5G  0 part /home
├─sda3   8:3    0  315M  0 part /tmp
└─sda4   8:4    0 15.7G  0 part /
sdb      8:16   0   50G  0 disk 
└─sdb1   8:17   0   50G  0 part 
sdc      8:32   0   50G  0 disk 
└─sdc1   8:33   0   50G  0 part 
sdd      8:48   0   50G  0 disk 
└─sdd1   8:49   0   50G  0 part 
sde      8:64   0   50G  0 disk 
└─sde1   8:65   0   50G  0 part 
sdf      8:80   0   20G  0 disk 
sr0     11:0    1 1024M  0 rom  

组建阵列

安装磁盘阵列组件

# apt install mdadm
mdadm: partition table exists on /dev/sdb1
mdadm: partition table exists on /dev/sdb1 but will be lost or
       meaningless after creating array
mdadm: partition table exists on /dev/sdc1
mdadm: partition table exists on /dev/sdc1 but will be lost or
       meaningless after creating array
mdadm: partition table exists on /dev/sdd1
mdadm: partition table exists on /dev/sdd1 but will be lost or
       meaningless after creating array
mdadm: partition table exists on /dev/sde1
mdadm: partition table exists on /dev/sde1 but will be lost or
       meaningless after creating array
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md5 started.

组建 RAID5 阵列

mdadm -C /dev/md5 -ayes -l5 -n4 /dev/sd[b,c,d,e]1
参数长参数说明
-C--create创建新磁盘阵列名字
-a/-ayes 用于忽略二次操作认证
-l/RAID 级别
-n/阵列级别
-I--incremental移除磁盘阵列

此时需要创建配置文件,防止重启后磁盘阵列名称变化。

$ sudo echo "DEVICE /dev/sdb1 /dev/sdc1 /dev/sdd1 /dev/sde1" >> /etc/mdadm.conf
$ sudo mdadm -Ds >> /etc/mdadm.conf

结果检查

# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda      8:0    0   20G  0 disk  
├─sda1   8:1    0  512M  0 part  /boot/efi
├─sda2   8:2    0  3.5G  0 part  /home
├─sda3   8:3    0  315M  0 part  /tmp
└─sda4   8:4    0 15.7G  0 part  /
sdb      8:16   0   50G  0 disk  
└─sdb1   9:5    0   50G  0 part
  └─md5  9:23   0  150G  0 raid5 
sdc      8:32   0   50G  0 disk  
└─sdc1   9:5    0   50G  0 part
  └─md5  9:34   0  150G  0 raid5
sdd      8:48   0   50G  0 disk  
└─sdd1   9:5    0   50G  0 part 
  └─md5  9:45   0  150G  0 raid5
sde      8:64   0   50G  0 disk  
└─sde1   9:5    0   50G  0 part 
  └─md5  9:59   0  150G  0 raid5
sdf      8:80   0   20G  0 disk  
└─sdf1   8:81   0   20G  0 part  
sr0     11:0    1 1024M  0 rom 

移除磁盘阵列

mdadm --stop /dev/md5
mdadm --remove /dev/md5

逻辑卷

补充依赖

apt install lvm2

制作逻辑卷

创建物理卷

# pvcreate /dev/md5 
  Physical volume "/dev/md5" successfully created.
# pvs
  PV         VG Fmt  Attr PSize    PFree  
  /dev/md5      lvm2 ---  <149.99g <149.99g

创建卷组

# vgcreate uosvg /dev/md5 
  Volume group "uosvg" successfully created
# vgs
  VG    #PV #LV #SN Attr   VSize   VFree 
  uosvg   1   0   0 wz--n- 149.98g 149.98g

创建逻辑卷

# lvcreate -l 100%FREE uosvg -n uoslv
  Logical volume "uoslv" created.
# lvs
  LV    VG    Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  uoslv uosvg -wi-a----- 149.98g                                                    

创建存储系统

# mkfs.ext4 /dev/uosvg/uoslv 
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 3927040 4k blocks and 983040 inodes
Filesystem UUID: 198a0d72-b5f1-4621-9bed-bd01c386eee0
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208

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

热扩容

查看当前状态

# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/uosvg/uoslv
  LV Name                uoslv
  VG Name                uosvg
  LV UUID                yoP7Q8-KEwc-Fm7c-c9Ec-XjIi-iSAI-ox9pm1
  LV Write Access        read/write
  LV Creation host, time kubernetes, 2020-08-30 09:10:59 -0400
  LV Status              available
  # open                 1
  LV Size                149.98 GiB
  Current LE             38350
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     6144
  Block device           253:0

增加新磁盘

# vgextend uosvg /dev/sdf1
  Physical volume "/dev/sdf1" successfully created.
  Volume group "uosvg" successfully extended

查看扩容后卷组

# vgdisplay 
  --- Volume group ---
  VG Name               uosvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <169.98 GiB
  PE Size               4.00 MiB
  Total PE              4346
  Alloc PE / Size       38350 / 149.98 GiB
  Free  PE / Size       5114 / <20.00 GiB
  VG UUID               sTIeUa-JCWT-rr4k-SKJF-Czvb-eyVU-c2jTQT
   
小贴士:可以看到 Free PE 空间为已经加入卷组,但是并未实际提升逻辑卷空间。

将逻辑卷扩容

# lvresize /dev/uosvg/uoslv --extents +100%FREE
  Size of logical volume uosvg/uoslv changed from 149.98 GiB (38350 extents) to <169.98 GiB (43464 extents).
  Logical volume uosvg/uoslv successfully resized.

查看逻辑卷扩容后详情

# pvs
  PV         VG    Fmt  Attr PSize   PFree
  /dev/md5   uosvg lvm2 a--  149.98g     0 
  /dev/sdf1  uosvg lvm2 a--  <20.00g     0 
# vgs
  VG    #PV #LV #SN Attr   VSize    VFree
  uosvg   2   1   0 wz--n- <169.98g     0 
# lvs
  LV    VG    Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  uoslv uosvg -wi-ao---- <169.98g                                                    
# blkid /dev/uosvg/uoslv
/dev/uosvg/uoslv: UUID="198a0d72-b5f1-4621-9bed-bd01c386eee0" TYPE="ext4"
# echo "UUID=198a0d72-b5f1-4621-9bed-bd01c386eee0 /opt/data ext4 defaults 0 0" >> /etc/fstab 
# mount -a

附录

参考链接

如果遇到问题或者对文章内容存疑,请在下方留言,博主看到后将及时回复,谢谢!
回复
回答19+21=