VMware virtual machine extends Linux root disk space (Centos)

Recently, Centos was used on VMware virtual machine, and it was found that the disk space of the virtual machine was insufficient. The disk space of the root directory is successfully expanded through the following steps

1. Turn off Centos, select Edit virtual machine settings, hard disk, and select "extend" in the utility

Fill in the expanded disk capacity

After clicking expand, VMware will prompt that the disk has been successfully expanded. You must repartition the disk and expand the file system from within the client operating system. In other words, the disk space expanded here is not available in the operating system and has not taken effect. It can only be used after setting in the operating system.

2. Start the client operating system Centos and check the disk condition

Input instruction

fdisk -l

The display results are as follows

Disk /dev/loop0: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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/loop1: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 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: 0x000c8278

   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        1306     9972736   8e  Linux LVM

Disk /dev/mapper/vg_test001-lv_root: 9135 MB, 9135194112 bytes
255 heads, 63 sectors/track, 1110 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_test001-lv_swap: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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/docker-253:0-264224-pool: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 65536 bytes
Disk identifier: 0x00000000

Among them, you can see the current partition as follows

   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        1306     9972736   8e  Linux LVM

3. Create partition sda3

Input instruction

fdisk /dev/sda

Then create the zone according to the command prompt. See the notes below # later

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): n   # Enter n to create a new partition
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3   # Input 3 means sda3, because there are already two in front
First cylinder (1306-4568, default 1306):     # Enter, press default
Using default value 1306
Last cylinder, +cylinders or +size{K,M,G} (1306-4568, default 4568):   # Enter, press default
Using default value 4568

Command (m for help): w     # Enter w to save and exit 
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.

Check the partition again

fdisk -l

Then you can see

Disk /dev/loop0: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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/loop1: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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/sda: 37.6 GB, 37580963840 bytes
255 heads, 63 sectors/track, 4568 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: 0x000c8278

   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        1306     9972736   8e  Linux LVM
/dev/sda3            1306        4568    26206700   83  Linux

Disk /dev/mapper/vg_test001-lv_root: 9135 MB, 9135194112 bytes
255 heads, 63 sectors/track, 1110 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_test001-lv_swap: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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/docker-253:0-264224-pool: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 65536 bytes
Disk identifier: 0x00000000

Among them, there are three partitions, but they are not mounted and cannot be used

   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        1306     9972736   8e  Linux LVM
/dev/sda3            1306        4568    26206700   83  Linux

4. Restart the operating system

reboot

5. Format the new partition as ext3

Input instruction

mkfs -t ext3 /dev/sda3

The result is

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1638400 inodes, 6551675 blocks
327583 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
200 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

6. The hard disk is initialized as a physical partition using the LVM instruction

lvs
pvcreate /dev/sda3

7. Add physical volumes to the volume group to increase the capacity of the volume group

View the lvm group name and enter the command df -h

[root@test001 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_test001-lv_root
                      8.3G  5.4G  2.5G  69% /
tmpfs                1001M     0 1001M   0% /dev/shm
/dev/sda1             477M   54M  394M  12% /boot

Where, vg_test001 is the group name of the root

Enter instructions to increase the capacity of the volume group

vgextend vg_test001 /dev/sda3

8. View scalable space size

Enter an instruction to display the metadata information of the LNM volume group

vgdisplay

The result is

  --- Volume group ---
  VG Name               vg_test001
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               34.50 GiB
  PE Size               4.00 MiB
  Total PE              8831
  Alloc PE / Size       2434 / 9.51 GiB
  Free  PE / Size       6397 / 24.99 GiB
  VG UUID               JuPKc8-Ydps-1bsN-0Wld-fbYS-0Hd2-NgvX1q

Free PE / Size is the free space available for allocation, with a maximum of 24.99G. Enter a value less than this when expanding

9. Expand disk space

lvextend -L+24G /dev/mapper/vg_test001-lv_root /dev/sda3

10. Use the e2fsck command to check for file system errors

e2fsck -a /dev/mapper/vg_test001-lv_root

11. Use the resize 2fs instruction to increase or shrink the size of the unloaded "ext2/ext3" file system

resize2fs /dev/mapper/vg_test001-lv_root

12. View the results after expanding the space

[root@test001 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_test001-lv_root
                       32G  5.4G   26G  18% /
tmpfs                1001M     0 1001M   0% /dev/shm
/dev/sda1             477M   54M  394M  12% /boot

It can be seen that the disk space under the root directory has been expanded. It's done!

Keywords: CentOS

Added by machina3k on Sun, 30 Jan 2022 12:33:58 +0200