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 +79G /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)

[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

标准根分区扩容

查看磁盘大小

[root@rke ~]# fdisk -l

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

查看当前根分区

[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 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