Location>code7788 >text

VirtualBox Expands CentOS-7 Virtual Machine Disks

Popularity:31 ℃/2024-08-02 20:33:51

1. Background description

image

As shown in the above figure, the file system where the root path "/" is located has no more free disk space and needs to expand the disk.

df -h

2、VirtualBox operation

2.1, view the current size of the virtual disk

image

Click to open the Settings screen for the selected virtual machine as shown above.

image

As shown above, the current virtual disk size of the virtual machine is 8GB.

2.2 Modify the size of virtual disks

image

As shown in the above figure, click "File"-> "Virtual Media Manager" to enter the virtual disk management interface.

image

As shown in the above figure, select a virtual machine to modify the virtual disk size of that virtual machine.
image

As shown in the above figure, this example modifies the virtual disk size to 16GB.

Note: You need to shut down the virtual machine before you can modify the size of the virtual disk.

2.3、View the effect of modification

image

As shown in the above figure, the virtual disk size of the current VM has been modified to 16GB.

Note: Although the virtual disk size has been modified to 16GB, it has not been allocated for use in the CentOS operating system.

3、CentOS operation

3.1. Disk partitioning stage

3.1.1 Checking the status of disk partitions

image

As shown in the figure above, the disk /dev/sda has a size of 16G, and the partitions /dev/sda1 and /dev/sda2 only use a total of 8G, so there is still 8G available.

lsblk -p

3.1.2 Creating new partitions

image

fdisk /dev/sda

image

As shown above, type "n" to create a new partition (new).

image

As shown above, type "p" to set the partition type as primary.

Note: A disk can be divided into up to four partitions, of which the types are "primary" and "extended".
There can only be one "Extended Partition" at most, and it is generally recommended that the fourth partition be set as the "Extended Partition" and the first three as the "Primary Partition".
Combined with section 3.1.1, we can see that there are already two partitions, /dev/sda1 and /dev/sda2, and the third one is about to be created, so it is more appropriate to set the partition type as primary.

image

As shown above, enter "3" to set the partition number.

image

As shown above, just enter and use the default starting sector.

image

As shown above, just enter and use the default end sector.

image

As shown in the above figure, enter "w", save and exit.

Tip: The input order is "n"-> "p"-> "3 (or enter)"-> "enter "-> "Enter"-> "w"

Note: If you make a mistake, you can enter "q", quit without saving, and start again.
In addition, some articles have the step of changing the system id to 8e, but this step is just to allow some LVM detection commands to detect this partition, in fact, it is possible to omit this operation.

3.1.3 Updating partition table information

image

As shown in the above figure, you can use the partprobe command to update the partition table information so that the system recognizes the newly created partition.

Although rebooting the system using the reboot command will also update the partition table so that the system recognizes the newly created partition, this is not recommended.

partprobe -s

3.1.4 Checking the status of disk partitions again

image

As shown in the above figure, the new partition /dev/sda3 has been created successfully with a size of 8G.

lsblk -p

3.2. PV Physics Volume Stage

3.2.1 Viewing PV Physical Volume Status

image

As shown above, there is currently only a physical volume created based on the /dev/sda2 partition with a size of 7G.

pvdisplay

3.2.2 Creating a new PV physical volume

image

Create a new partition of /dev/sda3, as shown above, as a new physical volume.

pvcreate new partition name

3.2.3. Checking the PV physical volume status again

image

As shown above, a new physical volume was successfully created based on the /dev/sda3 partition.

pvdisplay

Note: At this point, the value of the VG Name of the /dev/sda2 physical volume is centos, indicating that the physical volume belongs to the volume group with the name centos.
The value of VG Name for the /dev/sda3 physical volume is null, indicating that the physical volume does not belong to any volume group yet.

3.3. VG Volume Group Phase

3.3.1 Viewing VG Volume Group Status

image

As shown above, there is currently only one volume group with the name centos and a size of 7G.

