RAID磁盘冗余阵列 部署磁盘阵列 mdadm命令用于管理Linux系统中的软件RAID硬盘阵列,格式为“mdadm [模式] <RAID设备名称> [选项] [成员设备名称]”。
mdadm命令的常用参数和作用
参数 作用 1. -a 检测设备名称 2. -n 指定设备数量 3. -l 指定RAID级别 4. -C 创建 5. -v 显示过程 6. -f 模拟设备损坏 7. -r 移除设备 8. -Q 查看摘要信息 9. -D 查看详细信息 10. -S 停止RAID磁盘阵列加4块硬盘(关机状态下)
[root@zzy Desktop]# ls -l /dev/sd* brw-rw----. 1 root disk 8, 0 Oct 21 2020 /dev/sda brw-rw----. 1 root disk 8, 1 Oct 21 2020 /dev/sda1 brw-rw----. 1 root disk 8, 2 Oct 21 2020 /dev/sda2 brw-rw----. 1 root disk 8, 16 Oct 21 2020 /dev/sdb brw-rw----. 1 root disk 8, 17 Oct 21 2020 /dev/sdb1 brw-rw----. 1 root disk 8, 32 Oct 21 2020 /dev/sdc brw-rw----. 1 root disk 8, 48 Oct 21 2020 /dev/sdd brw-rw----. 1 root disk 8, 64 Oct 21 2020 /dev/sde brw-rw----. 1 root disk 8, 80 Oct 21 2020 /dev/sdf [root@zzy Desktop]# mdadm -Cv /dev/md0 -a yes -n 4 -l 10 /dev/sd[c-f] mdadm: layout defaults to n2 mdadm: layout defaults to n2 mdadm: chunk size defaults to 512K mdadm: size set to 20954624K mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@zzy Desktop]# mkfs mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat [root@zzy Desktop]# mkfs.ext4 /dev/md0 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=128 blocks, Stripe width=256 blocks 2621440 inodes, 10477312 blocks 523865 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=2157969408 320 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 4096000, 7962624 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@zzy Desktop]# mkdir /lm [root@zzy Desktop]# mount /dev/md0 /lm [root@zzy Desktop]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Wed Oct 21 07:10:00 2020 Raid Level : raid10 Array Size : 41909248 (39.97 GiB 42.92 GB) Used Dev Size : 20954624 (19.98 GiB 21.46 GB) Raid Devices : 4 Total Devices : 4 Persistence : Superblock is persistent Update Time : Wed Oct 21 07:12:37 2020 State : active, resyncing Active Devices : 4 Working Devices : 4 Failed Devices : 0 Spare Devices : 0 Layout : near=2 Chunk Size : 512K Resync Status : 76% complete Name : zzy:0 (local to host zzy) UUID : a6b873fe:0afc151e:f54a950d:58e87321 Events : 13 Number Major Minor RaidDevice State 0 8 32 0 active sync /dev/sdc 1 8 48 1 active sync /dev/sdd 2 8 64 2 active sync /dev/sde 3 8 80 3 active sync /dev/sdf [root@zzy Desktop]# vim /etc/fstab磁盘阵列+备份盘
部署RAID 5 磁盘阵列时,至少需要用3块硬盘,还需要再加一块备份硬盘。 还原虚拟机,部署RAID 5 + 1备份盘 。
mdadm -Cv /dev/md0 -n 3 -l 5 -x 1 /dev/sd[b-e] #用3块硬盘创建RAID 5磁盘阵列,再用1块作为备份盘 mdadm -D /dev/md0 #查看磁盘阵列详细信息,显示3个盘为actvie,1个盘为spare,RAID类型为RAID 5 mkfs.ext4 /dev/md0 echo "/dev/md0 /RAID ext4 defaults 0 0" >> /etc/fstab #往/etc/fstab文件追加挂载信息,以实现永久挂载 mkdir /RAID mount -a mdadm /dev/md0 -f /dev/sdb #故意移除RAID 5阵列中的其中一个盘(active的盘) mdadm -D /dev/md0 #再查看磁盘阵列/dev/md0详细信息,显示备份盘自动定提上去并开始数据同步(spare rebuilding)。LVM逻辑卷管理器 部署逻辑卷
部署LVM时,需要逐个配置物理卷、卷组和逻辑卷。常用的部署命令如表所示。 部署逻辑卷步骤:(PV -> VG -> LV)
[root@zzy Desktop]# ls -l /dev/sd* brw-rw----. 1 root disk 8, 0 Oct 21 2020 /dev/sda brw-rw----. 1 root disk 8, 1 Oct 21 2020 /dev/sda1 brw-rw----. 1 root disk 8, 2 Oct 21 2020 /dev/sda2 brw-rw----. 1 root disk 8, 16 Oct 21 2020 /dev/sdb brw-rw----. 1 root disk 8, 17 Oct 21 2020 /dev/sdb1 brw-rw----. 1 root disk 8, 32 Oct 21 2020 /dev/sdc brw-rw----. 1 root disk 8, 48 Oct 21 2020 /dev/sdd [root@zzy Desktop]# pvcreat /dev/sd[c-d] bash: pvcreat: command not found... [root@zzy Desktop]# pvcreate /dev/sd[c-d] Physical volume "/dev/sdc" successfully created Physical volume "/dev/sdd" successfully created [root@zzy Desktop]# vgcreate qq /dev/sd[c-d] Volume group "qq" successfully created [root@zzy Desktop]# lvcreate -n ww -L 100M qq Logical volume "ww" created [root@zzy Desktop]# lvdisplay --- Logical volume --- LV Path /dev/rhel_zzy/swap LV Name swap VG Name rhel_zzy LV UUID pCibUQ-UJpr-ayel-VQ00-u20S-orF2-LDGjO4 LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/rhel_zzy/root LV Name root VG Name rhel_zzy LV UUID AYaN4k-d3jX-ATQK-P7nK-adrf-N5Ak-dRWeoR LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 1 LV Size 17.51 GiB Current LE 4482 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/qq/ww LV Name ww VG Name qq LV UUID prNTg3-XYv0-MmG6-QQfx-1OZo-1Kx3-9LLIkC LV Write Access read/write LV Creation host, time zzy, 2020-10-21 07:35:26 -0400 LV Status available # open 0 LV Size 100.00 MiB Current LE 25 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 [root@zzy Desktop]# mkfs mkfs mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat [root@zzy Desktop]# mkfs.ext4 /dev/qq/ww mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=1024 (log=0) Fragment size=1024 (log=0) Stride=0 blocks, Stripe width=0 blocks 25688 inodes, 102400 blocks 5120 blocks (5.00%) reserved for the super user First data block=1 Maximum filesystem blocks=33685504 13 block groups 8192 blocks per group, 8192 fragments per group 1976 inodes per group Superblock backups stored on blocks: 8193, 24577, 40961, 57345, 73729 Allocating group tables: done Writing inode tables: done Creating journal (4096 blocks): done Writing superblocks and filesystem accounting information: done [root@zzy Desktop]# mkdir /ee [root@zzy Desktop]# mount /dev/qq/ww /ee [root@zzy Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot /dev/mapper/qq-ww 93M 1.6M 85M 2% /ee [root@zzy Desktop]# vim /etc/fstab [root@zzy Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot /dev/mapper/qq-ww 93M 1.6M 85M 2% /ee扩容逻辑卷
扩容前,先卸载设备和挂载点的关联。
扩容逻辑卷(lvextend); 检查硬盘完整性(e2fsck),并重置硬盘容量(resize2fs); 重新挂载硬盘设备并查看挂载状态。mount -a df -h [root@zzy Desktop]# umount /ee [root@zzy Desktop]# e2fsck -f /dev/qq/ww e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/qq/ww: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks [root@zzy Desktop]# resize2fs /dev/qq/ww resize2fs 1.42.9 (28-Dec-2013) The filesystem is already 102400 blocks long. Nothing to do! [root@zzy Desktop]# lvextend -L 300M /dev/qq/ww Extending logical volume ww to 300.00 MiB Logical volume ww successfully resized [root@zzy Desktop]# e2fsck -f /dev/qq/ww e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/qq/ww: 11/25688 files (9.1% non-contiguous), 8896/102400 blocks [root@zzy Desktop]# resize2fs /dev/qq/ww resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/qq/ww to 307200 (1k) blocks. The filesystem on /dev/qq/ww is now 307200 blocks long. [root@zzy Desktop]# mount -a [root@zzy Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot /dev/mapper/qq-ww 287M 2.0M 266M 1% /ee缩小逻辑卷
缩容前,先卸载设备和挂载点的关联。
1. 检查硬盘完整性(e2fsck); 2. 先向系统报备一下即将要执行逻辑卷缩容的操作(resize2fs),再缩容逻辑卷(lvreduce); 3. 重新挂载硬盘设备并查看挂载状态。mount -a df -h [root@zzy Desktop]# umount /ee [root@zzy Desktop]# e2fsck -f /dev/qq/ww e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/qq/ww: 11/75088 files (9.1% non-contiguous), 15637/307200 blocks [root@zzy Desktop]# resize2fs /dev/qq/ww 50M resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/qq/ww to 51200 (1k) blocks. The filesystem on /dev/qq/ww is now 51200 blocks long. [root@zzy Desktop]# lvreduce -L 50M /dev/qq/ww Rounding size to boundary between physical extents: 52.00 MiB WARNING: Reducing active logical volume to 52.00 MiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce ww? [y/n]: y Reducing logical volume ww to 52.00 MiB Logical volume ww successfully resized [root@zzy Desktop]# e2fsck -f /dev/qq/ww e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/qq/ww: 11/13832 files (18.2% non-contiguous), 6886/51200 blocks [root@zzy Desktop]# resize2fs /dev/qq/ww 50M resize2fs 1.42.9 (28-Dec-2013) The filesystem is already 51200 blocks long. Nothing to do! [root@zzy Desktop]# mount -a [root@zzy Desktop]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot /dev/mapper/qq-ww 45M 1.1M 40M 3% /ee [root@zzy Desktop]#逻辑卷快照
LVM还具备有“快照卷”功能,该功能类似于虚拟机软件的还原时间点功能。例如,可以对某一个逻辑卷设备做一次快照,如果日后发现数据被改错了,就可以利用之前做好的快照卷进行覆盖还原。LVM的快照卷功能有两个特点:
1. 快照卷的容量必须等同于逻辑卷的容量; 2. 快照卷仅一次有效,一旦执行还原操作后则会被立即自动删除。往逻辑卷设备新建一个文件。然后为逻辑卷创建快照卷。再在逻辑卷目录生成一个垃圾文件。后先卸载逻辑卷与挂载点关联,再尝试快照还原操作(lvconvert --merge),再重新挂载,查看逻辑卷目录是否回到创建快照前的状态。
[root@zzy Desktop]# echo "蔺亚棋小姐姐" > /ee/rr [root@zzy Desktop]# cd /ee [root@zzy ee]# ls lost+found rr [root@zzy ee]# cat rr 蔺亚棋小姐姐 [root@zzy ee]# lvcreate -L 150M -s -n dd /dev/qq/ww Rounding up size to full physical extent 152.00 MiB Logical volume "dd" created [root@zzy ee]# lvdisplay --- Logical volume --- LV Path /dev/rhel_zzy/swap LV Name swap VG Name rhel_zzy LV UUID pCibUQ-UJpr-ayel-VQ00-u20S-orF2-LDGjO4 LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/rhel_zzy/root LV Name root VG Name rhel_zzy LV UUID AYaN4k-d3jX-ATQK-P7nK-adrf-N5Ak-dRWeoR LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 1 LV Size 17.51 GiB Current LE 4482 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/qq/ww LV Name ww VG Name qq LV UUID prNTg3-XYv0-MmG6-QQfx-1OZo-1Kx3-9LLIkC LV Write Access read/write LV Creation host, time zzy, 2020-10-21 07:35:26 -0400 LV snapshot status source of dd [active] LV Status available # open 1 LV Size 152.00 MiB Current LE 38 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/qq/dd LV Name dd VG Name qq LV UUID nbELql-H1GV-OYlH-fF8T-HCgU-ackJ-XVesGX LV Write Access read/write LV Creation host, time zzy, 2020-10-22 03:59:22 -0400 LV snapshot status active destination for ww LV Status available # open 0 LV Size 152.00 MiB Current LE 38 COW-table size 152.00 MiB COW-table LE 38 Allocated to snapshot 0.01% Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 [root@zzy ee]# cd ~ [root@zzy ~]# dd if=/dev/zero of=/ee/ff bs=100M count=1 1+0 records in 1+0 records out 104857600 bytes (105 MB) copied, 1.19315 s, 87.9 MB/s [root@zzy ~]# lvdisplay --- Logical volume --- LV Path /dev/rhel_zzy/swap LV Name swap VG Name rhel_zzy LV UUID pCibUQ-UJpr-ayel-VQ00-u20S-orF2-LDGjO4 LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 2 LV Size 2.00 GiB Current LE 512 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 --- Logical volume --- LV Path /dev/rhel_zzy/root LV Name root VG Name rhel_zzy LV UUID AYaN4k-d3jX-ATQK-P7nK-adrf-N5Ak-dRWeoR LV Write Access read/write LV Creation host, time localhost, 2020-09-14 11:26:59 -0400 LV Status available # open 1 LV Size 17.51 GiB Current LE 4482 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:1 --- Logical volume --- LV Path /dev/qq/ww LV Name ww VG Name qq LV UUID prNTg3-XYv0-MmG6-QQfx-1OZo-1Kx3-9LLIkC LV Write Access read/write LV Creation host, time zzy, 2020-10-21 07:35:26 -0400 LV snapshot status source of dd [active] LV Status available # open 1 LV Size 152.00 MiB Current LE 38 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/qq/dd LV Name dd VG Name qq LV UUID nbELql-H1GV-OYlH-fF8T-HCgU-ackJ-XVesGX LV Write Access read/write LV Creation host, time zzy, 2020-10-22 03:59:22 -0400 LV snapshot status active destination for ww LV Status available # open 0 LV Size 152.00 MiB Current LE 38 COW-table size 152.00 MiB COW-table LE 38 Allocated to snapshot 66.12% Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 [root@zzy ~]# umount /ee [root@zzy ~]# lvconvert --merge /dev/qq/dd Merging of volume dd started. ww: Merged: 38.6% ww: Merged: 100.0% Merge of snapshot into logical volume ww has finished. Logical volume "dd" successfully removed [root@zzy ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot [root@zzy ~]# mount -a [root@zzy ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/rhel_zzy-root 18G 2.9G 15G 17% / devtmpfs 985M 0 985M 0% /dev tmpfs 994M 140K 994M 1% /dev/shm tmpfs 994M 8.9M 986M 1% /run tmpfs 994M 0 994M 0% /sys/fs/cgroup /dev/sdb1 2.0G 33M 2.0G 2% /zzy /dev/sda1 497M 119M 379M 24% /boot /dev/mapper/qq-ww 142M 1.6M 130M 2% /ee [root@zzy ~]# ls /ee lost+found rr [root@zzy ~]# cat /ee/rr 蔺亚棋小姐姐删除逻辑卷
当生产环境中想要重新部署LVM或者不再需要使用LVM时,则需要执行LVM的删除操作。为此,需要提前备份好重要的数据信息,然后依次删除逻辑卷、卷组、物理卷设备,这个顺序不可颠倒。
删除逻辑卷
[root@zzy ~]# umount /ee [root@zzy ~]# lvremove /qq/ww "/qq/ww": Invalid path for Logical Volume [root@zzy ~]# vgremove qq Do you really want to remove volume group "qq" containing 1 logical volumes? [y/n]: y Do you really want to remove active logical volume ww? [y/n]: y Logical volume "ww" successfully removed Volume group "qq" successfully removed [root@zzy ~]# ls -l /dev/sd* brw-rw----. 1 root disk 8, 0 Oct 21 15:31 /dev/sda brw-rw----. 1 root disk 8, 1 Oct 21 15:31 /dev/sda1 brw-rw----. 1 root disk 8, 2 Oct 21 15:31 /dev/sda2 brw-rw----. 1 root disk 8, 16 Oct 21 15:31 /dev/sdb brw-rw----. 1 root disk 8, 17 Oct 21 15:31 /dev/sdb1 brw-rw----. 1 root disk 8, 32 Oct 22 04:15 /dev/sdc brw-rw----. 1 root disk 8, 48 Oct 22 04:15 /dev/sdd [root@zzy ~]# pvremove /dev/sdc /dev/sdd Labels on physical volume "/dev/sdc" successfully wiped Labels on physical volume "/dev/sdd" successfully wiped