CentOS7 基于lvm扩容根目录
Linux操作系统安装好之后,可以通过 df -h 命令查看磁盘挂载信息和空间使用率
[root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 7.8G 0 7.8G 0% /dev tmpfs 7.8G 60K 7.8G 1% /dev/shm tmpfs 7.8G 9.0M 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/mapper/centos-root 46G 6.6G 40G 15% / /dev/sda1 197M 166M 32M 84% /boot tmpfs 1.6G 0 1.6G 0% /run/user/1000 tmpfs 1.6G 0 1.6G 0% /run/user/0
如上所示,磁盘根目录只有不到50G,根据实际业务需要将磁盘空间扩容到200G,如果是物理机只能增加磁盘,如果是虚拟机则可以直接扩容。
具体操作步骤如下
1.分区 fdisk命令
[root@localhost ~]# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n #创建一个新分区
分区创建后需要将新的分区信息刷新到内核中,可执行两次,确保生效
[root@localhost ~]# partprobe
查看分区信息
[root@localhost ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 200G 0 disk 忖sda1 8:1 0 200M 0 part /boot 忖sda2 8:2 0 49.8G 0 part ?忖centos-root 253:0 0 45.8G 0 lvm / ?忖centos-swap 253:1 0 4G 0 lvm [SWAP] 忖sda3 8:3 0 150G 0 part sr0 11:0 1 1024M 0 rom [root@localhost ~]# fdisk -l Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000a3b4c Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 104857599 52222976 8e Linux LVM /dev/sda3 104857600 419430399 157286400 8e Linux LVM
2.格式化 /dev/sda3
[root@localhost ~]# mkfs.xfs /dev/sda3 meta-data=/dev/sda3 isize=512 agcount=4, agsize=9830400 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=39321600, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=19200, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
3.创建物理卷pv
[root@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size 49.80 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 12749 Free PE 0 Allocated PE 12749 PV UUID 7EnsrT-fESl-zmbK-3F1p-Y3rH-QZIz-m9oe1T [root@localhost ~]# 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@localhost ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos PV Size 49.80 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 12749 Free PE 0 Allocated PE 12749 PV UUID 7EnsrT-fESl-zmbK-3F1p-Y3rH-QZIz-m9oe1T --- Physical volume --- PV Name /dev/sda3 VG Name centos PV Size 150.00 GiB / not usable 4.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 38399 Free PE 0 Allocated PE 38399 PV UUID YRcEFb-fW4e-GOKO-jjK2-QGyS-SRQ7-Uv3ZQu
4.将物理卷添加到卷组vg
[root@localhost ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 49.80 GiB PE Size 4.00 MiB Total PE 12749 Alloc PE / Size 12749 / 49.80 GiB Free PE / Size 0 / 0 VG UUID chkr0E-Tgym-oiZ3-3fsp-Fe4C-3EOT-fcZGiL [root@localhost ~]# vgextend centos /dev/sda3 Volume group "centos" successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 4 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 2 Act PV 2 VG Size <199.80 GiB PE Size 4.00 MiB Total PE 51148 Alloc PE / Size 12749 / 49.80 GiB Free PE / Size 38399 / <150.00 GiB VG UUID chkr0E-Tgym-oiZ3-3fsp-Fe4C-3EOT-fcZGiL
5.扩容逻辑卷lv
[root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID sFhcD2-fltF-S1sY-p3Gu-OR2j-Entv-HJ1u0f LV Write Access read/write LV Creation host, time localhost, 2018-11-24 00:21:24 +0800 LV Status available # open 2 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID VB75Pk-eCAN-gZtR-HboO-PBNF-1Kf9-Esw3dX LV Write Access read/write LV Creation host, time localhost, 2018-11-24 00:21:24 +0800 LV Status available # open 1 LV Size 45.80 GiB Current LE 11725 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 [root@localhost ~]# lvextend -l +100%FREE /dev/centos/root Size of logical volume centos/root changed from 45.80 GiB (11725 extents) to <195.80 GiB (50124 extents). Logical volume centos/root successfully resized. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/centos/swap LV Name swap VG Name centos LV UUID sFhcD2-fltF-S1sY-p3Gu-OR2j-Entv-HJ1u0f LV Write Access read/write LV Creation host, time localhost, 2018-11-24 00:21:24 +0800 LV Status available # open 2 LV Size 4.00 GiB Current LE 1024 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID VB75Pk-eCAN-gZtR-HboO-PBNF-1Kf9-Esw3dX LV Write Access read/write LV Creation host, time localhost, 2018-11-24 00:21:24 +0800 LV Status available # open 1 LV Size <195.80 GiB Current LE 50124 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
6.重新识别分区
[root@localhost ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3001600 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=12006400, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=5862, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 12006400 to 51326976 [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 7.8G 0 7.8G 0% /dev tmpfs 7.8G 60K 7.8G 1% /dev/shm tmpfs 7.8G 9.0M 7.8G 1% /run tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup /dev/mapper/centos-root 196G 6.6G 190G 4% / /dev/sda1 197M 166M 32M 84% /boot tmpfs 1.6G 0 1.6G 0% /run/user/1000 tmpfs 1.6G 0 1.6G 0% /run/user/0
通过 df -h 命令可以看到根目录已成功扩容到200G,且扩容后无需重启机器,不得不说,lvm确认强。