LVM
1、准备partition
[root@localhost ~]# fdisk -l
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
Disk /dev/sdc: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
磁盘 /dev/sda:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00073beb
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
磁盘 /dev/mapper/cl-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/cl-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
2、创建PV
[root@localhost ~]# pvcreate /dev/sdc #sdb已经创建过了
Physical volume "/dev/sdc" successfully created.
[root@localhost ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 cl lvm2 a-- 19.00g 0
/dev/sdb lvm2 --- 20.00g 20.00g
/dev/sdc lvm2 --- 20.00g 20.00g
3、创建VG
-n 接要创建的VG名;
-s 接自定义PE的大小,默认是4M
VG最多只能含65534个PE,因此默认的VG有4M*65534/(1024M/G)= 256G
[root@localhost ~]# vgcreate -n testvg -s 16 M /dev/sd{b,c}
Volume group "testvg" successfully created
[root@localhost ~]# vgs
VG #PV #LV #SN Attr VSize VFree
cl 1 2 0 wz--n- 19.00g 0
testvg 2 0 0 wz--n- 39.97g 39.97g
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name testvg
PV Size 20.00 GiB / not usable 16.00 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 1279
Free PE 1279
Allocated PE 0
PV UUID XH9t0L-VHEz-aHxR-pHLI-rCF3-XLc6-SKAURX
--- Physical volume ---
PV Name /dev/sdc
VG Name testvg
PV Size 20.00 GiB / not usable 16.00 MiB
Allocatable yes
PE Size 16.00 MiB
Total PE 1279
Free PE 1279
Allocated PE 0
PV UUID StCJQv-PPSY-ZW0g-YYRG-vsBz-ufk9-bZC9Xq
4、创建LV
-L 直接加要创建逻辑卷大小;-l 指定PE的个数
[root@localhost ~]# lvcreate -L 5G -n testlv testvg
Logical volume "testlv" created.
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root cl -wi-ao---- 17.00g
swap cl -wi-ao---- 2.00g
testlv testvg -wi-a----- 5.00g
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID 9C9kEA-crG8-5a0H-XFmG-xViN-vtjz-K1jJ51
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-24 22:51:32 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 320
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@localhost ~]# mkfs.ext4 /dev/testvg/testlv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# mount /dev/testvg/testlv /usrs/
5、扩展LV
为了保证不丢失用户数据,我们必须先取消挂载,再进行扩展,扩展完一定要先检查文件系统,再重置硬盘硬盘容量,否则挂载硬盘大小还是之前的容量。
[root@localhost ~]# umount /dev/testvg/testlv
[root@localhost ~]# lvextend -L 7G /dev/testvg/testlv
Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
Logical volume testvg/testlv successfully resized.
[root@localhost ~]# e2fsck -f /dev/testvg/testlv #检查硬盘完整性即检查文件系统
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/testvg/testlv: 11/327680 files (0.0% non-contiguous), 58462/1310720 blocks
[root@localhost ~]# resize2fs /dev/testvg/testlv #重置硬盘容量
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/testlv to 1835008 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 1835008 blocks long.
[root@localhost ~]# mount /dev/testvg/testlv /usrs/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 17G 8.8G 8.3G 52% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/testvg-testlv 6.8G 23M 6.4G 1% /usrs
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID 9C9kEA-crG8-5a0H-XFmG-xViN-vtjz-K1jJ51
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-24 22:51:32 +0800
LV Status available
# open 0
LV Size 7.00 GiB
Current LE 448
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
6、压缩LV
如果文件系统是xfs时是不支持压缩空间的
虽然xfs文件系统只支持增加,不支持减少。但并不是说在xfs系统文件下不能减小,只是减小后,需要重新格式化才能挂载上。这样原来的数据就丢失了!以下是ext4文件系统。
[root@localhost ~]# umount /usrs/
[root@localhost ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/cl-root 17811456 9185972 8625484 52% /
devtmpfs 917772 0 917772 0% /dev
tmpfs 933632 84 933548 1% /dev/shm
tmpfs 933632 9088 924544 1% /run
tmpfs 933632 0 933632 0% /sys/fs/cgroup
/dev/sda1 1038336 176600 861736 18% /boot
tmpfs 186728 16 186712 1% /run/user/42
tmpfs 186728 0 186728 0% /run/user/0
[root@localhost ~]# e2fsck -f /dev/testvg/testlv #检查文件系统
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/testvg/testlv: 11/458752 files (0.0% non-contiguous), 67327/1835008 blocks
[root@localhost ~]# resize2fs /dev/testvg/testlv 3G #检查是否会报错
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/testlv to 786432 (4k) blocks.
The filesystem on /dev/testvg/testlv is now 786432 blocks long.
[root@localhost ~]# lvreduce -L 3G /dev/testvg/testlv
WARNING: Reducing active logical volume to 3.00 GiB.
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
Logical volume testvg/testlv successfully resized.
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID 8koYPi-U1aZ-0fJL-4PnS-Aa6p-Tp6Z-LcFBTd
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-27 21:18:31 +0800
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 192
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@localhost ~]# mount /dev/testvg/testlv /usrs/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 17G 8.8G 8.3G 52% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/testvg-testlv 2.9G 15M 2.7G 1% /usrs
7、创建快照
-s 创建快照 ;-p 指定快照读写性;-L 定义大小,理论上快照需要与逻辑卷相同大小
[root@localhost usrs]# lvcreate -n testlv_snap -s -p r -L 1G /dev/testvg/testlv
Using default stripesize 64.00 KiB.
Logical volume "testlv_snap" created.
[root@localhost usrs]# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID 9C9kEA-crG8-5a0H-XFmG-xViN-vtjz-K1jJ51
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-24 22:51:32 +0800
LV snapshot status source of
testlv_snap [active]
LV Status available
# open 1
LV Size 7.00 GiB
Current LE 448
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/testvg/testlv_snap
LV Name testlv_snap
VG Name testvg
LV UUID izf6Tn-lAB5-xkil-VelC-j9Yy-sGYt-WGaeQH
LV Write Access read only
LV Creation host, time localhost.localdomain, 2019-05-27 19:54:15 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 7.00 GiB
Current LE 448
COW-table size 1.00 GiB
COW-table LE 64
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:5
[root@localhost usrs]# dd if=/dev/zero of=/usrs/file count=1 bs=100M
1+0 records in
1+0 records out
104857600 bytes (105 MB) copied, 2.85488 s, 36.7 MB/s
[root@localhost usrs]# lvdisplay
--- Logical volume ---
LV Path /dev/testvg/testlv
LV Name testlv
VG Name testvg
LV UUID 8koYPi-U1aZ-0fJL-4PnS-Aa6p-Tp6Z-LcFBTd
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-05-27 21:18:31 +0800
LV snapshot status source of
testlv_snap [active]
LV Status available
# open 1
LV Size 3.00 GiB
Current LE 192
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
--- Logical volume ---
LV Path /dev/testvg/testlv_snap
LV Name testlv_snap
VG Name testvg
LV UUID 1IDy8Z-z0Bp-AVED-miUM-r8Y9-Zwow-FruUkF
LV Write Access read only
LV Creation host, time localhost.localdomain, 2019-05-27 21:48:01 +0800
LV snapshot status active destination for testlv
LV Status available
# open 0
LV Size 3.00 GiB
Current LE 192
COW-table size 1.00 GiB
COW-table LE 64
Allocated to snapshot 9.82%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:5
[root@localhost usrs]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 17G 8.8G 8.3G 52% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/testvg-testlv 2.9G 116M 2.6G 5% /usrs
8、恢复快照
恢复快照时必须卸载挂载点
[root@localhost usrs]# umount /usrs/ #卸载报错
umount: /usrs: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@localhost usrs]# fuser -mv /usrs/ #查看用户占用进程
USER PID ACCESS COMMAND
/usrs: root kernel mount /usrs
root 4026 ..c.. bash
[root@localhost usrs]# fuser -kv /usrs/ #杀死占用进程用户
USER PID ACCESS COMMAND
/usrs: root kernel mount /usrs
root 4026 ..c.. bash
[root@localhost ~]# umount /usrs/
[root@localhost ~]# lvconvert --merge /dev/testvg/testlv_snap
Merging of volume testvg/testlv_snap started.
testlv: Merged: 91.08%
testlv: Merged: 100.00%
[root@localhost ~]# mount /dev/testvg/testlv /usrs/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/cl-root 17G 8.8G 8.3G 52% /
devtmpfs 897M 0 897M 0% /dev
tmpfs 912M 84K 912M 1% /dev/shm
tmpfs 912M 8.9M 903M 1% /run
tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 183M 16K 183M 1% /run/user/42
tmpfs 183M 0 183M 0% /run/user/0
/dev/mapper/testvg-testlv 2.9G 16M 2.7G 1% /usrs