LVM logical volume expansion and reduction disk array + backup disk physical volume merge volume group!

Disk array + backup disk

When deploying a RAID 5 disk array, at least 3 hard disks are required, and another backup hard disk is required.
Restore the virtual machine and deploy a RAID 5 + 1 backup disk.

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 Logical Volume Manager

LVM (Logical Volume Manager) allows users to dynamically adjust hard disk resources. (Let users flexibly change the size of the partition)

The logical volume manager is a mechanism used by the Linux system to manage hard disk partitions. It is theoretically strong. The original intention of its creation is to solve the defect that the hard disk device is not easy to modify the partition size after the partition is created. Although it is theoretically feasible to force a traditional hard disk partition to expand or shrink, it may cause data loss. The LVM technology adds a logical layer between the hard disk partition and the file system. It provides an abstract volume group that can combine multiple hard disks. In this way, users do not need to care about the underlying structure and layout of the physical hard disk device, and can realize dynamic adjustment of hard disk partitions. The technical architecture of LVM is shown in the figure.
Insert picture description here

The core idea of ​​LVM

物理卷处于LVM中的最底层,可以将其理解为物理硬盘、硬盘分区或者RAID磁盘阵列,这都可以。
卷组建立在物理卷之上,一个卷组可以包含多个物理卷,而且在卷组创建之后也可以继续向其中添加新的物理卷。
逻辑卷是用卷组中空闲的资源建立的,并且逻辑卷在建立后可以动态地扩展或缩小空间。

Deploy logical volume

When deploying LVM, you need to configure physical volumes, volume groups, and logical volumes one by one. The commonly used deployment commands are shown in the table.
Insert picture description here

Steps to deploy logical volume: (PV -> VG -> LV)

让硬盘设备支持LVM技术(pvcreate)。
把硬盘设备加入到卷组(vgcreate)。
从卷组中切割一定空间作为逻辑卷(lvcreate)。
把生成好的逻辑卷进行格式化,然后挂载使用(mkfs,mount,/etc/fstab)。
pvcreate /dev/sdb /dev/sdc                                            #创建物理卷,让硬盘设备支持LVM
vgcreate storage /dev/sdb /dev/sdc                                    #把创建好的物理卷组成卷组,命名为storge
vgdisplay                                                             #查看卷组状态
lvcreate -n vo -l 40 storage                                          #从卷组storge切割出40个基本单元(默认大小4M)创建一个160M的逻辑卷,命名为vo。-l 40效果等同于-L 160M
lvdisplay                                                             #查看逻辑卷状态
mkfs.ext4 /dev/storage/vo                                             #格式化逻辑卷vo
echo "/dev/storage/vo /linuxprobe ext4 defaults 0 0" >> /etc/fstab    #往/etc/fatab文件追加挂载信息,实现永久挂载
mkdir /linuxprobe                                                     #创建挂载点
mount -a                                                              #挂载
df -h                                                                 #查看挂载状态

linux system operation experiment:

Combine devsdc devsdd into zhuxing and add zhuxing to logical volume zhu

After success, mount a volume composition normally!

[root@lizhiqiang Desktop]# pvcreate /dev/sd[c-d]
  Physical volume "/dev/sdc" successfully created
  Physical volume "/dev/sdd" successfully created
[root@lizhiqiang Desktop]# vgcreate zhuxing /dev/sd[c-d]
  Volume group "zhuxing" successfully created
[root@lizhiqiang Desktop]# lvcreate -n zhu -L 300M /dev/zhuxing
  Logical volume "zhu" created
[root@lizhiqiang Desktop]# mkfs.ext4 /dev/zhuxing/zhu
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
76912 inodes, 307200 blocks
15360 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33947648
38 block groups
8192 blocks per group, 8192 fragments per group
2024 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185

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

