Home > Article > Computer Tutorials > Basic steps to create lvm expansion
Use the df -Th command to view disk information. You can see that the current LVM logical volume capacity is 38G
[root@Z ~]# df -TH 文件系统类型容量已用可用 已用% 挂载点 /dev/mapper/centos-root xfs38G2.4G 36G7% / devtmpfsdevtmpfs1.1G 01.1G0% /dev tmpfs tmpfs 1.1G 01.1G0% /dev/shm tmpfs tmpfs 1.1G 10M1.1G1% /run tmpfs tmpfs 1.1G 01.1G0% /sys/fs/cgroup /dev/sda1 xfs 1.1G139M925M 14% /boot tmpfs tmpfs 208M 0208M0% /run/user/0
Looking at the disk partition, you can see that the sda disk is 64G, and the sda2 partition below is divided into 40G
[root@Z ~]# fdisk -l 磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x000b05f5 设备 BootStart EndBlocks IdSystem /dev/sda1 *2048 2099199 1048576 83Linux /dev/sda2 20992008388607940893440 8eLinux LVM 磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘 /dev/mapper/centos-swap:4160 MB, 4160749568 字节,8126464 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节
New disk partition sda3
[root@Z ~]# fdisk /dev/sda 欢迎使用 fdisk (util-linux 2.23.2)。 更改将停留在内存中,直到您决定将更改写入磁盘。 使用写入命令前请三思。 命令(输入 m 获取帮助):p (p是查看当前分区) 磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x000b05f5 设备 BootStart EndBlocks IdSystem /dev/sda1 *2048 2099199 1048576 83Linux /dev/sda2 20992008388607940893440 8eLinux LVM 命令(输入 m 获取帮助):n(n是新建分区) Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): (回车默认选择p主分区) Using default response p 分区号 (3,4,默认 3): (回车默认3) 起始 扇区 (83886080-125829119,默认为 83886080): 将使用默认值 83886080 Last 扇区, +扇区 or +size{K,M,G} (83886080-125829119,默认为 125829119):+19G(这里给新建分区19G) 分区 3 已设置为 Linux 类型,大小设为 19 GiB 命令(输入 m 获取帮助):t(指定类型) 分区号 (1-3,默认 3):(要指定的分区号,刚刚默认3,这里也默认3) Hex 代码(输入 L 列出所有代码):8e (8e代表LVM) 已将分区“Linux”的类型更改为“Linux LVM” 命令(输入 m 获取帮助):w(w保存) The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 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) 正在同步磁盘。 ### 注意:### 保存后,最后几行提示设备资源忙,这时同步不了磁盘 需要重启服务器或者执行partprobe,我这里是执行的命令,如果这里不做操作的话,下面格式化磁盘的时候会报错!!!
View partitions and format partitions
[root@Z ~]# fdisk -l 磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 磁盘标签类型:dos 磁盘标识符:0x000b05f5 设备 BootStart EndBlocks IdSystem /dev/sda1 *2048 2099199 1048576 83Linux /dev/sda2 20992008388607940893440 8eLinux LVM /dev/sda383886080 12373196719922944 8eLinux LVM 磁盘 /dev/mapper/centos-root:37.7 GB, 37706792960 字节,73646080 个扇区 Units = 扇区 of 1 * 512 = 512 bytes 扇区大小(逻辑/物理):512 字节 / 512 字节 I/O 大小(最小/最佳):512 字节 / 512 字节 ### 格式化分区 ### [root@Z ~]# mkfs.xfs -f /dev/sda3 meta-data=/dev/sda3isize=512agcount=4, agsize=1245184 blks = sectsz=512 attr=2, projid32bit=1 = crc=1finobt=0, sparse=0 data = bsize=4096 blocks=4980736, imaxpct=25 = sunit=0swidth=0 blks naming =version 2bsize=4096 ascii-ci=0 ftype=1 log=internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 不执行"注意"命令或不重启设备,格式化时报找不到设备: [root@Z ~]# mkfs.xfs-f/dev/sda3 /dev/sda3: 没有那个文件或目录
Convert the partition to a physical volume and use the pvdisplay command to view the physical volume information: pvdisplay
### 转换物理卷 ### [root@Z ~]# pvcreate /dev/sda3 WARNING: xfs signature detected on /dev/sda3 at offset 0. Wipe it? [y/n]: y Wiping xfs signature on /dev/sda3. Physical volume "/dev/sda3" successfully created. ### 查看物理卷 ### [root@Z ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <39.00 GiB / not usable 3.00 MiB Allocatable yes PE Size 4.00 MiB Total PE9983 Free PE 1 Allocated PE9982 PV UUID v5DSQS-Cumb-xtyK-BWsx-ayfS-T0iT-FC3rbE "/dev/sda3" is a new physical volume of "19.00 GiB" --- NEW Physical volume --- PV Name /dev/sda3 VG Name PV Size 19.00 GiB Allocatable NO PE Size 0 Total PE0 Free PE 0 Allocated PE0 PV UUID Xkbr0u-Ce9h-jvqW-JSZr-nwxY-qYNo-OSqqWm
Using vgdisplay, we see that the total capacity Alloc PE is 38.9, and the Free allocable capacity is gone
[root@Z ~]# vgdisplay --- Volume group --- VG Name centos System ID Formatlvm2 Metadata Areas1 Metadata Sequence No3 VG Access read/write VG Status resizable MAX LV0 Cur LV2 Open LV 2 Max PV0 Cur PV1 Act PV1 VG Size <39.00 GiB PE Size 4.00 MiB Total PE9983 Alloc PE / Size 9982 / 38.99 GiB FreePE / Size 1 / 4.00 MiB VG UUID hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
Add the physical volume just initialized to the physical volume of the primary partition: vgextend centos(vg name) /dev/sda3. After adding, check vgdisplay and you can see that the Free allocable capacity is 19G
[root@Z ~]# vgextend centos /dev/sda3 Volume group "centos" successfully extended [root@Z ~]# vgdisplay --- Volume group --- VG Name centos System ID Formatlvm2 Metadata Areas2 Metadata Sequence No4 VG Access read/write VG Status resizable MAX LV0 Cur LV2 Open LV 2 Max PV0 Cur PV2 Act PV2 VG Size 57.99 GiB PE Size 4.00 MiB Total PE14846 Alloc PE / Size 9982 / 38.99 GiB FreePE / Size 4864 / 19.00 GiB VG UUID hTO0Fm-c4q8-yboo-cSig-XLcK-CXo7-JoTInz
View logical volume LVM information: lvdisplay, mainly look at LV Path and LV Size
[root@Z ~]# lvdisplay --- Logical volume --- LV Path/dev/centos/swap LV Nameswap VG Namecentos LV UUIDvfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb LV Write Accessread/write LV Creation host, time localhost, 2022-03-30 16:58:54 +0800 LV Statusavailable # open 2 LV Size<3.88 GiB Current LE 992 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path/dev/centos/root LV Nameroot VG Namecentos LV UUID4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx LV Write Accessread/write LV Creation host, time localhost, 2022-03-30 16:58:54 +0800 LV Statusavailable # open 1 LV Size<35.12 GiB Current LE 8990 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
Use the lvextend command to expand the partition: lvextend -l 4864 /dev/centos/root. After the expansion, check the LV size using lvdisplay. The size 4864 is: the ID number of Free PE. Use the vgdisplay command to check the /dev/centos/root is: LV path. , view through lvdisplay command
[root@Z ~]# lvextend -l +4864 /dev/centos/root Size of logical volume centos/root changed from <35.12 GiB (8990 extents) to <54.12 GiB (13854 extents). Logical volume centos/root successfully resized. [root@Z ~]# lvdisplay --- Logical volume --- LV Path/dev/centos/swap LV Nameswap VG Namecentos LV UUIDvfUMhU-5XVp-s4uB-lVs3-436b-29Vh-StDsYb LV Write Accessread/write LV Creation host, time localhost, 2022-03-30 16:58:54 +0800 LV Statusavailable # open 2 LV Size<3.88 GiB Current LE 992 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path/dev/centos/root LV Nameroot VG Namecentos LV UUID4KsFu6-jk8H-WsUa-1RVM-2DUq-gyPE-Nb6Rcx LV Write Accessread/write LV Creation host, time localhost, 2022-03-30 16:58:54 +0800 LV Statusavailable # open 1 LV Size<54.12 GiB Current LE 13854 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
Mount: xfs_growfs /dev/centos/root; The disk type is xfs, so use: xfs_frowfs command. If the disk type is ext4, use: resize2fs command [resize2fs /dev/centos/root]
[root@Z ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=512agcount=4, agsize=2301440 blks = sectsz=512 attr=2, projid32bit=1 = crc=1finobt=0 spinodes=0 data = bsize=4096 blocks=9205760, imaxpct=25 = sunit=0swidth=0 blks naming =version 2bsize=4096 ascii-ci=0 ftype=1 log=internal bsize=4096 blocks=4495, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 9205760 to 14186496
df -TH View disk information, expansion has been completed
[root@Z ~]# df -TH 文件系统类型容量已用可用 已用% 挂载点 /dev/mapper/centos-root xfs59G2.4G 56G5% / devtmpfsdevtmpfs1.1G 01.1G0% /dev tmpfs tmpfs 1.1G 01.1G0% /dev/shm tmpfs tmpfs 1.1G 10M1.1G1% /run tmpfs tmpfs 1.1G 01.1G0% /sys/fs/cgroup /dev/sda1 xfs 1.1G139M925M 14% /boot tmpfs tmpfs 208M 0208M0% /run/user/0
Summary of LVM working principle
First create the partition into a PV (physical volume) according to the LVM format, add the physical volume to the volume group (VG), and then separate the LV (logical volume) through the logical volume to achieve partition expansion, so use LVM Expansion is actually to expand the separated logical volume to the root partition.
Summary of steps on how to use LVM to expand capacity
Prepare a new disk with good partitions → modify the disk type to 8e → create a logical volume (PV) (use the newly added disk partition as a physical volume) → create a volume group (VG) → create a logical volume (LV) → Format and mount logical volumes → achieve capacity expansion
Simply put:
PV: 是物理的磁盘分区 VG: LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘 LV: 从VG中划分的逻辑分区
Overview Steps
#把硬盘分区创建成pv pvresize /dev/sdb1 (新分区) #把创建成pv的分区 加入vg vgextend centos/dev/sdb1(新分区) # 给vg中的/dev/centos/home 扩容 lvextend -L +500G /dev/centos/home #调整XFS文件系统以利用新增的空间。使用xfs_growfs命令来调整XFS文件系统的大小 xfs_growfs /dev/centos/home(xfs) #调整文件系统大小以利用新增的空间。如果逻辑卷上运行的是ext2、ext3或ext4文件系统,可以使用resize2fs命令调整文件系统大小 resize2fs /dev/centos/home(ext4)
The above is the detailed content of Basic steps to create lvm expansion. For more information, please follow other related articles on the PHP Chinese website!