本节所讲内容:
创建LVM的基本步骤:
准备分区
[root@xuegod61 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb3
1、创建PV
[root@xuegod61 ~]# pvcreate /dev/sdb{1,2}
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
2、创建Vg
[root@xuegod61 ~]# vgcreate Vg1 /dev/sdb{1,2}
Volume group "Vg1" successfully created
3、创建LV
[root@xuegod61 ~]# lvcreate -n LV1 -L 1.5G Vg1
Logical volume "LV1" created.
各种查看的命令
#pvs #pvscan #pvdisplay
#vgs #vgscan #vgdisplay
#lvs #lvscan #lvdisplay
查看PE大小
[root@xuegod61 ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sdb2
VG Name Vg1
PV Size 1.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 255
Free PE 126
Allocated PE 129
PV UUID BZI5Pv-odsd-ki9c-gkuU-7xoB-AHhr-WHKtEv
指定PE的大小:-s
[root@xuegod61 ~]# vgcreate -s 16M VGrm /dev/sdb3
使用LVM:
[root@xuegod61 ~]# mkfs.xfs /dev/Vg1/LV1
[root@xuegod61 ~]# mkdir /lv1
[root@xuegod61 ~]# mount /dev/Vg1/LV1 /lv1/
LV扩容:
首先,确定一下是否有可用的扩容空间
[root@xuegod61 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VGrm 1 0 0 wz--n- 1008.00m 1008.00m
Vg1 2 1 0 wz--n- 1.99g 504.00m
rhel 1 2 0 wz--n- 12.00g 4.00m
扩容逻辑卷
[root@xuegod61 ~]# lvextend -L +300M /dev/Vg1/LV1
Size of logical volume Vg1/LV1 changed from 1.50 GiB (384 extents) to 1.79 GiB(459 extents).
Logical volume LV1 successfully resized.
[root@xuegod61 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LV1 Vg1 -wi-ao---- 1.79g
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g
RHEL7对文件系统进行扩容
[root@xuegod61 ~]# xfs_growfs /dev/Vg1/LV1
RHEL6对文件系统进行扩容
[root@xuegod61 ~]# resize2fs /dev/Vg1/LV1
VG扩容:
[root@xuegod61 ~]# vgextend Vg1 /dev/sdb5
Physical volume "/dev/sdb5" successfully created
Volume group "Vg1" successfully extended
[root@xuegod61 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 12.00g 4.00m
/dev/sdb1 Vg1 lvm2 a-- 1020.00m 0
/dev/sdb2 Vg1 lvm2 a-- 1020.00m 204.00m
/dev/sdb3 VGrm lvm2 a-- 1008.00m 1008.00m
/dev/sdb5 Vg1 lvm2 a-- 1020.00m 1020.00m
LVM缩减
lvm支持在线缩小,但是xfs文件系统不支持在线缩小。btrfs支持在线缩小
扩展:
一直使用 ext2/3,ext 文件系统以其卓越的稳定性成为了事实上的 Linux 标准文件系统。近年来 ext2/3 暴露出了一些扩展性问题,于是便催生了ext4 。 ext4 的作者 Theodore Tso 也盛赞btrfs 并认为 btrfs 将成为下一代 Linux 标准文件系统。
首先,是扩展性 (scalability) 相关的特性,btrfs 最重要的设计目标是应对大型机器对文件系统的扩展性要求。其整体性能而不会随着系统容量的增加而降低。
其次是数据一致性 (data integrity) 相关的特性。
第三是和多设备管理相关的特性。 Btrfs 支持创建快照 (snapshot),和克隆 (clone) 。
最后,总结一些特性: 这些特性都是比较先进的技术,能够显著提高文件系统的时间 / 空间性能,包括延迟分配,小文件的存储优化,目录索引等。
LV可以缩减
把LV1缩减到1G
[root@xuegod61 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
LV1 Vg1 -wi-ao---- 1.79g
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g
[root@xuegod61 ~]# lvreduce -L 1G /dev/Vg1/LV1
WARNING: Reducing active and open logical volume to 1.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LV1? [y/n]: y
Size of logical volume Vg1/LV1 changed from 1.79 GiB (459 extents) to 1.00 GiB(256 extents).
Logical volume LV1 successfully resized.
VG缩减:
注:缩减时,可以不卸载正在使用中的LV。另外,只能缩减没有被使用的pv。否则会提示以下内容:
[root@xuegod61 ~]# vgreduce Vg1 /dev/sdb1
Physical volume "/dev/sdb1" still in use
缩减之前先确认物理卷是否被使用
[root@xuegod61 ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 rhel lvm2 a-- 12.00g 4.00m
/dev/sdb1 Vg1 lvm2 a-- 1020.00m 0
/dev/sdb2 Vg1 lvm2 a-- 1020.00m 1016.00m
/dev/sdb3 VGrm lvm2 a-- 1008.00m 1008.00m
/dev/sdb5 Vg1 lvm2 a-- 1020.00m 1020.00m
[root@xuegod61 ~]# vgreduce Vg1 /dev/sdb5
Removed "/dev/sdb5" from volume group "Vg1"
LVM删除
删除之前必须把设备进行卸载,否则会产生以下错误
[root@xuegod61 ~]# lvremove /dev/Vg1/LV1
Logical volume Vg1/LV1 contains a filesystem in use.
卸载设备
[root@xuegod61 ~]# umount /dev/Vg1/LV1
删除LV
[root@xuegod61 ~]# lvremove /dev/Vg1/LV1
Do you really want to remove active logical volume LV1? [y/n]: y
Logical volume "LV1" successfully removed
删除VG
[root@xuegod61 ~]# vgremove Vg1
Volume group "Vg1" successfully removed
删除PV
[root@xuegod61 ~]# pvremove /dev/sdb{1,2,5}
Labels on physical volume "/dev/sdb1" successfully wiped
Labels on physical volume "/dev/sdb2" successfully wiped
Labels on physical volume "/dev/sdb5" successfully wiped
LVM快照
lvm快照有两大用途,一是用来克隆虚拟机,例如做xen虚拟机时,可先新建一台完整虚拟机,假设大小为10G,然后我们可以在这台10G的虚拟机上创建1个3G的快照,接着通过这个3G的快照启动虚拟机,做完实验后,可以直接删除快照而保持原来完整虚拟机的纯净。
lvm快照的第二大用途是实时备份(moment-in-time),即为了保持系统的一致性,我们先做一个快照冻结当前系统状态,这样快照里面的内容可暂时保持不变,系统本身继续运行,通过备份快照来实现不中断服务的的备份。
首先准备一个LV
[root@xuegod61 ~]# vgcreate vg1 /dev/sdb{1,2}
[root@xuegod61 ~]# lvcreate -n lv1 -L 1.5G vg1
格式化lv1并进行挂载
[root@xuegod61 ~]# mkfs.xfs /dev/vg1/lv1
[root@xuegod61 ~]# mount /dev/vg1/lv1 /lv1/
准备一个测试文件
[root@xuegod61 ~]# cp /etc/passwd /lv1/
[root@xuegod61 ~]# ls /lv1/
passwd
针对lv1创建一个300M快照
lvcreate -s -n 快照名 -L 快照大小 创建快照的设备
[root@xuegod61 ~]# lvcreate -s -n lv1_sp -L 300M vg1/lv1
Logical volume "lv1_sp" created.
[root@xuegod61 ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
root rhel -wi-ao---- 10.00g
swap rhel -wi-ao---- 2.00g
lv1 vg1 owi-aos--- 1.50g
lv1_sp vg1 swi-a-s--- 300.00m lv1 0.00
使用快照
[root@xuegod61 ~]# mount /dev/vg1/lv1_sp /opt/
mount: wrong fs type, bad option, bad superblock on /dev/mapper/vg1-lv1_sp,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so.
[root@xuegod61 ~]# umount /dev/vg1/lv1
[root@xuegod61 ~]# mount /dev/vg1/lv1_sp /opt/
[root@xuegod61 ~]# ls /opt/
passwd
系统存储管理器的使用
系统存储管理器(又称ssm)是RHEL7/CentOS7 新增的功能,是一种统一的命令界面,由红帽公司开发,用于管理各种各样的存储设备。目前,有三种可供ssm使用的卷管理后端:LVM、brtfs和crypt
1)安装ssm系统存储管理器
[root@xuegod61 ~]# yum -y install system-storage-manager.noarch
查看ssm命令的帮助信息:
[root@xuegod61 ~]# ssm --help
usage: ssm [-h] [--version] [-v] [-f] [-b BACKEND] [-n]
{check,resize,create,list,add,remove,snapshot,mount} ...
System Storage Manager
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
-v, --verbose Show aditional information while executing.
-f, --force Force execution in the case where ssm has some doubts
or questions.
-b BACKEND, --backend BACKEND
Choose backend to use. Currently you can choose from
(lvm,btrfs,crypt).
-n, --dry-run Dry run. Do not do anything, just parse the command
line options and gather system information if
necessary. Note that with this option ssm will not
perform all the check as some of them are done by the
backends themselves. This option is mainly used for
debugging purposes.
Commands:
{check,resize,create,list,add,remove,snapshot,mount}
check Check consistency of the file system on the device.
resize Change or set the volume and file system size.
create Create a new volume with defined parameters.
list List information about all detected, devices, pools,
volumes and snapshots in the system.
add Add one or more devices into the pool.
remove Remove devices from the pool, volumes or pools.
snapshot Take a snapshot of the existing volume.
mount Mount a volume withfile system to specified locaion.
列出:设备
列出:存储池
列出:卷
列出:快照
将物理磁盘sdc 添加到LVM存储池
语法格式:ssm add -p 存储池 设备
[root@xuegod61 ~]# ssm add -p vg1 /dev/sdc
[root@xuegod61 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
VGrm 1 0 0 wz--n- 1008.00m 1008.00m
rhel 1 2 0 wz--n- 12.00g 4.00m
vg1 3 2 1 wz--n- 21.99g 20.20g
扩容LV(不能针对创建了快照的lv进行扩容)
[root@xuegod61 ~]# ssm add -p rhel /dev/sdd
[root@xuegod61 ~]# ssm resize -s+10G /dev/rhel/root
注:
使用ssm存储管理器扩容,不需要再针对文进系统进行扩容
实例:
创建一个名为vg2的存储池,并在其上创建一个名为lv2,大小为1G的lvm卷,格式化为xfs文件系统,并将其挂载/lv2目录下
创建目录
[root@xuegod61 ~]# mkdir /lv2
[root@xuegod61 ~]# ssm create -s 1G -n lv2 --fstype xfs -p vg2 /dev/sde /lv2
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40535: /usr/bin/python
Physical volume "/dev/sde" successfully created
Volume group "vg2" successfully created
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40535: /usr/bin/python
Logical volume "lv2" created.
meta-data=/dev/vg2/lv2 isize=256 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=262144, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
查看挂载状况
[root@xuegod61 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 20G 3.2G 17G 16% /
devtmpfs 2.0G 0 2.0G 0% /dev
tmpfs 2.0G 84K 2.0G 1% /dev/shm
tmpfs 2.0G 9.0M 2.0G 1% /run
tmpfs 2.0G 0 2.0G 0% /sys/fs/cgroup
/dev/sr0 3.8G 3.8G 0 100% /mnt
/dev/sda1 197M 125M 72M 64% /boot
tmpfs 394M 16K 394M 1% /run/user/42
tmpfs 394M 0 394M 0% /run/user/0
/dev/mapper/vg1-lv1_sp 1.5G 33M 1.5G 3% /opt
/dev/mapper/vg2-lv2 1014M 33M 982M 4% /lv2
创建快照
[root@xuegod61 ~]# ssm snapshot -s 500M -n lv2_sp /dev/vg2/lv2
使用快照
[root@xuegod61 ~]# umount /lv2/
[root@xuegod61 ~]# mkdir /lv2_snapshot
[root@xuegod61 ~]# mount /dev/vg2/lv2_sp /lv2_snapshot/
移除LVM卷
[root@xuegod61 ~]# ssm remove /dev/vg2/lv2_sp
Device '/dev/vg2/lv2_sp' is mounted on '/lv2_snapshot' Unmount (N/y/q) ? Y
File descriptor 7 (/dev/urandom) leaked on lvm invocation. Parent PID 40749: /usr/bin/python
Do you really want to remove active logical volume lv2_sp? [y/n]: y
Logical volume "lv2_sp" successfullyremoved
注:删除已经挂载的卷,ssm会自动先将其卸载
磁盘配额
RHEL7磁盘配额
确认配额命令已经安装
[root@xuegod61 ~]# rpm -qf `which xfs_quota`
xfsprogs-3.2.2-2.el7.x86_64
首先创建新的测试分区/dev/sda3
启用配额
[root@xuegod61 ~]# mkfs.xfs /dev/sda3
[root@xuegod61 ~]# mkdir /sda3
[root@xuegod61 ~]# mount -o uquota,gquota /dev/sda3 /sda3/
查看:
[root@xuegod61 ~]# mount | grep sda3
/dev/sda3 on /sda3 type xfs (rw,relatime,attr2,inode64,usrquota,grpquota)
如何开机自动启动配额
[root@xuegod61 ~]# vim /etc/fstab
/dev/sda3 /sda3 xfs defaults,usrquota,grpquota 0 0
设置目录权限,并创建配额用户
[root@xuegod61 ~]# chmod 777 /sda3/
[root@xuegod61 ~]# useradd swk
查看配额状态
[root@xuegod61 ~]# xfs_quota -x -c 'report' /sda3/
User quota on /sda3 (/dev/sda3)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
Group quota on /sda3 (/dev/sda3)
Blocks
Group ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
参数:
-x 使用专家模式,只有此模式才能设置配额
-c 启用命令模式
report 显示配额信息
limit 设置配额
设置配额
[root@xuegod61 ~]# xfs_quota -x -c 'limit bsoft=100M bhard=120M -u swk' /sda3/
[root@xuegod61 ~]# xfs_quota -x -c 'report' /sda3/
User quota on /sda3 (/dev/sda3)
Blocks
User ID Used Soft Hard Warn/Grace
---------- --------------------------------------------------
root 0 0 0 00 [--------]
swk 0 102400 122880 00 [--------]
验证:
[root@xuegod61 ~]# su - swk
[swk@xuegod61 ~]$ dd if=/dev/zero of=/sda3/swk.txt bs=1M count=130
dd: error writing ‘/sda3/swk.txt’: Disk quota exceeded
121+0 records in
120+0 records out
125829120 bytes (126 MB) copied, 0.426091 s, 295 MB/s
[root@xuegod61 ~]# ll -h /sda3/swk.txt
-rw-rw-r-- 1 swk swk 120M Feb 27 09:21 /sda3/swk.txt
RHEL6.5 磁盘配额
限定用户或组对磁盘空间的使用。
[root@xuegod63 ~]# rpm -q quota
quota-3.17-16.el6.x86_64
2.启用quota磁盘配额功能
[root@xuegod63 ~]# mkfs.ext4 /dev/sdb3
[root@xuegod63 ~]# mkdir /tmp/sdb3
[root@xuegod63 ~]# mount /dev/sdb3 /tmp/sdb3/
[root@xuegod63 ~]# mount -o remount,usrquota,grpquota /tmp/sdb3/
[root@xuegod63 ~]# mount
/dev/sdb3 on /tmp/sdb3 type ext4 (rw,usrquota,grpquota)
[root@xuegod63 ~]# tail -n 1 /etc/fstab
/dev/sdb3 /tmp/sdb3 ext4 defaults,usrquota,grpquota 0 0
3.检测磁盘配额并生成配额文件
[root@xuegod63 ~]# quotacheck -cugv /tmp/sdb3/
[root@xuegod63 ~]# ll -a /tmp/sdb3/
total 40
drwxr-xr-x 3 root root 4096 Feb 2 22:18 .
drwxrwxrwt. 18 root root 4096 Feb 2 22:15 ..
-rw------- 1 root root 6144 Feb 2 22:18 aquota.group
-rw------- 1 root root 6144 Feb 2 22:18 aquota.user
setenforce 0 #要关闭selinux
4.用户和组账号的配额设置
[root@xuegod63 ~]# useradd rm
[root@xuegod63 ~]# edquota -g rm
Disk quotas for group mk (gid 500):
Filesystem blocks soft hard inodes soft hard
/dev/sdb3 0 50 80 0 0 0
激活磁盘配额
[root@xuegod63 ~]# quotaon -ugv /tmp/sdb3/
/dev/sdb3 [/tmp/sdb3]: group quotas turned on
/dev/sdb3 [/tmp/sdb3]: user quotas turned on
验证:
[root@xuegod63 ~]# mkdir /tmp/sdb3/test
[root@xuegod63 ~]# chmod 777 /tmp/sdb3/test/
[mk@xuegod63 test]$ dd if=/dev/zero of=mk.txt bs=1K count=70 # 大于50K报警
sdb3: warning, group block quota exceeded.
70+0 records in
70+0 records out
71680 bytes (72 kB) copied, 0.00388255 s, 18.5 MB/s
[mk@xuegod63 test]$ ls
mk.txt
[mk@xuegod63 test]$ rm -rf mk.txt
[mk@xuegod63 test]$ dd if=/dev/zero of=mk.txt bs=1K count=90 # 大于80,报错
sdb3: write failed, group block limit reached.
dd: writing `mk.txt': Disk quota exceeded
[mk@xuegod63 test]$ ll -h
total 80K
-rw-rw-r-- 1 mk mk 80K Feb 2 22:30 mk.txt
说明:
[root@xuegod63 ~]# tune2fs -l /dev/sdb3 | grep size
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recoveryextent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Block size: 4096
Fragment size: 4096
说明,磁盘配额中的 blocks 。一个block是1K。