Resizing the disk of a VirtualBox virtual machine

Resizing the disk of a VirtualBox virtual machine

Created a CentOS virtual machine with VM VirtualBox, divided 40GB of disk space, used the method of dynamic allocation of storage, and the file system is LVM. After tossing for a while, I found that the disk capacity is not enough. After reading some information, I finally increased the disk capacity of the virtual machine of VirtualBox and let the LVM of CentOS take advantage of it.

First look at the partition table of the virtual disk in the virtual machine:

 

[root@10 ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM
/dev/sda3            1045        5221    33549074+  8e  Linux LVM

Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@10 ~]#

 

Disk Management for VirtualBox

When resizing a virtual disk, the virtual machine must be powered off!

Let's use VirtualBox's command line to adjust the virtual machine's disk allocation from 40GB to 50GB.

The method of resizing the virtual machine disk is clearly given in the VirtualBox documentation.

The modifymedium command is used to adjust the disk size. The object can be the uuid of the virtual machine, or it can directly specify the virtual machine disk filename (full path) to be resized. It should be noted that if VirtualBox is installed without adding its path to the system path, you need to specify the path or switch to the installation path when running the command line, such as,

The above command adjusts the virtual machine disk CentOS6.3.vdi in the F:\VirtualBoxImgs directory to (parameter –resize) 51200, in MB.

If you want to see the corresponding uuid, you can use the command:

The above instructions are verified to work on Win10+VirtualBox 5.0.10.

2. CentOS LVM management

 

