> 文章列表 > linux 给根目录扩容(lvm CentOS 7.6 )

linux 给根目录扩容(lvm CentOS 7.6 )

linux 给根目录扩容(lvm CentOS 7.6 )

问题:Linux系统挂载到根目录的磁盘空间满了,如何扩容? 

命令:lsblk 可以查看磁盘和分区情况,可以发现磁盘vda下面的还有大部分空间没有使用。

操作步骤

1、使用 fdisk -l 查看硬盘序号,并用 fdisk 对硬盘操作,格式化成lvm的格式

(用命令lsblk可以看到,挂载到根目录的分区是lvm。)

(并且系统有一块虚拟磁盘vda,有2个分区vda1 和 vda2)

(fdisk -l   查看硬盘进行分区。)

[root@host-172-20-1-113 ~]# lsblk
NAME            MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda             252:0    0   1T  0 disk 
├─vda1          252:1    0   1G  0 part /boot
└─vda2          252:2    0  49G  0 part ├─centos-root 253:0    0  44G  0 lvm  /└─centos-swap 253:1    0   5G  0 lvm  [SWAP]
[root@host-172-20-1-113 ~]# fdisk -l      #查看硬盘分区磁盘 /dev/vda:1099.5 GB, 1099511627776 字节,2147483648 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00095e6c设备 Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857599    51379200   8e  Linux LVM磁盘 /dev/mapper/centos-root:47.2 GB, 47240445952 字节,92266496 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节磁盘 /dev/mapper/centos-swap:5368 MB, 5368709120 字节,10485760 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节

 用 fdisk 对硬盘vda进行分区操作:fdisk /dev/vda   

[root@host-172-20-1-113 ~]# fdisk /dev/vda   #对硬盘vda进行分区操作
欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。命令(输入 m 获取帮助):n   #n 代表新建new
Partition type:p   primary (2 primary, 0 extended, 2 free)e   extended
Select (default p): p     #前面已有2个分区,所以分区从3开始
分区号 (3,4,默认 3):3
起始 扇区 (104857600-2147483647,默认为 104857600):   #直接回车,选择默认值
将使用默认值 104857600
Last 扇区, +扇区 or +size{K,M,G} (104857600-2147483647,默认为 2147483647):
将使用默认值 2147483647  #直接回车,选择默认值
分区 3 已设置为 Linux 类型,大小设为 974 GiB命令(输入 m 获取帮助):t  #t 代表 change a partition's system id
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e  #8e 代表Linux LVM
已将分区“Linux”的类型更改为“Linux LVM”命令(输入 m 获取帮助):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)
正在同步磁盘。

2、重新识别磁盘。命令:partprobe

以及用lsblk查看已经给增加了一个分区vda3,并且有974G的空间

