VirtualBox Ubuntu 16.04 disk non-adjacent partition expansion solution

foreword

The blogger used the Ubuntu 16.04 LTS virtual machine installed in VirtualBox 6.1 to do the final homework. At the beginning, only a 20GB hard disk was allocated. When running the experiment, he found that the space was not enough, and he needed to expand the disk capacity. After a long time, he made a small record here.
Warning: The blogger is not proficient in Linux. The specific operation methods are all I searched on the Internet. Although the solutions provided here include backup steps, disk partitioning is generally a very dangerous operation, and it is not guaranteed to be 100% safe.

operate

Close the virtual machine, in the VirtualBox console, click Management in the upper left corner - Virtual Media Management.

insert image description here
Find the disk that needs to be expanded, and drag the slider to change the maximum capacity of the disk. Here I will expand the original 20GB to 60GB.

insert image description here
Start the virtual machine, run

sudo apt-get install gparted

Install the disk partition tool gparted.

If you get an error "gparted : Depends: libglibmm-2.4-1v5 (>= 2.54.0) but 2.46.3-1 is about to be installed", "The error cannot be fixed because you asked some packages to remain as they are and they broke Dependencies in the software package", in addition to other solutions on the Internet, please open the system settings - software and updates - other software, and check whether the configured source matches your Ubuntu version! ! The blogger kept prompting various conflicts during the installation step and tossed for a long time, but found that bionic was configured on Ubuntu 16, which is the source of Ubuntu 18! ! I really want to kill myself who copied other blogs directly when configuring the environment two months ago (angry)

After the installation is complete, start gparted.

sudo gparted

However, I found that the partition structure looks like this:

insert image description here
Speechless, the swap partition separates the main partition from the newly expanded capacity. Right-click the sda1 partition to see that we cannot directly expand to the unallocated capacity like other tutorials on the Internet. Shut down the system, return to the virtual media management of the VirtualBox console, create a new VDI virtual hard disk, and select the capacity as the expanded capacity. Then in the virtual machine settings, mount this new hard disk as well.

insert image description here
Start the virtual machine, and now you can see that two hard disks are mounted in gparted, of which sdb is not partitioned.

insert image description here
First create a partition table for it in gparted, then run in the terminal:

sudo dd if=/dev/sda of=/dev/sdb bs=1M

This operation will copy all the data in the original hard disk to the new hard disk. It will take a while according to the data volume of your original disk, and then you can see that sdb has become the same distribution as sda in gparted. In terminal:

sudo swapoff -a

Temporarily turn off the swap function, and then delete the swap partition in the middle and the extended partition outside in sdb, and then you can expand the sdb1 partition on the left. My original swap partition here is about 1GB, so the size of sdb1 is pulled to the remaining 1GB or so. Then create a new one on the remaining unallocated space of about 1GB, and select linux-swap as the file system, so the sdb becomes the following structure, and the main partition is expanded successfully:

insert image description here

Restart the swap partition:

sudo swapon -a

Enter the command top to check the memory status. If the swap display capacity is 0, double-click the newly created swap partition in gparted, record the UUID
insert image description here
and then

sudo gedit /etc/fstab

Change the UUID of the swap partition in the second line to that of the new partition
insert image description here
, then sudo swapon -a again, and you can see that the swap partition is enabled on top.

Shut down the virtual machine, unmount the original mounted disk Master, and only keep the new disk Master_1, then turn it on again, open gparted, and you can see that the new disk has replaced the original sda.

insert image description here
After confirming that the operation is stable, the original disk can be deleted or kept as a backup.

Guess you like

Origin blog.csdn.net/Altair_alpha/article/details/128203527