Dynamic expansion (Linux Logical Volume) by LVM disks under Linux

What is 1 LVM

1.1 Explain the concept

LVM (Logical Volume Manager), a logical volume manager, a plurality of hard disks to the pair of partitions logically combined, as a large hard disk is used.

When the lack of hard disk space, you can dynamically add additional hard disk partition to an existing volume group - dynamic management of disk space.

1.2 Why use LVM

LVM equipment commonly used for large numbers of disks, such as disk array server.

But LVM also applies to only one or two hard drives for small systems.

1.2.1 idea of ​​expansion when not in use LVM

Traditional file systems are partitioned, a file system based on a corresponding one partition, this approach is relatively straightforward, but not easy to change:

(1) different partitions independently of each other, not individual files across partitions to store, prone to hard disk utilization is not balanced;

(2) When a file system / partition is full, the expansion can not be, can only be used to re-partition / filesystem setup, re-partition data loss, it is necessary:

① make migration and backup data;
② or data partition to another partition greater;
③ symbolic links, or by way of other partitions use of space - very troublesome;

(3) If you want to merge multiple partitions on the hard drive used together, it can only be used to re-partition the way - need to do backup and recovery of data.

When the idea of ​​the expansion of the use of LVM 1.2.2

When using LVM when technology, the situation is different:

More (1) hard disk partition LVM volume group for the unified management can be easily added or removed a partition - that is, to expand or reduce the available capacity of the volume group, make full use of hard disk space;

(2) file system based on the logical volume, the logical volume can be changed according to the size (capacity range within the volume group) to meet the requirements;

(3) file system is built on LVM, partitions across storage access, more convenient;

It is strongly recommended for systems with multiple disks using LVM disk management.

1.3 Glossary

PV (Physical Volume): physical volume, in the LVM bottom, may be a physical hard disk or partition;

PP (Physical Extend): physical region, PV may be the smallest unit for allocation of memory, can be specified in the creation of the PV, such as 1M, 2M, 4M, 8M ... .. PE composition in the same VG size should be the same for all PV ;

VG (Volume Group): a volume group, based on PV, may contain one to a plurality of the PV;

LV (Logical Volume): logical, built on VG, equivalent to the concept of the original partition, but the size can be changed dynamically.

2 ordinary mount disk method

2.1 Creating the main partition operations

(1) View partitioning - fdisk -l

[root@localhost ~]# fdisk -l

Disk /dev/sda: 299.0 GB, 298999349248 bytes         # 磁盘/dev/sda
255 heads, 63 sectors/track, 36351 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: 0x4d69fe0e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux       # 分为2个区, sda1
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       36352   291785728   8e  Linux LVM   # sda2

# 磁盘/dev/sdb没有分区
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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

......

(2) reviewing existing disk - lsblk

[the root @ localhost ~] # lsblk 
NAME the MAJ: the TYPE MOUNTPOINT the RO SIZE MIN RM 
SDA. 8: 0 0 0 278.5G Disk 
├─sda1. 8: Part. 1 0 0 200M / Boot 
└─sda2. 8: Part 2 0 0 278.3G 
  └ ─VolGroup-LogVol (dm-0) 253: 0 0 1.9T 0 lvm / # LVM partition type 
sdb 8:32 0 3.7T 0 disk # yet a new disk partitions

(3) the new disk partition - fdisk /dev/sdb

