当前位置:首页 » 《我的小黑屋》 » 正文

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

2 人参与  2024年05月05日 15:05  分类 : 《我的小黑屋》  评论

点击全文阅读


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

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

目录

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

1. 根分区扩容

1.1 标准分区扩容(本站 OVF 默认)

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

(1)扩容前状态如下:

[root@sysin-c8 ~]# lsblkNAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda      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 -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  386M     0  386M   0% /devtmpfs          tmpfs     400M     0  400M   0% /dev/shmtmpfs          tmpfs     400M   11M  389M   3% /runtmpfs          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/sdaWelcome 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): mHelp:  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 tableCommand (m for help):

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

Command (m for help): pDisk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x7bb4c495Device     Boot Start       End   Sectors Size Id Type/dev/sda1  *     2048 125829119 125827072  60G 83 Linux# 本例该磁盘仅仅一个分区,Boot 下面有个可启动标记 *,/boot 没有独立分区

(6)按 d 删除 / 分区:

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

(7)按 n 创建新分区:

Command (m for help): nPartition type   p   primary (0 primary, 0 extended, 4 free)   e   extended (container for logical partitions)Select (default p): p  #选择 p primaryPartition number (1-4, default 1):  #直接回车默认 1 即 sda1First sector (2048-209715199, default 2048):  #直接回车默认值 (sysin)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): pDisk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x6a72cc03Device     Boot Start       End   Sectors  Size Id Type/dev/sda1        2048 209715199 209713152  100G 83 LinuxFilesystem/RAID signature on partition 1 will be wiped.Command (m for help):

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

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

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

Command (m for help): aSelected partition 1The bootable flag on partition 1 is enabled now.# 按 p 再次确认,Boot 下面有了 * 符号Command (m for help): pDisk /dev/sda: 100 GiB, 107374182400 bytes, 209715200 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x6a72cc03Device     Boot Start       End   Sectors  Size Id Type/dev/sda1  *     2048 209715199 209713152  100G 83 LinuxFilesystem/RAID signature on partition 1 will be wiped.Command (m for help):

(10)按 w 保存:

Command (m for help): wThe 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 ~]# lsblkNAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda      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 -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  386M     0  386M   0% /devtmpfs          tmpfs     400M     0  400M   0% /dev/shmtmpfs          tmpfs     400M   11M  389M   3% /runtmpfs          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-SYSIN in ~ [12:41:56]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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-SYSIN in ~ [12:41:56]$ df -ThFilesystem              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% /devtmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shmtmpfs                   tmpfs     1.9G   12M  1.9G   1% /runtmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup/dev/sda1               xfs      1014M  142M  873M  14% /boottmpfs                   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 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000af364   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     2099199     1048576   83  Linux/dev/sda2         2099200   335544319   166722560   8e  Linux LVMCommand (m for help): n  #n 新建分区Partition type:   p   primary (2 primary, 0 extended, 2 free)   e   extendedSelect (default p): p  #p 主分区Partition number (3,4, default 3):  #默认按顺序,直接回车First sector (335544320-545259519, default 335544320):  #默认直接回车Using default value 335544320Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519):  #默认直接回车使用全部剩余空间Using default value 545259519Partition 3 of type Linux and of size 100 GiB is setCommand (m for help): p  #p 再次查看分区Disk /dev/sda: 279.2 GB, 279172874240 bytes, 545259520 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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  LinuxCommand (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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.

(2)刷新分区

# root @ C7-SYSIN in ~ [13:31:00]$ partprobe /dev/sda# root @ C7-SYSIN in ~ [13:31:54]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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 partsr0              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               <159.00 GiB  PE Size               4.00 MiB  Total PE              40703  Alloc PE / Size       40703 / <159.00 GiB  Free  PE / Size       0 / 0  VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw

VG 名称为 centos

(5)扩展 VG

使用 /dev/sda3 PV 扩展到 centos VG 中。

$ vgextend centos /dev/sda3  Volume group "centos" successfully extended

(6)查看 LV

$ lvdisplay  --- Logical volume ---  LV Path                /dev/centos/root  LV Name                root  VG Name                centos  LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS  LV Write Access        read/write  LV Creation host, time localhost, 2021-08-22 14:12:50 +0800  LV Status              available  # open                 1  LV Size                <155.00 GiB  Current LE             39679  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0  --- Logical volume ---  LV Path                /dev/centos/swap  LV Name                swap  VG Name                centos  LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi  LV Write Access        read/write  LV Creation host, time localhost, 2021-08-22 14:12:51 +0800  LV Status              available  # open                 2  LV Size                4.00 GiB  Current LE             1024  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:1

