Virtual machine LVM volume expansion practice

Virtual machine LVM volume expansion actual operation

One, LVM

LVM is the abbreviation of Logical Volume Manager. It is a mechanism for managing disk partitions in the Linux environment. LVM is a logical layer built on hard disks and partitions to improve the flexibility of disk partition management Sex. LVM is a logical layer added between the disk partition and the file system to shield the lower layer disk partition layout for the file system, provide an abstract volume, and establish a file system on the volume. Physical volume (physical volume) refers to a hard disk partition or a device that logically has the same function as a disk partition (such as RAID). It is the basic storage logical block of LVM, but is not compatible with basic physical storage media (such as partitions, disks, etc.). ) Comparison, but it contains management parameters related to LVM.
Structure : The entire LVM structure is divided into: "physical volume, physical volume partition, volume group, logical volume", let me explain these four structures in detail below:

Physical Volume : Physical Volume, referred to as PV, a physical volume is just a physical storage medium with LVM management data added to it. To use the LVM system, first initialize the disk to be used for LVM. The purpose of initialization is to identify the disk or partition as a physical volume of LVM. Use the pvcreate command to mark a disk as an LVM physical volume.
Physical Partition : Physical Extents, PE for short, LVM calls each physical volume an addressable storage unit of a physical partition. The size of the storage unit is usually a few MB. The beginning part of the disk is the LVM metadata, and then starting from the index of zero, the index of each physical partition is incremented by one, and the allocation is performed in order.
Volume group : Volume Group, VG for short, physical volumes can be organized into volume groups. A volume group can consist of one or more physical volumes, and there can be multiple volume groups in the system. After the volume group is created, the volume group (not the disk) is the entity that represents the data storage. Therefore, although disks were moved from one system to another in the past, after using LVM, the volume group will be moved from one system to another. For this reason, it is often convenient to create multiple volume groups on a system.
Logical partition : Logical Extents, referred to as LE, the basic allocation unit of a logical volume is called a logical partition. The logical partition is mapped to the physical partition. Therefore, if the size of the physical partition is as small as 4MB, the size of the logical partition will also be 4MB. The size of the logical volume depends on the number of logical partitions allocated.

2. Actual operation

Scenario: Extend the existing disk space of the virtual machine.

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)

1. vmware increases storage space for virtual machines

Before expansion

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        47G  2.0G   45G   5% /
devtmpfs                devtmpfs  903M     0  903M   0% /dev
tmpfs                   tmpfs     915M     0  915M   0% /dev/shm
tmpfs                   tmpfs     915M  9.4M  906M   2% /run
tmpfs                   tmpfs     915M     0  915M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  146M  869M  15% /boot
tmpfs                   tmpfs     183M     0  183M   0% /run/user/0
[root@localhost ~]# fdisk -l

Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x000ad192

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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

  • Extended operation

Turn off the virtual machine-Settings-Hard Disk (SCSI)-Expand!

After expansion (increase by 10G)
Note : As a result of the execution of fdisk -l, the space has increased by about 10G, but there is no change in df -hT at this time.

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        47G  2.1G   45G   5% /
devtmpfs                devtmpfs  903M     0  903M   0% /dev
tmpfs                   tmpfs     915M     0  915M   0% /dev/shm
tmpfs                   tmpfs     915M  9.3M  906M   2% /run
tmpfs                   tmpfs     915M     0  915M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  146M  869M  15% /boot
tmpfs                   tmpfs     183M     0  183M   0% /run/user/0
[root@localhost ~]# fdisk -l

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000ad192

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

Disk /dev/mapper/centos-root: 50.5 GB, 50457477120 bytes, 98549760 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

2. Create partition

Description :
m-help
n-new partition
t-modify the partition type (the same as the original, both are 8e, representing Linux LVM)
w-save
q-exit

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


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
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   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): p

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000ad192

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p):
Using default response p
Partition number (3,4, default 3):
First sector (104857600-125829119, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-125829119, default 125829119):
Using default value 125829119
Partition 3 of type Linux and of size 10 GiB is set

Command (m for help): p

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000ad192

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM
/dev/sda3       104857600   125829119    10485760   83  Linux

Command (m for help): t
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
1e  Hidden W95 FAT1 80  Old Minix
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 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: 0x000ad192

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   104857599    51379200   8e  Linux LVM
/dev/sda3       104857600   125829119    10485760   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@localhost ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
[root@localhost ~]# reboot

