LVM磁盘管理
LVM基本术语
前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层磁盘分区布局,提供一个抽象的盘卷,在盘卷上建立文件系统。首先我们讨论以下几个LVM术语:
物理存储介质(The physical media):这里指系统的存储设备:硬盘,如:/dev/hda1、/dev/sda等等,是存储系统最低层的存储单元。
物理卷(physical volume):物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。
卷组(Volume Group):LVM卷组类似于非LVM系统中的物理硬盘,其由物理卷组成。可以在卷组上创建一个或多个“LVM分区”(逻辑卷),LVM卷组由一个或多个物理卷组成。
逻辑卷(logical volume):LVM的逻辑卷类似于非LVM系统中的硬盘分区,在逻辑卷之上可以建立文件系统(比如/home或者/usr等)。
PE(physical extent):每一个物理卷被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。
LE(logical extent):逻辑卷也被划分为被称为LE(Logical Extents) 的可被寻址的基本单位。在同一个卷组中,LE的大小和PE是相同的,并且一一对应。
简单来说就是:
PV:是物理的磁盘分区
VG:LVM中的物理的磁盘分区,也就是PV,必须加入VG,可以将VG理解为一个仓库或者是几个大的硬盘。
LV:也就是从VG中划分的逻辑分区
如下图所示PV、VG、LV三者关系:
创建一个大小为300M,分区类型为LVM的sdb1
[root@IHS ~]# fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9d2251e5. Changes will remain in memory only, until you decide to write them. After that, of course, the previous content won't be recoverable. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) WARNING: DOS-compatible mode is deprecated. It's strongly recommended to switch off the mode (command 'c') and change display units to sectors (command 'u'). Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +300M Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9d2251e5 Device Boot Start End Blocks Id System /dev/sdb1 1 39 313236 83 Linux Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk identifier: 0x9d2251e5 Device Boot Start End Blocks Id System /dev/sdb1 1 39 313236 8e Linux LVM Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
让内核同步分区信息(此步骤仅在没有找到分区设备的情况下才需要执行,非必要动作。):
[root@IHS ~]# partprobe
启用LVM并创建lv_data逻辑卷
1.将新建的分区设置为物理卷:
[root@IHS ~]# pvcreate /dev/sdb1 dev_is_mpath: failed to get device for 8:17 Physical volume "/dev/sdb1" successfully created
2.将物理卷加入卷组vg_data:
[root@IHS ~]# vgcreate vg_date /dev/sdb1 Volume group "vg_date" successfully created
3.查看卷组信息
[root@IHS ~]# vgdisplay --- Volume group --- VG Name vg_date System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size 304.00 MiB PE Size 4.00 MiB Total PE 76 Alloc PE / Size 0 / 0 Free PE / Size 76 / 304.00 MiB VG UUID LfClWd-y3Oo-pZ0U-Nwmb-6QiA-L0sb-dJgASE [root@IHS ~]# vgs VG #PV #LV #SN Attr VSize VFree vg_date 1 0 0 wz--n- 304.00m 304.00m
4.生成150M的逻辑卷lv_data
[root@IHS ~]# lvcreate -L 150M -n lv_data vg_date Rounding up size to full physical extent 152.00 MiB Logical volume "lv_data" created
5.格式化逻辑卷
[root@IHS ~]# mkfs.ext4 /dev/vg_date/lv_data Writing superblocks and filesystem accounting information: done
6.创建挂载目录/data,并将lv_data挂载在该目录。
[root@IHS ~]# mkdir /data [root@IHS ~]# mount /dev/vg_date/lv_data /data
7.查看挂载信息
[root@IHS ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 89G 20G 66G 23% / tmpfs 4.1G 72K 4.1G 1% /dev/shm /dev/sda1 194M 34M 151M 19% /boot /dev/mapper/vg_date-lv_data 148M 5.6M 135M 4% /data
扩容卷组
vgextend vg_wls12c /dev/sdb1
调整逻辑卷大小
xfs和ext2/3/4文件系统的调整方法不一样。
ext/2/3/4支持通过resize2fs 命令直接增大和减小
xfs只能通过xfs_growfs增大,减小需要通过卸载磁盘格式化后重新挂载,数据会丢失
扩容逻辑卷
将var_log_audit逻辑卷增大1G,这里以XFS磁盘为例
如果EXT格式磁盘将xfs_growfs替换为resize2fs
# 放大 LV 吧!利用 lvresize 的功能來增加
[root@frog ~]# lvresize -L +1G /dev/centos/var_log_audit Size of logical volume centos/var_log_audit changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents). Logical volume centos/var_log_audit successfully resized. [root@frog ~]# lvscan ACTIVE '/dev/centos/var' [300.00 GiB] inherit ACTIVE '/dev/centos/var_log_audit' [2.00 GiB] inherit ACTIVE '/dev/centos/var_log' [2.00 GiB] inherit ACTIVE '/dev/centos/var_tmp' [500.00 MiB] inherit ACTIVE '/dev/centos/root' [100.00 GiB] inherit ACTIVE '/dev/centos/swap' [8.00 GiB] inherit ACTIVE '/dev/centos/home' [100.00 MiB] inherit
[root@frog ~]# xfs_growfs /var/log/audit meta-data=/dev/mapper/centos-var_log_audit isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 262144 to 524288
将var_log_audit逻辑卷减小1G,这里以XFS磁盘为例
如果是EXT格式磁盘可以不用umount磁盘
1.卸载文件系统
[root@frog ~]# umount /var/log/audit
2.将逻辑卷的减小1G
[root@frog ~]# lvresize -L -1G /dev/centos/var_log_audit WARNING: Reducing active logical volume to 1.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce centos/var_log_audit? [y/n]: y Size of logical volume centos/var_log_audit changed from 2.00 GiB (512 extents) to 1.00 GiB (256 extents). Logical volume centos/var_log_audit successfully resized.
3. 查看逻辑卷大小
[root@frog ~]# lvscan ACTIVE '/dev/centos/var' [300.00 GiB] inherit ACTIVE '/dev/centos/var_log_audit' [1.00 GiB] inherit ACTIVE '/dev/centos/var_log' [2.00 GiB] inherit ACTIVE '/dev/centos/var_tmp' [500.00 MiB] inherit ACTIVE '/dev/centos/root' [100.00 GiB] inherit ACTIVE '/dev/centos/swap' [8.00 GiB] inherit ACTIVE '/dev/centos/home' [100.00 MiB] inherit
4. 格式化磁盘
[root@frog ~]# mkfs.xfs -f /dev/centos/var_log_audit meta-data=/dev/centos/var_log_audit isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 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
5.挂载文件系统,查看逻辑卷大小是否是减小1G
[root@frog ~]# mount /dev/centos/var_log_audit /var/log/audit
[root@frog ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 12G 0 12G 0% /dev
tmpfs 12G 0 12G 0% /dev/shm
tmpfs 12G 8.9M 12G 1% /run
tmpfs 12G 0 12G 0% /sys/fs/cgroup
/dev/mapper/centos-root 100G 1.3G 99G 2% /
tmpfs 12G 8.0K 12G 1% /tmp
/dev/sda1 187M 144M 44M 77% /boot
/dev/mapper/centos-home 97M 5.3M 92M 6% /home
/dev/mapper/centos-var 300G 226M 300G 1% /var
/dev/mapper/centos-var_log 2.0G 37M 2.0G 2% /var/log
/dev/mapper/centos-var_tmp 497M 26M 472M 6% /var/tmp
tmpfs 2.4G 0 2.4G 0% /run/user/1000
/dev/mapper/centos-var_log_audit 1014M 33M 982M 4% /var/log/audit
使用逻辑卷快照功能
1.移动一些文件到/data目录下,做测试
[root@IHS ~]# ls /data
2.docx 3.docx 4.docx 5.docx lost+found
2.查看逻辑卷的使用量
[root@IHS data]# df -h Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_date-lv_data 97M 75M 18M 81% /data
3.创建逻辑卷快照
[root@IHS data]# lvcreate -L 50M -s -n SNAP /dev/vg_date/lv_data Rounding up size to full physical extent 52.00 MiB Logical volume "SNAP" created
4.删除/data下的文件,看恢复快照后是否能恢复该时间点的文件
[root@IHS data]# rm -rf /data/*
5.卸载目录
[root@IHS data]# umount /data umount: /data: device is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1))
出现报错,则执行: [root@IHS data]# fuser -k /data
重新挂载:
[root@IHS data]# umount /data
6.恢复快照
[root@IHS ~]# lvconvert --merge /dev/vg_date/SNAP Merging of volume SNAP started. lv_data: Merged: 100.0% Merge of snapshot into logical volume lv_data has finished. Logical volume "SNAP" successfully removed
7.挂载文件系统
[root@IHS ~]# mount /dev/vg_date/lv_data /data
8.检验效果,文件是否恢复
[root@IHS ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 89G 20G 66G 23% / tmpfs 4.1G 72K 4.1G 1% /dev/shm /dev/sda1 194M 34M 151M 19% /boot /dev/mapper/vg_date-lv_data 97M 75M 18M 81% /data [root@IHS ~]# ls /data 2.docx 3.docx 4.docx 5.docx lost+found
开机自动挂载
echo "/dev/vg_date/lv_data /data ext4 defaults 0 0" >> /etc/fstab