Linux 分区扩容(根分区扩容,SWAP 分区扩容,挂载新分区为目录) 您所在的位置:网站首页 linux系统如何挂载现有磁盘 Linux 分区扩容(根分区扩容,SWAP 分区扩容,挂载新分区为目录)

Linux 分区扩容(根分区扩容,SWAP 分区扩容,挂载新分区为目录)

2024-01-05 18:20| 来源: 网络整理| 查看: 265

请访问原文链接:https://sysin.org/blog/linux-partition-expansion/,查看最新版。原创作品,转载请保留出处。

作者主页:sysin.org

Linux 系统在运行过程中,出现磁盘空间不足,需要扩容该如何处理?本文描述了常见的扩容场景,包括根分区、SWAP 分区以及扩容某个目录。

目录

根分区扩容 1.1 标准分区扩容(非 LVM)1.2 LVM 分区扩容 SWAP 分区扩容 2.1 创建文件作为 SWAP 分区2.2 标准分区 SWAP 扩容2.3 LVM SWAP 扩容 挂载新的磁盘到新的分区 3.1 将磁盘挂载为新的目录3.2 将磁盘挂载原有目录 小结 1. 根分区扩容 1.1 标准分区扩容(非 LVM)

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

(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 Type Size Used Avail Use% Mounted on /dev/mapper/centos-root xfs 155G 1.5G 154G 1% / devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm tmpfs tmpfs 1.9G 12M 1.9G 1% /run tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 142M 873M 14% /boot tmpfs tmpfs 378M 0 378M 0% /run/user/0

将该虚拟磁盘扩展到 260G。

说明:也可以添加一块磁盘,可以模拟新增了物理磁盘,只是下面盘符对应修改 “/dev/sda=/dev/sdb”,“/dev/sda3=/dev/sdb1”。

(1)创建分区

$ fdisk /dev/sda #对原磁盘 /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 #p 查看当前分区 Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM Command (m for help): n #n 新建分区 Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p #p 主分区 Partition number (3,4, default 3): #默认按顺序,直接回车 First sector (335544320-545259519, default 335544320): #默认直接回车 Using default value 335544320 Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519): #默认直接回车使用全部剩余空间 Using default value 545259519 Partition 3 of type Linux and of size 100 GiB is set Command (m for help): p #p 再次查看分区 Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 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: 0x000af364 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM /dev/sda3 335544320 545259519 104857600 83 Linux Command (m for help): w #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.

(2)刷新分区

# root @ C7 in ~ [13:31:00] $ partprobe /dev/sda # root @ C7 in ~ [13:31:54] $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 260G 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] └─sda3 8:3 0 100G 0 part sr0 11:0 1 1024M 0 rom

(3)创建 PV

$ pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created.

(4)查看 VG

$ vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size K,M,G} (335544320-360710143, default 360710143): #直接回车,使用全部剩余空间 Using default value 360710143 Partition 3 of type Linux and of size 12 GiB is set #12G 没错 Command (m for help): p #再次查看分区 Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 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: 0x000af364 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 335544319 166722560 8e Linux LVM /dev/sda3 335544320 360710143 12582912 83 Linux 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. You have new mail.

(3)刷新分区

# root @ C7 in ~ [13:31:00] $ partprobe /dev/sda # root @ C7 in ~ [13:31:54] $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 172G 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 └─sda3 8:3 0 12G 0 part sr0 11:0 1 1024M 0 rom

(4)创建 PV

$ pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created.

(5)查看 VG

$ vgdisplay --- Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size K,M,G,T,P} (2048-209715199, default 209715199): #直接回车,使用全部可用空间 Created a new partition 1 of type 'Linux' and of size 100 GiB. Command (m for help): p #查看分区 Disk /dev/sdc: 100 GiB, 107374182400 bytes, 209715200 sectors Disk model: VMware Virtual S 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: 0x9a7ae73a Device Boot Start End Sectors Size Id Type /dev/sdc1 2048 209715199 209713152 100G 83 Linux Command (m for help): w #保存退出 The partition table has been altered. Calling ioctl() to re-read partition table. Syncing disks.

(2)创建文件系统(格式化分区)

sudo mkfs -t xfs /dev/sdc1 # 上述使用 xfs,也可以使用 ext4

(3)挂载到 /varnew 目录

sudo mkdir /varnew sudo mount /dev/sdc1 /varnew

(4)关键步骤:拷贝 /var 下的所有内容到新的硬盘

sudo cp -a /var/** /varnew

释义:

“-a” 选项相当于 “-d、-p、-r” 选项,使用 “-a” 选项时,目标文件和源文件的所有属性都一致,包括源文件的所有者,所属组、时间和软链接性。

这里使用了两个 “**”,确保如果 Bash 开启了 Globstar 选项后可以复制所有文件。

(5)重命名当前 /var 目录(可选,也可以直接删除)

sudo mv /var /varold

(6)重新挂载硬盘到 /var 目录

sudo umount /dev/sdc1 sudo mkdir /var sudo mount /dev/sdc1 /var

(7)设置开机启动自动挂载

#sudo vi /etc/fstab #在文件的最后增加一行 /dev/sdc1 /var xfs defaults 0 0

(8)删除原有 /var 目录

经过确认没有问题后,彻底删除原有 /var

sudo rm -rf /varold 4. 小结

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

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

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



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有