Disk partition LVM--disk structure

Hard disk device name:

Everything in linux is a file, so the files of the hard disk are automatically generated and recognized in the kernel. After the recognition, they are placed under /dev, and a corresponding file is generated under one hard disk, and two hard disks will generate consecutive files. The corresponding file, its name is related to the hard disk type, for example: sas interface, sata interface

The name of the virtual disk is

/dev/sd? //following a is the first hard disk, following b is the second hard disk 
[root@www ~]# ls /dev/sd* 
/dev/sda /dev/sda1 /dev/sda2 //behind The one, two, and three represent partitions. 
The name of the virtual disk is 
/dev/vd 
/dev/xvd

add hard drive

Refresh to identify the hard drive

[root@www ~]# echo '- - -' > /sys/class/scsi_host/host2/scan //Refresh 
[root@www ~]# lsblk //It is used to list all available block device information, And it can also show the dependencies between them 
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT 
sda 8:0 0 50G 0 disk 
├─sda1 8:1 0 1G 0 part /boot 
└─sda2 8:2 0 49G 0 part 
  ├ ─centos-root 253:0 0 45.1G 0 lvm / 
  └─centos-swap 253:1 0 3.9G 0 lvm [SWAP] 
sdb 8:16 0 20G 0 disk 
sdc 8:32 0 10G 0 disk 
sr0 11:0 1 4.4G 0 rom  

Identify hard disk format

# 1 means mechanical 
# 0 means SSD 
[root@www ~]# lsblk -d -o name,rota 
NAME ROTA 
sda 1 
sdb 1 
sdc 1 
sr0 1

There are two partition modes MBR and GPT partition

A hard disk in the MBR partition can have up to 4 primary partitions or 3 primary partitions and 1 extended partition (n logical partitions)

The command to create partitions 
fdisk manages MBR partitions 
gdisk manages GPT partitions 
parted Advanced partition operations can be interactive or non-interactive // ​​just a little understanding 
partproble Synchronize the data in the partition 
and input partprobe to let the system identify the newly added partition, and then create physical volume

Partitioning tools fdisk and gdisk

p partition list//display partition table 
t change partition type 
e logical partition 
n create new partition 
d delete partition 
w save and exit 
q do not save and exit 
fdisk /dev/sdb

Logical volume (lvm allows modification of the volume) VG

We group multiple hard disks or partition logic together and we call it volume group VG (volume group)

Implementation process

Designate the device as a physical volume--""Use one or more physical volumes to create a volume group--""Then create a logical volume through the volume group--""Then use the logical volume (partition) by mounting

start testing

Prepare a partition and remember to reformat a hard disk

First turn the physical disk into a physical volume

[root@www ~]# pvcreate /dev/sd{b1,c}
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc" successfully created.
[root@www ~]# pvs  //查看物理卷
  PV         VG     Fmt  Attr PSize   PFree 
  /dev/sda2  centos lvm2 a--  <49.00g  4.00m
  /dev/sdb1         lvm2 ---   10.00g 10.00g
  /dev/sdc          lvm2 ---   10.00g 10.00g 
[root@www ~]# pvdisplay
"/dev/sdc" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM
   
  "/dev/sdb1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name               
  PV Size               10.00 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               qKEnlk-oAr1-OfFM-cM7W-jBTP-ZOe3-lPT56V

Add physical volume to volume group

PE is called a physical extent. It is responsible for allocating the smallest unit in the logical volume. For example, the expansion and shrinkage to be used later must use the PE as the unit.

PE size can be specified, the default is 4M

vgcreate -s (specify PE size) 16 vg0 /dev/sd{b1,c}

[root@www ~]# vgcreate vggroup /dev/sd{b1,c}
  Volume group "vggroup" successfully created
[root@www ~]# vgs
  VG      #PV #LV #SN Attr   VSize   VFree 
  centos    1   2   0 wz--n- <49.00g  4.00m
  vggroup   2   0   0 wz--n-  19.99g 19.99g
