LVM disk logical volume expansion

Time:2020-10-12

At the beginning of virtual machine installation, we chose LVM to partition the disk. Later, when the disk is not enough, it is very convenient to add, so there is the LVM expansion. This internal virtual machine is mainly used for sharing and data backup, which requires a large amount of disk space. Record the LVM expansion below

Expansion operation

First of all, add a new disk. After CentOS adds a new hard disk, the system cannot automatically identify it

[[email protected]]# fdisk -l

Disk / dev / SDA: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sector of 1 * 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: 0x0007efbe

   Boot start end blocks ID system
/dev/sda1   *        2048      501759      249856   83  Linux
/dev/sda2          501760  1048575999   524037120   8e  Linux LVM

Disk / dev / mapper / vg-0: 536.6 GB, 536610865152 bytes, 1048068096 sectors
Units = sector of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 512 bytes
I / O size (minimum / best): 512 bytes / 512 bytes

[[email protected]]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        8:0    0   500G  0 disk 
├─sda1     8:1    0   244M  0 part /boot
└─sda2     8:2    0 499.8G  0 part 
  └─vg-0 253:0    0 499.8G  0 lvm  /
sr0       11:0    1  1024M  0 rom

The lsblk command is “list block” in English, which is used to list the information of all available block devices, and also to display the dependency relationship between them

Since you do not know the location of the new hard disk, you can first check the adapter attached to the existing hard disk.

[[email protected]]# ls -l /sys/block/sda
Lrwxrwxrwx 1 root root 0 May 29 10:26 / sys / block / SDA - > // devices / pci0000:00 / 0000:00:10.0 / host16 / target16:0:0 / 16:0:0 / block / SDA

If you find that the disk is mounted to host16, you can try to use the following command to scan the host16 information again

[[email protected]]# echo "- - -" > /sys/class/scsi_host/host16/scan
[[email protected]]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        8:0    0   500G  0 disk 
├─sda1     8:1    0   244M  0 part /boot
└─sda2     8:2    0 499.8G  0 part 
  └─vg-0 253:0    0 499.8G  0 lvm  /
sdb        8:16   0   500G  0 disk 
sr0       11:0    1  1024M  0 rom  

Disk partitioning and formatting

[[email protected]]# fdisk /dev/sdb
Welcome to fdisk (util Linux 2.23.2).

The changes remain in memory until you decide to write the changes to disk.
Think twice before using the write command.

Device does not contain a recognized partition table
Create a new DOS disk label using the disk identifier 0x7bfce339.

