SoFunction
Updated on 2025-04-14

Linux partition expansion method (root partition expansion, SWAP partition expansion, mount new partition as directory)

Linux partition expansion (root partition expansion, SWAP partition expansion, mount new partition as directory)

During the running process of Linux system, there is insufficient disk space and what should I do if I need to expand capacity?

This article describes common scaling scenarios, including root partition, SWAP partition, and scaling a directory.

1. Root partition expansion

1.1 Standard partition expansion (OVF default)

This example is a CentOS 8 virtual machine, two disks, disk 1 capacity 60G for the root directory (including/boot), Disk 2 capacity 4G for SWAP.

(1) The status before expansion is as follows:

[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) Expand the capacity of disk 1 in the virtual machine to 100G, and this process will not be taken.

If online addition is supported, you can refresh the disk status through the command:partprobe /dev/sda

(3) Start expanding the root directory:

[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) You can press m to view the help:

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) Press p to view the partition on the current disk:

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

# In this case, the disk is only one partition,Boot There is a bootable tag below *,/boot No separate partition

(6) Press d to delete/partition:

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

Command (m for help):
# There is only one partition in this case,So deleted directly,If there are multiple partitions,It will prompt to enter the number to select

(7) Press n to create a new partition:

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  #Select p primaryPartition number (1-4, default 1):  #Directly enter the carriage default 1 i.e. sda1First sector (2048-209715199, default 2048):  #Direct Enter the default value (sysin)Last sector, +sectors or +size{K,M,G,T,P} (2048-209715199, default 209715199): #Turn directly enter the default value, use all remaining space
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  #Press N to keep the xfs signature. If removed, the UUID of the partition will change.
The signature will be removed by a write command.

Command (m for help):

(8) Press p to view the status again:

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) Important steps: Press a to set to boot:

This example/bootThere is no independent partition, you need to set boot flag. That is, the partition is set to bootable:

/bootThis step is not required for independent partitions.

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

# Press p to confirm again, there is a * symbol under BootCommand (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) Press w to save:

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

(11) Important steps: Synchronize the capacity in the file system.

CentOS 7 starts using the xfs file system by default, using the xfs_growfs command to synchronize file system capacity.

If it is Ext4 (including 2 and 3), use the resize2fs command.

xfs_growfs /
# Note xfs_growfs uses mountpoint#resize2fs /dev/sda1
# resize2fs Use device

(12) Confirm the partition results, you can restart the system to confirm whether it is normal

[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 ~]#

The expansion was successfully completed.

1.2 LVM partition expansion

LVM definition:

LVM (logical volume manager) Logical volume manager

It is mainly divided into these concepts:

  • Physical volume - Physical volume PV
  • Physical volumes are at the lowest level in the logical volume manager. Any logical volumes and volume groups must be established by physical volumes. The physical volume can be a complete hard disk or a partition in the hard disk.
  • Volume group - Volume group VG
  • A volume group is built on a physical volume, and a volume group can contain one or more physical volumes.
  • Logical volume - Logical volume LV
  • Logical volumes are similar to hard disk partitions in non-LVM systems, and a file system can be established on top of the logical volume (e.g./homeor/usrwait).

A process for establishing a logical volume is as follows: PV -> VG -> LV, the physical volume contains the volume group, and the volume group contains the logical volume.

This example is CentOS 7, a disk, independent/bootPartition, two LVM partitions, as follows:

# root @ C7-SYSIN 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-SYSIN 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

Scales this virtual disk to 260G.

Note: You can also add a disk, which can simulate the addition of a physical disk, but the following disk letters are modified correspondingly "/dev/sda=/dev/sdb" and "/dev/sda3=/dev/sdb1".

(1) Create a partition

