一、LVM–逻辑卷管理
1)LVM: Logical Volume Manager(逻辑卷管理)是Linux环境下对底层磁盘的一种管理机制,处在物理磁盘和文件系统之间的
2)概念
PV :Physical Volume(物理卷)
VG :Volume Group(卷组)
LV :Logical Volume(逻辑卷)
最小存储单位为:PE
3)创建步骤
a)添加磁盘 sdb sdc sdd
[root@lszlab01 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sdb 8:16 0 20G 0 disk
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 200M 0 part /boot
├─sda2 8:2 0 8G 0 part /
└─sda3 8:3 0 3.7G 0 part [SWAP]
sdd 8:48 0 20G 0 disk
sdc 8:32 0 20G 0 disk
b)制作PV:
先检查是否有lvm工具
[root@lszlab01 ~]# rpm -qa |grep lvm
然后使用pvcreate命令
[root@lszlab01 ~]# pvcreate /dev/sdb /dev/sdc
Physical volume "/dev/sdb" successfully created
Physical volume "/dev/sdc" successfully created
c)制作VG:
[root@lszlab01 ~]# vgcreate Vg00 /dev/sdb /dev/sdc
Volume group "Vg00" successfully created
指定PE的大小
[root@lszlab01 ~]# vgcreate -s 16M Vg00 /dev/sdb /deb/sdbc
d)制作LV:
[root@lszlab01 ~]# lvcreate -n lvo01 -L 35G Vg00
Logical volume "lvo01" created
[root@lszlab01 ~]# lvcreate -n lvo02 -L 4.5G Vg00
Logical volume "lvo02" created
e)查看相关信息
[root@lszlab01 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb Vg00 lvm2 a-- 20.00g 0
/dev/sdc Vg00 lvm2 a-- 20.00g 504.00m
[root@lszlab01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
Vg00 2 2 0 wz--n- 39.99g 504.00m
[root@lszlab01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvo01 Vg00 -wi-a---- 35.00g
lvo02 Vg00 -wi-a---- 4.50g
[root@lszlab01 ~]# pvscan
PV /dev/sdb VG Vg00 lvm2 [20.00 GiB / 0 free]
PV /dev/sdc VG Vg00 lvm2 [20.00 GiB / 504.00 MiB free]
Total: 2 [39.99 GiB] / in use: 2 [39.99 GiB] / in no VG: 0 [0 ]
[root@lszlab01 ~]# vgscan
Reading all physical volumes. This may take a while...
Found volume group "Vg00" using metadata type lvm2
[root@lszlab01 ~]# lvscan
ACTIVE '/dev/Vg00/lvo01' [35.00 GiB] inherit
ACTIVE '/dev/Vg00/lvo02' [4.50 GiB] inherit
[root@lszlab01 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb
VG Name Vg00
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 5119
Free PE 0
Allocated PE 5119
PV UUID xqc1yf-gs4A-v2Xa-7Tne-s2rl-cBRz-XKhAou
--- Physical volume ---
PV Name /dev/sdc
VG Name Vg00
PV Size 20.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 5119
Free PE 126
Allocated PE 4993
PV UUID xoxAkF-9yYx-PDJ4-45xJ-fuap-3wWN-YtHPXh
[root@lszlab01 ~]# vgdisplay
--- Volume group ---
VG Name Vg00
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 0
Max PV 0
Cur PV 2
Act PV 2
VG Size 39.99 GiB
PE Size 4.00 MiB
Total PE 10238
Alloc PE / Size 10112 / 39.50 GiB
Free PE / Size 126 / 504.00 MiB
VG UUID wwfTjS-Ogu7-MWHY-dhlm-syvj-oUeL-pUyLJw
[root@lszlab01 ~]# lvdisplay
--- Logical volume ---
LV Path /dev/Vg00/lvo01
LV Name lvo01
VG Name Vg00
LV UUID xMpAIJ-ffy7-GGxA-gOJ9-4vPN-lgKU-51LQho
LV Write Access read/write
LV Creation host, time lszlab01, 2017-05-23 23:34:22 +0000
LV Status available
# open 0
LV Size 35.00 GiB
Current LE 8960
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
--- Logical volume ---
LV Path /dev/Vg00/lvo02
LV Name lvo02
VG Name Vg00
LV UUID 8WM1IQ-fQc5-UKsm-Nl0M-SOSC-J4YZ-71nM0t
LV Write Access read/write
LV Creation host, time lszlab01, 2017-05-23 23:45:01 +0000
LV Status available
# open 0
LV Size 4.50 GiB
Current LE 1152
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:1
f)格式化分区
[root@lszlab01 ~]# mkfs -t ext3 -c /dev/Vg00/lvo01
[root@lszlab01 ~]# mkfs -t ext3 -c /dev/Vg00/lvo02
挂载,采用UUID的方式挂载到 /home/LV /home/LV1
[root@lszlab01 ~]# blkid
/dev/sda2: UUID="f0e8507a-67a3-4c5b-817a-22a10118eeb5" TYPE="ext4"
/dev/sdb: UUID="xqc1yf-gs4A-v2Xa-7Tne-s2rl-cBRz-XKhAou" TYPE="LVM2_member"
/dev/sda1: UUID="a52fc10a-b145-40cd-b037-2780c6579379" TYPE="ext4"
/dev/sda3: UUID="361e88a4-3972-4157-a90b-0a2280aa634c" TYPE="swap"
/dev/mapper/Vg00-lvo01: UUID="8282b190-75a2-481c-ad94-c5ca53f7b0fb" SEC_TYPE="ext2" TYPE="ext3"
/dev/sdc: UUID="xoxAkF-9yYx-PDJ4-45xJ-fuap-3wWN-YtHPXh" TYPE="LVM2_member"
/dev/mapper/Vg00-lvo02: UUID="ebccf12d-ef22-4576-b98e-8c845c9097cb" SEC_TYPE="ext2" TYPE="ext3"
[root@lszlab01 ~]# mkdir /home/LV
[root@lszlab01 ~]# mkdir /home/LV1
编辑/etc/fstab,退出保存
[root@lszlab01 ~]# vi /etc/fstab
UUID=ebccf12d-ef22-4576-b98e-8c845c9097cb /home/LV ext3 defaults 0 0
UUID=8282b190-75a2-481c-ad94-c5ca53f7b0fb /home/LV1 ext3 defaults 0 0
重新挂载
[root@lszlab01 LV]# mount -a
[root@lszlab01 LV]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/Vg00-lvo01 on /home/LV type ext3 (rw)
/dev/mapper/Vg00-lvo02 on /home/LV type ext3 (rw)
/dev/mapper/Vg00-lvo01 on /home/LV1 type ext3 (rw)
/dev/mapper/Vg00-lvo02 on /home/LV1 type ext3 (rw)
g)LV扩展(LVM支持动态扩容)
先查看一下基本情况
[root@lszlab01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
Vg00 2 2 0 wz--n- 39.99g 504.00m
[root@lszlab01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvo01 Vg00 -wi-ao--- 35.00g
lvo02 Vg00 -wi-ao--- 4.50g
增加一块磁盘sdd,创建成pv 扩容卷组,然后扩容lv
[root@lszlab01 ~]# pvcreate /dev/sdd
Physical volume "/dev/sdd" successfully created
[root@lszlab01 ~]# vgextend Vg00 /dev/sdd
Volume group "Vg00" successfully extended
查看一下,卷组增加了20G空闲空间
[root@lszlab01 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
Vg00 3 2 0 wz--n- 59.99g 20.49g
然后扩展lvo02的大小+20G
[root@lszlab01 ~]# lvextend -L +20G /dev/Vg00/lvo02
Extending logical volume lvo02 to 24.50 GiB
Logical volume lvo02 successfully resized
查看一下
[root@lszlab01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvo01 Vg00 -wi-ao--- 35.00g
lvo02 Vg00 -wi-ao--- 24.50g
在df -h看一下 lvo02大小没有变
root@lszlab01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.9G 5.1G 2.5G 67% /
tmpfs 206M 0 206M 0% /dev/shm
/dev/sda1 194M 32M 153M 18% /boot
/dev/mapper/Vg00-lvo02 4.5G 138M 4.1G 4% /home/LV
/dev/mapper/Vg00-lvo01 35G 177M 33G 1% /home/LV1
最后要使用resizefs2命令重新加载逻辑卷的大小才能生效
[root@lszlab01 ~]# resize2fs /dev/Vg00/lvo02
在查看一下
[root@lszlab01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.9G 5.1G 2.5G 67% /
tmpfs 206M 0 206M 0% /dev/shm
/dev/sda1 194M 32M 153M 18% /boot
/dev/mapper/Vg00-lvo02 25G 141M 23G 1% /home/LV
/dev/mapper/Vg00-lvo01 35G 177M 33G 1% /home/LV1
扩容成功,而且数据不受影响
[root@lszlab01 ~]# cd /home/LV
[root@lszlab01 LV]# ll
total 16
-rw-r--r-- 1 root root 0 May 24 00:33 1
-rw-r--r-- 1 root root 0 May 24 00:33 2
-rw-r--r-- 1 root root 0 May 24 00:33 3
-rw-r--r-- 1 root root 0 May 24 00:33 4
-rw-r--r-- 1 root root 0 May 24 00:33 5
-rw-r--r-- 1 root root 0 May 24 00:33 6
-rw-r--r-- 1 root root 0 May 24 00:33 7
drwx------ 2 root root 16384 May 23 23:58 lost+found
h)LV缩减
[root@lszlab01 LV]# fuser -k /dev/Vg00/lvo02
[root@lszlab01 LV]# umount /home/LV #卸载分区
[root@lszlab01 LV]# e2fsck -f /dev/Vg00/lvo02 #检查文件系统是否正常工作,-f : 强制检查
e2fsck 1.41.12 (17-May-2010)
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/Vg00/lvo02: 48/1605632 files (2.1% non-contiguous), 143377/6422528 blocks
[root@lszlab01 LV]# resize2fs /dev/Vg00/lvo02 10G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/Vg00/lvo02 to 2621440 (4k) blocks.
The filesystem on /dev/Vg00/lvo02 is now 2621440 blocks long.
[root@lszlab01 LV]# lvreduce -L 10G /dev/Vg00/lvo02
WARNING: Reducing active logical volume to 10.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvo02? [y/n]: Y
Reducing logical volume lvo02 to 10.00 GiB
Logical volume lvo02 successfully resized
[root@lszlab01 LV]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvo01 Vg00 -wi-ao--- 35.00g
lvo02 Vg00 -wi-a---- 10.00g
[root@lszlab01 LV]# mount -a
[root@lszlab01 LV]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.9G 5.1G 2.5G 67% /
tmpfs 206M 0 206M 0% /dev/shm
/dev/sda1 194M 32M 153M 18% /boot
/dev/mapper/Vg00-lvo01 35G 177M 33G 1% /home/LV1
/dev/mapper/Vg00-lvo02 9.9G 165M 9.2G 2% /home/LV
vg缩减
缩减时,可以不卸载正在使用中的LV。另外,只能缩减没有被使用的pv
[root@lszlab01 ~]# vgreduce /dev/Vg00/lvo02
LVM删除:
1)删除逻辑卷
[root@lszlab01 ~]# umount /dev/Vg00/lvo02
[root@lszlab01 ~]# lvremove /dev/Vg00/lvo02
2)删除卷
[root@lszlab01 ~]# vgremove Vg00
3)删pv
[root@lszlab01 ~]# pvremove /dev/sdd
二、LVM快照
创建一个快照,备份lvo02, (原来要有lv,vg需要有足够空间)
[root@lszlab01 ~]# lvcreate -s -n lvo02_sp_bak -L 2G /dev/Vg00/lvo02
Logical volume "lvo02_sp_bak" created
[root@lszlab01 ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy% Sync Convert
lvo01 Vg00 -wi-ao--- 35.00g
lvo02 Vg00 owi-aos-- 10.00g
lvo02_sp_bak Vg00 swi-a-s-- 2.00g lvo02 0.00
挂载快照到/home/LV1/data_bak
[root@lszlab01 ~]# vi /etc/fstab
/dev/Vg00/lvo02_sp_bak /home/LV1/data_bak ext3 defaults 0 0
重新挂载
[root@lszlab01 ~]# mount -a
[root@lszlab01 ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
/dev/mapper/Vg00-lvo01 on /home/LV1 type ext3 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/Vg00-lvo02 on /home/LV type ext3 (rw)
/dev/mapper/Vg00-lvo02_sp_bak on /home/LV1/data_bak type ext3 (rw)
查看一下挂载目录里面的文件
[root@lszlab01 ~]# ll /home/LV1/data_bak
total 20
-rw-r--r-- 1 root root 0 May 24 00:33 1
-rw-r--r-- 1 root root 0 May 24 03:53 1.sh
-rw-r--r-- 1 root root 0 May 24 00:33 2
-rw-r--r-- 1 root root 0 May 24 03:53 2.sh
-rw-r--r-- 1 root root 0 May 24 00:33 3
-rw-r--r-- 1 root root 0 May 24 03:53 3.sh
-rw-r--r-- 1 root root 0 May 24 00:33 4
-rw-r--r-- 1 root root 0 May 24 03:53 4.sh
-rw-r--r-- 1 root root 0 May 24 00:33 5
-rw-r--r-- 1 root root 0 May 24 03:53 5.sh
-rw-r--r-- 1 root root 0 May 24 00:33 6
-rw-r--r-- 1 root root 0 May 24 03:53 6.sh
-rw-r--r-- 1 root root 0 May 24 00:33 7
dr-xr-xr-x 5 root root 4096 May 24 01:57 boot
drwx------ 2 root root 16384 May 23 23:58 lost+found
备份到备份目录
[root@lszlab01 ~]# cp -r /home/LV1/data_bak /home/bak
三、磁盘配额
查看安装包
[root@lszlab01 data_bak]# rpm -q quota
quota-3.17-18.el6.x86_64
启用quota磁盘配额功能
[root@lszlab01 ~]# mount -o remount,usrquota,grpquota /home/LV1
[root@lszlab01 ~]# mount
[root@lszlab01 data_bak]# vi /etc/fstab
UUID=8282b190-75a2-481c-ad94-c5ca53f7b0fb /home/LV1 ext3 defaults,usrquota,grpquota 0 0
重新挂载
[root@lszlab01 data_bak]# mount -a
检测磁盘配额并生成配额文件
[root@lszlab01 ~]# quotacheck -cugv /home/LV1
[root@lszlab01 ~]# ll /home/LV1
total 36
-rw------- 1 root root 6144 May 24 04:34 aquota.group
-rw------- 1 root root 6144 May 24 04:34 aquota.user
drwxr-xr-x 4 root root 4096 May 24 03:53 data_bak
drwx------ 2 root root 16384 May 23 23:57 lost+found
用户和组账号的配额设置
[root@lszlab01 ~]# groupadd dba
[root@lszlab01 ~]# useradd -g dba -d /home/dba dba02
配置限额
[root@lszlab01 ~]# edquota -g dba
Disk quotas for group dba (gid 503):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/Vg00-lvo01 0 120 150 0 0 0
~激活磁盘配额
[root@lszlab01 dba]# quotaon -ugv /home/LV1
/dev/mapper/Vg00-lvo01 [/home/LV1]: group quotas turned on
/dev/mapper/Vg00-lvo01 [/home/LV1]: user quotas turned on
测试:
[root@lszlab01 LV1]# mkdir test_dba
[root@lszlab01 LV1]# chmod 777 /home/LV1/test_dba
[dba02@lszlab01 test_dba]$ dd if=/dev/zero of=123.txt bs=1K count=120
dm-0: warning, group block quota exceeded.
120+0 records in
120+0 records out
122880 bytes (123 kB) copied, 0.000342237 s, 359 MB/s
[dba02@lszlab01 test_dba]$ dd if=/dev/zero of=3.txt bs=1K count=1800
dm-0: warning, group block quota exceeded.
dm-0: write failed, group block limit reached.
dd: writing `3.txt': Disk quota exceeded
145+0 records in
144+0 records out
147456 bytes (147 kB) copied, 0.000889251 s, 166 MB/s