Linux partition expansion (root partition expansion, SWAP partition expansion, mounting new partitions as directories)
When the Linux system is running, there is insufficient disk space and what should I do if it needs to be expanded? This article describes common expansion scenarios, including root partition, SWAP partition, and expansion of a directory.
Table of contents
-
- Root partition expansion
- 1.1 Standard partition expansion (default)
- 1.2 LVM partition expansion
-
- SWAP partition expansion
- 2.1 Create file as SWAP partition (default)
- 2.2 Standard partition SWAP expansion
- 2.3 LVM SWAP expansion
-
- Mount new disk to new partition
- 3.1 Mount the disk as a new directory
- 3.2 Mount the disk to the original directory
-
- summary
1. Root partition expansion
1.1 Standard partition expansion (this site’s OVF default)
This example is a CentOS 8 virtual machine with two disks. Disk 1 has a capacity of 60G for the root directory (including /boot
), and disk 2 has a capacity of 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. No screenshots will be taken during this process.
If online addition is supported, the disk status can be refreshed 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 partitions under 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
# 本例该磁盘仅仅一个分区,Boot 下面有个可启动标记 *,/boot 没有独立分区
(6) Press d to delete / partition:
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.
Command (m for help):
# 本例中只有一个分区,所以直接删除了,如果是有多个分区,会提示输入数字选择
(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 #选择 p primary
Partition number (1-4, default 1): #直接回车默认 1 即 sda1
First 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) 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 up bootable:
In this example /boot
, there is no independent partition. You need to set the boot flag to make the partition bootable:
/boot
Independent partitions do not require this step.
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) Press w to save:
Command (m for help): w
The partition table has been altered.
Syncing disks.
(11) Important step: Synchronize the capacity in the file system.
CentOS 7 starts using the xfs file system by default, and uses the xfs_growfs command to synchronize the file system capacity.
If it is Ext4 (including 2 and 3), use the resize2fs command.
xfs_growfs /
# 注意 xfs_growfs 使用 mountpoint
#resize2fs /dev/sda1
# resize2fs 则使用 device
(12) Confirm the partition result. 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 has been successfully completed.
1.2 LVM partition expansion
LVM terminology explanation:
LVM (logical volume manager) logical volume manager
It is mainly divided into the following concepts:
-
Physical volume - Physical volume referred to as PV
The physical volume belongs to the lowest level in the logical volume manager. Any logical volume and volume group must be created based on the physical volume. The physical volume can be a complete hard disk or a partition in the hard disk.
-
Volume group-Volume group abbreviated as VG
Volume groups are built on physical volumes, and a volume group can contain one or more physical volumes.
-
Logical volume - Logical volume abbreviated as LV
/home
Logical volumes are similar to hard disk partitions in non-LVM systems, and file systems (such as or etc.) can be established on logical volumes/usr
.
The process of creating 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, one disk, independent /boot
partition, and 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
Expand the virtual disk to 260G.
Note: You can also add a disk to simulate the addition of a new physical disk, but the drive letters below should be modified to "/dev/sda=/dev/sdb" and "/dev/sda3=/dev/sdb1".
(1) Create partition
$ 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) Refresh 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) Extend VG
Use /dev/sda3 PV to expand 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 LV 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 has been successfully expanded.
2. SWAP partition expansion
2.1 Create a file as a SWAP partition (OVF default on this site)
This is the simplest way. 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 the swap partition:
To add a 4GB swap partition, the command is written as follows, where count is equal to the number of desired blocks (bs*count = file size).
# 这里定义为 /swap.img
dd if=/dev/zero of=/swap.img bs=1M count=4096
# 修改权限
chmod 600 /swap.img
(2) Create a SWAP partition file system:
mkswap /swap.img #mkswap - set up a Linux swap area
(3) Enable swap partition file:
swapon /swap.img #启用 swap 文件
At this time, using free -m
the command, you can see that the capacity of Swap is equal to the sum of the original capacity plus the capacity of the created files above.
(4) Edit: /etc/fstab
Automatically load the above swap file when booting:
Add the following line.
/swap.img none swap defaults 0 0
# 第二个字段在 CentOS 中默认使用 swap,两者并没有不一样。
/swap.img swap swap defaults 0 0
# 关于字段的风格,使用 tab 或者空格都可以,数量没有要求,通常为了对齐使用多个空格
Direct command to add:
sudo sh -c "echo '/swap.img none swap defaults 0 0' >> /etc/fstab"
(5) Cancel swap file
swapoff /swap.img
rm -r /swap.img
Then edit /etc/fstab
and delete the line added above.
Ubuntu 20.04 uses
/swap.img
files as SWAP partitions by default. You can directly swapoff and delete the original files, and then re-create the files with the same name for simple expansion. We draw lessons from Ubuntu's approach here and recommend this method.
2.2 Standard partition SWAP expansion
This example is a CentOS 8 virtual machine with two disks. Disk 1 has a capacity of 60G for the root directory (including /boot
), and disk 2 has a capacity of 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. No screenshots will be taken during this process.
Refresh disk status:partprobe /dev/sdb
(3) Close swap
swapoff /dev/sdb1
(4) Re-create /dev/sdb1
partition
[root@sysin-c8 ~]# fdisk /dev/sdb #选择 /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 #查看当然分区
Disk /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 #删除分区,只有一个分区不会提示选择数字
Selected partition 1
Partition 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): #直接回车按顺序 1
First 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 1
Hex 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 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 <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 #输入 82,更改为 SWAP
Changed type of partition 'Linux' to 'Linux swap / Solaris'.
Command (m for help): p #再次查看分区,已经是 swap
Disk /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 #保存退出
The 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, it prompts that you need to restart or run the partprobe and kpartx commands to take effect. Execute the command here: partprobe /dev/sdb1
.
The author reported an error when executing the partprobe command during a test. It may be that the swap signature was removed during the partitioning process, and the UUID of the partition was changed. Restarting directly requires waiting a while before entering the system, but it does not affect normal operation.
(5) Create swap file system
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) Open swap
swapon /dev/sdb1
(7) Change/etc/fstab
If the swap signature is removed during partition re-creation, the disk UUID changes and needs to be edited /etc/fstab
to make corresponding changes.
Even if the swap signature is not removed, the UUID has changed after using the mkswap command, and it needs to be edited /etc/fstab
to make corresponding changes.
To view the UUID of a partition use the command blkid
or lsblk -f
.
#UUID=c154479a-28e1-40b2-b10c-646b41693f51 swap swap defaults 0 0 #原有
UUID=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 partitions will remain unchanged. You
/etc/fstab
can just edit and load them automatically at the end.
(8)Change/etc/default/grub
There is a resume option in the grub configuration file, which points to the swap partition. If the standard partition uses the UUID of the swap partition by default.
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 expansion
This example is CentOS 7, one disk, independent /boot
partition, and 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 the virtual disk capacity by 12G.
Note: You can also add a disk to simulate the addition of a new physical disk, but the drive letters below should be modified to "/dev/sda=/dev/sdb" and "/dev/sda3=/dev/sdb1".
(1) Close swap
Check /etc/fstab
to see the LV name of swap:
$ cat /etc/fstab | grep swap
/dev/mapper/centos-swap swap swap defaults 0 0
To turn it off:
swapoff /dev/mapper/centos-swap
(2) Create partition
$ 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 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 #新建分区
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): #直接回车,默认主分区
Using default response p
Partition number (3,4, default 3): #直接回车默认按顺序 3
First sector (335544320-360710143, default 335544320): #直接回车
Using default value 335544320
Last sector, +sectors or +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) Refresh 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) Extend VG
Use /dev/sda3 PV to expand 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 LV 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 has been successfully expanded.
(11) Create 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
Use free -m
to check that the swap partition capacity has been successfully changed to 16G /etc/fstab
without modification.
3. Mount the new disk to the new partition
This example uses Ubuntu 20.04. The partitions are as follows. We add a 100G disk and mount it as. /data
At the same time, we add a 100G disk to replace the original /var
directory (assuming that the original disk partition is not enough). Of course, we can also directly expand the root partition, see the description above in this article. Here we create independent /data
and /var
partitions respectively.
You can also use LVM to create independent
/data
and partitions. The specific operations are different but the logical steps are the same. You can refer to other articles./var
This article uses standard partitioning.
$ 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 special. There is a 1M partition called bios_grub, which will be automatically created regardless of whether LVM or standard partitions are used. Maybe see the discussion here .
After adding a new hard disk, as shown below, 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 partition
# 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): #直接回车,按顺序编号 1
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.
Command (m for help): p #查看分区
Disk /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 #保存退出
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
(2) Create file system (format partition)
sudo mkfs -t ext4 /dev/sdb1
# 上述使用 ext4,也可以使用 xfs
(3) Mount to /data
directory
sudo mkdir /data
sudo mount /dev/sdb1 /data
(4) Automatic loading at startup
Edit /etc/fstab
, add the following line:
/dev/sdb1 /data ext4 defaults 0 0
(5) Verification
After restarting, check whether it is 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 to the original directory
(1) Create partition
# 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): #直接回车,按顺序编号 1
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.
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) Create file system (format partition)
sudo mkfs -t xfs /dev/sdc1
# 上述使用 xfs,也可以使用 ext4
(3) Mount to /varnew
directory
sudo mkdir /varnew
sudo mount /dev/sdc1 /varnew
(4) Key step: Copy /var
all content under 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, the group to which it belongs, time and Soft linkability.
Two "**"s are used here to ensure that all files can be copied if Bash has the Globstar option turned on.
(5) Rename the current /var
directory (optional, you can also delete it directly)
sudo mv /var /varold
(6) Remount the hard disk to /var
the directory
sudo umount /dev/sdc1
sudo mkdir /var
sudo mount /dev/sdc1 /var
(7) Set up automatic mounting at startup
#sudo vi /etc/fstab
#在文件的最后增加一行
/dev/sdc1 /var xfs defaults 0 0
(8) Delete the original /var
directory
After confirming that there are no problems, completely delete the original/var
sudo rm -rf /varold
4. Summary
Using standard partitions to expand capacity seems simpler than LVM, especially in a virtual machine environment. Virtual disks can directly expand capacity without using LVM features.
In a physical machine environment, the use of LVM can take advantage of new physical disk expansion.
Therefore, it is recommended to use standard partitions in virtual machines or cloud environments (it is more convenient to use independent swap virtual disks or swap files). For physical machines, especially in application scenarios with large amounts of data, you can consider using LVM first.