概述:
我们都知道linux是多用户的操作系统,而用户使用操作系统是为了更好的使用资源,比如磁盘,网络,但是一台主机所拥有的资源是有限的,不能让某些用户自己独占掉,这时操作系统就需要出面做一些规定了,对于磁盘来说,操作系统就有一个叫做磁盘配额的东西来规定用户对磁盘资源的占有情况。
前面讲了这么多磁盘相关的操作,但是一直没提到一个问题,就是如果我们的磁盘分区好了,格式化好了,也挂载,到一个目录上面了,但是突然有一天,我发现这个挂载的目录不够我使用了,我扩大怎么办,这就是逻辑卷(LVM)就要登场了。
1.磁盘配额
磁盘配额到底是对用户限制的还是对文件系统限制的呢?如果是EXT系列的文件系统那么这个quota只能针对整个文件系统来设计,但如果是XFS文件系统,那么quota就可以支持对很多东西进行限制了,比如针对用户,用户组,甚至是目录。
下面我们先来简单的对一个student用户限制配额。
再做之前我们首先创建好实验环境
[root@station ~]# mount | grep /mnt
/dev/vdb1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
我们想在/mnt目录下做这个配额实验,但首先要看下这个目录被挂载的时候支持quota吗,根据上面结果发现并不支持。
[root@station ~]# umount /mnt/
[root@station ~]# mount -o usrquota /dev/vdb1 /mnt/
[root@station ~]# mount | grep /mnt/
[root@station ~]# mount | grep /mnt
/dev/vdb1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,usrquota)
我选择重新挂载,并且加上-o参数添加上挂载参数usrquota,有的小伙伴可能感觉我做的有点繁琐了,是不是可以直接一条命令解决如下:
[root@station ~]# mount | grep /mnt
/dev/vdb1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@station ~]# mount -o remount,usrquota /dev/vdb1 /mnt/
[root@station ~]# mount | grep /mnt
/dev/vdb1 on /mnt type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
[root@station /]# quotaon -uv /dev/vdb1
quotaon: Enforcing user quota already on /dev/vdb1
现在启用这个
盘的quota功能
[root@station ~]# edquota -u student
对student设置权限
Disk quotas for user student (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/vdb1 256 0 256 1 0 0
[student@station ~]$ dd if=/dev/zero of=/mnt/a bs=1K count=300
dd: error writing ‘/mnt/a’: Disk quota exceeded
257+0 records in
256+0 records out
262144 bytes (262 kB) copied, 0.000966842 s, 271 MB/s
[student@station ~]$ du /mnt/a
256 /mnt/a
发现只赋值过去了256K的数据
对xfs的磁盘限额还有一个更专业的命令就是xfs_quota。
首先是查看指令
xfs_quota
-x专家模式,后接-c可以指定参数
-c 后面加指令
指令:
print:单纯列出目前主机内文件系统参数等资料
df:与正常的df一致
report:返回当前设置的quota项目于
state:说明目前支持quota的文件系统信息
[root@station ~]# xfs_quota -x -c "print" /mnt/
Filesystem Pathname
/mnt /dev/vdb1 (uquota)
[root@station ~]# xfs_quota -x -c "report" /mnt/
User quota on /mnt (/dev/vdb1)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
student 0 0 256 00 [--------]
可以看到我们刚才设置的quota项目
对student在/mnt不许超过256K。
现在我们要通过这个命令设置quota
[root@station mnt]# xfs_quota -x -c "limit -u bhard=400M student" /mnt/
[root@station mnt]# xfs_quota -x -c "report" /mnt/
User quota on /mnt (/dev/vdb1)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
student 0 0 409600 00 [--------]
2.LVM逻辑卷
逻辑卷是一种非常灵活的方法,有了它我们可以随时扩大分区的大小,还可以做一个分区的镜像保护数据安全。讲逻辑卷之前首先要理解三个概念。pv,vg,pe,lvm,pv是physical volume(物理卷的意思)我们把他类比为橙子,vg是volume group是卷组的概念,我们将他类比为装橙汁的杯子,pe是physical extend是物理扩展,我们将他类比成橙汁的颗粒度(看个人喜好。。。有的人喜欢颗粒大的,有的喜欢小的),lvm就是我们真正的逻辑卷了,logical volume。我们把他类别成分成的橙汁。
根据上面的类比我们就很好理解了,我现在想喝橙汁,首先我买了橙子,又买了榨汁机,根据我自己的喜好,我榨汁三分钟(三分钟和五分钟颗粒度肯定不一样),把橙子倒进了一个杯子里,现在我要喝100ML就直接倒就好了,如果还想喝就继续倒,这就是LVM的用法类似。说了这么多该真正的试试了。
创建pv
Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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
Disk label type: dos
Disk identifier: 0x1278e24b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2099199 1048576 8e Linux LVM
/dev/vdb2 2099200 4196351 1048576 8e Linux LVM
/dev/vdb3 4196352 6293503 1048576 8e Linux LVM
/dev/vdb4 6293504 16779263 5242880 5 Extended
/dev/vdb5 6295552 8392703 1048576 8e Linux LVM
/dev/vdb6 8394752 10491903 1048576 82 Linux swap / Solaris
[root@station ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
[root@station ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb1 lvm2 a-- 1.00g 1.00g
通过pvcreate命令很简单的我们就建立好了一个pv,也就是榨了一杯汁;通过pvs或者pvdisplay显示这个pv的信息。
[root@station ~]# vgcreate vg0 /dev/vdb1
Volume group "vg0" successfully created
[root@station ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg0 1 0 0 wz--n- 1020.00m 1020.00m
通过vgcreate命令再选择一个名字,我们就将卷组建立起来,通过vgs或者vgdisplay可以显示vg的信息。现在榨好的橙汁也被倒进了杯子中。就等我们喝他了。
[root@station ~]# lvcreate -L 500M vg0
Logical volume "lvol0" created
[root@station ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lvol0 vg0 -wi-a----- 500.00m
[root@station ~]# ls /dev/mapper/
control vg0-lvol0
[root@station ~]# ls /dev/vg
vg0/ vga_arbiter
[root@station ~]# ls /dev/vg0/lvol0
/dev/vg0/lvol0
[root@station ~]# mkfs.xfs /dev/vg0/lvol0
meta-data=/dev/vg0/lvol0 isize=256 agcount=4, agsize=32000 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=128000, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[root@station ~]# mount /dev/vg0/lvol0 /mnt/
[root@station ~]# touch /mnt/file{1..10}
到这里逻辑卷的基本操作就讲完了,现在我们假设一个场景,我们需要再加入一个pv,倒入刚才杯子中,然后再把刚才的第一个pv去掉,但是全过程,我们不想让lvm上的数据损坏,仍要留下。我们试一下:
[root@station ~]# vgextend vg0 /dev/vdb2
Volume group "vg0" successfully extended
[root@station ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 1020.00m 520.00m
/dev/vdb2 vg0 lvm2 a-- 1020.00m 1020.00m
[root@station ~]# pvmove /dev/vdb1 /dev/vdb2
/dev/vdb1: Moved: 12.0%
/dev/vdb1: Moved: 100.0%
[root@station ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/vdb1 vg0 lvm2 a-- 1020.00m 1020.00m
/dev/vdb2 vg0 lvm2 a-- 1020.00m 520.00m
[root@station ~]# ls /mnt/
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
数据都在,完好无损,这时就可以安心的将/dev/vdb1拆除了
[root@station ~]# vgreduce vg0 /dev/vdb1
Removed "/dev/vdb1" from volume group "vg0"
[root@station ~]# pvre
pvremove pvresize
[root@station ~]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
lvcreate -L 500M -s -n snap /dev/vg0/lvol0
[root@station ~]# ls /dev/vg0/
lvol0 snap
通过上面的命令即可创建快照卷了,快照卷的内容和照快照的时候一模一样。
/dev/mapper/vg0-snap 497M 26M 472M 6% /mnt
现在挂载的是快照卷
[root@station ~]# ls /mnt/
file1 file10 file2 file3 file4 file5 file6 file7 file8 file9
快照卷的数据与被照的卷是一模一样的。
到此对LVM的介绍就基本结束了。
最近看过鸟哥的写的lvm部分后,发现这个快照卷还有妙用,强大到甚至可以将真机当虚拟机一样使用,想把快照卷完成这样,还需要一个工具,就是文件系统备份与恢复工具xfsdump和xfsrestore。
首先将接下xfsdump的用处(切记,xfsdump只能针对一个文件系统使用)
这个命令有很多的选项参数,但是咱们其实只要记住几个常用的就可以了
-l #指定备份的等级0是完整备份,1是增量备份
-L #为备份的会话(session)过程指定一个卷标(对文件系统的简单说明)
-M #对媒体的简单说明
-f #指定备份文件的路径
上面的-L和-M基本上随便设置就可以,但你一定要记住,比较重要的就只有-l和-f。至于备份的等级,我们只使用0这个完整备份。
xfsrestore命令是用于恢复文件系统用的,他的使用更加简单,我们只要使用-f参数指定备份文件路径,和一个分区的挂载点,上次备份的文件就恢复到那个挂载点下了。
现在开始实战:
首先建立lvm主卷大小是500m
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 500.00m
在这个卷上创建一些数据
[root@localhost mnt]# ls
grub.conf shadow test10 test3 test5 test7 test9
passwd test1 test2 test4 test6 test8
现在创建快照卷
[root@localhost ~]# lvcreate -s -L 26m -n snap /dev/vg0/lv0
Rounding up size to full physical extent 28.00 MiB
Logical volume "snap" created
[root@localhost ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv0 vg0 owi-a-s--- 500.00m
snap vg0 swi-a-s--- 28.00m lv0 0.00
快照卷的原理前面已经讲解过,他只是存储修改过的数据,所以不用给他分配太大的空间(最大最大是与lvm主卷一样大小)。
接下来到了重点了,就是备份数据。根据我们的需求,我们是备份哪个盘呢,lvm主盘,还是快照卷呢?当然是快照卷了,主盘太大了,备份起来也很慢,但其实这时,主盘和快照卷是一模一样的,快照卷只存储与主盘不一样的数据,如果出现问题直接xfsrestore恢复快照卷就可以了。
[root@localhost ~]# xfsdump -l 0 -L lvm1 -M lvm1 -f /root/snap.dump /media
xfsdump: using file dump (drive_simple) strategy
xfsdump: version 3.1.3 (dump format 3.0) - type ^C for status and control
xfsdump: level 0 dump of localhost:/media
xfsdump: dump date: Tue Nov 22 13:50:58 2016
xfsdump: session id: fb741ee4-dd59-4582-9010-fef5097c612d
xfsdump: session label: "lvm1"
xfsdump: ino map phase 1: constructing initial dump list 704 bytes
省略。。。
xfsdump: dump complete: 0 seconds elapsed
xfsdump: Dump Summary:
xfsdump: stream 0 /root/snap.dump OK (success)
xfsdump: Dump Status: SUCCESS
备份成功了,现在我们就可以随便我玩我们的快照卷了。
[root@localhost media]# ls
grub.conf shadow test10 test3 test5 test7 test9
passwd test1 test2 test4 test6 test8
[root@localhost media]# rm -rf *
一不小心把快照卷的东西全删了,好慌,老板要炒我了,不怕,我们有备份,现在开始恢复
[root@localhost media]# xfsrestore -f /root/snap.dump -L lvm1 /media
xfsrestore: using file dump (drive_simple) strategy
xfsrestore: version 3.1.3 (dump format 3.0) - type ^C for status and control
xfsrestore: searching media for dump
省略。。。
xfsrestore: stream 0 /root/snap.dump OK (success)
xfsrestore: Restore Status: SUCCESS
[root@localhost media]# ls
grub.conf shadow test10 test3 test5 test7 test9
passwd test1 test2 test4 test6 test8