$ fdisk /dev/sda  #Parallel to the original disk /dev/sdaWelcome 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 View the current partition
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 Create a new partitionPartition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p  #p Main partitionPartition number (3,4, default 3):  #Default order, enter directlyFirst sector (335544320-545259519, default 335544320):  #Default direct returnUsing default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-545259519, default 545259519):  #Default direct input to use all remaining spaceUsing default value 545259519
Partition 3 of type Linux and of size 100 GiB is set

Command (m for help): p  #p Check the partition again
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 SaveThe 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) Refresh the partition

# root @ C7-SYSIN in ~ [13:31:00]
$ partprobe /dev/sda

# root @ C7-SYSIN 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) Create PV

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

(4) View 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 name is centos

(5) Extended VG

Use /dev/sda3 PV to extend into centos VG.

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

(6) View 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

The root partition that needs to be extended is /dev/centos/root

(7) Expand the free space in VG to the root partition 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) Refresh the root partition

If it is an ext4 file system (including 2 and 3), use the resize2fs command.

xfs_growfs /dev/centos/root

(9) Verification results

# root @ C7-SYSIN in ~ [13:39:53]
$ 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  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 -Th
Filesystem              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% /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

You can see that the root directory capacity expansion is successful.

2. SWAP partition expansion

2.1 Create a file as a SWAP partition (OVF default in this site)

This is the easiest way to create a file directly in the / directory as a swap partition, which can be very flexible and perfect to enable and disable swap and resize online.

(1) Create the file to be used as swap partition:

If you increase the 4GB size swap partition, the command is written as follows, where count is equal to the desired number of blocks (bs*count = file size).

# Here is defined as /dd if=/dev/zero of=/ bs=1M count=4096
# Modify permissionschmod 600 /

(2) Create a SWAP partition file system:

mkswap / #mkswap - set up a Linux swap area

(3) Enable swap partition files:

swapon / #Enable swap document

Used at this timefree -mThe command shows that the capacity of Swap is equal to the sum of the original capacity plus the above-mentioned created file capacity.

(4) Edit/etc/fstabAutomatically load the above swap file on the startup:

Add the following line.

/    none    swap    defaults    0 0
# The second field uses swap by default in CentOS, and the two are not different./    swap    swap    defaults    0 0
# About the style of the field,use tab Or spaces are OK,No quantity requirement,通常为了对齐use多个空格

Direct command to add:

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

(5) Cancel the swap file

swapoff /
rm -r /

Then edit/etc/fstab, just delete the above added line.

Used by default in Ubuntu 20.04/As a SWAP partition, you can swapoff directly and delete the original file, and then recreate the file with the same name to simply expand the capacity. We have borrowed from Ubuntu's practices here and recommend this method.

2.2 Standard partition SWAP capacity expansion

This example is a CentOS 8 virtual machine, two disks, disk 1 capacity 60G for the root directory (including/boot), Disk 2 capacity 4G for SWAP.

(1) The status before expansion is as follows:

[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) Expand the capacity of disk 2 in the virtual machine to 16G, and this process will not be taken.

Refresh disk status:partprobe /dev/sdb

(3) Close swap

swapoff /dev/sdb1

(4) Recreate/dev/sdb1Partition

[root@sysin-c8 ~]# fdisk /dev/sdb #Select /dev/sdb
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): p  #View the partition of courseDisk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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: 0x316023cd

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 8388607 8386560   4G 82 Linux swap / Solaris

Command (m for help): d  #Delete the partition, only one partition will not prompt to select the numberSelected partition 1
Partition 1 has been deleted.

Command (m for help): n  #Create a new partitionPartition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p  #Main partition, enter P or enter directlyPartition number (1-4, default 1):   #Enter directly in order 1First sector (2048-33554431, default 2048):   #Enter it directlyLast sector, +sectors or +size{K,M,G,T,P} (2048-33554431, default 33554431):   #Enter it directly to use all available space
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  # Select N Do not remove the swap signature, the disk UUID remains unchanged
The signature will be removed by a write command.

