Linux下如何扩展根分区

Linux下如何扩展根分区

服务器可能会有根分区存储被占满的情况,那就需要我们将根分区的容量进行扩容,下面详细介绍根分区的扩容步骤。

  • 给服务器添加一块硬盘,用于给根分区进行扩充

在这里插入图片描述

  • 先查看系统的分区情况
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        17G  1.5G   16G   9% /              # 可见这里的根分区的可使用空间为17G
devtmpfs                devtmpfs  979M     0  979M   0% /dev
tmpfs                   tmpfs     991M     0  991M   0% /dev/shm
tmpfs                   tmpfs     991M  9.6M  981M   1% /run
tmpfs                   tmpfs     991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  133M  882M  14% /boot
tmpfs                   tmpfs     199M     0  199M   0% /run/user/0

  • 查看新硬盘是否已经成功被添加到主机上
[root@localhost ~]# fdisk -l   # 使用fdisk命令查看所有系统分区

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000e72fb

   Device Boot      Start         End      Blocks   Id  System     # 下面显示的就是硬盘/dev/sda的分区情况,包括起始到截止扇区是多少
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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-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


  • 输入fdisk命令开始对新硬盘进行分区
[root@localhost ~]# fdisk /dev/sdb      # 对新硬盘进行分区
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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x838024c5.

Command (m for help): n             # 输入“n”添加新分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p):p                # 输入“p”创建主分区,不输入默认是“p”
Using default response p
Partition number (1-4, default 1):  # 输入分区号,不输入默认分区号为“1”
First sector (2048-41943039, default 2048):    # 选择分区的起始扇区,不输入默认为“2048”
Using default value 2048                                   # 提示使用了默认起始扇区208
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):        # 选择截止扇区,不输入默认为“41943039”
Using default value 41943039                               # 提示使用了默认的截止扇区41943039
Partition 1 of type Linux and of size 20 GiB is set        # 提示已设置Linux类型、大小为20 GiB的分区1

Command (m for help): w             # 输入“w”保存分区的设置
The partition table has been altered!                      # 提示分区表已经被修改

Calling ioctl() to re-read partition table.                # 提示调用ioctl()重新读取分区表。
Syncing disks.                                             # 提示正在同步磁盘。

  • 必须格式化新硬盘的分区,不格式化的话没有办法使用
[root@localhost ~]# mkfs.ext3 /dev/sdb1          # 格式化命令为mkfs.ext3
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242624 blocks
262131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000

Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

开始扩展根分区,即扩展/dev/mapper/centos-root这个分区

  • 给刚才新建的分区创建pv(物理卷)
[root@localhost ~]# pvcreate /dev/sdb1
WARNING: ext3 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y    # 输入“y”确认创建
  Wiping ext3 signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.

  • 把pv(物理卷)加入vg(卷组)中,相当于扩充vg(卷组)的大小
# 扩充vg大小之前先查看卷组信息

[root@localhost ~]# vgs     # 显示系统中的卷组有关信息,注意这里显示的是VSize是19.00G
  VG     #PV #LV #SN Attr   VSize   VFree
  centos   1   2   0 wz--n- <19.00g    0

# 扩充vg(卷组)
[root@localhost ~]# vgextend centos /dev/sdb1    # vgextend命令为扩展vg命令,后面跟上想要扩充的组名和用哪一个分区扩充,这里意为使用/dev/sdb1这个分区扩容centos卷组
  Volume group "centos" successfully extended

# 查看卷组是否扩充成功
[root@localhost ~]# vgs
  VG     #PV #LV #SN Attr   VSize  VFree
  centos   2   2   0 wz--n- 38.99g <20.00g       # 这里的VSize已经由原来的19.00G被控充到38.99G


  • 扩展lv(逻辑卷)
# 查看lv(逻辑卷)大小
[root@localhost ~]# lvs
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <17.00g
  swap centos -wi-ao----   2.00g    
# 可见,虽然我们把vg扩展了,但是lv(逻辑卷)还没有扩展,这里的逻辑卷还是小于17.00g的

# 查看想要扩展根分区名称
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        17G  1.5G   16G   9% /                 # 根分区名称为/dev/mapper/centos-root
devtmpfs                devtmpfs  979M     0  979M   0% /dev
tmpfs                   tmpfs     991M     0  991M   0% /dev/shm
tmpfs                   tmpfs     991M  9.6M  981M   1% /run
tmpfs                   tmpfs     991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  133M  882M  14% /boot
tmpfs                   tmpfs     199M     0  199M   0% /run/user/0

# 开始扩展根分区
[root@localhost ~]# lvextend -L +19G /dev/mapper/centos-root    # 意为给根分区/dev/mapper/centos-root扩展19G的空间
  Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <36.00 GiB (9215 extents).
  Logical volume centos/root successfully resized.

注意:如果执行命令lvextend命令的时候报如下错误,是因为分区中没有那么多可以扩充的空间:

[root@localhost ~]# lvextend -L +20G /dev/mapper/centos-root
  Insufficient free space: 5120 extents needed, but only 5119 available 
# 上面的提示信息为:可用空间不足:需要5120个扩展数据块,但只有5119个可用

  • 再次查看lv的大小,检查是否已经扩容
[root@localhost ~]# lvs       
  LV   VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root centos -wi-ao---- <36.00g            # 这里可以看到lv(逻辑卷)大小已经由原来的17.00g扩充到36.00g
  swap centos -wi-ao----   2.00g

  • 使用 xfs_growfs命令使系统重新读取大小
# 查看一下根分区的大小是否已经改变
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        17G  1.5G   16G   9% /                      # 可见根分区的大小还是17G,说明还没有被扩充
devtmpfs                devtmpfs  979M     0  979M   0% /dev
tmpfs                   tmpfs     991M     0  991M   0% /dev/shm
tmpfs                   tmpfs     991M  9.6M  981M   1% /run
tmpfs                   tmpfs     991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  133M  882M  14% /boot
tmpfs                   tmpfs     199M     0  199M   0% /run/user/0


# 使用 xfs_growfs命令使系统重新读取大小
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root     # 在xfs_growfs命令后面跟上重新读取的分区路径
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 9436160


# 再次检测根分区是否被扩充
[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        36G  1.5G   35G   4% /                   
# 可见这里的根分区已经被扩充到36G了

[root@localhost ~]# df -Th
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        36G  1.5G   35G   4% /                   # 可见这里的根分区已经被扩充到36G了
devtmpfs                devtmpfs  979M     0  979M   0% /dev
tmpfs                   tmpfs     991M     0  991M   0% /dev/shm
tmpfs                   tmpfs     991M  9.6M  981M   1% /run
tmpfs                   tmpfs     991M     0  991M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  133M  882M  14% /boot
tmpfs                   tmpfs     199M     0  199M   0% /run/user/0


至此:根分区已经成功扩展

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值