linux 下的md和dm模块:
md(Multiple devices):
A Btrfs filesystem can be created on top of many devices, and more devices can be added after the FS has been created.
By default, metadata will be mirrored across two devices and data will be striped across all of the devices present.
If only one device is present, metadata will be duplicated on that one device.
dm(device Mapper):
Device Mapper 是 Linux2.6 内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构,如图 1。
md很多功能dm都可以实现,比如linear模块可以实现md中jbod功能,mirror可以实现raid 1 ,而dm很多模块是md所不具备的。比如:
snapshot(快照):
快照实质是为某个时间点的文件系统中的所有文件提供另外一个路径存储在快照中,当文件要被更改的时候,文件先拷贝一份放在快照中,而未被更改的则不做任何改动,这样当访问某个快照的时候,如果文件被更改则直接在快照中找到,否则则通过快照访问原来文件的位置。
快照的用途很多:比如用于虚拟机,另外一个比较有用的用途是在实时性要求很高的场景中进行数据备份,由于服务器不能中断,当我们用其他方法备份的时候,由于每个时间段的数据都不一样,导致备份出来的数据不能用,而利用快照则可以实现我们的要求。
multipath(多路径):
有时候我们对生产环境安全要求比较高,这个时候我们可以提供冗余链路,而利用这个模块,可以对链路进行控制利用。
lvm:lvm利用dm的功能实现了从逻辑设备到物理设备的映射框架机制,在该机制下,用户可以很方便的根据自己的需要制定实现存储资源的管理策略
lvm模型:
基本概念:
pv(physical volume物理卷):即图中的/dev/sda1,/dev/sdb1,/dev/sdc1,/dev/sdd1,真正的物理存储设备。
VG(volume Group卷组):即图中的逻辑集合,也就是pv的集合,向上层提供接口
lv(logic Volume逻辑卷):即图中的逻辑分区,建立在卷组之上,可以创建文件系统
PE(physical Extent 物理盘区):每一个物理卷PV被划分为称为PE(Physical Extents)的基本单元,具有唯一编号的PE是可以被LVM寻址的最小单元。PE的大小是可配置的,默认为4MB。所以物理卷(PV)由大小等同的基本单元PE组成。
LE(logic Extent逻辑盘区)和物理盘区是一样的,只是站在逻辑卷的角度来看
lvm的机制就是把物理设备的功能通过dm模块虚拟化,dm为其创建卷组,当需要某个物理设备的时候,就把这个物理设备作为pv加入到卷组,并向上提供给逻辑卷,在一个逻辑卷看来,卷组类似于一个大的磁盘,可以使用其提供的LE,也可以把不用的LE归还给它,让其他的逻辑卷使用,而对底层的物理设备的使用并不关心,由卷组实现其功能。 所有同一卷组里面的逻辑卷磁盘大小总和不能大于卷组本身,卷组可以通过增删PV进行扩容,逻辑卷可以通过增删LE进行扩容。
当一个卷组创建的时候,它的PE就固定了,当一个PV加入到某个卷组的时候,它会被格式化成与卷组相同的PE
例子:
安装lvm工具:
[[email protected] ~]# yum install lvm-common lvm2 evms -y
创建pv:
[[email protected] ~]# pvcreate /dev/sdb1 /dev/sdb2
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
[[email protected] ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 --- 2.01g 2.01g
/dev/sdb2 lvm2 --- 3.01g 3.01g
创建VG:
[[email protected] ~]# vgcreate myvg /dev/sdb1 /dev/sdb2 -s 8M(-s指定pe大小,可以不加默认4M)
Volume group "myvg" successfully created
[[email protected] ~]# vgdisplay
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
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 2
Act PV 2
VG Size 5.01 GiB
PE Size 8.00 MiB
Total PE 641
Alloc PE / Size 0 / 0
Free PE / Size 641 / 5.01 GiB
VG UUID 2EpSci-0YXx-HAYV-zOIn-H4cP-jMlq-DRsVBq
创建LV:
[[email protected] ~]# lvcreate -L 5G myvg
Logical volume "lvol0" created.
[[email protected] ~]# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/lvol0
LV Name lvol0
VG Name myvg
LV UUID 2Uwnl6-4p0M-uRSB-BHM7-1w1J-9GNw-paARoV
LV Write Access read/write
LV Creation host, time logstash, 2016-03-09 16:30:10 +0800
LV Status available
# open 0
LV Size 5.00 GiB
Current LE 640
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
格式化lv并挂载
[[email protected] ~]# mke2fs -j /dev/myvg/lvol0
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[[email protected] ~]# mount /dev/myvg/lvol0 /data
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 4.0G 15G 22% /
tmpfs 234M 0 234M 0% /dev/shm
/dev/sda1 190M 41M 140M 23% /boot
/dev/mapper/myvg-lvol0
5.0G 139M 4.6G 3% /data
lv的扩容:
查看vg的剩余容量:
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 2 1 0 wz--n- 5.01g 8.00m
对vg进行扩容
[[email protected] ~]# vgextend myvg /dev/{sdb3,sdb4}
Volume group "myvg" successfully extended
[[email protected] ~]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 4 1 0 wz--n- 9.01g 4.01g
对lv进行扩容
[[email protected] ~]# lvextend -L +3G /dev/mapper/myvg-lvol0
Size of logical volume myvg/lvol0 changed from 5.00 GiB (640 extents) to 8.00 GiB (1024 extents).
Logical volume lvol0 successfully resized
[[email protected] /]# umount /data/
[[email protected] /]# e2fsck -f /dev/mapper/myvg-lvol0 (检查磁盘)
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/mapper/myvg-lvol0: 12/327680 files (0.0% non-contiguous), 55936/1310720 blocks
[[email protected] /]# resize2fs /dev/mapper/myvg-lvol0 (重新读取大小)
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/myvg-lvol0 to 2359296 (4k) blocks.
The filesystem on /dev/mapper/myvg-lvol0 is now 2359296 blocks long.
[[email protected] /]# mount /dev/mapper/myvg-lvol0 /data/ (重新挂载)
[[email protected] /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 20G 4.0G 15G 22% /
tmpfs 234M 0 234M 0% /dev/shm
/dev/sda1 190M 41M 140M 23% /boot
/dev/mapper/myvg-lvol0
8.9G 140M 8.3G 2% /data
lv和vg的缩减以及pv的移除
注意: reduce和remove的区别 reduce是缩减,而remove则是移除!会删除磁盘上的数据,所以注意lvreduce,lvremove还有vg和pv相关命令的使用。
lv的缩减:
[[email protected] /]# umount /data/
[[email protected] /]# resize2fs /dev/mapper/myvg-lvol0 2G
[[email protected] /]# lvreduce -L 2G /dev/mapper/myvg-lvol0
WARNING: Reducing active and open logical volume to 2.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvol0? [y/n]: y
Size of logical volume myvg/lvol0 changed from 9.00 GiB (1152 extents) to 2.00 GiB (256 extents).
Logical volume lvol0 successfully resized
也可以用lvreduce -L 2G -f -r /dev/mapper/myvg-lvol0一条命令完成,reduce有风险,注意数据的备份。
vg的缩减:
[[email protected] /]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 4 1 0 wz--n- 9.01g 7.01g
[[email protected] /]# vgreduce myvg /dev/sdb3
Removed "/dev/sdb3" from volume group "myvg"
[[email protected] /]# vgs
VG #PV #LV #SN Attr VSize VFree
myvg 3 1 0 wz--n- 7.01g 5.01g
pv的移除:
[[email protected] /]# pvmove /dev/sdb4 (移除pv前必须使用这条命令把pv上的数据转移到其他pv)
No data to move for myvg
[[email protected] /]# pvremove /dev/sdb4
PV /dev/sdb4 belongs to Volume Group myvg so please use vgreduce first.
(If you are certain you need pvremove, then confirm by using --force twice.)
[[email protected] /]# vgreduce myvg /dev/sdb4
Removed "/dev/sdb4" from volume group "myvg"
[[email protected] /]# pvremove /dev/sdb4 (这条命令会抹除数据,慎用!!)
Labels on physical volume "/dev/sdb4" successfully wiped