Command (m for help): t  #Change partition typeSelected partition 1
Hex code (type L to list all codes): L  #Enter L to view available codes
 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 &lt;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            88  Linux plaintext de  Dell Utility
 8  AIX             4e   2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f   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  GPT
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        ef  EFI (FAT-12/16/
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f0  Linux/PA-RISC b
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f1  SpeedStor
14  Hidden FAT16 &lt;3 61  SpeedStor       ab  Darwin boot     f4  SpeedStor
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      f2  DOS secondary
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fb  VMware VMFS
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fc  VMware VMKCORE
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fd  Linux raid auto
1c  Hidden W95 FAT3 75  PC/IX           bc  Acronis FAT32 L fe  LANstep
1e  Hidden W95 FAT1 80  Old Minix       be  Solaris boot    ff  BBT
Hex code (type L to list all codes): 82  #Enter 82, change to SWAPChanged type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p  # Check the partition again, it is already swapDisk /dev/sdb: 16 GiB, 17179869184 bytes, 33554432 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: 0x316023cd

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 33554431 33552384  16G 82 Linux swap / Solaris

Filesystem/RAID signature on partition 1 will be wiped.

Command (m for help): w  #Save ExitThe partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

Finally, the prompt needs to restart or run the partprobe and kpartx commands to take effect. Execute the command here:partprobe /dev/sdb1

The author reported an error by executing the partprobe command in a test. It may be that the swap signature was removed during the partitioning process, the UUID of the partition was changed, and it was restarted directly. It took a while before entering the system, but it did not affect normal operation.

(5) Create a swap file system

mkswap /dev/sdb1
# The prompt is as followsSetting up swapspace version 1, size = 16 GiB (17178816512 bytes)
no label, UUID=df11dbb4-9665-4732-b865-03913713fa5e
# Pay attention to the UUID change,Need to be modified and replaced /etc/fstab

(6) Open swap

swapon /dev/sdb1

(7) Change/etc/fstab

If the swap signature is removed during the recreation of the partition, the disk UUID changes and needs to be edited/etc/fstabMake corresponding changes.

Even if the swap signature is not removed, the UUID has changed after using the mkswap command and needs to be edited./etc/fstabMake corresponding changes.

To view the UUID of the partition Use the commandblkidorlsblk -f

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

Of course, you can also create additional partitions as additional swap partitions. The original swap partition remains unchanged. Finally, edit/etc/fstabIt can be loaded automatically.

(8) Change/etc/default/grub

There is a resume option in the grub configuration file, pointing to the swap partition. If the standard partition uses the UUID of the swap partition by default.

vi /etc/default/grub
# Find the following lineGRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=c154479a-28e1-40b2-b10c-646b41693f51 rhgb quiet"  #original# Modify as followsGRUB_CMDLINE_LINUX="crashkernel=auto resume=UUID=df11dbb4-9665-4732-b865-03913713fa5e rhgb quiet"
# Rebuild the configurationgrub2-mkconfig -o /boot/grub2/
# UEFI boot mode:
#grub2-mkconfig -o /boot/efi/EFI/redhat/

2.3 LVM SWAP capacity expansion

This example is CentOS 7, a disk, independent/bootPartition, two LVM partitions, as follows:

# root @ C7-SYSIN 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-SYSIN 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

Increase this virtual disk capacity by 12G.

Note: You can also add a disk, which can simulate the addition of a physical disk, but the following disk letters are modified correspondingly "/dev/sda=/dev/sdb" and "/dev/sda3=/dev/sdb1".

(1) Close swap

Check/etc/fstabYou can see the LV name of swap:

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

Close it:

swapoff /dev/mapper/centos-swap

(2) Create a partition

$ fdisk /dev/sda  #The original disk /dev/sda partition can also be a newly added disk. The operation method is the same when using /dev/sdb in order.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  #View the current partition
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

Command (m for help): n  #Create a new partitionPartition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):  #Enter it directly, default main partitionUsing default response p
Partition number (3,4, default 3):   #Direct Enter in order by default 3First sector (335544320-360710143, default 335544320):   #Enter it directlyUsing default value 335544320
Last sector, +sectors or +size{K,M,G} (335544320-360710143, default 360710143):   #Enter it directly, use all remaining spaceUsing default value 360710143
Partition 3 of type Linux and of size 12 GiB is set   #12G That's right
Command (m for help): p  #View partition again
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  #Save ExitThe 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) Refresh the partition

# root @ C7-SYSIN in ~ [13:31:00]
$ partprobe /dev/sda

# root @ C7-SYSIN 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) Create PV

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

(5) View 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 name is centos

(6) Extended VG

Use /dev/sda3 PV to extend into centos VG.

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

(7) View 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

The root partition that needs to be extended is /dev/centos/swap

(8) Expand the free space in VG to the root partition 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) Refresh the swap partition

