Linux根分区扩容

LVM根分区

磁盘分区扩容

查看lvm卷组

VG #PV #LV #SN Attr   VSize  VFree
cl 1 2 0 wz--n- 19.00g 0

添加磁盘并将分区转换为lvm分区,新建的lvm分区为sda3

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000af7fc

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
/dev/sda3 41943040 209715199 83886080 8e Linux LVM

创建pv

pvcreate /dev/sda3

扩展vg

vgextend cl /dev/sda3

查看扩展的vg

[root@acai ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 2 2 0 wz--n- 98.99g 80.00g

扩展lv

[root@acai ~]# lvextend -l +100%FREE /dev/cl/root 
Size of logical volume cl/root changed from 17.00 GiB (4351 extents) to 96.00 GiB (24575 extents).
Logical volume cl/root successfully resized.

刷新分区表(如果分区的文件系统为xfs,需要使用xfs_growfs)

#ext4
resize2fs /dev/mapper/bigcloud--enterprise--linux--for--euler-root
#xfs
[root@acai ~]# xfs_growfs /dev/mapper/cl-root
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 25164800

验证根分区

[root@acai ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 96G 1.3G 95G 2% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.6M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 139M 876M 14% /boot
tmpfs 378M 0 378M 0% /run/user/0

磁盘扩容

这种方式适用于创建lvm时没有给磁盘创建分区,直接将磁盘格式化为lvm格式的情况。
查看磁盘大小

lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
└─sda1 8:1 0 200G 0 part /
sdb 8:16 0 2T 0 disk
└─vg_data-lv_data 253:0 0 500G 0 lvm /data
sr0 11:0 1 474K 0 rom

查看vg

[root@ecs-03968264-007 ~]#  vgs
File descriptor 3 (socket:[335678288]) leaked on vgs invocation. Parent PID 2415349: -bash
VG #PV #LV #SN Attr VSize VFree
vg_data 1 1 0 wz--n- <500.00g 0

查看lv

[root@ecs-03968264-007 ~]#  lvs
File descriptor 3 (socket:[335678288]) leaked on lvs invocation. Parent PID 2415349: -bash
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_data vg_data -wi-ao---- <500.00g

查看pv

[root@ecs-03968264-007 ~]#  pvdisplay /dev/sdb
File descriptor 3 (socket:[335678288]) leaked on pvdisplay invocation. Parent PID 2415349: -bash
--- Physical volume ---
PV Name /dev/sdb
VG Name vg_data
PV Size 500.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 127999
Free PE 0
Allocated PE 127999
PV UUID LLONwh-Ggf1-EZei-veko-q74E-kJyI-eAgJ8u

扩容pv

[root@ecs-03968264-007 ~]#  pvresize /dev/sdb
File descriptor 3 (socket:[335678288]) leaked on pvresize invocation. Parent PID 2415349: -bash
Physical volume "/dev/sdb" changed
1 physical volume(s) resized or updated / 0 physical volume(s) not resized

查看pv是否扩容

[root@ecs-03968264-007 ~]#  pvdisplay /dev/sdb
File descriptor 3 (socket:[335678288]) leaked on pvdisplay invocation. Parent PID 2415349: -bash
--- Physical volume ---
PV Name /dev/sdb
VG Name vg_data
PV Size 1.95 TiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 511999
Free PE 384000
Allocated PE 127999
PV UUID LLONwh-Ggf1-EZei-veko-q74E-kJyI-eAgJ8u

查看vg是否扩容

[root@ecs-03968264-007 ~]#  vgs
File descriptor 3 (socket:[335678288]) leaked on vgs invocation. Parent PID 2415349: -bash
VG #PV #LV #SN Attr VSize VFree
vg_data 1 1 0 wz--n- 1.95t 1.46t

扩容lv

[root@ecs-03968264-007 ~]#  lvextend -l +100%FREE /dev/vg_data/lv_data
File descriptor 3 (socket:[335678288]) leaked on lvextend invocation. Parent PID 2415349: -bash
Size of logical volume vg_data/lv_data changed from <500.00 GiB (127999 extents) to 1.95 TiB (511999 extents).
Logical volume vg_data/lv_data successfully resized.

查看lv是否扩容

[root@ecs-03968264-007 ~]#  lvs
File descriptor 3 (socket:[335678288]) leaked on lvs invocation. Parent PID 2415349: -bash
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv_data vg_data -wi-ao---- 1.95t

刷新分区表

[root@ecs-03968264-007 ~]#  xfs_growfs /data
meta-data=/dev/mapper/vg_data-lv_data isize=512 agcount=4, agsize=32767744 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=1, sparse=1, rmapbt=0
= reflink=1
data = bsize=4096 blocks=131070976, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0, ftype=1
log =internal log bsize=4096 blocks=63999, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

确认扩容

[root@ecs-03968264-007 ~]#  df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 16G 0 16G 0% /dev
tmpfs 16G 0 16G 0% /dev/shm
tmpfs 16G 1.8M 16G 1% /run
tmpfs 16G 0 16G 0% /sys/fs/cgroup
/dev/sda1 197G 7.3G 182G 4% /
tmpfs 16G 18M 16G 1% /tmp
/dev/mapper/vg_data-lv_data 2.0T 319G 1.7T 16% /data

标准分区

查看当前根分区

[root@rke ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 40G 0 disk
├─sda1 8:1 0 1G 0 part /boot
├─sda2 8:2 0 2G 0 part [SWAP]
└─sda3 8:3 0 17G 0 part /
sr0 11:0 1 906M 0 rom

重建sda3根分区分区表

[root@rke ~]# 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): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x0000266a

Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 6293503 2097152 82 Linux swap / Solaris
/dev/sda3 6293504 41943039 17824768 83 Linux

Command (m for help): d
Partition number (1-3, default 3):
Partition 3 is deleted

Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (6293504-83886079, default 6293504):
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-83886079, default 83886079):
Using default value 83886079
Partition 3 of type Linux and of size 37 GiB is set

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

查看根分区磁盘格式

[root@rke ~]# mount|grep sda3
/dev/sda3 on / type xfs (rw,relatime,attr2,inode64,noquota)

动态扩容根分区

[root@rke ~]# xfs_growfs /dev/sda3
meta-data=/dev/sda3 isize=512 agcount=4, agsize=1114048 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4456192, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/Linux根分区扩容/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