[root@lizhiqiang Desktop]# cd /
[root@lizhiqiang /]# mkdir zhuxx
[root@lizhiqiang /]# mount /dev/zhuxing/zhu /zhuxx
[root@lizhiqiang /]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_lizhiqiang-root   18G  3.0G   15G  17% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  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% /opo
/dev/sda1                         497M  119M  379M  24% /boot
/dev/mapper/zhuxing-zhu           283M  2.1M  262M   1% /zhuxx

Expand logical volume

Before expansion, first unmount the association between the device and the mount point.

扩容逻辑卷(lvextend);
检查硬盘完整性(e2fsck),并重置硬盘容量(resize2fs);
重新挂载硬盘设备并查看挂载状态。mount -a   df -h

Unmount the volume group first, use e2fsck /dev/zhuxing/zhu to check the integrity, and then expand the capacity. After the expansion, use resize2fs to reset the hard disk capacity. After the volume group is successfully mounted, the volume group is mounted. After checking, it shows that 477M is a normal phenomenon. The test is successful!

[root@lizhiqiang /]# umount /zhuxx
[root@lizhiqiang /]# e2fsck /dev/zhuxing/zhu
e2fsck 1.42.9 (28-Dec-2013)
/dev/zhuxing/zhu: clean, 11/76912 files, 19977/307200 blocks
[root@lizhiqiang /]# lvextend -L 500M /dev/zhuxing/zhu
  Extending logical volume zhu to 500.00 MiB
  Logical volume zhu successfully resized
[root@lizhiqiang /]# mount /dev/zhuxing/zhu /zhuxx
[root@lizhiqiang /]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_lizhiqiang-root   18G  3.0G   15G  17% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  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% /opo
/dev/sda1                         497M  119M  379M  24% /boot
/dev/mapper/zhuxing-zhu           283M  2.1M  262M   1% /zhuxx
[root@lizhiqiang /]# resize2fs /dev/zhuxing/zhu
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/zhuxing/zhu is mounted on /zhuxx; on-line resizing required
old_desc_blocks = 3, new_desc_blocks = 4
The filesystem on /dev/zhuxing/zhu is now 512000 blocks long.

[root@lizhiqiang /]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_lizhiqiang-root   18G  3.0G   15G  17% /
devtmpfs                          985M     0  985M   0% /dev
tmpfs                             994M   84K  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% /opo
/dev/sda1                         497M  119M  379M  24% /boot
/dev/mapper/zhuxing-zhu           477M  2.3M  448M   1% /zhuxx

Shrink logical volume

Before shrinking, first unmount the association between the device and the mount point.

After the uninstallation is complete, check the integrity of the hard disk, use the e2fsck -f command and add the f parameter, and use resize2fs /dev/zhuxing/zhu [reduced space] after completion,

After the completion, use the lvreduce command to make him completely shrink, then mount the volume group, check after success, and the shrink experiment is complete! ! ! ! !

[root@lizhiqiang /]# umount /zhuxx
[root@lizhiqiang /]# e2fsck -f /dev/zhuxing/zhu
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/zhuxing/zhu: 11/129024 files (0.0% non-contiguous), 22696/512000 blocks
[root@lizhiqiang /]# resize2fs /dev/zhuxing/zhu 200M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/zhuxing/zhu to 204800 (1k) blocks.
The filesystem on /dev/zhuxing/zhu is now 204800 blocks long.

[root@lizhiqiang /]# lvreduce -L 200M /dev/zhuxing/zhu
  WARNING: Reducing active logical volume to 200.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce zhu? [y/n]: y
  Reducing logical volume zhu to 200.00 MiB
  Logical volume zhu successfully resized
[root@lizhiqiang /]# mount -a
[root@lizhiqiang /]# df -h
Filesystem                        Size  Used Avail Use% Mounted on
/dev/mapper/rhel_lizhiqiang-root   18G  3.0G   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% /opo
/dev/sda1                         497M  119M  379M  24% /boot
/dev/mapper/zhuxing-zhu           190M  1.6M  176M   1% /zhuxx

Guess you like

Origin blog.csdn.net/SYH885/article/details/109207444