[root@www ~]# vgdisplay
--- Volume group ---
  VG Name               vggroup
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               19.99 GiB
  PE Size               4.00 MiB
  Total PE              5118
  Alloc PE / Size       0 / 0   
  Free  PE / Size       5118 / 19.99 GiB
  VG UUID               Ugx1bj-bL7L-yZcH-btjC-n0dO-0skf-EWDTGa

Divide the volume group into logical volumes

In the logical volume, the PE is called LE pvdisplay, and you can view the logical volume that is separated from the physical volume.

[root@www ~]# lvcreate -n mysql -l 1000 vggroup 
  Logical volume "mysql" created.
[root@www ~]# lvs
  LV    VG      Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root  centos  -wi-ao---- <45.12g                                                    
  swap  centos  -wi-ao----  <3.88g                                                    
  mysql vggroup -wi-a-----  <3.91g                                                    
[root@www ~]# lvdisplay

--- Logical volume ---
  LV Path                /dev/vggroup/mysql
  LV Name                mysql
  VG Name                vggroup
  LV UUID                IJbyFS-wOjQ-4hpV-iK1x-Fqj9-YO3H-qgWdZ4
  LV Write Access        read/write
  LV Creation host, time www, 2023-02-15 15:13:26 +0800
  LV Status              available
  # open                 0
  LV Size                <3.91 GiB
  Current LE             1000
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:2

View the location of the logical volume

[root@www ~]# ll /dev/vggroup/log 
lrwxrwxrwx 1 root root 7 2月  15 15:20 /dev/vggroup/log -> ../dm-3
[root@www ~]# ll /dev/vggroup/mysql 
lrwxrwxrwx 1 root root 7 2月  15 15:13 /dev/vggroup/mysql -> ../dm-2

Formatting is to write to the file system, the formatting is very simple, just use mkfs. file system name

create file system

[root@www ~]# mkfs.ext4 /dev/vggroup/mysql 
mke2fs 1.42.9 (28-Dec-2013) 
file system label = 
OS type: Linux 
block size = 4096 (log = 2) 
block size = 4096 ( log=2) 
Stride=0 blocks, Stripe width=0 blocks 
256000 inodes, 1024000 blocks 
51200 blocks (5.00%) reserved for the super user 
first data block=0 
Maximum filesystem blocks=1048576000 
32 block groups 
32768 blocks per g roup, 32768 fragments per group 
8000 inodes per group 
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736 

Allocating group tables: Complete                             
Writing inode table: Complete                             
Creat ing journal (16384 blocks): done
Writing superblocks and filesystem accounting information: 完成 

[root@www ~]# blkid
/dev/sr0: UUID="2020-11-04-11-36-43-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" 
/dev/sda1: UUID="4605ff7f-243b-4a27-a39e-52e6cdfe1846" TYPE="xfs" 
/dev/sda2: UUID="LtW5Jh-Tdbs-XeQE-5oyj-M3m1-1CBf-mqPmRa" TYPE="LVM2_member" 
/dev/sdb1: UUID="qKEnlk-oAr1-OfFM-cM7W-jBTP-ZOe3-lPT56V" TYPE="LVM2_member" PARTUUID="1ae63c86-a655-44a0-9d50-fb8cab83a8e5" 
/dev/mapper/centos-root: UUID="a1bb456a-312d-4ad6-805f-ec9e12e701c7" TYPE="xfs" 
/dev/sdc: UUID="f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM" TYPE="LVM2_member" 
/dev/mapper/centos-swap: UUID="4d3f8a28-2f87-408f-bda4-74d531098319" TYPE="swap" 
/dev/mapper/vggroup-mysql: UUID="04e0df52-54b5-42ab-bced-f8f30afce3e4" TYPE="ext4" 

mount

