Linux下的LVM和磁盘配额的配置和管理
一:实验目标
LVM日常管理
LVM快照
磁盘配额
二:实验概念
概念(名词):最小存储单位为:PE
PV
Physical Volume(物理卷)
VG
Volume Group(卷组)
LV
Logical Volume(逻辑卷)
总结:
名称 最小存储单位 cre硬盘 扇区(512字节)
文件系统 block(1K或4K )
raid chunk (512K) mdadm -c
LVM PE (16M自己定义)
三:实验拓扑
LVM层次结构图:
图解:
1第一步先制作PV物理卷,并将创建的指向默认的分区。
2:第二步制作VG卷组,命名为Vg1,G功能是将第一步创建的PV进行整合,两个分区,各1G,所以VG大小为2G。
3:第三步制作LV逻辑卷,命名为LV1,大小是从VG卷组中划出来。
实验要求
1:在虚拟机中添加一块硬盘:sdb或者多块。
四:实验步骤
第一块
1:实验分区,将sdb分为3个主分区
[[email protected]~]# fdisk /dev/sdb
[[email protected]~]# ll /dev/sdb*
brw-rw----1 root disk 8, 16 Jul 3 05:49 /dev/sdb
brw-rw----1 root disk 8, 17 Jul 3 05:49 /dev/sdb1
brw-rw----1 root disk 8, 18 Jul 3 05:49 /dev/sdb2
brw-rw----1 root disk 8, 19 Jul 3 05:49 /dev/sdb3
2:制作PV
[[email protected]~]# pvcreate /dev/sdb{1,2}
Writing physical volume data to disk"/dev/sdb1"
Physical volume "/dev/sdb1"successfully created
Writing physical volume data to disk"/dev/sdb2"
Physical volume "/dev/sdb2"successfully created
3:制作VG
[[email protected]~]# vgcreate Vg1 /dev/sdb{1,2}
Volume group "Vg1" successfullycreated
4:制作LV
[[email protected]~]# lvcreate -n LV1 -L 1.5G Vg1 #名字叫LV1 大小是1.5G 从Vg1中划过来
Logical volume "LV1" created
5:各种查看:
#pvs #pvscan #pvdisplay
#vgs #vgscan #vgdisplay
#lvs #lvscan #lvdisplay
6:查看验证
[[email protected]~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb1 #物理卷包括了sdb1
VG Name Vg1 #此物理卷属于卷组Vg1
PV Size 1.01 GiB / not usable 3.41 MiB #物理卷sdb1默认分配为1G
PESize 4.00 MiB #物理卷的默认大小为4M
--- Physical volume ---
PV Name /dev/sdb2 #物理卷包括了sdb2
VG Name Vg1 #此物理卷属于卷组Vg1
PV Size 1.01 GiB / not usable 3.44 MiB #物理卷sdb2默认分配为1G
PESize 4.00 MiB #物理卷的默认大小为4M
#从表中可以看出此物理卷包括了sdb1和sdb2
[[email protected]~]# vgdisplay
--- Volume group ---
VG Name Vg1 #卷组的名字为Vg1
VG Size 2.02 GiB #卷组的大小为2G,由sdb{1,2}两个各1G相加而得。
PE Size 4.00 MiB #卷组的默认大小为4M
Alloc PE / Size 384 / 1.50 GiB #卷组已用了1.5G
Free PE / Size 132 / 528.00 MiB #卷组剩下0.5G
[[email protected]~]# lvdisplay
--- Logical volume ---
LV Name /dev/Vg1/LV1#逻辑卷名字为LV1,属于卷组Vg1
VG Name Vg1 #属于卷组Vg1
LV Size 1.50 GiB #此逻辑卷空间大小为1.5G
创建时指定一下PE:
[[email protected]~]# vgcreate -s 16M Vg1 /dev/sdb{1,2}
#-s 指定PE的大小
#在创建前就指定大小,这里只写命令,实际并不执行
6:对LV1进行格式化
[[email protected]~]# mkfs.ext4 /dev/Vg1/LV1
7:对LV1进行挂载
[[email protected]~]# mount /dev/Vg1/LV1 /opt
[[email protected]~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 3.9G 5.3G 42% /
tmpfs 996M 24K 996M 1% /dev/shm
/dev/sda1 2.0G 57M 1.8G 4% /boot
/dev/mapper/VG1-LV1 1.5G 35M 1.4G 3% /opt
[[email protected]~]# ls /opt/
lost+found
[[email protected]~]# mount #查看
/dev/mapper/VG1-LV1 on /opt type ext4 (rw) #
[[email protected]~]# ll /dev/VG1/LV1
lrwxrwxrwx1 root root 7 Jul 3 06:30 /dev/VG1/LV1 -> ../dm-0 #指向dm-0
[[email protected]~]# ll /dev/mapper/VG1-LV1
lrwxrwxrwx1 root root 7 Jul 3 06:30 /dev/mapper/VG1-LV1 -> ../dm-0 #指向dm-0
两个指向相同,
第二块
1:LV扩展:磁盘与文件系统
LVM支持动态扩容。
了解基本情况------>>>与实验拓扑图解一样
[root[email protected]~]# vgslv
VG #PV #LV #SN Attr VSizeVFree
VG1 2 1 0 wz--n- 2.02g 528.00m #vg下有2个pv ,1个lv。
[[email protected]~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LV1 VG1 -wi-ao 1.50g #lv是在VG1下,大小为1.5G
[[email protected]~]# p-L
PV VG Fmt Attr PSize PFree
/dev/sdb1 VG1 lvm2 a-- 1.01g 0
/dev/sdb2 VG1 lvm2 a-- 1.01g 528.00m #:sdb1.2下都有一个Vg1 ,大小为1G
2:LV逻辑卷进行扩容
[[email protected]~]# lvextend -L +300M /dev/Vg1/LV1
Extending logical volume LV1 to 1.79 GiB # 1.5+0.3=1.8 这里说明扩容成功了
Logical volume LV1 successfully resized
[[email protected]~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 2.0G 57M 1.8G 4% /boot
/dev/mapper/VG1-LV1 1.5G 35M 1.4G 3% /opt
#这里我们看到,LV1挂载再这里,明明lv1扩容成功了,这里大小却依然是1.5G,为什么呢?
答:
因为lvm出于磁盘与文件系统之间,需要将磁盘和文件系统进行一起扩展,才会真正的扩展成功
3:将文件系统也进行扩容
[[email protected]~]# resize2fs /dev/Vg1/LV1
resize2fs1.41.12 (17-May-2010)
Filesystemat /dev/VG1/LV1 is mounted on /opt; on-line resizing required
olddesc_blocks = 1, new_desc_blocks = 1
Performingan on-line resize of /dev/VG1/LV1 to 470016 (4k) blocks.
Thefilesystem on /dev/VG1/LV1 is now 470016 blocks long.
[[email protected] ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LV1 VG1 -wi-ao 1.79g
[[email protected]~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 3.9G 5.3G 42% /
tmpfs 996M 24K 996M 1% /dev/shm
/dev/sda1 2.0G 57M 1.8G 4% /boot
/dev/mapper/VG1-LV1 1.8G 35M 1.7G 2% /opt
#这个时候就变为1.8G了,实验成功。
4:VG扩容,将sdb3加入进去:
[[email protected]~]# vgextend VG1 /dev/sdb3
No physical volume label read from /dev/sdb3
Physical volume "/dev/sdb3"successfully created
Volume group "VG1"successfully extended
扩容前
[[email protected]~]# vgs
VG #PV #LV #SN Attr VSize VFree
VG1 2 1 0 wz--n- 2.02g528.00m
扩容后
[[email protected]~]# vgs
VG #PV #LV #SN Attr VSize VFree
VG1 3 1 0 wz--n- 3.02g1.23g
[[email protected]~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 VG1 lvm2 a-- 1.01g 0
/dev/sdb2 VG1 lvm2 a-- 1.01g 228.00m
/dev/sdb3 VG1 lvm2 a-- 1.01g 1.01g
VG1中有3个sdb
试验成功
4:Lv缩减
[[email protected]~]# resize2fs /dev/Vg1/LV1 1000M
resize2fs1.41.12 (17-May-2010)
Filesystemat /dev/Vg1/LV1 is mounted on /opt; on-line resizing required
On-lineshrinking from 470016 to 256000 not supported.
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sda2 9.7G 3.1G 6.1G 34% /
tmpfs 1000M 264K 1000M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
/dev/mapper/Vg1-LV1 1.8G 35M 1.7G 3% /opt
[[email protected]~]# umount /opt/
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sda2 9.7G 3.1G 6.1G 34% /
tmpfs 1000M 264K 1000M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
[[email protected]~]# resize2fs /dev/Vg1/LV1 1000M
resize2fs1.41.12 (17-May-2010)
请先运行 ‘e2fsck-f /dev/Vg1/LV1‘.
[[email protected]~]# e2fsck -f /dev/Vg1/LV1
e2fsck1.41.12 (17-May-2010)
第一步: 检查inode,块,和大小
第二步: 检查目录结构
第三步: 检查目录连接性
Pass4: Checking reference counts
5: 检查簇概要信息
/dev/Vg1/LV1:11/117600 files (9.1% non-contiguous), 16274/470016 blocks
[[email protected]~]# resize2fs /dev/Vg1/LV1 1000M
resize2fs1.41.12 (17-May-2010)
Resizingthe filesystem on /dev/Vg1/LV1 to 256000 (4k) blocks.
Thefilesystem on /dev/Vg1/LV1 is now 256000 blocks long.
[[email protected]~]# echo $?
0
[[email protected]~]# lvreduce -L 1000M /dev/Vg1/LV1
WARNING: Reducing active logical volume to1000.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Doyou really want to reduce LV1? [y/n]: y
Reducing logical volume LV1 to 1000.00 MiB
Logical volume LV1 successfully resized
[[email protected]~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LV1 Vg1 -wi-a- 1000.00m
6:Vg减少
[[email protected]~]# mount /dev/Vg1/LV1 /opt
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sda2 9.7G 3.1G 6.1G 34% /
tmpfs 1000M 264K 1000M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
/dev/mapper/Vg1-LV1 985M 35M 901M 4% /opt
:
[[email protected]~]# vgs
VG #PV #LV #SN Attr VSize VFree
Vg1 3 1 0 wz--n- 3.02g 2.05g
[[email protected]~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 Vg1 lvm2 a-- 1.01g 32.00m
/dev/sdb2 Vg1 lvm2 a-- 1.01g 1.01g
/dev/sdb3 Vg1 lvm2 a-- 1.01g 1.01g
[[email protected]~]# vgreduce Vg1 /dev/sdb1
Physical volume "/dev/sdb1" stillin use
[[email protected]~]# vgreduce Vg1 /dev/sdb3
Removed "/dev/sdb3" from volumegroup "Vg1"
[[email protected]~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 Vg1 lvm2 a-- 1.01g 32.00m
/dev/sdb2 Vg1 lvm2 a-- 1.01g 1.01g
/dev/sdb3 lvm2 a-- 1.01g 1.01g
[[email protected]~]# vgs
VG #PV #LV #SN Attr VSize VFree
Vg1 2 1 0 wz--n- 2.02g 1.04g
7:LVM删除
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sda2 9.7G 3.1G 6.1G 34% /
tmpfs 1000M 264K 1000M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
/dev/mapper/Vg1-LV1 985M 35M 901M 4% /opt
[[email protected]~]# umount /opt/
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sda2 9.7G 3.1G 6.1G 34% /
tmpfs 1000M 264K 1000M 1% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
[[email protected]~]# lvremove /dev/Vg1/LV1
Doyou really want to remove active logical volume LV1? [y/n]: y
Logical volume "LV1" successfullyremoved
[[email protected]~]# lvs
[[email protected]~]# vgremove Vg1
Volume group "Vg1" successfullyremoved
[[email protected]~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 a-- 1.01g 1.01g
/dev/sdb2 lvm2 a-- 1.01g 1.01g
/dev/sdb3 lvm2 a-- 1.01g 1.01g
[[email protected]~]# pvremove /dev/sdb3
Labels on physical volume"/dev/sdb3" successfully wiped
第三块
LVM快照功能:
lvm快照有两大用途,一是用来克隆虚拟机,例如做xen虚拟机时,可先新建一台完整虚拟机,假设大小为10G,然后我们可以在这台10G的虚拟机上创建1个3G的快照,接着通过这个3G的快照启动虚拟机,做完实验后,可以直接删除快照而保持原来完整虚拟机的纯净。
lvm快照的第二大用途是实时备份(moment-in-time),即为了保持系统的一致性,我们先做一个快照冻结当前系统状态,这样快照里面的内容可暂时保持不变,系统本身继续运行,通过备份快照来实现不中断服务的的备份。
本实验基于快照的热备。
原来有一个lv的基础上
[[email protected]~]# pvcreate /dev/sdb{1,2}
Writing physical volume data to disk"/dev/sdb1"
Physical volume "/dev/sdb1"successfully created
Writing physical volume data to disk"/dev/sdb2"
Physical volume "/dev/sdb2" successfullycreated
[[email protected]~]# vgcreate Vg1 /dev/sdb{1,2}
Volume group "Vg1" successfullycreated
[[email protected]~]# lvcreate -n LV1 -L 1G Vg1
Logical volume "LV1" created
[[email protected]~]# mkfs.ext4 /dev/Vg1/LV1
[[email protected]~]# mount /dev/Vg1/LV1 /opt
[[email protected]~]# df -h
文件系统 容量 已用 可用已用%% 挂载点
/dev/sr0 3.4G 3.4G 0 100% /media/RHEL_6.2 x86_64 Disc 1
/dev/mapper/Vg1-LV1 1008M 34M 924M 4% /opt
[[email protected]~]# cp /etc/passwd /opt
[[email protected]~]# ls /opt
lost+found passwd
[[email protected]~]# lvcreate -s -n lv1_sp -L 300M /dev/Vg1/LV1
Logical volume "lv1_sp" created
[[email protected]~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
LV1 Vg1 owi-a- 1.00g
lv1_sp Vg1 swi-a- 300.00m LV1 0.00
[[email protected]~]# mkdir /tmp/lv1-back
[[email protected]~]# mount /dev/Vg1/lv1_sp2 /tmp/lv1-back
[[email protected]~]# ls !$
ls/tmp/lv1-back
lost+found passwd
[[email protected]~]# ls /opt
lost+found passwd
[[email protected]~]# du -sh /boot
22M /boot
[[email protected]~]# cp -r /boot/ /opt/
[[email protected]~]# ls !$
ls/opt/
boot lost+found passwd
[[email protected]~]# ls /tmp/lv1-back/
lost+found passwd
[[email protected]~]#
第四块
磁盘配额
1:限定用户或组对磁盘空间的使用。
[[email protected] ~]# rpm -qquota
quota-3.17-20.el6.x86_64
#关闭selinux
[[email protected] ~]# setenforce0
2.启用quota磁盘配额功能
[[email protected] ~]# mkfs.ext4/dev/sdd1
[[email protected] ~]# mkdir/sdd1
[[email protected] ~]# mount/dev/sdd1 /sdd1/
[[email protected] ~]# mount -oremount,usrquota,grpquota /sdd1/
[[email protected] ~]# vim/etc/fstab
/dev/sdd1 /sdd1 ext4 defaults,usrquota,grpquota 0 0
3.检测磁盘配额并生成配额文件
[[email protected] ~]# quotacheck-cugv /sdd1/
[[email protected] ~]# ll /sdd1/
total 32
-rw------- 1 root root 6144 Jul 18 22:18 aquota.group
-rw------- 1 root root 6144 Jul 18 22:18 aquota.user
drwx------ 2 root root 16384Jul 18 22:11 lost+found
4.用户和组账号的配额设
[[email protected]~]# edquota -u rm
Disk quotas for user rm (uid500):
Filesystem blocks soft hard inodes soft hard
/dev/sdd1 0 50 80 0 0 0
~
~参数:
激活磁盘配额
[[email protected] ~]#quotaon -ugv /sdd1/
/dev/sdd1 [/sdd1]: groupquotas turned on
/dev/sdd1 [/sdd1]: user quotasturned on
验证:
[[email protected] sdd1]# mkdirtest
[[email protected] sdd1]# chmod777 /sdd1/test/
[[email protected] sdd1]# su – rm
[[email protected] ~]$ cd/sdd1/test/
[[email protected] test]$ ddif=/dev/zero of=rm.txt bs=1K count=70
sdd1: warning, user blockquota exceeded.
记录了70+0 的读入
记录了70+0 的写出
71680字节(72 kB)已复制,0.00363215 秒,19.7 MB/秒
[[email protected] test]$ ll
总用量 72
-rw-rw-r-- 1 rm rm 71680 7月 18 22:30 rm.txt
[[email protected] test]$ ddif=/dev/zero of=rm.txt bs=1K count=100k
sdd1: warning, user blockquota exceeded.
sdd1: write failed, user blocklimit reached.
dd: 正在写入"rm.txt": 超出磁盘限额
记录了81+0 的读入
记录了80+0 的写出
81920字节(82 kB)已复制,0.0127658 秒,6.4 MB/秒
[[email protected] test]$ ll -h
总用量 80K
-rw-rw-r-- 1 rm rm 80K 7月 18 22:30 rm.txt
说明:
[[email protected] ~]#tune2fs -l /dev/sdb3 | grep size
Filesystemfeatures: has_journal ext_attrresize_inode dir_index filetype needs_recovery extent flex_bg sparse_superlarge_file huge_file uninit_bg dir_nlink extra_isize
Blocksize: 4096
Fragment size: 4096
说明,磁盘配额中的 blocks 。一个block是1K