利用单一磁盘的剩余空间
在同一块磁盘下,有剩余空间未分配,将该空间合并到根目录中
本文以/dev/sda为例
- 查看磁盘分区情况
lsblk
或者fdisk -l /dev/sda
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 500M 0 part /boot
└─sda2 8:2 0 9.5G 0 part
├─centos-root 253:0 0 8.5G 0 lvm /
└─centos-swap 253:1 0 1G 0 lvm [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
[root@localhost ~]# fdisk -l /dev/sda
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 4096 字节
I/O 大小(最小/最佳):4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x000f1903
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20971519 9972736 8e Linux LVM
可以看到 sda 总容量为30G,而 sda1和 sda2 加起来总共使用了10G,因此我们可以将剩余的容量添加到目录中
- 对剩余空间进行分区格式化操作
fdisk /dev/sda
[root@localhost ~]# fdisk /dev/sda
The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):3
起始 扇区 (20971520-62914559,默认为 20971520):#默认回车
将使用默认值 20971520
Last 扇区, +扇区 or +size{K,M,G} (20971520-62914559,默认为 62914559):#默认回车
将使用默认值 62914559
分区 3 已设置为 Linux 类型,大小设为 20 GiB
命令(输入 m 获取帮助):t
分区号 (1-3,默认 3):3
Hex 代码(输入 L 列出所有代码):8e #选择分区类型
已将分区“Linux”的类型更改为“Linux LVM”
命令(输入 m 获取帮助):p
磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 4096 字节
I/O 大小(最小/最佳):4096 字节 / 4096 字节
磁盘标签类型:dos
磁盘标识符:0x000f1903
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20971519 9972736 8e Linux LVM
/dev/sda3 20971520 62914559 20971520 8e 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)
正在同步磁盘。
- 重启机器, 让系统识别新增的分区,重启之后使用
partprobe
命令
partprobe /dev/sda3
[root@localhost ~]# partprobe /dev/sda3
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 9.5G 0 part
│ ├─centos-root 253:0 0 8.5G 0 lvm /
│ └─centos-swap 253:1 0 1G 0 lvm [SWAP]
└─sda3 8:3 0 20G 0 part
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
- 创建物理卷
pvcreate /dev/sda3
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
- 查看新建的物理卷和大小,
pvdisplay
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 9.51 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2434
Free PE 10
Allocated PE 2424
PV UUID P6xf6f-NPgl-s3Dm-123h-B5e3-NJBh-GbNM1n
"/dev/sda3" is a new physical volume of "20.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 20.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID uUmihf-SUzq-7t9C-1DL7-fa5j-sXJX-E7WOkL
- 扩展卷组
vgextend centos /dev/sda3
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
- 可通过
vgdisplay
查看卷组信息
vgdisplay
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name centos
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 5
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 29.50 GiB
PE Size 4.00 MiB
Total PE 7553
Alloc PE / Size 2424 / <9.47 GiB
Free PE / Size 5129 / <20.04 GiB
VG UUID OyB5dN-0LOk-6vb1-Qmtu-1EQp-1GtB-Szuco0
- 增加根目录大小,扩容20G
lvresize -L +20G /dev/centos/root
[root@localhost ~]# lvresize -L +20G /dev/centos/root
Size of logical volume centos/root changed from <8.47 GiB (2168 extents) to <28.47 GiB (7288 extents).
Logical volume centos/root successfully resized.
- 刷新文件系统
xfs_growfs /dev/centos/root
[root@localhost ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=555008 blks
= sectsz=4096 attr=2, projid32bit=1
= crc=0 finobt=0 spinodes=0
data = bsize=4096 blocks=2220032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal bsize=4096 blocks=2560, version=2
= sectsz=4096 sunit=1 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 2220032 to 7462912
- 查看扩容后大小
df -Th
[root@localhost ~]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 3.8G 0 3.8G 0% /dev
tmpfs tmpfs 3.8G 0 3.8G 0% /dev/shm
tmpfs tmpfs 3.8G 8.7M 3.8G 1% /run
tmpfs tmpfs 3.8G 0 3.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 29G 2.3G 27G 8% /
/dev/sda1 xfs 494M 211M 284M 43% /boot
tmpfs tmpfs 763M 0 763M 0% /run/user/0
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 30G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 9.5G 0 part
│ ├─centos-root 253:0 0 28.5G 0 lvm /
│ └─centos-swap 253:1 0 1G 0 lvm [SWAP]
└─sda3 8:3 0 20G 0 part
└─centos-root 253:0 0 28.5G 0 lvm /
sdb 8:16 0 10G 0 disk
sr0 11:0 1 1024M 0 rom
文章来源:https://www.cnaaa.net,转载请注明出处:https://www.cnaaa.net/archives/4227