vi /etc/fstab 
/dev/vggroup/mysql /mnt/mysql (mount point) ext4 (file system) defaults 0 0 
mkdir -p /mnt/mysql 
mount -a[root@www ~]# df 
file system 1K- blocks used available % used mount point 
devtmpfs 1918592 0 1918592 0% /dev 
tmpfs 1930644 0 1930644 0% /dev/shm 
tmpfs 1930644 11800 1918844 1% /run 
tmpfs 1930644 0 193 0644 0% /sys/fs/cgroup 
/dev /mapper/centos-root 47285700 1624100 45661600 4% / 
/dev/sda1 1038336 153892 884444 15% /boot 
tmpfs 386132 0 386132 0% /run/user/0
/dev/mapper/vggroup-mysql  3966144   15992  3728968    1% /mnt/mysql

Expansion of Logical Volume

lvextend -l 50%free /dev/vggroup/mysql 
Size of logical volume vggroup/mysql changed from <3.91 GiB (1000 extents) to <6.09 GiB (1559 extents). 
Logical volume vggroup/mysql successfully resized. 
Synchronized data 
resize2fs /dev /vggroup/mysql 
resize2fs 1.42.9 (28-Dec-2013) 
Filesystem at /dev/vggroup/mysql is mounted on /mnt/mysql; on-line resizing required 
old_desc_blocks = 1, new_desc_blocks = 1 
The filesystem on /dev/vggroup /mysql is now 1596416 blocks long. 

]# df -Th 
file system type capacity used available used % mount point  
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev 
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs tmpfs 1.9G 12M 1.9G 1% /run 
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs /cgroup 
/dev/mapper/centos-root xfs 46G 1.6G 44G 4% / 
/dev/sda1 xfs 1014M 151M 864M 15% /boot 
tmpfs tmpfs 378M 0 378M 0% /run/user /0 
/dev/mapper/vggroup-mysql ext4 3.8G 16M 3.6G 1% /mnt/mysql 
/dev/mapper/vggroup-log xfs 3.9G 33M 3.9G 1% /mnt/log 
[root@www ~]# df -Th 
file system type capacity used available used% mount point 
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev 
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm 
tmpfs tmpfs 1.9G 12M 1.9G 1% /run 
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup / 
dev/mapper/centos-root xfs 46G 1.6G 44G 4% / 
/dev/sda1 xfs 1014M 151M 864M 15% /boot 
tmpfs tmpfs 378M 0 378M 0% /run/user/0 
/dev/mapper/vggroup -mysql ext4 6.0G 16M 5.7G 1% /mnt/mysql 
/dev/mapper/vggroup-log xfs 3.9G 33M 3.9G 1% /mnt/log 
xfs file system expansion 
 lvextend -L 1G /dev/vggroup/log 
New size given (256 extents) not larger than existing size (1000 extents) 
 synchronous data 
xfs_growfs /mnt/log/ 
meta-data=/dev/mapper/vggroup-log isize=512 agcount=4, agsize=256000 blks 
         = sectsz=512 attr =2, projid32bit=1
         = crc=1 finobt=0 spinodes=0 
data = bsize=4096 blocks=1024000, imaxpct=25 
         = sunit=0 width=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 
[root@www ~]# df -h 
file system capacity used available available used% mount point 
devtmpfs 1.9G 0 1.9G 0% /dev 
tmpfs 1.9G 0 1.9G 0% /dev/shm 
tmpfs 1.9G 12M 1.9G 1% /run 
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mapper/centos-root 46G 1.6G 44G 4% / 
/dev/sda1 1014M 151M 864M 15% /boot 
tmpfs 378M 0 378M 0% /run/user/0 
/dev/mapper/vggroup-mysql 6.0G 16M 5.7 G 1% /mnt/mysql 
/dev/mapper/vggroup-log 3.9G 33M 3.9G 1% /mnt/log 

Re-partition a disk, select it as a physical partition, add it to the vg volume group, and then Expand 
fdisk /dev/sdc
pvcreate /dev/sdc1 
pvs 
vgextend vggroup /dev/sdc1 //Add the new physical volume to the volume group 
vgs 
vgdisplay

Logical volume shrinkage (xfs does not support shrinkage, it can only be formatted before shrinking, then the data on the volume is gone, and shrinking is meaningless)