[root@localhost ~]# fdisk /dev/sdb 
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf91f8c4c.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: The size of this disk is 4.0 TB (4000225165312 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         The MODE OFF Switch (Command 'C') and the display Change Units to
         sectors (Command 'U'). 

the Command (m for Help): n-n-# represents the new partition 
the Command Action 
   E Extended 
   P Primary Partition (1-4) 
P # P represents a partition type primary partition, the primary partition is only one to four select 
partition number (1-4): 1 # primary partition number 
First cylinder (1-486333, default 1) : # start sector numbers, directly enter, default value. 1 
the using default value. 1 

# end sector number, the default --- value here only half a new disk is loaded (2T), so it needs to create a partition / dev / sdb2 again using the remaining half. 
Last Cylinder, or Cylinders + size + {K, M, G} (l- 267 349, default 267 349):    
the Using default value 267 349 

the Command (m for Help): W # write disposed above the partition table and exit 
of the partition table has been Altered! 

the Calling the ioctl () to Re-Read partition table. 
Syncing Disks.

(4) Check again partitioning - fdisk -l

More out of a / dev / sdb1 area, that is, before the specified partition number 1 after the primary partition.

[root@localhost ~]# fdisk -l 

Disk /dev/sda: 299.0 GB, 298999349248 bytes
255 heads, 63 sectors/track, 36351 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: 0x4d69fe0e

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26       36352   291785728   8e  Linux LVM

# /dev/sdb磁盘: 
Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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: 0x8f3043b5

# 多出来的分区/dev/sdb1
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      267349  2147480811   83  Linux

......

(5) view the partition information for the current partition table - cat /proc/partitions

[the root @ localhost ~] # CAT / proc / Partitions 
Major Minor #blocks name 

   . 8 0 291 991 552 SDA 
   . 8. 1 204800 sda1 
   . 8 2 291 785 728 sda2 
   . 8 3,906,249,728 SDB # 32 adding a new disk 
   new partition 8 33 2147480811 sdb1 # created 
 25302046660608 dm-0

If after you've created, cat / proc / partitions to see less than the corresponding partition, use parprobe Refresh command:

[root@localhost ~]# partprobe /dev/sdc

2.2 format the new partition

(1) to format the new partition - mkfs -t

It is recommended that the new partition is formatted as ext4 file types, as well as ext2, ext3 and other file types, please refer to the difference between blog ext2, ext3 and ext4 the difference .

[root@localhost ~]# mkfs -t ext4 /dev/sdb1
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
134217728 inodes, 536870202 blocks
26843510 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
16384 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, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
        102400000, 214990848, 512000000

Writing inode tables:  8874/16384

(2) After waiting for a little while, the following prompt appears, indicating that the format is complete:

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

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

2.3 Mounting the New Partition

(1) Create a directory, and / dev / sdb1 hanging on to the directory:

[root@localhost /]# mkdir data && cd /data
[root@localhost data]# mount /dev/sdc1 /data1

(2) Check the mount is successful:

[root@localhost data]# df -l
Filesystem                   1K-blocks       Used  Available Use% Mounted on
/dev/mapper/VolGroup-LogVol  286901696   18601728  253726196   7% /
tmpfs                         66020980          0   66020980   0% /dev/shm
/dev/sda1                       495844      33476     436768   8% /boot

# 挂载成功: 
/dev/sdb1                   2113784984     202776 2006208168   1% /data

Automatically mount the boot provided 2.4

Edit the file / etc / fstab:

[root@localhost data]# vim /etc/fstab

# 文件内容如下: 
# /etc/fstab
# Created by anaconda on Wed Sep 12 10:41:40 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-LogVol  /                     ext4    defaults        1 1
/dev/sdb1                    /data                 ext4    defaults        1 1
UUID=22b1d425-d050-43c3-a735-06d48bbb9051 /boot    ext4    defaults        1 2 
tmpfs                        /dev/shm              tmpfs   defaults        0 0
devpts                       /dev/pts              devpts  gid=5,mode=620  0 0
sysfs                        /sys                  sysfs   defaults        0 0
proc                         /proc                 proc    defaults        0 0

3 LVM mount the disk - Recommended

3.1 Viewing disk capacity information

[root@localhost ~]# df -h
Filesystem               Size  Used  Avail  Use%  Mounted on
/dev/mapper/VG-LogVol    1.9T  1.8T    61G   97%  /          # LVM卷组-逻辑卷
tmpfs                     63G     0    63G    0%  /dev/shm
/dev/sda1                485M   40M   421M    9%  /boot

3.2 Viewing disk sector information

[the root @ localhost ~] # the fdisk -l 

Disk / dev / SDA: 299.0 GB, disk # 298 999 349 248 bytes / dev / SDA 
255 Heads, 63 is sectors / Track, 36351 Cylinders 
Units = Cylinders of 16065 * 512 = 8.22528 million bytes 
Sector size ( Logical / PHYSICAL): 512 bytes / 512 bytes 
the I / O size (Minimum / Optimal): 512 bytes / 512 bytes 
Disk identifier: 0x4d69fe0e 

   Device Id the Boot the Start End Blocks the System 
/ dev / sda1. 1 * 83 204800 26 is divided into the Linux # 2 districts, sda1 
partition 1 does not End ON Cylinder boundary. 
/ dev / sda2 26 36 352 291 785 728 8E sda2 Linux LVM # LVM type 

# newly added disk / dev / sdb, not a partition 
disk / dev / sdb: 4000.0 GB , 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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

# LVM格式的卷组信息: 
Disk /dev/mapper/VolGroup-LogVol: 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

3.3 Creating partitions

[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x5b3d66ba.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: The size of this disk is 4.0 TB (3999999721472 bytes).
DOS partition table format can not be used on drives for volumes
larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID 
partition table format (GPT).


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         # 添加分区
Command action
   e   extended
   p   primary partition (1-4)
p                               # 添加主分区
Partition number (1-4): 1       # 1号主分区, 即/dev/sdb1
First cylinder (1-486305, default 1):               
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): 486305
Value out of range.
Last cylinder, +cylinders or +size{K,M,G} (1-267349, default 267349): 
Using default value 267349

Command (m for help): n         # 继续添加分区
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2       # 2号主分区, 即/dev/sdc2
First cylinder (267350-486305, default 267350): 
Using default value 267350
Last cylinder, +cylinders or +size{K,M,G} (267350-486305, default 486305): 
Using default value 486305

Command (m for help): p         # 打印分区信息: 

Disk /dev/sdb: 4000.0 GB, 3999999721472 bytes
255 heads, 63 sectors/track, 486305 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: 0x5b3d66ba

   Device Boot      Start         End      Blocks   Id  System
/ dev / sdb1. 1 83 267 349 2,147,480,811 the Linux 
/ dev / sdb2 267 350 83 486 305 1,758,764,070 the Linux 

the Command (m for Help): T # conversion type 
the Partition Number (1-4):. 1 
the Partition Number (1-4): Review. 1 # / dev / sdb1 as Linux LVM type: 
Hex code (type L to List Codes): L # See available type: 

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

the Command ( Help for m): T 
the Partition Number (1-4): 2 # modify / dev / sdc2 Linux LVM type is 
Hex code (type L to List Codes): 8E 
the Changed type of System Partition 2 to 8E (Linux LVM) 

the Command ( m for help): p # again to view information: 

Disk / dev / SDC: 4000.0 GB, 3999999721472 bytes 
255 Heads, 63 is sectors / Track, 486 305 Cylinders 
Units = Cylinders of 16065 * 512 = 8.22528 million bytes 
Sector size (Logical / PHYSICAL) : 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5b3d66ba

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      267349  2147480811   8e  Linux LVM   # Id已改变
/dev/sdb2          267350      486305  1758764070   8e  Linux LVM

Command (m for help): w                 # 保存并退出
The partition table has been altered!   # 修改成功

Calling ioctl() to re-read partition table.
Syncing disks.

3.4 Creating Physical Volumes

[root@localhost ~]# pvcreate /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
[root@localhost ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created

3.5 extend the volume group

# View existing volume group, the volume group found that the volume group is the need for expansion, you do not have to create a volume group again, but directly expand the volume group can be: 
[root @ localhost ~] # Vgs of the 
  VG #PV #LV #SN Attr vfree VSize 
  volgroup WZ 2 2 0 - 0 N- 1.91T 

# expansion volume group: 
[the root @ localhost ~] # the vgextend volgroup / dev / sdb1 
  volume group "volgroup" extended successfully 
[the root @ localhost ~] # the vgextend volgroup / dev / sdb2 
  Volume Group "volgroup" successfully Extended

Note: If the following information to the physical disk can not be mounted in a volume group, indicating that this physical disk has been mounted, the need to uninstall, and then perform operations to create partitions + volume group:

[root@localhost /]# vgextend VolGroup /dev/sdb1 
  No physical volume label read from /dev/sdb1
  Physical volume /dev/sdb1 not found
  Can't open /dev/sdb1 exclusively.  Mounted filesystem?
  Unable to add physical volume '/dev/sdb1' to volume group 'VolGroup'.

3.6 Extending a Logical Volume

# Logical volume expansion, i.e., expansion: 
[the root @ localhost ~] # 100 + -l% the lvextend the FREE / dev / Mapper / volgroup-LogVol 
  the Extending Logical Volume lv_root to 5.54 of TiB 
  the Logical Volume lv_root successfully Resized 

# above command is all idle are the logical volume expansion space, the expansion of the size can also be specified: 
the lvextend 100G + -l / dev / Mapper / volgroup-LogVol

3.7 View disk volume group information

[root@localhost ~]# lsblk 
NAME                        MAJ:MIN RM   SIZE RO  TYPE  MOUNTPOINT
sda                           8:0    0 278.5G  0  disk  
├─sda1                        8:1    0   500M  0  part  /boot
└─sda2                        8:2    0   278G  0  part  
  ├─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
sdb                           8:16   0   1.6T  0  disk  
└─sdb1                        8:17   0   1.6T  0  part  
  └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
sdc                           8:32   0   3.7T  0  disk  
├─sdc1                        8:33   0     2T  0  part  
│ └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /
└─sdc2                        8:34   0   1.7T  0  part  
  └─VolGroup-LogVol (dm-0)  253:0    0   5.6T  0  lvm   /

3.8 Adjusting the size of the file system

Reread the disk size # 7: 
[the root @ localhost ~] # xfs_growfs / dev / Mapper / volgroup-LogVol 
xfs_growfs: / dev / Mapper / A Not volgroup IS-LogVol Mounted the XFS filesystem 

# 6.5 reread the CentOS disk size: 
# ext4 format, resize2fs goes through the entire disk, slower, but does not affect the reading and writing of data, so that it can run in the background. 
[the root @ localhost ~] # resize2fs / dev / Mapper / volgroup-LogVol 
resize2fs 1.41.12 (17- On May-2010) 
the Filesystem AT / dev / Mapper / volgroup-LogVol IS Mounted ON /; ON-Line Resizing required 
Old desc_blocks = 122, new_desc_blocks = 355 
Performing AN ON-Line a resize of / dev / Mapper / volgroup-lv_root to 1487098880 ( . 4K) Blocks 

# waited almost 20 minutes, which came out the following goods: 
 at the filesystem ON / dev / Mapper / volgroup-LogVol IS now 1,487,098,880 Long Blocks. 
 
# expansion quickly look achievements it:
[root@localhost ~]#   df -h
Filesystem                    Size  Used Avail Use%  Mounted on
/dev/mapper/VolGroup-LogVol   5.5T  1.8T  3.5T  34%  /          # 扩容成功
tmpfs                          63G     0   63G   0%  /dev/shm
/dev/sda1                     485M   40M  421M   9%  /boot

3.9 Appendix - create a volume group

The corresponding [3.5] section expansion volume group, if the volume group does not exist, create it. VolGroup following is the volume group name.

volgroup the vgcreate / dev / sdb1 
# Create a logical volume name:. mylv (operating system will produce: / dev / VolGroup / mylv directory) 
# current volume group 100G space allocated to the logical volume 
lvcreate -L 100G VolGroup mylv -n 
# is assigned to all or all of the free space in the current logical volumes in the volume group:   
the lvcreate -l + 100% mylv the fREE volgroup -n 

# formatted logical volume group: 
the mkfs -t ext4 / dev / volgroup / mylv 

# to mount the volume group to the specified directory, if it is mounted to the root directory, there is no need to add items to the start / etc / fstab file. 
Mount -t ext4 / dev / volgroup / mylv / Data

Guess you like

Origin www.linuxidc.com/Linux/2019-08/160262.htm