[root@host-172-20-1-113 ~]# partprobe
[root@host-172-20-1-113 ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda             252:0    0    1T  0 disk 
├─vda1          252:1    0    1G  0 part /boot
├─vda2          252:2    0   49G  0 part 
│ ├─centos-root 253:0    0   44G  0 lvm  /
│ └─centos-swap 253:1    0    5G  0 lvm  [SWAP]
└─vda3          252:3    0  974G  0 part 

3.将刚刚的硬盘重新格式化,格式化为xfs格式;命令 mkfs.xfs /dev/vda3

备注:系统是CentOS 7.6的系统,所以格式化的格式是xfs

[root@host-172-20-1-113 ~]# mkfs.xfs /dev/vda3
meta-data=/dev/vda3              isize=512    agcount=4, agsize=63832064 blks=                       sectsz=512   attr=2, projid32bit=1=                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=255328256, imaxpct=25=                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=124672, version=2=                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

4.使用lvm命令,对根目录进行扩容

4.1 进入lvm模式。用pvdisplay查看物理卷详情,发现只有1个物理卷 /dev/vda2

[root@host-172-20-1-113 ~]# lvm
lvm> pvdisplay--- Physical volume ---PV Name               /dev/vda2VG Name               centosPV Size               <49.00 GiB / not usable 3.00 MiBAllocatable           yes (but full)PE Size               4.00 MiBTotal PE              12543Free PE               0Allocated PE          12543PV UUID               N3THeY-Gw8G-ejIj-S6S0-UoXL-j5BS-3c0Gwd

4.2 新建pv卷。命令:pvcreate /dev/vda3  (创建名称叫vda3的pv卷)

lvm> pvcreate /dev/vda3
WARNING: xfs signature detected on /dev/vda3 at offset 0. Wipe it? [y/n]: yWiping xfs signature on /dev/vda3.Physical volume "/dev/vda3" successfully created.

4.3 再次查看pv卷,已经有两个pv卷了。可以创建看到第2个pv卷就是刚刚新建的vda3 

lvm> pvdisplay--- Physical volume ---PV Name               /dev/vda2VG Name               centosPV Size               <49.00 GiB / not usable 3.00 MiBAllocatable           yes (but full)PE Size               4.00 MiBTotal PE              12543Free PE               0Allocated PE          12543PV UUID               N3THeY-Gw8G-ejIj-S6S0-UoXL-j5BS-3c0Gwd"/dev/vda3" is a new physical volume of "974.00 GiB"--- NEW Physical volume ---PV Name               /dev/vda3VG Name               PV Size               974.00 GiBAllocatable           NOPE Size               0   Total PE              0Free PE               0Allocated PE          0PV UUID               ZIf1J5-ZjuB-kGiC-Gpqn-APLT-PWQO-XKCCjL

4.4 查看vg组。命令:vgdisplay   。可以发现VG Name  叫 centos

lvm> vgdisplay--- Volume group ---VG Name               centosSystem ID             Format                lvm2Metadata Areas        1Metadata Sequence No  3VG Access             read/writeVG Status             resizableMAX LV                0Cur LV                2Open LV               2Max PV                0Cur PV                1Act PV                1VG Size               <49.00 GiBPE Size               4.00 MiBTotal PE              12543Alloc PE / Size       12543 / <49.00 GiBFree  PE / Size       0 / 0   VG UUID               nYUiIj-mzYH-AT5v-7ZeX-HTU9-TR8v-xKcSRB

4.5 将刚刚生成的pv卷 /dev/vda3 加入到根目录所在的vg组:centos

命令:vgextend centos /dev/vda3

备注:这里在扩容另外一台服务器的时候碰到了一个问题(本台服务器没碰到):挂载根目录的磁盘使用100%,需要把根目录的文件移走到/boot 目录下,再进行加入组。

lvm> vgextend centos /dev/vda3Volume group "centos" successfully extended

4.6 再次查看,根目录所在的vg组:centos 已经扩容成功

lvm> vgdisplay--- Volume group ---VG Name               centosSystem ID             Format                lvm2Metadata Areas        2Metadata Sequence No  4VG Access             read/writeVG Status             resizableMAX LV                0Cur LV                2Open LV               2Max PV                0Cur PV                2Act PV                2VG Size               1022.99 GiBPE Size               4.00 MiBTotal PE              261886Alloc PE / Size       12543 / <49.00 GiBFree  PE / Size       249343 / <974.00 GiBVG UUID               nYUiIj-mzYH-AT5v-7ZeX-HTU9-TR8v-xKcSRB# 可以看到 Free PE / Size   249343 / <974.00 GiB   已经有974G了,
# 注意249343这个是PE值,下面会用到。

4.7 查看lv卷。命令:lvdisplay

lvm> lvdisplay--- Logical volume ---LV Path                /dev/centos/swapLV Name                swapVG Name                centosLV UUID                3wMMor-zryN-fe8u-1uhC-G5df-1IiX-rC3v2yLV Write Access        read/writeLV Creation host, time localhost, 2022-09-03 16:59:23 +0800LV Status              available# open                 2LV Size                5.00 GiBCurrent LE             1280Segments               1Allocation             inheritRead ahead sectors     auto- currently set to     8192Block device           253:1--- Logical volume ---LV Path                /dev/centos/rootLV Name                rootVG Name                centosLV UUID                qFWSSU-jttE-dRZD-3S6g-2bnv-hVkZ-ENLM3PLV Write Access        read/writeLV Creation host, time localhost, 2022-09-03 16:59:23 +0800LV Status              available# open                 1LV Size                <44.00 GiBCurrent LE             11263Segments               1Allocation             inheritRead ahead sectors     auto- currently set to     8192Block device           253:0

4.8 使用 lvextend 为根目录进行扩容。命令: lvextend -l +249343 /dev/centos/root

备注:这个239343 就是上面vgdisplay下展示的 Free PE值。

也可以直接用:lvextend -L +974G /dev/centos/root  也可以

lvm> lvextend -l +249343 /dev/centos/rootSize of logical volume centos/root 
changed from <44.00 GiB (11263 extents) to 1017.99 GiB (260606 extents).Logical volume centos/root successfully resized.#备注:这个249349 就是前面PE值
#这里还可以用另外一个方法,直接指定扩展大小:
# lvextend -L +974G /dev/centos/root

4.9 再次查看lv卷,LV Size 已经增加了974 GiB。命令:lvdisplay

lvm> lvdisplay--- Logical volume ---LV Path                /dev/centos/swapLV Name                swapVG Name                centosLV UUID                3wMMor-zryN-fe8u-1uhC-G5df-1IiX-rC3v2yLV Write Access        read/writeLV Creation host, time localhost, 2022-09-03 16:59:23 +0800LV Status              available# open                 2LV Size                5.00 GiBCurrent LE             1280Segments               1Allocation             inheritRead ahead sectors     auto- currently set to     8192Block device           253:1--- Logical volume ---LV Path                /dev/centos/rootLV Name                rootVG Name                centosLV UUID                qFWSSU-jttE-dRZD-3S6g-2bnv-hVkZ-ENLM3PLV Write Access        read/writeLV Creation host, time localhost, 2022-09-03 16:59:23 +0800LV Status              available# open                 1LV Size                1017.99 GiBCurrent LE             260606Segments               2Allocation             inheritRead ahead sectors     auto- currently set to     8192Block device           253:0
lvm> exitExiting.

4.10 扩容文件系统。命令: xfs_growfs /dev/mapper/centos-root

[root@host-172-20-1-113 ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=2883328 blks=                       sectsz=512   attr=2, projid32bit=1=                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=11533312, imaxpct=25=                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=5631, version=2=                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 11533312 to 266860544

4.11 扩容成功。

用df -h 查看,挂载到根目录的系统已经有1017G了,扩容成功。

[root@host-172-20-1-113 ~]# df -h
文件系统                 容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root 1018G  2.0G 1017G    1% /
devtmpfs                  16G     0   16G    0% /dev
tmpfs                     16G     0   16G    0% /dev/shm
tmpfs                     16G   25M   16G    1% /run
tmpfs                     16G     0   16G    0% /sys/fs/cgroup
/dev/vda1               1014M  166M  849M   17% /boot
tmpfs                    3.2G     0  3.2G    0% /run/user/0