3. Create a physical volume

Note :
The Allocatable of /dev/sda3 is NO, and the PE Size is 0

[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               9X3qlf-viCH-Cymx-D1eI-vKdd-Ua14-F4bweF

[root@localhost ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.
[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               centos
  PV Size               <49.00 GiB / not usable 3.00 MiB
  Allocatable           yes
  PE Size               4.00 MiB
  Total PE              12543
  Free PE               1
  Allocated PE          12542
  PV UUID               9X3qlf-viCH-Cymx-D1eI-vKdd-Ua14-F4bweF

  "/dev/sda3" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               dR5sDM-j21d-xGiB-32WH-xhe4-Sa96-t03jtb

4. Expand the volume group

[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               <49.00 GiB
  PE Size               4.00 MiB
  Total PE              12543
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       1 / 4.00 MiB
  VG UUID               682d9M-yRsn-60BJ-cY7f-nAVI-WA24-KmnhD1

[root@localhost ~]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
[root@localhost ~]# vgdisplay
  --- Volume group ---
  VG Name               centos
  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               58.99 GiB
  PE Size               4.00 MiB
  Total PE              15102
  Alloc PE / Size       12542 / 48.99 GiB
  Free  PE / Size       2560 / 10.00 GiB
  VG UUID               682d9M-yRsn-60BJ-cY7f-nAVI-WA24-KmnhD1

5. Logical volume expansion

[root@localhost ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/centos/swap
  LV Name                swap
  VG Name                centos
  LV UUID                DmOkUA-Fq7K-eTwy-I338-jwiv-Qt7g-s9kdFW
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-01-08 11:35:41 -0500
  LV Status              available
  # open                 2
  LV Size                2.00 GiB
  Current LE             512
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:1

  --- Logical volume ---
  LV Path                /dev/centos/root
  LV Name                root
  VG Name                centos
  LV UUID                KZEsXa-9uNA-Nei7-gnu3-1tmU-BDwq-LSFo7Q
  LV Write Access        read/write
  LV Creation host, time localhost, 2020-01-08 11:35:42 -0500
  LV Status              available
  # open                 1
  LV Size                46.99 GiB
  Current LE             12030
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0

[root@localhost ~]# lvextend -l +100%free /dev/mapper/centos-root
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
  Logical volume centos/root successfully resized.

6. Dynamically format and expand the disk

[root@localhost ~]# lvextend -l +100%free /dev/mapper/centos-root
  Size of logical volume centos/root changed from 46.99 GiB (12030 extents) to 56.99 GiB (14590 extents).
  Logical volume centos/root successfully resized.
[root@localhost ~]# man lvextend
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=3079680 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=12318720, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=6015, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 12318720 to 14940160


7. Verification (success)

[root@localhost ~]# df -hT
Filesystem              Type      Size  Used Avail Use% Mounted on
/dev/mapper/centos-root xfs        57G  2.1G   55G   4% /
devtmpfs                devtmpfs  903M     0  903M   0% /dev
tmpfs                   tmpfs     915M     0  915M   0% /dev/shm
tmpfs                   tmpfs     915M  9.3M  906M   2% /run
tmpfs                   tmpfs     915M     0  915M   0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  146M  869M  15% /boot
tmpfs                   tmpfs     183M     0  183M   0% /run/user/0

Three, summary

Before adjusting the disk size, it is strongly recommended to back up important data, especially database data.

df -hT
# 列出磁盘
fdisk -l
# 操作磁盘/dev/sda
fdisk /dev/sda

Command (m for help): p
Command (m for help): n
Command (m for help): t
Command (m for help): w

# 同步磁盘状态(若同步失败,重启虚拟机)
partprobe
reboot
# 查看物理卷
pvdisplay
# 创建物理卷
pvcreate /dev/sda3
# 查看卷组(1个或多个)
vgdisplay
# 扩展卷组centos 
vgextend centos /dev/sda3
# 查看lvm卷
lvdisplay
# 扩展lvm卷/dev/mapper/centos-root
lvextend -l +100%free /dev/mapper/centos-root
# 动态扩容
xfs_growfs /dev/mapper/centos-root
# 确认
df -hT

If the host computer adds a new disk; Step 2 (create partition) can be changed to operate disk /dev/sdb!

```shell
[root@localhost ~]# fdisk /dev/sdb

Reprinted:
LVM expansion volume

Guess you like

Origin blog.csdn.net/ory001/article/details/112055849