The first step is to unmount 
umount /mnt/mysql/ 
the second part to check the file (must be done, he will prompt you if you don’t do it) 
resize2fs /dev/vggroup/mysql 5G 
resize2fs 1.42.9 (28-Dec-2013) 
please First run 'e2fsck -f /dev/vggroup/mysql'. 

e2fsck -f /dev/vggroup/mysql 
e2fsck 1.42.9 (28-Dec-2013) 
Step 1: Check inodes, blocks, and sizes 
Step 2: Check Directory structure 
Step 3: Check directory connectivity 
Pass 4: Checking reference counts 
Step 5: Check cluster summary information 
/dev/vggroup/mysql: 11/392000 files (0.0% non-contiguous), 44996/1596416 blocks 
Step 3 Resize 
resize2fs /dev/vggroup/mysql 5G 
resize2fs 1.42.9 (28-Dec-2013)  
Resizing the filesystem on /dev/vggroup/mysql to 1310720 (4k) blocks. 
The filesystem on /dev/vggroup/mysql is now 1310720 blocks long.
The fourth step shrinks the logical volume //The value of the two reductions must be consistent 
	[root@www ~]# lvreduce -L 5G /dev/ vggroup/mysql 
  WARNING: Device for PV f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM not found or rejected by a filter. 
  Couldn't find device with uuid f4trEC -xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM. 
  Cannot change VG vggroup while PVs are missing. 
  Consider vgreduce --removemissing. 
  Cannot process volume group vggroup 
The fifth step is to remount 
mount -a 
The sixth step is to view the reduced 
 df -Th 
File system type capacity used available used% mount point 
devtmpfs devtmpfs 1.9G 0 1.9G 0% /dev 
tmpfs tmpfs 1.9G 0 1.9G 0% /dev/shm 
tmpfs tmpfs 1.9G 12M 1.9G 1% /run 
tmpfs tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup / 
dev/mapper/centos-root xfs 46G 1.6G 44G 4% / 
/dev/sda1 xfs 1014M 151M 864M 15% / boot
tmpfs                     tmpfs     378M     0  378M    0% /run/user/0
/dev/mapper/vggroup-log   xfs       3.9G   33M  3.9G    1% /mnt/log
/dev/mapper/vggroup-mysql ext4      4.9G   16M  4.7G    1% /mnt/mysql

Replace the hard disk in the same volume group

1. First transfer the data 
pvmove /dev/sdc1 
and then delete the volume group 
vgreduce vggroup /dev/sdc1 
and then delete the physical volume 
pvremove /dev/sdc 
before removing the hard disk

The same goes for deleting a logical volume

先取消挂载
[root@www ~]# umount /mnt/mysql/
[root@www ~]# umount /mnt/log/
2.先移除逻辑卷
lvremove /dev/vggroup/mysql 
  WARNING: Device for PV f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM not found or rejected by a filter.
  Couldn't find device with uuid f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM.
Do you really want to remove active logical volume vggroup/mysql? [y/n]: y
  Logical volume "mysql" successfully removed
[root@www ~]# lvremove /dev/vggroup/log 
  WARNING: Device for PV f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM not found or rejected by a filter.
  Couldn't find device with uuid f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM.
Do you really want to remove active logical volume vggroup/log? [y/n]: y
  Logical volume "log" successfully removed
[root@www ~]# lvs
  WARNING: Device for PV f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM not found or rejected by a filter. Couldn't 
  find device with uuid f4trEC-xRX1-XfhJ-XCTd-RDnv-FVYT-1blirM. 
  LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert 
  root centos -wi-ao---- <45.12g                                                     
  swap centos -wi-ao---- <3.88g     
3. Remove volume group 
vgremove vggroup 
4. Move Remove the physical volume 
pvremove /dev/sd{b1,c1} 
5. Delete the file format in fdiskl 
and restore to the original hard disk

Guess you like

Origin blog.csdn.net/m0_67849390/article/details/130192288