Linux分区扩容

目录前言1. 根分区扩容1.1 标准分区扩容(非 LVM)1.2 LVM 分区扩容2. SWAP 分区扩容2.1 创建文件作为 SWAP 分区2.2 标准分区 SWAP 扩容2.3 LVM SWAP 扩容3. 挂载新的磁盘到新的分区3.1 将磁盘挂载为新的目录3.2 将磁盘挂载原有目录前言使用标准分区扩容相比 LVM 要简单一些,特别是在虚机环境中,虚拟磁盘是可以直接扩展容量的,并不需要使用 LVM 的特性。而在物理机环境中,新增物理磁盘扩容,使用
摘要由CSDN通过智能技术生成

目录

前言

1. 根分区扩容

1.1 标准分区扩容(非 LVM)

1.2 LVM 分区扩容

2. SWAP 分区扩容

2.1 创建文件作为 SWAP 分区

2.2 标准分区 SWAP 扩容

2.3 LVM SWAP 扩容

3. 挂载新的磁盘到新的分区

3.1 将磁盘挂载为新的目录

3.2 将磁盘挂载原有目录


前言

使用标准分区扩容相比 LVM 要简单一些,特别是在虚机环境中,虚拟磁盘是可以直接扩展容量的,并不需要使用 LVM 的特性。

而在物理机环境中,新增物理磁盘扩容,使用 LVM 就发挥了优势。

所以推荐在虚机或者云环境中使用标准分区(独立 swap 虚拟磁盘或者使用 swap 文件更便捷),物理机特别是数据量大的应用场景,可以考虑优先使用 LVM。

1. 根分区扩容

1.1 标准分区扩容(非 LVM)

本例为 CentOS 8 虚机,两块磁盘,磁盘 1 容量 60G 用于根目录(包含 /boot),磁盘 2 容量 4G 用于 。

(1)扩容前状态如下:

[root@sysin-c8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   60G  0 disk
└─sda1   8:1    0   60G  0 part /
sdb      8:16   0    4G  0 disk
└─sdb1   8:17   0    4G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom

[root@sysin-c8 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  386M     0  386M   0% /dev
tmpfs          tmpfs     400M     0  400M   0% /dev/shm
tmpfs          tmpfs     400M   11M  389M   3% /run
tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
/dev/sda1      xfs        60G  1.8G   59G   3% /
tmpfs          tmpfs      80M     0   80M   0% /run/user/0

(2)将虚机中将磁盘 1 容量扩展为 100G,这个过程就不截图了。

若支持在线添加,可通过命令刷新磁盘状态:partprobe /dev/sda

(3)开始扩容根目录:

[root@sysin-c8 ~]# fdisk /dev/sda

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help):

(4)可以按 m 查看一下帮助:

Command (m for help): m

Help:

DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Command (m for help):

(5)按 p 查看当前磁盘下的分区:

Command (m for help): p
Disk /dev/sda: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x7bb4c495

Device     Boot Start       End   Sectors Size Id Type
/dev/sda1  *     2048 125829119 125827072  60G 83 Linux

# 本例该磁盘仅仅一个分区,Boot 下面有个可启动标记 *,/boot 没有独立分区

(6)按 d 删除 / 分区:

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

Command (m for help):
# 本例中只有一个分区,所以直接删除了,如果是有多个分区,会提示输入数字选择

(7)按 n 创建新分区:

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  #选择 p primary
Partition number (1-4, default 1):  #直接回车默认 1 即 sda1
First sector (2048-209715199, default 2048):  #直接回车默认值
Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): #直接回车默认值,使用全部剩余空间

Created a new partition 1 of type 'Linux' and of size 100 GiB.
Partition #1 contains a xfs signature.

Do you want to remove the signature? [Y]es/[N]o: N  #按 N 保留 xfs 签名,移除的话分区的 UUID 会变更。

The signature will be removed by a write command.

Command (m for help):

(8)按 p 再次查看状态:

Command (m for help): p
Disk /dev/sda: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x6a72cc03

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1        2048 209715199 209713152  100G 83 Linux

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help):

(9)重要步骤:按 a 设置可引导:

本例 /boot 没有独立分区,需要需要设置 boot flag 即将分区设置为可引导:

/boot 独立分区的不需要此步骤。

Command (m for help): a
Selected partition 1
The bootable flag on partition 1 is enabled now.

# 按 p 再次确认,Boot 下面有了 * 符号
Command (m for help): p
Disk /dev/sda: 100 GiB, 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
Disklabel type: dos
Disk identifier: 0x6a72cc03

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1  *     2048 209715199 209713152  100G 83 Linux

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help):

(10)按 w 保存:

Command (m for help): w
The partition table has been altered.
Syncing disks.

(11)重要步骤:同步文件系统中的容量。

CentOS 7 开始默认使用 xfs 文件系统,使用 xfs_growfs 命令同步文件系统容量。

如果是 Ext4(包括 2、3),使用 resize2fs 命令。

xfs_growfs /
# 注意 xfs_growfs 使用 mountpoint
#resize2fs /dev/sda1
# resize2fs 则使用 device

(12)确认分区结果,可以重启一下系统确认是否正常

[root@sysin-c8 ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  100G  0 disk
└─sda1   8:1    0  100G  0 part /
sdb      8:16   0    4G  0 disk
└─sdb1   8:17   0    4G  0 part [SWAP]
sr0     11:0    1 1024M  0 rom
[root@sysin-c8 ~]# df -Th
Filesystem     Type      Size  Used Avail Use% Mounted on
devtmpfs       devtmpfs  386M     0  386M   0% /dev
tmpfs          tmpfs     400M     0  400M   0% /dev/shm
tmpfs          tmpfs     400M   11M  389M   3% /run
tmpfs          tmpfs     400M     0  400M   0% /sys/fs/cgroup
/dev/sda1      xfs       100G  2.1G   98G   3% /
tmpfs          tmpfs      80M     0   80M   0% /run/user/0
[root@sysin-c8 ~]#

至此扩容成功完成。

1.2 LVM 分区扩容

LVM 名词解释:

LVM (logical volume manager) 逻辑卷管理器

其中主要分为这几个概念:

  • 物理卷 - Physical volume 简称 PV
    物理卷在逻辑卷管理器中属于最底层的,任何的逻辑卷和卷组都必需依靠物理卷来建立,物理卷可以是一个完整的硬盘,也可以是硬盘中的莫一个分区。
  • 卷组 - Volume group 简称 VG
    卷组是建立在物理卷之上,一个卷组中可以包含一个或者多个物理卷。
  • 逻辑卷 - Logical volume 简称 LV
    逻辑卷类似于非 LVM 系统中的硬盘分区,在逻辑卷之上可以建立文件系统 (比如 /home 或者 /usr 等)。

一个建立逻辑卷的流程如下:PV -> VG -> LV,物理卷包含卷组,卷组包含逻辑卷。

本例为 CentOS 7,一块磁盘,独立 /boot 分区,两个 LVM 分区,如下:

# root @ C7 in ~ [12:41:56]
$ lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0  160G  0 disk
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0  159G  0 part
  ├─centos-root 253:0    0  155G  0 lvm  /
  └─centos-swap 253:1    0    4G  0 lvm  [SWAP]
sr0              11:0    1 1024M  0 rom

# root @ C7 in ~ [12:41:56]
$ df -Th
Filesystem     

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值