partprobe /dev/centos/swap

(10) Verification results

# root @ C7-SYSIN in ~ [15:54:13]
$ 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   16G  0 lvm
└─sda3            8:3    0   12G  0 part
  └─centos-swap 253:1    0   16G  0 lvm
sr0              11:0    1 1024M  0 rom

# root @ C7-SYSIN in ~ [15:54:13]
$ 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

You can see that the root directory capacity expansion is successful.

(11) Create a swap file system

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

(12) Turn on swap

swapon /dev/mapper/centos-swap

usefree -mYou can see that the swap partition capacity has successfully changed to 16G./etc/fstabNo modification is required.

3. Mount the new disk to the new partition

This example uses Ubuntu 20.04, and the partition is as follows. We have added a 100G disk and mounted as/data, and add a 100G disk to replace the original one/varDirectory (assuming that the original disk partition is not enough). Of course, we can also directly expand the root partition and refer to the above description in this article. Here we create separate/dataand/varPartition.

Create a standalone/dataand/varPartitioning can also be done using LVM. The specific operations are different but the logical steps are the same. You can refer to other articles. This article uses standard partitions.

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         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 -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                             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/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                         ext4      976M  103M  806M  12% /boot
tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000

Ubuntu is quite special. There is a 1M partition here that is bios_grub, which will be automatically created whether using LVM or standard partitions.

After adding a new hard disk, as follows: If you cannot see the new disk, you need to restart or execute the partprobe command.

$ lsblk
NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda                         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 disk
sdc                         8:32   0  100G  0 disk
sr0                        11:0    1 1024M  0 rom

3.1 Mount the disk as a new directory

(1) Create a partition

# sa @ U20 in ~ [16:23:43] C:1
$ sudo fdisk /dev/sdb  #Create /dev/sdb partition[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 Create a new partitionPartition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):   #Enter it directly, main partition
Using default response p.
Partition number (1-4, default 1):   #Enter directly, number in order 1First sector (2048-209715199, default 2048):   #Enter it directlyLast sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199):   #Enter it directly, use all available space
Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): p  #View partitionDisk /dev/sdb: 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/sdb1        2048 209715199 209713152  100G 83 Linux

Command (m for help): w  #Save ExitThe partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

(2) Create a file system (format the partition)

sudo mkfs -t ext4 /dev/sdb1
# The above use ext4,Can also be used xfs

(3) Mount to/dataTable of contents

sudo mkdir /data
sudo mount /dev/sdb1  /data

(4) Automatic loading on the computer

edit/etc/fstab, add the following line:

/dev/sdb1 /data ext4 defaults 0 0

(5) Verification

After restarting, check whether it loaded correctly:

$ df -Th
Filesystem                        Type      Size  Used Avail Use% Mounted on
udev                              devtmpfs  1.9G     0  1.9G   0% /dev
tmpfs                             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/shm
tmpfs                             tmpfs     5.0M     0  5.0M   0% /run/lock
tmpfs                             tmpfs     2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda2                         ext4      976M  103M  806M  12% /boot
tmpfs                             tmpfs     391M     0  391M   0% /run/user/1000
/dev/sdb1                         ext4       98G   61M   93G   1% /data