[root@10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/ dev / mapper / vg_yuxuechengcentos6-lv_root
                       37G 12G 26G 31% /
tmpfs                 499M   76K  499M   1% /dev/shm
/dev/sda1             477M   51M  397M  12% /boot
/dev/sr0               57M   57M     0 100% /media/VBOXADDITIONS_5.0.10_104061
[root@10 ~]#

 Restart the virtual machine. Let's take a look at the disk situation:

 

 

[root@10 ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM
/dev/sda3            1045        5221    33549074+  8e  Linux LVM

Disk /dev/mapper/vg_yuxuechengcentos6-lv_root: 40.3 GB, 40298872832 bytes
255 heads, 63 sectors/track, 4899 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/vg_yuxuechengcentos6-lv_swap: 2113 MB, 2113929216 bytes
255 heads, 63 sectors/track, 257 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@10 ~]#

 Now the disk size is 53680MB. The disk usage is as follows, you can see that the total size is still 40G configuration.

 

 

[root@10 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/ dev / mapper / vg_yuxuechengcentos6-lv_root
                       37G 12G 26G 31% /
tmpfs                 499M   76K  499M   1% /dev/shm
/dev/sda1             477M   51M  397M  12% /boot
/dev/sr0               57M   57M     0 100% /media/VBOXADDITIONS_5.0.10_104061
[root@10 ~]#

 Try creating a 30G block file with the dd command:

 

[root@10 ~]# dd if=/dev/zero of=1.0G.img bs=1M count=30000
dd: writing `1.0G.img': No space left on device
26223+0 records in
26222+0 records out
27496136704 bytes (27 GB) copied, 104.717 s, 263 MB/s
[root@10 ~]#

 

The prompt says that the disk space is insufficient (No space left on device). Although we gave the virtual machine enough space, the system in the virtual machine (here, CentOS) did not make use of this part of the space. It can be seen that the disk space in the virtual machine is only 37G, and the originally allocated 50G has not been used up. This also shows that even if you use dynamic allocation of disk size when creating a virtual machine, the system in the virtual machine will not automatically modify its own disk. In normal use, you may encounter low disk space warnings in the virtual machine even if the virtual machine's virtual disk is large enough.

 

The following is to adjust the file system of the virtual machine operating system to make use of all the space.

Create spare disk as sda4

Restart the virtual machine, log in to CentOS, and use fdisk to create a new partition from the free space of the virtual disk. Note that the partition number 8e, which represents Linux LVM, is used as the ID.

 

[root@10 ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Selected partition 4
First cylinder (5222-6527, default 5222):
Using default value 5222
Last cylinder, +cylinders or +size{K,M,G} (5222-6527, default 6527):
Using default value 6527

Command (m for help): t
Partition number (1-4): 4
Hex code (type L to list codes): 8e
Changed system type of partition 4 to 8e (Linux LVM)

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.
[root@10 ~]#

 After the above command is executed, you can use fdisk -l /dev/sda to check the result to see if sda4 is successfully created and marked as LVM, if not, you need to restart.

 

 

[root@10 ~]# fdisk -l /dev/sda

Disk /dev/sda: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000698e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1045     7875584   8e  Linux LVM
/dev/sda3            1045        5221    33549074+  8e  Linux LVM
/dev/sda4            5222        6527    10490445   8e  Linux LVM
[root@10 ~]#

 Restart after execution.

 

Resize LVM

First look at the current Volume Group:

 

[root@10 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_yuxuechengcentos6
  System ID             
  Format lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  With LV 2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               39.50 GiB
  PE Size               4.00 MiB
  Total PE 10112
  Alloc PE / Size       10112 / 39.50 GiB
  Free  PE / Size       0 / 0   
  VG UUID               8oim1v-kbuK-5RjL-MchZ-PLqp-3Uco-OCTXMu
   
[root@10 ~]#

The name is vg_yuxuechengcentos6, it is resizable, and the current size is 39.50GB.

View active LVM volumes:

[root@10 ~]# lvscan
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_root' [37.53 GiB] inherit
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[root@10 ~]#

 First create the allocated new disk space as a new physical volume:

[root@localhost ~]# pvcreate /dev/sda4
  Writing physical volume data to disk "/dev/sda4"
  Physical volume "/dev/sda4" successfully created
[root@localhost ~]#

 Then use the new physical volume to extend LVM's vg_yuxuechengcentos6:

[root@localhost ~]# vgextend vg_yuxuechengcentos6 /dev/sda4
  Volume group "vg_yuxuechengcentos6" successfully extended
[root@localhost ~]#

 Then expand the LVM logical volume vg_yuxuechengcentos6-lv_root:

[root@localhost ~]# lvextend /dev/mapper/vg_yuxuechengcentos6-lv_root /dev/sda4
  Extending logical volume lv_root to 47.53 GiB
  Logical volume lv_root successfully resized
[root@localhost ~]#

 Finally, resize the logical volume filesystem,

[root@localhost ~]# resize2fs /dev/mapper/vg_yuxuechengcentos6-lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg_yuxuechengcentos6-lv_root is mounted on /; on-line resizing required
old desc_blocks = 3, new_desc_blocks = 3
Performing an on-line resize of /dev/mapper/vg_yuxuechengcentos6-lv_root to 12460032 (4k) blocks.
The filesystem on /dev/mapper/vg_yuxuechengcentos6-lv_root is now 12460032 blocks long.

[root@localhost ~]#

Executing resize2fs on CentOS7 will report an error, as shown below, this /dev/centos/root is different from the previous one, because this is added later, and this is the record processed on CentOS7.

[root@localhost linus_dev]# resize2fs /dev/centos/root
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block while trying to open /dev/centos/root
Couldn't find valid filesystem superblock.

 The reason why it cannot be found is that the default file system in CentOS 7 is XFS, which is different from CentOS 6, so the command to be used here is xfs_growfs instead of resize2fs, and the successful execution of CentOS is recorded as follows:

[root@localhost linus_dev]# xfs_growfs /dev/centos/root
meta-data=/dev/mapper/centos-root isize=256    agcount=4, agsize=1799936 blks
         =                       sectsz=512   attr=2, projid32bit=1
         = crc = 0 finobt = 0
data     =                       bsize=4096   blocks=7199744, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=3515, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 7199744 to 12441600
[root@localhost linus_dev]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   48G   16G   32G  34% /
devtmpfs                 488M     0  488M   0% /dev
tmpfs                    497M     0  497M   0% /dev/shm
tmpfs                    497M  6.7M  491M   2% /run
tmpfs                    497M     0  497M   0% /sys/fs/cgroup
/dev/sda1                497M  163M  335M  33% /boot
[root@localhost linus_dev]#

After adjustment, take a look at the effect:

[root@localhost ~]# lvscan
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_root' [47.53 GiB] inherit
  ACTIVE            '/dev/vg_yuxuechengcentos6/lv_swap' [1.97 GiB] inherit
[root@localhost ~]#

 

Also, several useful LVM instructions (lvm2):

 

  • lvmdiskscan
  • vgdisplay
  • pvdisplay
  • lvdispay
  • vgcreate
  • pvcreate
  • lvscan
  • pvscan
  • vgscan

I am using CentOS system here. If it's Windows it's even easier, you can use Windows' Disk Management to resize the partition to take up all the available disk space.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326994507&siteId=291194637