1. Expansion settings in VM
The current CentOS must be closed, otherwise the extension button will be gray.
The value entered must be greater than the current disk capacity. Then click Expand and wait for the expansion to complete. A pop-up box will appear. Click OK to continue confirming.
2. Operation of CentOS expansion – disk partition
The first step of setup is completed. Then start CentOS and enter the start operation:
1.df -h
View the disk. I found that nothing has changed. The capacity of /dev/mapper/centos-root
is 17G, haha.
[root@centos1 ~]# df -h File System Capacity Used Available % Used Mount Points devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 12M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/centos-root 17G 13G 4.8G 72% / /dev/sda1 1014M 154M 861M 16% /boot tmpfs 782M 0 782M 0% /run/user/0 [root@centos1 ~]#
2. fdisk -l
, view disk partitions. This time I saw it, the first line has been output Disk /dev/sda: 107.4 GB
[root@centos1 ~]# fdisk -l Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000ac81b Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes [root@centos1 ~]#
3. fdisk /dev/sda
, perform partition operation on the newly added disk (select the newly added disk). The following parameters are the disks queried above. Currently, this is the only one due to capacity expansion.
[root@centos1 ~]# fdisk /dev/sda Welcome to fdisk (util-linux 2.23.2). Changes will stay in memory until you decide to write them to disk. Think twice before using write commands. Command (enter m for help):
If there are multiple disks, you need to pay attention to distinguish them. For example, the following is what I will not be able to study later.
Expanding disks and adding disks. The commands are roughly the same, just don’t merge them in the end.
4. Enter p
to view the partition information. You can see that there are only two partitions. Blocks
adds up to about 20G. However, 107.4G is displayed at the top of the disk, indicating that there is disk space to be partitioned.
Command (enter m for help): p Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000ac81b Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM Command (enter m for help):
You can see that there is a command prompt when you enter m. In the spirit of exploration, you can take a look at what you see (has been translated into Chinese and the description has been corrected~):
Command (enter m for help): m
a toggle bootable flag
b Edit BSD disk labels
c Toggle DOS compatibility flag
d Delete partition
g creates a new
Empty GPT partition table
G Create an IRIX (SGI) partition table
l List known partition types
m Print out this command menu
n Add new partition
o Create a new empty DOS partition table
p Print partition table
q Quit without saving changes
s creates a new empty Sun
disk label
t Modify the system id of the partition
u Change display/input units
v Verify partition table
w Write table to disk and exit
x Extra features (for experts only)
5. The free space will be partitioned.
Enter n
to create a new partition.
Continue to enter p
or press Enter directly. We use the default partition type – primary partition.
Continue to enter 3
or press Enter directly. Use the default partition number – 3
Start sector, just press Enter to default, we don’t need to divide too much.
Last sector, just press Enter to default, we don’t need to divide too much.
The final output of “Partition ? has been set to Linux type, and the size is set to ? GiB” means that the partition is successful. These two values may be related to the actual partition number and the size of your own expansion.
Command (enter m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) eextended # Tutorial description: Enter directly Select (default p): Using default response p # Tutorial description: Enter directly Partition number (3,4, default 3): 3 # Tutorial description: Enter directly Starting sector (41943040-209715199, default is 41943040): The default value 41943040 will be used # Tutorial description: Enter directly Last sector, + sector or + size{K,M,G} (41943040-209715199, default is 209715199): The default value of 209715199 will be used Partition 3 is set to Linux type and size to 80 GiB Command (enter m for help):
When partitioning a disk in CentOS, there are two types to choose from: primary partition (Primary) and
Extended partition.
Primary partition (Primary):
The primary partition is a basic partition on the disk that can be used directly by the file system. Each hard drive can only contain up to 4 primary partitions, including a
Active partition (the active partition is the boot
operating system partition).
Extended partition (Extended):
An extended partition is a special type of partition that allows the creation of more logical partitions on the hard drive. Each hard drive can have only one extended partition, and it can contain multiple
Logical partition. Logical partitions are part of the file system, but they must be accessed through extended partitions.
The information printed above”
p primary (2 primary, 0 extended, 2 free)” means that there are 2 primary partitions and 2 free spaces on the disk, and there are no extended partitions.
6. Enter p
to view the partition results. You can see that a new /dev/sda3
partition has been added. The size of Blocks
is about 80G.
Command (enter m for help): p Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x000ac81b Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM /dev/sda3 41943040 209715199 83886080 83 Linux Command (enter m for help):
7. Enter w
to write the previous partition operation to the disk, indicating that the save will take effect. If you give up, enter q
.
Command (enter m for help): w # Tutorial description: Printing has been modified successfully! The partition table has been altered! # Tutorial description: Call ioctl() to re-read the partition table. Calling ioctl() to re-read partition table. # Tutorial description: Restart to take effect. 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) Synchronizing disks. [root@centos1 ~]#
WARING: Don’t panic when you see a warning. If you don’t understand it in English, you will see “Device or resource busy” in Chinese! I wondered if I had failed.
It did fail. After the partition was completed, I tried to read the new partition. The result was that I couldn’t read it. I told you that it is still using the old one. If you want to use the new one, execute it.
partprobe(8)
or
kpartx(8)
or
reboot
. Simply restart and it’ll be fine.
8. Enter reboot
to restart CentOS to make the configuration take effect
[root@centos1 ~]# reboot Connection closing...Socket close. Connection closed by foreign host. Disconnected from remote host(192.Like.Collect.+Follow) at 23:26:05. Type `help' to learn how to use Xshell prompt.
The restart was successful soon and we continued working on it. . .
Draw a key point: Learn about disks and volume groups.
LVM
LVM (Logical Volume Manager) volume group is a logical storage management mechanism in Linux systems. It can abstract multiple physical hard disks or partitions into a logical volume for convenient storage management. LVM volume groups allow you to dynamically add, delete and merge physical volumes, and dynamically adjust the size of logical volumes without having to care about the location and number of physical hard disks or partitions.
The basic components of LVM include:
- Physical Volume (Physical Volume, PV): refers to the physical storage device in LVM, such as a hard disk or partition. Before using LVM, physical storage devices need to be initialized as LVM physical volumes and assigned a unique identifier.
- Volume Group (VG): A logical group consisting of one or more physical volumes. In LVM, physical volumes can be allocated to one or more volume groups, and physical volumes in a volume group can be dynamically added, deleted, or merged to facilitate storage capacity management.
- Logical Volume (Logical Volume, LV): Logical volume refers to the logical storage device in LVM, which is dynamically allocated from the physical volume in the volume group. Logical volumes can be formatted as file systems and used like ordinary physical hard disk partitions.
The main advantage of LVM is that it provides a more flexible storage management method that can dynamically add, delete, merge and move storage space without caring about the location and number of physical hard disks or partitions. In addition, LVM also supports advanced storage functions such as snapshots, mirroring and migration, which can meet the needs of different application scenarios.
~~~~~~~~~~ Looked confused ~~~~~
What are the connections and differences between volume groups, disks, and partitions?
A disk is a computer storage device that usually consists of one or more platters that can store data and information. Disks can be partitioned, dividing a disk into multiple parts, and each part can have a different file system to better manage and use storage space.
Connection: Both volume groups and disks are related to storage. Disks are physical and volume groups are logical. A volume group is a logical storage device composed of multiple physical volumes, and a disk is a physical storage device that can be partitioned and used by a volume group. In LVM, the physical volume of a volume group can come from a partition of the disk or the entire disk, and a volume group can be composed of multiple physical volumes.
Difference: A volume group is a logical storage device in LVM. It consists of one or more physical volumes and can be regarded as a logical hard disk. It is mainly used to store and manage data. A disk is a computer storage device that can be partitioned, and each partition can have a different file system.
~~~ It’s a bit confusing, let’s sum it up ~~~~~
Disks and partitions are at the physical level and are used to better manage storage space. Different partitions can have different file systems.
Volume groups and logical volumes are at the logical level and are used to provide a more flexible and scalable storage management method.
A volume group is a collection of physical volumes that is logically a whole. They can be used to create logical volumes, and multiple logical volumes can be logically managed.
A physical volume can be understood as a number of access points. Following the access points, you will definitely find a disk that stores data. Volume groups can dynamically add, delete, merge, and move physical volumes to facilitate storage capacity management.
Logical volumes are dynamically allocated from physical volumes in a volume group and can be formatted as a file system and used like ordinary physical hard disk partitions. But it mainly manages storage space, not files, and needs to be distinguished from the file system. (Volume Group->Physical Volume->Logical Volume->File System)
The file system is mainly used to store and access files, and manages the computer’s storage devices (such as hard drives, partitions, files, etc.) and files. Linux supports a variety of file systems, such as Ext4, XFS, Btrfs, etc. Logical volumes can be used as the basis of file systems, providing a more flexible way to build file systems.
~~~ Grounding point ~~~~~:
Usually, there are things like mounting, mounting disks, adding disks, expanding capacity, etc. for Linux servers. That’s pretty much what I do.
In the mount operation, a disk will be added to the volume group, and a new logical volume will be added or space will be added to the existing logical volume, thereby expanding the storage capacity of the system. This hanging disk operation is dynamic and can easily add, delete, merge and move storage space without caring about the location and number of physical hard disks or partitions.
~~~Common commands: ~~~~~~~ Now that you know VG, PV, and LV, let’s take a look at the common commands: ~~~~~
VG related commands: Volume Group (Volume Group, VG)
vgcreate
: Create a volume group.vgdisplay
: Display volume group information.vgrename
: Rename the volume group.vgremove
: Delete the volume group.vgextend [option] VG name PV name 1 [PV name 2...]
: Add a new physical volume (Physical Volume).vgs
: Displays all volume group information in the system.vgscan
: Scans the system for volume groups.vgchange
: Change the attributes of the volume group.vgsplit
: Split the volume group into two volume groups.vgmerge
: Merge two volume groups into one volume group.vgreduce
: Reduce volume group size
PV related commands: Physical Volume (Physical Volume, PV)
pvcreate
: Create a physical volume.pvdisplay
: Display physical volume information.pvremove
: Removes a physical volume from a volume group.pvmove /dev/sdb1 /dev/sdb
will move the data in/dev/sdb1
to/dev/sdb
, thereby reducing the/dev/sdb1.
pvreduce
: Reduce the size of a disk volume
LV related commands: Logical Volume (Logical Volume, LV)
lvcreate
: Create a logical volume.lvdisplay
: Display logical volume information.lvrename
: Rename a logical volume.lvremove
: Delete a logical volume.lvreduce
: Reduce the size of a logical volumelvextend
: Extend the size of the logical volume
To use the above command, you can enter the command on the command line and add the --help
parameter to view the help document.
3. Operation of CentOS Expansion – Volume Group Expansion
9. vgdisplay
View the current volume group. VG Name
is centos
[root@centos1 ~]# vgdisplay ---Volume group --- VG Name centos System ID Format lvm2 Metadata Areas 1 Metadata Sequence No. 3 VG Access read/write VG Status resizable MAXLV 0 Cur LV 2 OpenLV2 Max PV 0 Cur PV 1 Act PV 1 VG Size <19.00 GiB PE Size 4.00 MiB Total PE 4863 AllocPE/Size 4863/<19.00 GiB FreePE/Size 0/0 VG UUID aISHGf-rAs9-4VKV-IGMH-1eQk-AscS-a1t6aM [root@centos1 ~]#
vgdisplay
The output shows information about the volume group (Volume Group). The name of the volume group is “centos”, which consists of a physical volume with a size of 19.00 GiB.
Output information translation:
- VG Name: The name of the volume group.
- Format: The format of the volume group, here is lvm2.
- Metadata Areas: The number of metadata areas in the volume group.
- Metadata Sequence No: Volume group metadata sequence number.
- VG Access: The access method of the volume group, here is read/write, which means it can read and write.
- VG Status: The status of the volume group. Here it is resizable, which means it can be resized.
- MAX LV: Maximum number of logical volumes.
- Cur LV: The current number of logical volumes.
- Open LV: Number of open logical volumes.
- MAX PV: Maximum number of physical volumes.
- Cur PV: The current number of physical volumes.
- Act PV: Number of activated physical volumes.
- VG Size: The total size of the volume group.
- PE Size: The size of the physical Extent.
- Total PE: The total number of physical Extents.
- Alloc PE/Size: The number of allocated physical Extents and their corresponding sizes.
- Free PE/Size: The number of unallocated physical Extents and their corresponding sizes.
- VG UUID: Unique identifier of the volume group.
10.pvcreate /dev/sda3
Create a physical volume.
[root@centos1 ~]# pvcreate /dev/sda3 Physical volume "/dev/sda3" successfully created. [root@centos1 ~]#
11. vgextend centos /dev/sda3
Add the physical volume to the volume group
[root@centos1 ~]# vgextend centos /dev/sda3 Volume group "centos" successfully extended [root@centos1 ~]#
12.vgdisplay
View the added results.
Comment out the unchanged ones, and first look at the most important Free PE / Size
unallocated ones, which have become about 80G.
[root@centos1 ~]# vgdisplay ---Volume group --- # VG Name centos # System ID # Format lvm2 # Tutorial description: Number of volume group metadata areas 1=》2 Metadata Areas 2 # Tutorial description: Volume group metadata serial number 3=》4 Metadata Sequence No. 4 # VG Access read/write # VG Status resizable #MAXLV 0 #curLV2 #OpenLV2 #MaxPV0 # Tutorial description: Current number of physical volumes 1=》2 Cur PV 2 # Tutorial description: Number of activated physical volumes 1=》2 Act PV 2 # Tutorial description: Total volume group size '<19.00 GiB' =》'98.99 GiB' VG Size 98.99 GiB # PE Size 4.00 MiB Total PE 25342 # Alloc PE / Size 4863 / <19.00 GiB Free PE/Size 20479/<80.00 GiB VG UUID aISHGf-rAs9-4VKV-IGMH-1eQk-AscS-a1t6aM [root@centos1 ~]#
13. Before allocating to a logical volume, check the logical volume name.
If we want to add the expansion space to the root directory /
, we must first find the logical volume file system.
Enter df -h
and see that the mount point is the root directory /
and the file system is /dev/mapper/centos-root
[root@centos1 ~]# df -h File System Capacity Used Available % Used Mount Points devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 12M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/centos-root 17G 13G 4.8G 72% / /dev/sda1 1014M 154M 861M 16% /boot tmpfs 782M 0 782M 0% /run/user/0 [root@centos1 ~]#
You can see that there are only two file systems. Others with tmp are temporary and can be ignored.
Enter lvs /dev/mapper/centos-root
to see the logical volume information of the file system corresponding to the root directory. 【! This was added after I wrote the tutorial after the operation was completed, and it has been expanded]
[root@centos1 ~]# lvdisplay /dev/mapper/centos-root --- Logical volume --- LV Path /dev/centos/root LV Name root VG Name centos LV UUID lnijLZ-BFbj-d0L8-gpil-s9GN-6Dk8-0LIKIH LV Write Access read/write LV Creation host, time localhost.localdomain, 2021-09-29 16:10:31 + 0800 LV Status available # open 1 LV Size <96.00 GiB Current LE 24575 Segments 2 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0
Enter lvs to view all logical volume information. You can see the logical volume LV root seen above, which belongs to the volume group VG centos.
Enter vgs
, pvs
to continue to see more information about the volume group and the corresponding physical volume.
[root@centos1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- <96.00g swap centos -wi-ao---- 2.00g [root@centos1 ~]# vgs VG #PV #LV #SN Attr VSize VFree centos 2 2 0 wz--n- 98.99g 1020.00m [root@centos1 ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- <19.00g 0 /dev/sda3 centos lvm2 a-- <80.00g 1020.00m [root@centos1 ~]#
14. lvextend -L + 79G /dev/mapper/centos-root
Allocate free space to logical volumes
The size of the number depends on your own above. In step 12, you can see Free PE / Size 20479 / <80.00 GiB
, which is less than 80G, so write 79
[root@centos1 ~]# lvextend -L + 79G /dev/mapper/centos-root Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <96.00 GiB (24575 extents). Logical volume centos/root successfully resized. [root@centos1 ~]#
See details of logical volume centos/root from
<17.00 GiB (4351 extents) to <96.00 GiB (24575 extents)
Allocation successful
15.pvdisplay
View allocation results
[root@centos1 ~]# pvdisplay ---Physical volume --- PV Name /dev/sda2 VG Name centos PV Size <19.00 GiB / not usable 3.00 MiB Allocable yes (but full) PE Size 4.00 MiB Total PE 4863 Free PE 0 Allocated PE 4863 PV UUID Y1Oc21-XEm2-8Oj6-AjfM-ZjV3-SX4l-UFWnjw ---Physical volume --- PV Name /dev/sda3 VG Name centos PV Size 80.00 GiB / not usable 4.00 MiB Allocable yes PE Size 4.00 MiB Total PE 20479 Free PE 255 Allocated PE 20224 PV UUID S5deMQ-Ldz9-s3Kp-YldN-fIzG-aFFm-jHdG8b [root@centos1 ~]#
As you can see, there are two physical volumes in total, both under the centos volume group (VG name), and then we see our initial partition
/dev/sda3
Yes
PV Size 80.00 GiB
, blocks allocated
Allocated PE 20224
, calculate
20224*4/1024=79G
and no block is allocated
Free PE 255
, calculate it
255*4 =1024MB
It means that they have been allocated from the physical volume to the logical volume.
16. Expanding the file system
At this time, only the storage space of the logical volume where the file system is located has been expanded, but the file system still does not know the expansion when managing files, so it needs to be informed.
(1) cat /etc/fstab | grep centos-root
Check the file system type
[root@centos1 ~]# cat /etc/fstab | grep centos-root /dev/mapper/centos-root/xfs defaults 0 0 [root@centos1 ~]#
You can see that it is xfs
, so use the xfs_growfs
command.
If it is an Ext4
file system, you can use the resize2fs
command to extend the file system.
(2) xfs_growfs /dev/mapper/centos-root
expands the file system.
[root@centos1 ~]# xfs_growfs /dev/mapper/centos-root meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=4455424, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 4455424 to 25164800 [root@centos1 ~]#
You can see that the data data block has been expanded
17. df -h
View the final results
[root@centos1 ~]# df -h File System Capacity Used Available % Used Mount Points devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 12M 3.9G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/mapper/centos-root 96G 13G 84G 13% / /dev/sda1 1014M 154M 861M 16% /boot tmpfs 782M 0 782M 0% /run/user/0 [root@centos1 ~]#