需要扩展的根分区 LV 为 /dev/centos/root

(7)将 VG 中的空闲空间扩展到根分区 LV

$ lvextend -l +100%FREE /dev/centos/root  Size of logical volume centos/root changed from <155.00 GiB (39679 extents) to 254.99 GiB (65278 extents).  Logical volume centos/root successfully resized.

(8)刷新根分区

如果是 ext4 文件系统(包括 2、3),使用 resize2fs 命令。

xfs_growfs /dev/centos/root

(9)验证结果

# root @ C7-SYSIN in ~ [13:39:53]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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  255G  0 lvm  /│ └─centos-swap 253:1    0    4G  0 lvm  [SWAP]└─sda3            8:3    0  100G  0 part  └─centos-root 253:0    0  255G  0 lvm  /sr0              11:0    1 1024M  0 rom# root @ C7-SYSIN in ~ [13:39:53]$ df -ThFilesystem              Type      Size  Used Avail Use% Mounted on/dev/mapper/centos-root xfs       255G  1.5G  254G   1% /devtmpfs                devtmpfs  1.9G     0  1.9G   0% /devtmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shmtmpfs                   tmpfs     1.9G   12M  1.9G   1% /runtmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup/dev/sda1               xfs      1014M  142M  873M  14% /boottmpfs                   tmpfs     378M     0  378M   0% /run/user/0

可以看到根目录容量扩展成功。

2. SWAP 分区扩容

2.1 创建文件作为 SWAP 分区(本站 OVF 默认)

这是最简单的方式,直接在 / 目录下创建一个文件作为交换分区,可以非常灵活完美的启用和禁用 swap 并在线调整大小。

(1)创建要作为 swap 分区的文件:

增加 4GB 大小的交换分区,则命令写法如下,其中的 count 等于想要的块的数量(bs*count = 文件大小)。

# 这里定义为 /swap.imgdd if=/dev/zero of=/swap.img bs=1M count=4096# 修改权限chmod 600 /swap.img

(2)创建 SWAP 分区文件系统:

mkswap /swap.img #mkswap - set up a Linux swap area

(3)启用交换分区文件:

swapon /swap.img #启用 swap 文件

此时使用 free -m 命令可以看到 Swap 的容量等于原有容量加上上述创建文件容量之和。

(4)编辑 /etc/fstab 开机自动加载上述 swap 文件:

增加如下一行。

/swap.img    none    swap    defaults    0 0#  第二个字段在 CentOS 中默认使用 swap,两者并没有不一样。/swap.img    swap    swap    defaults    0 0# 关于字段的风格,使用 tab 或者空格都可以,数量没有要求,通常为了对齐使用多个空格

直接命令添加:

sudo sh -c "echo '/swap.img    none    swap    defaults    0 0' >> /etc/fstab"

(5)取消 swap 文件

swapoff /swap.imgrm -r /swap.img

然后编辑 /etc/fstab,删除上述添加的一行即可。

Ubuntu 20.04 中默认使用 /swap.img 文件作为 SWAP 分区,可以直接 swapoff 并删除原文件,然后重新创建同名文件来简单扩容。我们这里借鉴了 Ubuntu 的做法,并推荐使用该种方式。

2.2 标准分区 SWAP 扩容

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

(1)扩容前状态如下:

[root@sysin-c8 ~]# lsblkNAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda      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 -ThFilesystem     Type      Size  Used Avail Use% Mounted ondevtmpfs       devtmpfs  386M     0  386M   0% /devtmpfs          tmpfs     400M     0  400M   0% /dev/shmtmpfs          tmpfs     400M   11M  389M   3% /runtmpfs          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)将虚机中将磁盘 2 容量扩展为 16G,这个过程就不截图了。

刷新磁盘状态:partprobe /dev/sdb

(3)关闭 swap

swapoff /dev/sdb1

(4) 重新创建 /dev/sdb1 分区

