Previously, a CentOS VIRTUAL machine was created using VirtualBox with 40GB of disk space allocated dynamically with LVM file system. It was later discovered that the disk capacity was insufficient. After some trouble, the virtual disk capacity of the VirtualBox virtual machine was increased and the CentOS LVM used it. For the record.

Windows CMD command
  • CD to the vboxmanage.exe directory.
  • Run.\ vboxmanage. exe modifyhd your_hard_disk. vdi –resize SIZE_IN_MB
  • The parameter your_hard_disk. vdi is the absolute path of the VirtualBox virtual disk image file that you want to modify. The SIZE_IN_MB parameter indicates the new disk capacity, in MB.
  • The virtual machine must be shut down when the virtual disk is resized.
View the new disk space
[root@localhost ~]# df -h File system capacity used Available Used % mount point /dev/mapper/centos-root 37G 25G 13G 67% / devtmpfs 1.9g 0 1.9g 0% /dev TMPFS 1.1g 0 1.1g 0% /dev/shm TMPFS 1.1g 8.6m 1.1g 1% /run TMPFS 1.1g 0 1.1g 0% /sys/fs/cgroup /dev/sda1 1014M 144M 871M /run/user/0 [root@localhost ~]# fdisk -l /dev/sda: Units = Sector of 1 x 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/Best) : 512 bytes / 512 bytes Disk label type: DOS Disk Identifier: 0x000B4001 Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 83884031 40892416 8e Linux LVM disk /dev/mapper/centos-root: Units = Sector of 1 x 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/Best) : 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: Units = Sector of 1 x 512 = 512 bytes Sector size (logical/physical) : 512 bytes / 512 bytes I/O size (minimum/Best) : 512 bytes / 512 bytesCopy the code
  • After the VM restarts, run the df -h and fdisk -l commands to check the disk space. You can see that the disk space has been expanded to 128 GB, but the capacity of /dev/mapper/centos-root is still 37 GB.
Creating a new partition
  • Run the fdisk /dev/sda command to create a new partition. Note that the partition number 8E, which represents the Linux LVM, is used as the ID.
[root@localhost ~]# fdisk /dev/sda welcome to fdisk (util-linux 2.23.2) Changes stay in memory until you decide to write them to disk. Think twice before using write commands. N Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default P): P Partition number (3,4, default 3) : 3 Start sector (83884032-251658239, default 83884032) : Will use the default value 83884032 Last sector, + sector or +size{K,M,G} (83884032-251658239, default 251658239) : Partition 3 is set to Linux type and size is set to 80 GiB command (enter m for help) : t Partition number (1-3, default 3) : 3 Hex code (enter L to list all codes) : 8E The partition "Linux" has been altered to The "Linux LVM" command (enter 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: The device or resource is 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) is synchronizing disks.Copy the code
  • If there is a prompt to set the size or something, press enter. When you are done, restart the system if you receive the same WARNING as above.
Adjust the LVM size
  • The Volume Group Name is centos. VG Status is resizable.
[root@localhost ~]# 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 <39.00 GiB PE Size 4.00 MiB Total PE 9983 Alloc PE/Size 9982/38.99 GiB Free PE/Size 1/4.00 MiB VG UUID XW42Pm-PvhZ-ubgy-xMdX-53VI-QfmL-Lc7n8oCopy the code
  • Run pvcreate /dev/sda3 to create the newly allocated disk space as a new physical volume.
[root@localhost ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.
Copy the code
  • Run vgextend centos /dev/sda3 to extend LVM centos with new physical volumes.
[root@localhost ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
Copy the code
  • Run lvextend /dev/mapper/centos-root /dev/sda3 to extend the logical volume centos-root of the LVM.
[root@localhost ~]# lvextend /dev/mapper/centos-root /dev/sda3
  Size of logical volume centos/root changed from 36.99 GiB (9470 extents) to 116.99 GiB (29950 extents).
  Logical volume centos/root successfully resized.
Copy the code
  • CentOS6 Run the resize2fs /dev/mapper/centos-root command, and CentOS7 run the xfs_growfs /dev/centos/root command to adjust the file system size of the logical volume.
[root@localhost ~]# xfs_growfs /dev/centos/root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=2424320 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=9697280, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=4735,  version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 9697280 to 30668800Copy the code
See the effect
[root@localhost ~]# lvscan ACTIVE '/dev/centos/swap' [2.00 GiB] inherit ACTIVE '/dev/centos/root' [116.99 GiB] inherit [root@localhost ~]# df -h File system capacity used Available Used % Mount point /dev/mapper/centos-root 117G 25G 93G 22% / devtmpfs 1.9g 0 1.9g 0% /dev TMPFS 1.1g 0 1.1g 0% /dev/shm TMPFS 1.1g 8.4m 1.1g 1% /run TMPFS 1.1g 0 1.1g 0% /sys/fs/cgroup /dev/sda1 1014M 144M 871M  15% /boot tmpfs 380M 0 380M 0% /run/user/0Copy the code