[Solution] Linux Disk Expansion Encyclopedia: New Disk, Original Disk Expansion, Home Partition Root Partition Expansion

1. Background

        In the use of Linux, the disk space is often insufficient, and the disk needs to be expanded. Summarize and organize according to your own experience, a variety of scenarios, to help those in need and for reference

The commands executed in this article are all run in the centos7 environment. The ideas in other environments are similar, and the commands are basically the same.

Partition description:

sda is a separate disk with a total size of 50G. sda1 and sda2 are two partitions . There are three logical partitions under sda2: centos-root, centos-home, and centos-swap (the mount point that can be seen when using the df command); 

The mount point corresponding to  centos-root  is the root directory

The mount point corresponding to centos-home is /home

The name of the volume group is centos ( viewed through pvdisplay )

[root@vm-210 templates]# lsblk 
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   50G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   49G  0 part 
  ├─centos-root 253:0    0   40G  0 lvm  /
  ├─centos-home 253:1    0    7G  0 lvm  /home
  └─centos-swap 253:2    0    2G  0 lvm  [SWAP]
[root@vm-210 templates]# pvdisplay 
  --- Physical volume ---
  PV Name               /dev/sda2
  #卷组名称  
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               1Fpioh-HkxD-f4Pd-78An-on4A-vGS1-B9UXr4

Two, the scene

Refer to the article for the following problems: http://t.csdn.cn/cbViW

The expansion disk does not display the expanded disk or disk size in two cases:
                Case 1: Modify the original disk size
                Case 2: Add a new disk

Scenario 1: Move the centos-home partition to the root partition centos-root

        When partitioning, the centos-home partition space is very large, and the centos-root partition is very small. It is necessary to delete the centos-home partition and expand to the centos-root partition

