The environment

The name of the attribute
CPU x5650
memory 4G
disk 20G+4TB

At this point, we assume that the server already has the following:

  • Mysql > install Parted (Parted)

  • Partition capacity has been achieved and the company has added a 4TB hard disk to the server to increase capacity!

  • Except the /boot partition, all partitions are extended logical partitions in LVM mode

Software installation and information viewing

First check whether the software is installed:

[root@Candy ~]# rpm -qa | grep lvmLvm2 - libs - 2.02.118-2. El6. X86_64 lvm2 2.02.118-2. El6. X86_64 / root @ Candy ~# lv
lvchange     lvextend     lvmdiskscan  lvmsar       lvresize
lvconvert    lvm          lvmdump      lvreduce     lvs
lvcreate     lvmchange    lvmetad      lvremove     lvscan
lvdisplay    lvmconf      lvmsadc      lvrename
[root@Candy ~]# rpm -qa | grep partedParted - 2.1-29. El6. X86_64Copy the code

After the software is ready, check the VM hardware:

[root@Candy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/mapper/vg_candy-root 7.3g 231M 6.7g 4% / TMPFS 1.9g 0 1.9g 0% /dev/shm /dev/sda1 477M 30M 422M 7% /boot /dev/mapper/vg_candy-home 3.9g 8.0m 3.7g 1% /home /dev/mapper/vg_candy- 3.9g 382M /usr/dev/mapper/vg_candy -var 3.2g 3.3m 3.2g 2.2g /var [root@Candy ~]# fdisk -lDisk /dev/sda: 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x0006e71c 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 2611 20458496 8e Linux LVM Disk /dev/sdb: 4398.0 GB, 4398046511104 bytes 255 heads, 63 sectors/track, 534698 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_candy-root: 8061 MB, 8061452288 bytes 255 heads, 63 sectors/track, 980 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_candy-usr: 4294 MB, 4294967296 bytes 255 heads, 63 sectors/track, 522 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_candy-var: 4294 MB, 4294967296 bytes 255 heads, 63 sectors/track, 522 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_candy-home: 4294 MB, 4294967296 bytes 255 heads, 63 sectors/track, 522 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@Candy ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb
Copy the code

Centos has known a disk SDB, but has not mounted or partitioned it.


Use Parted to perform partitioning before capacity expansion

Now assume all partitions are full except /boot! We need to partition first, using a 4TB hard disk (/dev/sdb).

[root@Candy ~]# parted /dev/sdbGNU Parted 2.1 Use /dev/sdb Welcome to GNU Parted! Type'help'To view a list of commands. (parted) mkLabel New disk label type? GPT (parted) mkpart Partition name? []? File system type? [ext2]? Ext4 starting point? Zero t end point? 4T (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 4398GB Sector size (logical/physical): 512B/512B Partition Table: GPT Number Start End Size File System Name flag 1 1049kB 4398GB 4398GB (Parted)set1 lvm on (parted)p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 4398GB Sector size (logical/physical): 512B/512B Partition Table: GPT Number Start End Size File System Name flag 1 1049kB 4398GB 4398GB LVM (Parted) Q [root@Candy ~]# ls /dev/sd*
/dev/sda  /dev/sda1  /dev/sda2  /dev/sdb  /dev/sdb1
Copy the code

Now let’s explain the operation:

  1. Mklabel Converts the disk to a GPT partition, as MBR partition cannot support A TB disk (see parted for details, and of course supports operations smaller than TB).

  2. Mkpart will partition the converted GPT disk. You need to provide the file system type, start point, and end point.

  3. Type p to print out the partition you just did.

  4. Set 1 LVM on set num to 1, tager to LVM, and add to vm volume group

Next, let’s look at what our LVM group is called:

[root@Candy ~]# vgs
  VG       #PV #LV #SN Attr VSize VFreeVg_candy 1 4 0 wz-- n-19.5g 0 [root@Candy ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_candy
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  5
  VG Access             read/write VG Status Resizable MAX LV 0 Cur LV 4 Open LV 4 MAX PV 0 Cur PV 1 Act PV 1 VG Size 19.51 GiB PE Size 4.00 MiB Total PE 4994 Alloc PE/Size 4994/19.51 GiB Free PE/Size 0/0 VG UUID GCM5ML-FC88-XK5q-xUPc-KJ7Y-AlfK-I770GC [root@Candy ~]# lvscan
  ACTIVE            '/dev/vg_candy/usr'GiB [4.00] inherit ACTIVE'/dev/vg_candy/var'GiB [4.00] inherit ACTIVE'/dev/vg_candy/home'GiB [4.00] inherit ACTIVE'/dev/vg_candy/root' [7.51 GiB] inherit
[root@Candy ~]#
Copy the code

Knowing that the LVM volume group is called ‘vg_candy’, we use vgextend to add members to the LVM ‘vg_candy’ volume group.

[root@Candy ~]# vgextend vag_candy /dev/sdb1
Physical volume "/dev/sdb1" successfully created
Volume group "vg_candy" successfully extended
Copy the code

Now let’s use the VGS command to look at the pv number:

[root@Candy ~]# vgs
VG       #PV #LV #SN Attr VSize VFreeVg_candy 2 0 v -n- 4.02t 4.00tCopy the code

Did you see the number of PV changed from 1 to 2 when comparing the VGS without addition?

Now let’s use vgdisplay to see the current partition capacity.

[root@Candy ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_candy
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  6
  VG Access             read/write VG Status Resizable MAX LV 0 Cur LV 4 Open LV 4 MAX PV 0 Cur PV 2 Act PV 2 VG Size 4.02 TiB PE Size 4.00 MiB Total PE 1053569 Alloc PE/Size 4994/19.51 GiB Free PE/Size 1048575/4.00 TiB VG UUID Gcm5Ml-fc88-Xk5q-xuPC-Kj7Y-alfK-i770GcCopy the code

The value of FreePE changes from 0/0 to 1048575/4.00 TiB. Candy has successfully added a 4TB LVM partition to the VG_Candy logical volume group.

Now, before we expand all logical partitions, df -h will see which partitions are available.

[root@Candy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/mapper/vg_candy-root 7.3g 231M 6.7g 4% / TMPFS 1.9g 0 1.9g 0% /dev/shm /dev/sda1 477M 30M 422M 7% /boot /dev/mapper/vg_candy-home 3.9g 8.0m 3.7g 1% /home /dev/mapper/vg_candy- 3.9g 382M /usr/dev/mapper/vg_candy -var 3.2g 63M 3.2g 2% /varCopy the code

Now that the hard work is done, it’s just time to expand the partition.


Expand the disk capacity

Suppose: we need to add 500GB capacity to each of these partitions: /, /home, /usr, /var

[root@Candy ~]# lvextend -L +500G /dev/mapper/vg_candy-rootSize of logical Volume vg_candy/root changed from 7.51 GiB (1922 extents) to 507.51 GiB (129922 extents). Logical Volume  root successfully resized [root@Candy ~]# lvextend -L +500G /dev/mapper/vg_candy-homeSize of logical Volume vg_candy/home changed from 4.00 GiB (1024 extents) to 504.00 GiB (129024 extents). Logical Volume  home successfully resized [root@Candy ~]# lvextend -L +500G /dev/mapper/vg_candy-usr
  Size of logical volume vg_candy/usr changed from 4.00 GiB (1024 extents) to 504.00 GiB (129024 extents).
  Logical volume usr successfully resized
  
[root@Candy ~]# lvextend -L +500G /dev/mapper/vg_candy-var
  Size of logical volume vg_candy/var changed from 4.00 GiB (1024 extents) to 504.00 GiB (129024 extents).
  Logical volume var successfully resized
Copy the code

Notice that this step is irreversible! If you typed the wrong partition name, fine! If added to other partitions, you have to repeat N times to shrink, more trouble oh! Note carefully check the above FileSystem. Remember to remember….

Expansion succeeded! Let’s look at the LVM information again with the command above:

[root@Candy ~]# vgs
  VG       #PV #LV #SN Attr VSize VFreeVg_candy 2 4 0 wz-- n-4.02t 2.05t [root@Candy ~]# lvscan
  ACTIVE            '/dev/vg_candy/usr'GiB [504.00] inherit ACTIVE'/dev/vg_candy/var'GiB [504.00] inherit ACTIVE'/dev/vg_candy/home'GiB [504.00] inherit ACTIVE'/dev/vg_candy/root' [507.51 GiB] inherit
  
[root@Candy ~]# vgdisplay
  --- Volume group ---
  VG Name               vg_candy
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  10
  VG Access             read/write VG Status Resizable MAX LV 0 Cur LV 4 Open LV 4 MAX PV 0 Cur PV 2 Act PV 2 VG Size 4.02 TiB PE Size 4.00 MiB Total PE 1053569 Alloc PE/Size 516994/1.97TIB Free PE/Size 536575/2.05TIB VG UUID Gcm5Ml-fc88-Xk5q-xuPC-Kj7Y-alfK-i770Gc [root@Candy ~]# df -hFilesystem Size Used Avail Use% Mounted on /dev/mapper/vg_candy-root 7.3g 231M 6.7g 4% / TMPFS 1.9g 0 1.9g 0% /dev/shm /dev/sda1 477M 30M 422M 7% /boot /dev/mapper/vg_candy-home 3.9g 8.0m 3.7g 1% /home /dev/mapper/vg_candy- 3.9g 382M /usr/dev/mapper/vg_candy -var 3.2g 63M 3.2g 2% /varCopy the code

See here everybody may be strange! Why hasn’t dF-H changed yet?


Refreshes disk information and writes

We need to resize the partition with resize2FS so that DF-H can work as well.

The time required for resize2FS is linearly superimposed on the capacity you give. That is to say, the more capacity you give, the more time it takes to resize2FS.

[root@Candy ~]# resize2fs /dev//mapper/vg_candy-rootFilesystem at /dev/mapper/vg_candy -root is mounted on /; Filesystem at /dev/mapper/vg_candy -root is mounted on /; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32 Performing an on-line resize of /dev//mapper/vg_candy-root to 133040128 (4k) blocks. The filesystem  on /dev//mapper/vg_candy-root is now 133040128 blocks long. [root@Candy ~]# resize2fs /dev//mapper/vg_candy-usrFilesystem at /dev/ mapper/vg_candy-usr is mounted on /usr; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32 Performing an on-line resize of /dev//mapper/vg_candy-usr to 132120576 (4k) blocks. The filesystem on /dev//mapper/vg_candy-usr is now 132120576 blocks long. [root@Candy ~]# resize2fs /dev//mapper/vg_candy-varFilesystem at /dev/mapper/vg_candy -var is mounted on /var; Filesystem at /dev/mapper/vg_candy -var is mounted on /var; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32 Performing an on-line resize of /dev//mapper/vg_candy-var to 132120576 (4k) blocks. The filesystem on /dev//mapper/vg_candy-var is now 132120576 blocks long. [root@Candy ~]# resize2fs /dev//mapper/vg_candy-homeFilesystem at /dev/ mapper/vg_candy-home is mounted on /home; Filesystem at /dev/ mapper/vg_candy-home is mounted on /home; Filesystem at /dev/ mapper/vg_candy-home is mounted on /home; on-line resizing required old desc_blocks = 1, new_desc_blocks = 32 Performing an on-line resize of /dev//mapper/vg_candy-home to 132120576 (4k) blocks. The filesystem  on /dev//mapper/vg_candy-home is now 132120576 blocks long. [root@Candy ~]# df -h
Filesystem                    Size  Used Avail Use%  Mounted on
/dev/mapper/vg_candy-root     500G  245M  474G   1%  /
tmpfs                         1.9G     0  1.9G   0%  /dev/shm
/dev/sda1                     477M   30M  422M   7%  /boot
/dev/mapper/vg_candy-home     496G   16M  471G   1%  /home
/dev/mapper/vg_candy-usr      496G  390M  471G   1%  /usr
/dev/mapper/vg_candy-var      496G   71M  471G   1%  /var
Copy the code

conclusion

By the way, I will give you a picture of the system automatically mounted after the restart!

Linux is smarter. In fact, restart and do not restart can be used, just to demonstrate the effect of the restart.

Hope this article can help you smooth horizontal expansion!