[root@sysin-c8 ~]# fdisk /dev/sdb  #选择 /dev/sdbWelcome 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): p  #查看当然分区Disk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x316023cdDevice     Boot Start     End Sectors Size Id Type/dev/sdb1        2048 8388607 8386560   4G 82 Linux swap / SolarisCommand (m for help): d  #删除分区,只有一个分区不会提示选择数字Selected partition 1Partition 1 has been deleted.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 或者直接回车Partition number (1-4, default 1):   #直接回车按顺序 1First sector (2048-33554431, default 2048):   #直接回车Last sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431):   #直接回车使用全部可用空间Created a new partition 1 of type 'Linux' and of size 16 GiB.Partition #1 contains a swap signature.Do you want to remove the signature? [Y]es/[N]o: N  # 选择 N 不要移除 swap 签名,磁盘 UUID 不变The signature will be removed by a write command.Command (m for help): t  #更改分区类型Selected partition 1Hex code (type L to list all codes): L  #输入 L,查看可用代码 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT- 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT- 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden or  c6  DRDOS/sec (FAT- 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / . 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi ea  Rufus alignment e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         eb  BeOS fs f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ee  GPT10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBTHex code (type L to list all codes): 82  #输入 82,更改为 SWAPChanged type of partition 'Linux' to 'Linux swap / Solaris'.Command (m for help): p  #再次查看分区,已经是 swapDisk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 sectorsUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x316023cdDevice     Boot Start      End  Sectors Size Id Type/dev/sdb1        2048 33554431 33552384  16G 82 Linux swap / SolarisFilesystem/RAID signature on partition 1 will be wiped.Command (m for help): w  #保存退出The partition table has been altered.Calling ioctl() to re-read partition table.Re-reading the partition table failed.: Device or resource busyThe 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).

最后提示需要重启或者运行 partprobe、kpartx 命令生效,这里执行命令:partprobe /dev/sdb1

笔者在一次测试中执行 partprobe 命令报错,可能是分区过程中选择移除了 swap 签名,分区的 UUID 变更,直接重启,要多等一会儿才能进入系统,但是不影响正常运行。

(5) 创建 swap 文件系统

mkswap /dev/sdb1# 提示如下Setting up swapspace version 1, size = 16 GiB (17178816512 bytes)no label, UUID=df11dbb4-9665-4732-b865-03913713fa5e# 注意这里的 UUID 变更,需要修改替换 /etc/fstab

(6) 打开 swap

swapon /dev/sdb1

(7)更改 /etc/fstab

如果在重新创建分区的过程中移除了 swap 签名,则磁盘 UUID 变更,需要编辑 /etc/fstab 做相应变更。

即使没有移除 swap 签名,使用 mkswap 命令后 UUID 也变更了,也需要编辑 /etc/fstab 做相应变更。

要查看分区的 UUID 使用命令 blkid 或者 lsblk -f

#UUID=c154479a-28e1-40b2-b10c-646b41693f51 swap                    swap    defaults        0 0 #原有UUID=df11dbb4-9665-4732-b865-03913713fa5e swap                    swap    defaults        0 0

当然也可以新建额外的分区作为增加的 swap 分区,原有 swap 分区不变,最后编辑 /etc/fstab 自动加载即可。

(8)更改 /etc/default/grub

grub 配置文件中有个 resume 选项,指向 swap 分区,如果标准分区默认使用的是 swap 分区的 UUID。

vi /etc/default/grub# 找到如下行GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=c154479a-28e1-40b2-b10c-646b41693f51 rhgb quiet"  #原有# 修改如下GRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=df11dbb4-9665-4732-b865-03913713fa5e rhgb quiet"# 重建配置grub2-mkconfig -o /boot/grub2/grub.cfg# UEFI boot mode:#grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg

2.3 LVM SWAP 扩容

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

# root @ C7-SYSIN in ~ [12:41:56]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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-SYSIN in ~ [12:41:56]$ df -ThFilesystem              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% /devtmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shmtmpfs                   tmpfs     1.9G   12M  1.9G   1% /runtmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup/dev/sda1               xfs      1014M  142M  873M  14% /boottmpfs                   tmpfs     378M     0  378M   0% /run/user/0

将该虚拟磁盘容量增加 12G。

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

(1)关闭 swap

查看 /etc/fstab 可以看到 swap 的 LV 名称:

$ cat /etc/fstab | grep swap/dev/mapper/centos-swap swap                    swap    defaults        0 0

将其关闭:

swapoff /dev/mapper/centos-swap

(2)创建分区

$ fdisk /dev/sda  #对原磁盘 /dev/sda 分区,也可以是新增的磁盘,按顺序使用 /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.Command (m for help): p  #查看当前分区Disk /dev/sda: 184.7 GB, 184683593728 bytes, 360710144 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk identifier: 0x000af364   Device Boot      Start         End      Blocks   Id  System/dev/sda1   *        2048     2099199     1048576   83  Linux/dev/sda2         2099200   335544319   166722560   8e  Linux LVMCommand (m for help): n  #新建分区Partition type:   p   primary (2 primary, 0 extended, 2 free)   e   extendedSelect (default p):  #直接回车,默认主分区Using default response pPartition number (3,4, default 3):   #直接回车默认按顺序 3First sector (335544320-360710143, default 335544320):   #直接回车Using default value 335544320Last sector, +sectors or +size{K,M,G} (335544320-360710143, default 360710143):   #直接回车,使用全部剩余空间Using default value 360710143Partition 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 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk label type: dosDisk 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  LinuxCommand (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 atthe next reboot or after you run partprobe(8) or kpartx(8)Syncing disks.You have new mail.