Command (enter m for help): M
Command operation
   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 (enter m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
Starting sector (2048-1048575999, the default is 2048)
The default value of 2048 will be used
Last sector, + sector or + size {K, m, G} (2048-1048575999, the default is 1048575999)
The default value of 1048575999 will be used
Partition 1 is set to the Linux type with a size of 500 gib

Command (enter m for help): W
The partition table has been altered!

Calling ioctl() to re-read partition table.
Synchronizing disks.
[[email protected]]# fdisk -l

Disk / dev / SDA: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sector of 1 * 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: 0x0007efbe

   Boot start end blocks ID system
/dev/sda1   *        2048      501759      249856   83  Linux
/dev/sda2          501760  1048575999   524037120   8e  Linux LVM

Disk / dev / mapper / vg-0: 536.6 GB, 536610865152 bytes, 1048068096 sectors
Units = sector of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 512 bytes
I / O size (minimum / best): 512 bytes / 512 bytes


Disk / dev / SDB: 536.9 GB, 536870912000 bytes, 1048576000 sectors
Units = sector of 1 * 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: 0x7bfce339

   Boot start end blocks ID system
/dev/sdb1            2048  1048575999   524286976   83  Linux

[[email protected]]# mkfs -t xfs /dev/sdb
mkfs.xfs: /dev/sdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[[email protected]]# mkfs.xfs /dev/sdb
mkfs.xfs: /dev/sdb appears to contain a partition table (dos).
mkfs.xfs: Use the -f option to force overwrite.
[[email protected]]# mkfs.xfs -f /dev/sdb
meta-data=/dev/sdb               isize=512    agcount=4, agsize=32768000 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=131072000, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=64000, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

View VG logical volume groups

[[email protected]]# vgdisplay 
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <499.76 GiB
  PE Size               4.00 MiB
  Total PE              127938
  Alloc PE / Size       127938 / <499.76 GiB
  Free  PE / Size       0 / 0   
  VG UUID               kNi93D-daLZ-qYmt-hhzS-Kdm3-ZUHv-0eUYL3

Expand VG

[[email protected]]# vgextend vg /dev/sdb
WARNING: xfs signature detected on /dev/sdb at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/sdb.
  Physical volume "/dev/sdb" successfully created.
  Volume group "vg" successfully extended
[[email protected]]# vgdisplay 
  --- Volume group ---
  VG Name               vg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               999.75 GiB
  PE Size               4.00 MiB
  Total PE              255937
  Alloc PE / Size       127938 / <499.76 GiB
  Free  PE / Size       127999 / <500.00 GiB
  VG UUID               kNi93D-daLZ-qYmt-hhzS-Kdm3-ZUHv-0eUYL3

View LV and expand Lv to the remaining capacity of VG

[[email protected]]# lv
lvchange     lvcreate     lvextend     lvmconf      lvmdiskscan  lvmetad      lvmsadc      lvreduce     lvrename     lvs          
lvconvert    lvdisplay    lvm          lvmconfig    lvmdump      lvmpolld     lvmsar       lvremove     lvresize     lvscan       
[[email protected]]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg/0
  LV Name                0
  VG Name                vg
  LV UUID                JQVovf-H4tM-kGNi-Nq9b-QRyE-QDIW-lAg2KP
  LV Write Access        read/write
  LV Creation host, time samba, 2020-04-09 16:23:09 +0800
  LV Status              available
  # open                 1
  LV Size                <499.76 GiB
  Current LE             127938
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0   
[[email protected]]# lvextend /dev/vg/0 /dev/sdb
  Size of logical volume vg/0 changed from <499.76 GiB (127938 extents) to 999.75 GiB (255937 extents).
  Logical volume vg/0 successfully resized.
[[email protected]]# lvdisplay 
  --- Logical volume ---
  LV Path                /dev/vg/0
  LV Name                0
  VG Name                vg
  LV UUID                JQVovf-H4tM-kGNi-Nq9b-QRyE-QDIW-lAg2KP
  LV Write Access        read/write
  LV Creation host, time samba, 2020-04-09 16:23:09 +0800
  LV Status              available
  # open                 1
  LV Size                999.75 GiB
  Current LE             255937
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:0
 
[[email protected]]# lsblk
NAME     MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda        8:0    0   500G  0 disk 
├─sda1     8:1    0   244M  0 part /boot
└─sda2     8:2    0 499.8G  0 part 
  └─vg-0 253:0    0 999.8G  0 lvm  /
sdb        8:16   0   500G  0 disk 
└─vg-0   253:0    0 999.8G  0 lvm  /
sr0       11:0    1  1024M  0 rom  

Finally, let the new space take effect online. Centos7 recommends XFS file format.

[[email protected]]# xfs_
xfs_admin      xfs_copy       xfs_estimate   xfs_fsr        xfs_info       xfs_logprint   xfs_metadump   xfs_ncheck     xfs_repair     
xfs_bmap       xfs_db         xfs_freeze     xfs_growfs     xfs_io         xfs_mdrestore  xfs_mkfile     xfs_quota      xfs_rtcp       
[[email protected]]# xfs_growfs -h
xfs_ Growfs: invalid option -- H
Usage: xfs_growfs [options] mountpoint

Options:
    -d          grow data/metadata section
    -l          grow log section
    -r          grow realtime section
    -n          don't change anything, just show geometry
    -i          convert log from external to internal format
    -t          alternate location for mount table (/etc/mtab)
    -x          convert log from internal to external format
    -D size     grow data/metadata section to size blks
    -L size     grow/shrink log section to size blks
    -R size     grow realtime section to size blks
    -e size     set realtime extent size to size blks
    -m imaxpct  set inode max percent to imaxpct
    -V          print version information
[[email protected]]# xfs_growfs /
meta-data=/dev/mapper/vg-0       isize=512    agcount=4, agsize=32752128 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=131008512, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=63969, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 131008512 to 262079488

At this point, LVM capacity expansion is completed. Finally, check it out

[[email protected]]# df -h
File system capacity used% mount points available
devtmpfs          979M     0  979M    0% /dev
tmpfs             991M     0  991M    0% /dev/shm
tmpfs             991M   18M  973M    2% /run
tmpfs             991M     0  991M    0% /sys/fs/cgroup
/dev/mapper/vg-0 1000G  267G  734G   27% /
/dev/sda1         241M  133M  109M   55% /boot
tmpfs             199M     0  199M    0% /run/user/1000

Recommended Today

Share a simple blog

Series articles: Share a useful tp6 background management system Add Google login authentication in thinkphp6 background Share a simple blog JrkBlog_ Tp6.0 easy blog – you deserve it Project introduction Jrkblog blog is based on yfcmf background development Improve the following functions: Phpquerylist data collection,Phpword exports a word document,Practical and easy-to-use API interface,Upload OSS, cos […]