文章目录
1 新建三个分区
三个分区分别为:/dev/sdb5、/dev/sdb6、/dev/sdb7
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x1c6dbeb1
Device Boot Start End Blocks Id System
/dev/sdb1 1 13054 104856223+ 5 Extended
/dev/sdb5 1 1306 10490382 83 Linux
/dev/sdb6 1307 2351 8393931 83 Linux
/dev/sdb7 2352 3005 5253223+ 83 Linux
2 修改分区类型为8e(Linux LVM)
Command (m for help): p
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x1c6dbeb1
Device Boot Start End Blocks Id System
/dev/sdb1 1 13054 104856223+ 5 Extended
/dev/sdb5 1 1306 10490382 8e Linux LVM
/dev/sdb6 1307 2351 8393931 8e Linux LVM
/dev/sdb7 2352 3005 5253223+ 8e Linux LVM
# 保存退出
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3 强制重读分区表
[root@centos68 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 5
BLKPG: Device or resource busy
error adding partition 6
BLKPG: Device or resource busy
error adding partition 7
查看是否读取到/dev/sdb5、/dev/sdb6、/dev/sdb7三个分区
[root@centos68 ~]# cat /proc/partitions
major minor #blocks name
8 0 104857600 sda
8 1 512000 sda1
8 2 104344576 sda2
8 16 104857600 sdb
8 17 1 sdb1
8 21 10490382 sdb5
8 22 8393931 sdb6
8 23 5253223 sdb7
253 0 52428800 dm-0
253 1 4096000 dm-1
253 2 47816704 dm-2
4 pv 管理
4.1 创建pv
[root@centos68 ~]# pvcreate -v /dev/sdb5
Wiping cache of LVM-capable devices
Set up physical volume for "/dev/sdb5" with 20980764 available sectors
Zeroing start of device /dev/sdb5
Writing physical volume data to disk "/dev/sdb5"
Physical volume "/dev/sdb5" successfully created
4.2 查看pv简要信息
[root@centos68 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_centos68 lvm2 a--u 99.51g 0
/dev/sdb5 lvm2 ---- 10.00g 10.00g
4.3 查看pv详细信息
[root@centos68 ~]# pvdisplay /dev/sdb5
"/dev/sdb5" is a new physical volume of "10.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb5
VG Name
PV Size 10.00 GiB
Allocatable NO # 因为还没有分配到VG,所以一下信息为空
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID evkeIz-uWgd-yE9L-1L1G-yxPB-1BRX-LuqO1U
4.4 移除pv
[root@centos68 ~]# pvremove /dev/sdb5
Labels on physical volume "/dev/sdb5" successfully wiped
[root@centos68 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_centos68 lvm2 a--u 99.51g 0
5 vg 管理
5.1 创建vg
[root@centos68 ~]# vgcreate my_vg /dev/sdb5
Volume group "my_vg" successfully created
5.2 查看vg简要信息
[root@centos68 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
my_vg 1 0 0 wz--n- 10.00g 10.00g
vg_centos68 1 3 0 wz--n- 99.51g 0
5.3 查看指定vg详细信息
vgdisplay 后面加vg名查询
[root@centos68 ~]# vgdisplay my_vg
--- Volume group ---
VG Name my_vg
System ID
Format lvm2
Metadata Areas 1
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 1
Act PV 1
VG Size 10.00 GiB
PE Size 4.00 MiB
Total PE 2560
Alloc PE / Size 0 / 0
Free PE / Size 2560 / 10.00 GiB
VG UUID nr9VNe-T2hk-CZ53-QgYn-ZEZK-JxJE-C0MOSi
查看PE大小
[root@centos68 ~]# pvdisplay /dev/sdb5
--- Physical volume ---
PV Name /dev/sdb5
VG Name my_vg
PV Size 10.00 GiB / not usable 4.51 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2560
Free PE 2560
Allocated PE 0
PV UUID NFEGzd-bWlN-uRCw-92Mu-fFMy-67nx-wic5lB
5.4 扩展vg
[root@centos68 ~]# pvcreate /dev/sdb6
Physical volume "/dev/sdb6" successfully created
[root@centos68 ~]# vgextend my_vg /dev/sdb6
Volume group "my_vg" successfully extended
查看vg详细信息
[root@centos68 ~]# vgdisplay my_vg
--- Volume group ---
VG Name my_vg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 2
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 18.00 GiB # 观察vg大小
PE Size 4.00 MiB
Total PE 4609
Alloc PE / Size 0 / 0
Free PE / Size 4609 / 18.00 GiB
VG UUID gwsrit-zEJH-dwE0-Gd5A-W6uA-KdBn-Q1AxYb
5.5 移除vg
移除vg前,先进行pvmove,即移动pv上的PE到同一卷组中的其他pv上去
[root@centos68 ~]# pvmove /dev/sdb5
No data to move for my_vg
[root@centos68 ~]# vgreduce my_vg /dev/sdb5
Removed "/dev/sdb5" from volume group "my_vg"
[root@centos68 ~]# vgdisplay my_vg
--- Volume group ---
VG Name my_vg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 8.00 GiB
PE Size 4.00 MiB
Total PE 2049
Alloc PE / Size 0 / 0
Free PE / Size 2049 / 8.00 GiB
VG UUID gwsrit-zEJH-dwE0-Gd5A-W6uA-KdBn-Q1AxYb
6 lv管理
6.1 创建逻辑卷
-L
指定逻辑卷大小,单位可以为“kKmMgGtT”字节
-n
给逻辑卷命名
[root@centos68 ~]# lvcreate -L 2G -n my_lv my_vg
Logical volume "my_lv" created.
查看创建好的逻辑卷my_lv
[root@centos68 ~]# ls /dev/mapper/
control vg_centos68-lv_home vg_centos68-lv_swap
my_vg-my_lv vg_centos68-lv_root
格式化逻辑卷my_lv
-t
指定格式化系统类型,默认ext2
-b
指定块大小
-L
指定卷标
[root@centos68 ~]# mkfs -t ext4 -b 1024 -L MYLV /dev/my_vg/my_lv
[root@centos68 ~]# blkid /dev/my_vg/my_lv
/dev/my_vg/my_lv: LABEL="MYLV" UUID="a37464b9-51a1-4322-9352-bd3cffdb0702" TYPE="ext4"
挂载my_lv分区
[root@centos68 ~]# mkdir /backup
[root@centos68 ~]# mount /dev/my_vg/my_lv /backup/
[root@centos68 ~]# ls /backup/
lost+found
[root@centos68 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos68-lv_root
50G 861M 46G 2% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 39M 413M 9% /boot
/dev/mapper/vg_centos68-lv_home
45G 52M 43G 1% /home
/dev/mapper/my_vg-my_lv
2.0G 3.1M 1.9G 1% /backup
6.2 扩展逻辑卷
查看逻辑卷my_lv大小
[root@centos68 ~]# lvdisplay my_vg
--- Logical volume ---
LV Path /dev/my_vg/my_lv
LV Name my_lv
VG Name my_vg
LV UUID okVZsh-CrC0-bqLV-kwTz-rTno-WFFM-eid02b
LV Write Access read/write
LV Creation host, time centos68, 2020-06-25 08:23:41 +0800
LV Status available
# open 1
LV Size 2.00 GiB # 逻辑卷大小为2G
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
-L
指定要添加的逻辑卷大小,+5
表示增加5G空间大小,5G
表示增加到5G大小,即由原来的2G增加到5G,共增加了3G
[root@centos68 ~]# lvextend -L +5G /dev/my_vg/my_lv
Size of logical volume my_vg/my_lv changed from 2.00 GiB (512 extents) to 7.00 GiB (1792 extents).
Logical volume my_lv successfully resized.
再次查看lv大小
[root@centos68 ~]# lvdisplay my_vg
--- Logical volume ---
LV Path /dev/my_vg/my_lv
LV Name my_lv
VG Name my_vg
LV UUID okVZsh-CrC0-bqLV-kwTz-rTno-WFFM-eid02b
LV Write Access read/write
LV Creation host, time centos68, 2020-06-25 08:23:41 +0800
LV Status available
# open 1
LV Size 7.00 GiB # +5G,从原来的2G增加到了7G
Current LE 1792
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
扩展文件系统大小
注意:上述扩展的逻辑卷大小,实际的文件系统大小并没有增加,也就是使用df -lh
查看挂载点发现空间大小并没有变化,需要使用resize2fs
命令扩展文件系统大小,改命令只适用于ext系列文件系统
[root@centos68 ~]# resize2fs /dev/my_vg/my_lv
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/my_vg/my_lv is mounted on /backup; on-line resizing required
old desc_blocks = 8, new_desc_blocks = 28
Performing an on-line resize of /dev/my_vg/my_lv to 7340032 (1k) blocks.
The filesystem on /dev/my_vg/my_lv is now 7340032 blocks long.
查看挂载点空间是否增加
[root@centos68 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos68-lv_root
50G 861M 46G 2% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 39M 413M 9% /boot
/dev/mapper/vg_centos68-lv_home
45G 52M 43G 1% /home
/dev/mapper/my_vg-my_lv
6.9G 3.5M 6.6G 1% /backup
6.3 缩减逻辑卷
思路
- 卸载文件系统(umount)
- 强制文件系统检测和修复(e2fsck -f)
- 缩减文件系统逻辑边界(resize2fs -L)
- 缩减逻辑卷(lvreduce)
- 重新挂载(mount)
卸载文件系统
[root@centos68 ~]# ls /backup/
crontab issue lost+found passwd
[root@centos68 ~]# umount /backup/
强制文件系统检测和修复
[root@centos68 ~]# ls /backup/
crontab issue lost+found passwd
[root@centos68 ~]# umount /backup/
缩减文件系统到指定大小
缩减文件系统(逻辑边界)到3G
[root@centos68 ~]# resize2fs /dev/my_vg/my_lv 3G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/my_vg/my_lv to 3145728 (1k) blocks.
The filesystem on /dev/my_vg/my_lv is now 3145728 blocks long.
缩减逻辑卷(物理边界)
[root@centos68 ~]# lvreduce -L 3G /dev/my_vg/my_lv
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce my_vg/my_lv? [y/n]: y
Size of logical volume my_vg/my_lv changed from 7.00 GiB (1792 extents) to 3.00 GiB (768 extents).
Logical volume my_lv successfully resized.
重新挂载
[root@centos68 ~]# mount /dev/my_vg/my_lv /backup/
[root@centos68 ~]# ls /backup/
crontab issue lost+found passwd
查看空间大小已经变为了3G
[root@centos68 ~]# df -lh
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_centos68-lv_root
50G 861M 46G 2% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 477M 39M 413M 9% /boot
/dev/mapper/vg_centos68-lv_home
45G 52M 43G 1% /home
/dev/mapper/my_vg-my_lv
3.0G 3.3M 2.8G 1% /backu
检查文件是否正常访问
[root@centos68 ~]# cat /backup/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
7 快照卷
7.1 创建快照
-s
创建快照
-L
指定快照大小
-p
只读权限
-n
指定快照名
[root@centos68 ~]# lvcreate -s -L 512M -p r -n my_lv_snap /dev/my_vg/my_lv
Logical volume "my_lv_snap" created.
挂载快照
[root@centos68 ~]# mount /dev/my_vg/my_lv_snap /mnt
mount: block device /dev/mapper/my_vg-my_lv_snap is write-protected, mounting read-only
修改源文件,不会影响快照卷中的文件变化
[root@centos68 ~]# cat /mnt/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
[root@centos68 ~]# echo "Change this file" >>/backup/issue
[root@centos68 ~]# cat /backup/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
Change this file
[root@centos68 ~]# cat /mnt/issue
CentOS release 6.8 (Final)
Kernel \r on an \m
7.1 删除快照
备份快照卷中文件
[root@centos68 ~]# cp -a /mnt/* /tmp
[root@centos68 ~]# ls /tmp
crontab issue lost+found passwd yum.log
卸载快照挂载点
[root@centos68 ~]# umount /mnt
删除快照
[root@centos68 ~]# lvremove /dev/my_vg/my_lv_snap
Do you really want to remove active logical volume my_lv_snap? [y/n]: y
Logical volume "my_lv_snap" successfully removed
8 删除逻辑卷、卷组、物理卷
思路:
- 卸载逻辑卷挂载点(umount);
- 删除逻辑卷(lvremove);
- 删除卷组(vgremove);
- 删除物理卷
8.1 删除逻辑卷
卸载逻辑卷挂载点
[root@centos68 ~]# umount /backup/
删除逻辑卷
[root@centos68 ~]# lvremove /dev/my_vg/my_lv
Do you really want to remove active logical volume my_lv? [y/n]: y
Logical volume "my_lv" successfully removed
删除卷组
[root@centos68 ~]# vgremove my_vg
Volume group "my_vg" successfully removed
查看卷组没了
[root@centos68 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_centos68 1 3 0 wz--n- 99.51g 0
移除物理卷
[root@centos68 ~]# pvremove /dev/sdb5
Labels on physical volume "/dev/sdb5" successfully wiped
[root@centos68 ~]# pvremove /dev/sdb6
Labels on physical volume "/dev/sdb6" successfully wiped
[root@centos68 ~]# pvremove /dev/sdb7
Labels on physical volume "/dev/sdb7" successfully wiped
查看物理卷没了
[root@centos68 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name vg_centos68
PV Size 99.51 GiB / not usable 3.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 25474
Free PE 0
Allocated PE 25474
PV UUID cGHZV5-fP4H-HzRy-wbcj-luwm-JtRe-F8QegK