(3)刷新分区

# root @ C7-SYSIN in ~ [13:31:00]$ partprobe /dev/sda# root @ C7-SYSIN in ~ [13:31:54]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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 partsr0              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               <159.00 GiB  PE Size               4.00 MiB  Total PE              40703  Alloc PE / Size       40703 / <159.00 GiB  Free  PE / Size       0 / 0  VG UUID               Aul9M5-OJu8-3RB5-DU9Y-yi5m-ngyd-QyIKLw

VG 名称为 centos

(6)扩展 VG

使用 /dev/sda3 PV 扩展到 centos VG 中。

$ vgextend centos /dev/sda3  Volume group "centos" successfully extended

(7)查看 LV

$ lvdisplay  --- Logical volume ---  LV Path                /dev/centos/root  LV Name                root  VG Name                centos  LV UUID                ntq8LZ-qivt-B6ij-AZWi-97a9-H2Q5-YxznfS  LV Write Access        read/write  LV Creation host, time localhost, 2021-08-22 14:12:50 +0800  LV Status              available  # open                 1  LV Size                <155.00 GiB  Current LE             39679  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:0  --- Logical volume ---  LV Path                /dev/centos/swap  LV Name                swap  VG Name                centos  LV UUID                jvNgUR-KUsk-1hD4-h383-w3pc-OhBT-ZFMpyi  LV Write Access        read/write  LV Creation host, time localhost, 2021-08-22 14:12:51 +0800  LV Status              available  # open                 2  LV Size                4.00 GiB  Current LE             1024  Segments               1  Allocation             inherit  Read ahead sectors     auto  - currently set to     8192  Block device           253:1

需要扩展的根分区 LV 为 /dev/centos/swap

(8)将 VG 中的空闲空间扩展到根分区 LV

$ lvextend -l +100%FREE /dev/centos/swap  Size of logical volume centos/swap changed from 4.00 GiB (1024 extents) to <16.00 GiB (4095 extents).  Logical volume centos/swap successfully resized.

(9)刷新 swap 分区

partprobe /dev/centos/swap

(10)验证结果

# root @ C7-SYSIN in ~ [15:54:13]$ lsblkNAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda               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   16G  0 lvm└─sda3            8:3    0   12G  0 part  └─centos-swap 253:1    0   16G  0 lvmsr0              11:0    1 1024M  0 rom# root @ C7-SYSIN in ~ [15:54:13]$ df -ThFilesystem              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% /devtmpfs                   tmpfs     1.9G     0  1.9G   0% /dev/shmtmpfs                   tmpfs     1.9G   12M  1.9G   1% /runtmpfs                   tmpfs     1.9G     0  1.9G   0% /sys/fs/cgroup/dev/sda1               xfs      1014M  142M  873M  14% /boottmpfs                   tmpfs     378M     0  378M   0% /run/user/0

可以看到根目录容量扩展成功。

(11)创建 swap 文件系统

$ mkswap /dev/mapper/centos-swapmkswap: /dev/mapper/centos-swap: warning: wiping old swap signature.Setting up swapspace version 1, size = 16773116 KiBno label, UUID=fb85ef4d-59aa-4554-a3bd-b4cc37746c51

(12)开启 swap

swapon /dev/mapper/centos-swap

使用 free -m 可以查看 swap 分区容量成功变为 16G,/etc/fstab 无需修改。

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

本例使用 Ubuntu 20.04,分区如下,我们新增一块 100G 的磁盘,挂载为 /data ,同时新增一块 100G 的磁盘,用来替换原有的 /var 目录(假定原有磁盘分区不够用)。当然我们也可以直接扩容根分区参看本文上述描述。这里我们分别创建独立的 /data/var 分区。

创建独立的 /data/var 分区也可以使用 LVM 的方式,具体操作不同但是逻辑步骤是一样的,可以参看其他文章。本文使用标准分区。

$ lsblkNAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda                         8:0    0  160G  0 disk├─sda1                      8:1    0    1M  0 part├─sda2                      8:2    0    1G  0 part /boot└─sda3                      8:3    0  159G  0 part  └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /sr0                        11:0    1 1024M  0 rom# sa @ U20 in ~ [16:14:05]$ df -ThFilesystem                        Type      Size  Used Avail Use% Mounted onudev                              devtmpfs  1.9G     0  1.9G   0% /devtmpfs                             tmpfs     391M  1.1M  390M   1% /run/dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shmtmpfs                             tmpfs     5.0M     0  5.0M   0% /run/locktmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup/dev/sda2                         ext4      976M  103M  806M  12% /boottmpfs                             tmpfs     391M     0  391M   0% /run/user/1000

Ubuntu 比较特别的,这里有个 1M 的分区为 bios_grub,无论使用 LVM 或是标准分区都会自动创建。可能参看这里的讨论。

新增硬盘后,如下,如果看不到新增磁盘需要重启或者执行 partprobe 命令。

$ lsblkNAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTsda                         8:0    0  160G  0 disk├─sda1                      8:1    0    1M  0 part├─sda2                      8:2    0    1G  0 part /boot└─sda3                      8:3    0  159G  0 part  └─ubuntu--vg-ubuntu--lv 253:0    0  159G  0 lvm  /sdb                         8:16   0  100G  0 disksdc                         8:32   0  100G  0 disksr0                        11:0    1 1024M  0 rom

3.1 将磁盘挂载为新的目录

(1)创建分区

# sa @ U20 in ~ [16:23:43] C:1$ sudo fdisk /dev/sdb  #将 /dev/sdb 创建分区[sudo] password for sa:Welcome to fdisk (util-linux 2.34).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.Created a new DOS disklabel with disk identifier 0x9a7ae73a.Command (m for help): n  #n 新建分区Partition type   p   primary (0 primary, 0 extended, 4 free)   e   extended (container for logical partitions)Select (default p):   #直接回车,主分区Using default response p.Partition number (1-4, default 1):   #直接回车,按顺序编号 1First 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.Command (m for help): p  #查看分区Disk /dev/sdb: 100 GiB, 107374182400 bytes, 209715200 sectorsDisk model: VMware Virtual SUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x9a7ae73aDevice     Boot Start       End   Sectors  Size Id Type/dev/sdb1        2048 209715199 209713152  100G 83 LinuxCommand (m for help): w  #保存退出The partition table has been altered.Calling ioctl() to re-read partition table.Syncing disks.

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

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

(3)挂载到 /data 目录

sudo mkdir /datasudo mount /dev/sdb1  /data

(4)开机自动加载

编辑 /etc/fstab ,添加如下一行:

/dev/sdb1 /data ext4 defaults 0 0

(5)验证

重启后查看是否正确加载了:

$ df -ThFilesystem                        Type      Size  Used Avail Use% Mounted onudev                              devtmpfs  1.9G     0  1.9G   0% /devtmpfs                             tmpfs     391M  1.2M  390M   1% /run/dev/mapper/ubuntu--vg-ubuntu--lv ext4      156G  6.0G  142G   5% /tmpfs                             tmpfs     2.0G     0  2.0G   0% /dev/shmtmpfs                             tmpfs     5.0M     0  5.0M   0% /run/locktmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup/dev/sda2                         ext4      976M  103M  806M  12% /boottmpfs                             tmpfs     391M     0  391M   0% /run/user/1000/dev/sdb1                         ext4       98G   61M   93G   1% /data

3.2 将磁盘挂载原有目录

(1)创建分区

# sa @ U20 in ~ [16:23:43] C:1$ sudo fdisk /dev/sdc  #将 /dev/sdc 创建分区[sudo] password for sa:Welcome to fdisk (util-linux 2.34).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.Created a new DOS disklabel with disk identifier 0x9a7ae73a.Command (m for help): n  #n 新建分区Partition type   p   primary (0 primary, 0 extended, 4 free)   e   extended (container for logical partitions)Select (default p):   #直接回车,主分区Using default response p.Partition number (1-4, default 1):   #直接回车,按顺序编号 1First 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.Command (m for help): p  #查看分区Disk /dev/sdc: 100 GiB, 107374182400 bytes, 209715200 sectorsDisk model: VMware Virtual SUnits: sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisklabel type: dosDisk identifier: 0x9a7ae73aDevice     Boot Start       End   Sectors  Size Id Type/dev/sdc1        2048 209715199 209713152  100G 83 LinuxCommand (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 /varnewsudo 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/sdc1sudo mkdir /varsudo 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。


点击全文阅读


本文链接:http://zhangshiyu.com/post/103883.html

<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于我们 | 我要投稿 | 免责申明

Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1