In conjunction with section 3.2.3, this volume group includes only the /dev/sda2 physical volume with a size of 7G.

vgdisplay

3.3.2 Expanding VG Volume Groups

image

As shown in the above figure, add the newly created /dev/sda3 physical volume to the centos volume group to complete the expansion of the centos volume group.

vgextend volume group name new physical volume name

Note: If you execute the command to expand the VG, the error "Couldn't create temporary archive name" is reported;
It means that there is no space left on the disk at all, and you need to delete (or move) some unimportant files (such as log files and the like) before you can continue.
image

3.3.3 Checking VG Volume Group Status Again

image

As shown above, the centos volume group has been successfully scaled to 15G.

In conjunction with Section 3.2.3, this volume group includes the /dev/sda2 physical volume, which is 7G in size, and the /dev/sda3 physical volume, which is 8G in size.

3.4. LV Logical Volume Phase

3.4.1 Viewing LV Logical Volume Status

image

As shown in the above figure, the mount point "/" to be expanded corresponds to the file system "/dev/mapper/centos-root".

df -h

image

As shown in the above figure, the logical volume path corresponding to the file system "/dev/mapper/centos-root" is "/dev/centos/root".

The logical volume is approximately 6G in size and belongs to the centos volume group.

lvdisplay File system name

Note: Although the name of the logical volume is "root", you need to use the path of the logical volume "/dev/centos/root" to manipulate the logical volume.
The format of the logical volume path is: /dev/volume group name/logical volume name

3.4.2 Expanded logical volumes

image

As shown in the above figure, the physical volume space obtained by expanding the centos volume group is allocated to the /dev/centos/root logical volume, thus completing the expansion of the /dev/centos/root logical volume.

lvextend Logical volume path Physical volume resulting from volume group expansion

3.4.3 Checking LV Logical Volume Status Again

image

As shown above, the /dev/centos/root logical volume has been successfully expanded by an additional 8G.

lvdisplay File system name

3.5. Documentation system phase

3.5.1 Viewing the file system status

image

As shown in the above figure, at this point, the expansion space of the /dev/centos/root logical volume has not been synchronized to the /dev/mapper/centos-root file system.

In addition, you can see that the /dev/mapper/centos-root filesystem is of type xfs, so you can use the xfs_growfs command to complete the filesystem expansion.

df -hT

3.5.2 Expansion of the file system

image

xfs_growfs File system name

3.5.3. Checking the file system status again

image

As shown in the figure above, the file system "/dev/mapper/centos-root", which corresponds to the mount point "/" to be expanded, has been successfully expanded by 8G.

df -hT

this paper references

【1】【Walking All the Way to the Top】【VirtualBox Expansion Tutorial】【CSDN】【2022-10-26】)

【2】【jianmuzi】【mount point expansion-Linux-CentOS7】【blogspot】【2022-09-10

【3】【A ?Charis】【Using LVM way to expand the disk to report an error: Couldn't create temporary archive name.】【CSDN】【2023-10-11

[4] [careybobo] [virtualbox virtual machine how to expand /dev/mapper/centos-root] [CSDN] [2024-02-06

[5] [Crayon Little Child] [VirtualBox and VMware virtual machine centos (/dev/mapper/centos-root) disk expansion (pro-test effective)] [CSDN] [2020-05-31

【6】【Know Its Black, Receive Its White】【virtualbox Expanding Dynamic Disks Centos7 Expansion】【CSDN】【 2023-03-17

[7] [Brother Bird] [7.3 Partitioning, Formatting, Validating, and Mounting Disks] [Brother Bird's Linux Private Disks] [2017-09-04

[8] [Bird] [14.3 Logical Volume Manager (Logical Volume Manager)] [Bird's Linux Private Kitchen] [2015-07-28

[9] [Brother Bird] [2.2 Disk Partitioning] [Brother Bird's Linux Private Dining] [2015-04-28