Operation ideas ( commands used in brackets ):

  1. Back up the files under /home first
  2. Unmount ( umount ) /home (Note: If you cannot unmount, please close all processes using /home. Refer to http://t.csdn.cn/cbViW )
  3. Remove ( lvremove ) the home partition from the logical volume
  4. Expansion: Extend the vacated space ( lvextend ) to the centos-root partition
  5. enable extensions ( xfs_growfs )
  6. Comment out the boot mount entry in fstab
  7. Restore backup data to /home

The detailed code is as follows:

#!/bin/bash
#【功能】 将centos-home分区的空间移动到根centos-root分区下

#1.先备份 /home下文件: 将/home下的所有内容备份到/backup下,下面的命令自动创建/backup目录
mkdir /backup && mv /home/*  /backup

# 2.卸载 centos-home 
umount /home 
   ##  如提示失败则用下面的命令终止/home 分区下的进程 再进行卸载
   ##  fuser 安装方法  yum install -y psmisc
   ##  fuser -m -v -i -k /home
   ##  判断是哪个进程占用了此目录,请kill掉那个进程也可以

# 3.从逻辑卷中删除(lvremove)  centos-home 分区
echo y|lvremove /dev/mapper/centos-home 

# 4.扩展:将腾出的空间扩展(lvextend)到 centos-root分区下
lvextend -l+100%FREE /dev/mapper/centos-root 

# 5.使扩展生效(xfs_growfs)
xfs_growfs /dev/mapper/centos-root 

# 6.注释掉fstab中的启动挂载项
sed -i 's;/dev/mapper/centos-home;#/dev/mapper/centos-home;g' /etc/fstab 

# 7.恢复 原来home下的数据 并删除/backup
mv /backup/* /home  && rm -rf /backup

# 8.查看
df -H

illustrate:

        The idea is the same, if it is another partition, please modify the partition directory inside

Scenario 2: Add a new disk and expand it to the root directory or home

        The disk space is insufficient, and a new hard disk is added. For example, the name of a new disk is /dev/sdb

Operation ideas ( commands used in brackets ):

  1. View disk conditions ( lsblk, fdisk -l, pvdisplay )
  2. Create a physical volume ( pvcreate )
  3. Extend a disk to a volume group ( vgextend )
  4. Extend logical partition ( lvextend )
  5. enable extensions ( xfs_growfs )
  6. View ( df -H )
# 1.查看磁盘情况(lsblk、fdisk -l、pvdisplay)
lsblk

# 2.创建物理卷(pvcreate)
pvcreate /dev/sdb

# 3.将磁盘扩展到卷组(vgextend)
vgextend centos /dev/sdb

# 4.扩展逻辑分区(lvextend)
#   根据自己的情况选择扩容到centos-root
#                      还是centos-home逻辑分区,请修改注释
# 100%是将所有的磁盘空间扩容到指定分区,也可以根据需要进行调整比例
#      也支持指定大小比如10G等等(指定固定值时参数-l需要修改为-L)
lvextend -l+100%FREE /dev/mapper/centos-root
#lvextend -l+100%FREE /dev/mapper/centos-home

# 5.使扩展生效(xfs_growfs)根据扩容的分区进行选择使用
# xfs_growfs /dev/mapper/centos-home
xfs_growfs /dev/mapper/centos-root

# 6.查看(df -H)
df -H

    Scenario 3: Increase space on the original disk (often in a virtualized environment)

        In a virtualization or hyper-converged environment, or in a VMware virtual machine; the disk space is insufficient, and the size of the existing disk is increased, and the increased space is expanded to the root directory or centos-home.

Operation ideas ( commands used in brackets ):

Method 1 : partition with fdisk, the operation steps are basically the same as Scenario 2, with one more step of partitioning

  1. View disk conditions ( lsblk, fdisk -l, pvdisplay )
  2. create partition ( fdisk diskname)
  3. Create a physical volume ( pvcreate )
  4. Extend a disk to a volume group ( vgextend )
  5. Extend logical partition ( lvextend )
  6. enable extensions ( xfs_growfs )
  7. View ( df -H )

Method 2 : Resize with the parted command

  1. Check disk status ( parted  /dev/sda print free )
  2. Allocate remaining free space ( parted /dev/sda resizepart 2 100% )
  3. Refresh the physical volume ( pvresize /dev/sda2 )
  4. Extend logical partition ( lvextend )
  5. enable extensions ( xfs_growfs )
  6. View ( df -H )

 Method 1: partition with fdisk, the operation steps are basically the same as Scenario 2, with one more step of partitioning

# 1.创建分区(fdisk)
fdisk  /dev/sda
#在进入命令环境中,按n键  一路按回车,最后一步按w键保存,最后按q键退出

# 2.创建物理卷(pvcreate) sda3根据第一步中创建的分区名称进行替换
pvcreate /dev/sda3

# 3.将磁盘扩展到卷组(vgextend)  sda3根据第一步中创建的分区名称进行替换
vgextend centos /dev/sda3

# 4.扩展逻辑分区(lvextend)
#   根据自己的情况选择扩容到centos-root
#                      还是centos-home逻辑分区,请修改注释
# 100%是将所有的磁盘空间扩容到指定分区,也可以根据需要进行调整比例
#      也支持指定大小比如10G等等(指定固定值时参数-l需要修改为-L)
lvextend -l+100%FREE /dev/mapper/centos-root
#lvextend -l+100%FREE /dev/mapper/centos-home

# 5.使扩展生效(xfs_growfs)根据扩容的分区进行选择使用
# xfs_growfs /dev/mapper/centos-home
xfs_growfs /dev/mapper/centos-root

# 6.查看(df -H)
df -H

   Method 2: Resize with the parted command

Example: The system has only one disk and two partitions: the newly expanded disk space needs to be expanded to partition 2. The number in the figure below is 2, which actually corresponds to /dev/sda2

[root@vm-210 templates]# parted /dev/sda  print free
Model: ATA QEMU HARDDISK (scsi)
Disk /dev/sda: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
        32.3kB  1049kB  1016kB           Free Space
 1      1049kB  1075MB  1074MB  primary  xfs          boot
 2      1075MB  53.7GB  52.6GB  primary               lvm
方法二:用parted命令进行操作

#1. 查看磁盘情况:打印可用空间
parted /dev/sda  print free

#2.分配剩余的可用空间
#将剩余的可用空间分配到 /dev/sda2,根据实际修改磁盘
# resizepart 中的2 只的是第二个分区即:/dev/sda2 
#             100% 将所有的空闲空间分配给/dev/sda2,也可以用单位和百分比
parted /dev/sda resizepart 2 100%

# 3.刷新物理卷
#分区的空间修改了,也要刷新一下pv物理卷的大小,这样pv才能识别变动的空间
pvresize /dev/sda2

# 4.查看物理卷是否变化
#查看pv中free 项中是否显示了可用的空间
pvdisplay

# 5.扩展逻辑分区
#开始增加到逻辑卷中
lvextend -l+100%FREE /dev/mapper/centos-root

# 6.使扩展生效(xfs_growfs)
xfs_growfs /dev/mapper/centos-root

# 7.查看(df -H)
df -H

3. Summary

        I have summarized the problems of disk expansion encountered in my work. According to my command, you can execute it step by step. If you want to know more, you need to know what is a physical volume (pv), logical volume, logical grouping, logical partition, etc.
       

Guess you like

Origin blog.csdn.net/yy4545/article/details/125873011