3.2 Mount the disk in the original directory

(1) Create a partition

# sa @ U20 in ~ [16:23:43] C:1
$ sudo fdisk /dev/sdc  #Create /dev/sdc partition[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 Create a new partitionPartition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):   #Enter it directly, main partition
Using default response p.
Partition number (1-4, default 1):   #Enter directly, number in order 1First sector (2048-209715199, default 2048):   #Enter it directlyLast sector, +/-sectors or +/-size{K,M,G,T,P} (2048-209715199, default 209715199):   #Enter it directly, use all available space
Created a new partition 1 of type 'Linux' and of size 100 GiB.

Command (m for help): p  #View partitionDisk /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  #Save ExitThe partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

(2) Create a file system (format the partition)

sudo mkfs -t xfs /dev/sdc1
# The above use xfs,Can also be used ext4

(3) Mount to/varnewTable of contents

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

(4) Key steps: copy/varAll contents under it to the new hard drive

sudo cp -a /var/** /varnew

Definition:

The "-a" option is equivalent to the "-d, -p, -r" option. When using the "-a" option, all attributes of the target file and the source file are consistent, including the owner of the source file, group, time and soft linkability.

Two "**" are used here to ensure that if Bash is enabledGlobstarAll files can be copied after the option.

(5) Rename the current one/varDirectory (optional, can also be deleted directly)

sudo mv /var /varold

(6) Remount the hard drive to/varTable of contents

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

(7) Set up automatic mount on startup

#sudo vi /etc/fstab
#Add a line at the end of the file/dev/sdc1 /var xfs defaults 0 0

(8) Delete the original/varTable of contents

After confirming that there is no problem, completely delete the original one/var

sudo rm -rf /varold

4. Summary

Using standard partition expansion seems to be easier than LVM, especially in virtual machine environments, virtual disks can directly expand capacity and do not require the use of LVM features.

In the physical machine environment, adding physical disk capacity expansion will give full play to the advantages of using LVM.

Therefore, it is recommended to use standard partitions in virtual machines or cloud environments (independent swap virtual disks or swap files are more convenient). Physical machines, especially application scenarios with large data volumes, you can consider prioritizing the use of LVM.

5. Attached

Comparison of MBR and GPT

category Main boot method Number of primary partitions Maximum capacity How many bits of systems are supported Partition method
MBR BIOS+MBR 4 2T 32 and 64 fdisk
GPT UEFI+GPT 128 18EB(1EB=1024PB=1048576TB) 64 parted

Parted partitioning and creating logical volume LVM

When partitioning a hard disk larger than 2TB, fdisk is no longer applicable and requires using parted to partition the hard disk.

Parted partitioning process:

parted -l  #View all disk statusparted /dev/vdb   #Create partitions larger than 2T through parted toolmklabel gpt   #Create a disk tagmkpart primary 0% 100% #Create the entire partitionq #quit
#Other commands-------------------
(parted) mklabel    #Create a disk tagNew disk labeltype? gpt
(parted) p  #View partition status(parted) mkpart
Partition name? []? gpt2t   #Specify the partition nameFile system type? [ext2]ext3    #Specify partition typeStart? 1    #Specify the start positionEnd? 2190GB #Specify the end position(parted) P  #Display partition information(parted) Q  #quit

The process of creating a logical volume

fdiks -l #View partitionpvcreate /dev/vdb1  #Create a pv physical volumevgcreate vgdata /dev/vdb1  #Create a vg volume grouplvcreate -l +100%FREE -n lvdata vgdata #Create lv logical volume /dev/mapper/vgdata-lvdata #Format logical volumemkdir /data   #Create a data foldermount /dev/mapper/vgdata-lvdata /data #mount logical volumes to /datavim /etc/fastab  #Add to boot up/dev/mapper/vgdata-lvdata /data  xfs  defaults 0 0 
mount -a #Check the mount

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.