Linux Centos 根目录扩展分区(保级教程)

Centos 根目录扩展分区

  • [1. 扩展背景](#1. 扩展背景)
  • 2.列出磁盘信息
  • [3. 对磁盘进行分区](#3. 对磁盘进行分区)
  • [4. 重启Linux](#4. 重启Linux)
  • [5. 将PV加入卷组centos并分区](#5. 将PV加入卷组centos并分区)
  • 6.查看分区结果

1. 扩展背景

虚拟机初始分配20G内存,扩容到80G。

2.列出磁盘信息

可以得知容量信息以及即将创建的PV路径(通常为"/dev/sda累加数字"),下面例子为sda2,预期扩展60G

py 复制代码
# 列出各分区使用情况
[root@master02 ~]# df -hl
# 查看现有磁盘情况
[root@master02 ~]# fdisk -l
py 复制代码
Disk /dev/sda: 85.9 GB, 85899345920 bytes, 167772160 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: 0x000ae5b5

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

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

3. 对磁盘进行分区

py 复制代码
    sudo fdisk /dev/sda(你的磁盘号)
    命令行提示下输入:
  【n】添加新分区。
  【p】创建主分区。
  【回车】分区号   --默认分区编号 
  【回车】--默认起始扇区位置。
  【回车】--默认结束扇区位置。 如果要把100G全部分给这个分区,直接两次回车。如果分60G输入+60G
  【t】      --修改分区类型
  【8e】   --修改为LVM
  【w】    --保存

具体操作如下:

py 复制代码
[root@master02 ~]# 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】
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 【p】
Partition number (3,4, default 3): 【回车】
First sector (41943040-167772159, default 41943040): 【回车】
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-167772159, default 167772159): 【回车】
Using default value 167772159
Partition 3 of type Linux and of size 60 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 【回车】
Hex code (type L to list all codes): 【8e]
Changed type of partition 'Linux' to 'Linux LVM'

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.

4. 重启Linux

重启linux,必须reboot,否则无法格式化。

py 复制代码
reboot

5. 将PV加入卷组centos并分区

  • 始化PV并加入卷组centos
py 复制代码
 pvcreate /dev/sda3
 vgextend centos /dev/sda3
  • 将空余容量分配给逻辑分区
    +60G根据本次要扩展的容量得出,具体数值看分配扩展空间的大小,如果60G空间溢出,可以选择59G
py 复制代码
 lvextend -L +60G /dev/mapper/centos-root
  • 重载
py 复制代码
xfs_growfs /dev/centos/root

具体操作步骤如下:

py 复制代码
[root@master02 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                7FxqiT-LSfr-380m-aXO0-i8k4-O7TC-oRxCA0
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-11-10 18:42:59 +0800
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  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                2MCadI-YApB-DtaL-a8uo-eubT-SwzJ-CYvmif
  LV Write Access        read/write
  LV Creation host, time localhost, 2023-11-10 18:42:59 +0800
  LV Status              available
  # open                 1
  LV Size                <17.00 GiB
  Current LE             4351
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
   
[root@master02 ~]# lvextend -L +60G /dev/mapper/centos-root
  Insufficient free space: 15360 extents needed, but only 15359 available
[root@master02 ~]# lvextend -L +59G /dev/mapper/centos-root
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <76.00 GiB (19455 extents).
  Logical volume centos/root successfully resized.
[root@master02 ~]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-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 19921920

6.查看分区结果

/dev/mapper/centos-root已经扩展到75G

py 复制代码
[root@master02 ~]# df -hl
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 1.4G     0  1.4G   0% /dev
tmpfs                    1.4G     0  1.4G   0% /dev/shm
tmpfs                    1.4G   11M  1.4G   1% /run
tmpfs                    1.4G     0  1.4G   0% /sys/fs/cgroup
/dev/mapper/centos-root   76G  4.6G   72G   7% /
/dev/sda1               1014M  223M  792M  22% /boot
tmpfs                    283M   24K  283M   1% /run/user/1001
相关推荐
wVelpro22 分钟前
如何在Pycharm 2025.3 版本实现虚拟环境“Make available to all projects”
linux·ide·pycharm
程序员老舅1 小时前
C++高并发精髓:无锁队列深度解析
linux·c++·内存管理·c/c++·原子操作·无锁队列
雨中风华1 小时前
Linux, macOS系统实现远程目录访问(等同于windows平台xFsRedir软件的目录重定向)
linux·windows·macos
爱吃生蚝的于勒2 小时前
【Linux】进程信号之捕捉(三)
linux·运维·服务器·c语言·数据结构·c++·学习
The森2 小时前
Linux IO 模型纵深解析 01:从 Unix 传统到 Linux 内核的 IO 第一性原理
linux·服务器·c语言·经验分享·笔记·unix
翼龙云_cloud2 小时前
腾讯云代理商: Linux 云服务器搭建 FTP 服务指南
linux·服务器·腾讯云
纤纡.2 小时前
Linux中SQL 从基础到进阶:五大分类详解与表结构操作(ALTER/DROP)全攻略
linux·数据库·sql
好好学习天天向上~~3 小时前
6_Linux学习总结_自动化构建
linux·学习·自动化
冉佳驹3 小时前
Linux ——— 静态库和动态库的设计与使用
linux·动态库·静态库·fpic
陌上花开缓缓归以3 小时前
linux mtd-utils使用源码分析(ubuntu测试版)
linux·arm开发·ubuntu