- 设备映射内核架构
Device Mapper
称为设备映射,用于将多个底层的逻辑设备抽象成为一个统一的逻辑设备,并且提供一个统一的管理入口/dev
,设备映射是LVM
的基础;
core device mapper
表示的是核心的映射模块;liner
:表示的是线性设备,用于拼接多个物理设备;mirror
:表示的是镜像设备,这两个是快照功能的核心;multipath
:是多路径存储,也就是支持多条线路进行数据存储,外部可以使用多条线路,内部必须有一个调节机制;需要借助DM
模块进行配置;snapshots
:表示快照,保留数据在快照瞬间的状态,快照的大小小于元数据,快照是访问同一个数据的不同路径,只是一个路径文件;快照是一个数据管理机制,会在你修改数据的同时将将要被修改的数据保存在快照里面,同时讲需要修改的数据保留在原有路径的磁盘上,当访问修改后的数据时,使用初始路径进行 ,当需要访问快照之前的的数据时,使用,进行快照时生成的数据;快照主要是用来实现数据备份的,比传统的数据备份更加快速,只记录发生概念的数据,zfs
文件系统就具有快照功能;Device Mapper
:设备支持动态增减,不会影响原来的数据,需要三个层次来实现上述功能:- 物理卷组:位于最底层,也就是
physical volume
; - 卷组:也就是
volume group
; - 逻辑卷:
logical volume
;
- 物理卷组:位于最底层,也就是
什么是LVM
LVM
全称为Local Volume Manager
,表示逻辑卷管理器,用于弹性调整文件系统的容量,LVM
用于将多个多个物理分区整合在一起,使这些分区在使用起来就像一个分区一样,,并且允许动态的添加或者删除某些分区;- 什么是PV
PV
表示Physical Volume
表示物理卷,也就是将分区作为物理卷的成员出现,PV
命令就是执行这个操作,PV
支持的设备包括:磁盘分区,元设备,回送文件,物理卷只有在加入卷组之后,才会被划分出来PE (Physical Extend)
;- 关于
PV
的几个命令:
pvcreate
:表示将物理分区新建立为PV
;pvscan
:用于查询目前系统上面存在PV
属性的磁盘;pvdisplay
:表示显示目前系统上面的PV
状态;pvremove
:表示移除PV
属性;
- 什么是Volume Group
VG
是将多个PV
组合而成的一个大的虚拟的磁盘设备设备,是PV
的上一层抽象,每一个VG
最多可以容纳65534
个PE
;VG
相关的命令:
vgcreate
:用于新建立一个VG
-s
:用于指定PE
的大小,单位为M, G, T
;-p
:卷组中允许添加的最大物理卷数;-l
:表示卷组中允许创建的最大逻辑卷;
vgscan
: 用于查看系统上面是否有VG
存在;vgdisplay
:用于具体的查看VG
的信息;vgextend
:用于额外的添加PV
;vgreduce
:用于删除PV
;vgchange
:设置VG
是否启动;vgremove
:用于删除一个VG
;
- 什么是PE
PE
表示Physical Extend
,表示的含义是物理扩展块,PE
是LVM
进行数据存储的最小单位,类似于block
,,默认大小为4M
,动态改变大小的过程,就是在调整LV
里面PE
数量的过程;- 什么是LV
LV
表示Logical Volume
,表示的含义是逻辑卷,VG
可以被划分成为一个个需要的分区来进行使用,分区和磁盘分区具有同样的功能,可以进行格式化和挂载操作;- 数据写入
LV
同步进入磁盘的方式一般包括两种:
- 线性模式:当数据写入一个分区,只有当这个分区里面的数据存满时,才会将数据写入另一个分区里面,如果一个磁盘之后一个分区,这里的分区和磁盘就是等价的;
- 交错模式:这种模式,将数据划分成为两部分,一部分写入第一个分区,另一部分写入第二个分区,读写性能会比较高;
lv
相关的命令:
lvcreate
:用于新建LV
-L
:用于指定LV
的容量,必须时PE
的整数倍;-l
:用于指定PE
的个数;-n
:用于指定LV
的名称;
lvscan
:用于查询系统是否有已经存在的LV
;lvdisplay
:查看系统上面的LV
状态;lvextend
:在LV
里面增加容量;
-L
: 指定逻辑卷的大小,单位为k m g t
字节;-l
:指定逻辑卷的大小LE数
;
lvreduce
:在LV
里面减少容量;lvremove
:用于移除LV
设备;lvresize
:对于LV
进行容量大小调整;
- 注意:
- 在一个卷组中是可以划分成多个逻辑卷;设备本身有两种边界,物理边界和逻辑边界,逻辑卷是可以扩展的,也是可以创建快照的;将
pv
加入卷组中,卷组就应该将pv
划分成一个一个的存储单元进行存储,不是文件系统块,而是物理盘区,这个存储单元称为PE
, 创建一个
LVM
的过程逻辑卷的扩展实际上就是PE
的扩展,逻辑卷的大小可以超过单个物理设备的大小,数据的存储不再是按照物理设备的边界进行存储, 逻辑卷也是可以创建成镜像的模式,以防止数据丢失,创建之后可以进行格式化,挂载;首先创建分区,并且调整类型为
8e
/dev/vdb1 1 2082 1049296+ 8e Linux LVM
/dev/vdb2 2083 6244 2097648 8e Linux LVM
/dev/vdb3 6245 8326 1049328 8e Linux LVM
/dev/vdb4 8327 10408 1049328 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server8 /]# sync
[root@server8 /]# partx /dev/vdb
# 1: 63- 2098655 ( 2098593 sectors, 1074 MB)
# 2: 2098656- 6293951 ( 4195296 sectors, 2147 MB)
# 3: 6293952- 8392607 ( 2098656 sectors, 1074 MB)
# 4: 8392608- 10491263 ( 2098656 sectors, 1074 MB)
- 其次创建四个
PV
[root@server8 /]# pvcreate /dev/vdb{1,2,3,4}
Physical volume "/dev/vdb1" successfully created
Physical volume "/dev/vdb2" successfully created
Physical volume "/dev/vdb3" successfully created
Physical volume "/dev/vdb4" successfully created
[root@server8 /]# pvscan
PV /dev/vda2 VG VolGroup lvm2 [8.51 GiB / 0 free]
PV /dev/vdb1 lvm2 [1.00 GiB]
PV /dev/vdb2 lvm2 [2.00 GiB]
PV /dev/vdb3 lvm2 [1.00 GiB]
PV /dev/vdb4 lvm2 [1.00 GiB]
- 创建
VG
,并且添加PV
[root@server8 /]# vgcreate -s 16M linuxvg /dev/vdb{1,2,3}
Volume group "linuxvg" successfully created
- 通过扩展的方式添加
PV
[root@server8 /]# vgextend linuxvg /dev/vdb4
Volume group "linuxvg" successfully extended
查看linuxvg
的信息
[root@server8 /]# vgdisplay linuxvg
--- Volume group ---
VG Name linuxvg
System ID
Format lvm2
Metadata Areas 4
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 4
Act PV 4 <===一共包含四个
VG Size 4.94 GiB
PE Size 16.00 MiB <==指定的PE大小
Total PE 316
Alloc PE / Size 0 / 0
Free PE / Size 316 / 4.94 GiB
VG UUID 7vDcHt-plTh-36lG-FVaT-EnHH-1LWJ-82CnpE
- 创建
LV
,准备进行格式化
[root@server8 /]# lvcreate -L 2G -n linuxos linuxvg
Logical volume "linuxos" created
- 接下来就可以进行格式化,并且进行挂载
[root@server8 /]# mkfs.ext4 /dev/linuxvg/linuxos
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
131072 inodes, 524288 blocks
26214 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=536870912
16 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@server8 /]# mount /dev/linuxvg/linuxos /mnt/
[root@server8 /]# cd /mnt/
[root@server8 mnt]# ls
lost+found
- 进行
LV
的放大操作 - 首先需要准备的是,一块磁盘或者分区,一定需要调整类型为
8e
;
[root@server8 mnt]# fdisk /dev/vdc
[root@server8 mnt]# partx /dev/vdc
- 然后执行
PV
操作
[root@server8 mnt]# pvcreate /dev/vdc1
Physical volume "/dev/vdc1" successfully created
- 然后使用
vgextned
来扩展
[root@server8 mnt]# vgextend linuxvg /dev/vdc1
Volume group "linuxvg" successfully extended
- 利用
lvresize
来执行放大操作
[root@server8 mnt]# lvresize -l +128 /dev/linuxvg/linuxos
- 进行查看操作
[root@server8 mnt]# lvdisplay linuxvg
--- Logical volume ---
LV Path /dev/linuxvg/linuxos
LV Name linuxos
VG Name linuxvg
LV UUID oeVI8z-9x6S-b3g8-hRZ4-Uhqn-I5ok-qEAlri
LV Write Access read/write
LV Creation host, time server8.com, 2018-03-17 16:59:54 +0800
LV Status available
# open 1
LV Size 8.92 GiB
Current LE 571
Segments 5
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
- 接下来来执行文件系统级别的扩展操作
[root@server8 /]# df /mnt/ -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/linuxvg-linuxos 2.0G 67M 1.9G 4% /mnt
[root@server8 /]# resize2fs /dev/linuxvg/linuxos
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/linuxvg/linuxos is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/linuxvg/linuxos to 2338816 (4k) blocks.
The filesystem on /dev/linuxvg/linuxos is now 2338816 blocks long.
[root@server8 /]# df /mnt/ -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/linuxvg-linuxos 8.8G 69M 8.3G 1% /mnt
通过对比
/mnt
的大小已经发生改变,并且里面的数据是不会消失的;接下来执行缩小LV容量的操作
- 对于放大
LV
的操作是可以在线执行的,对于缩小的操作是必须卸载之后才能够执行的; - 首先需要执行文件系统检查可操作
[root@server8 /]# e2fsck -f /dev/linuxvg/linuxos
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/linuxvg/linuxos: 11/589824 files (0.0% non-contiguous), 54559/2338816 blocks
- 这里移除
/dev/vdb1
,因为默认的存储是线性存储的,这样可以用于检查文件是否存在丢失,为了验证数据,这里事先放入一个文件
[root@server8 mnt]# ll -lh
total 11M
-rw-r--r--. 1 root root 11M 3月 17 17:29 boot.dump.1
drwx------. 2 root root 16K 3月 17 17:00 lost+found
- 然后执行缩小操作
[root@server8 /]# resize2fs /dev/linuxvg/linuxos 7800M
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/linuxvg/linuxos to 1996800 (4k) blocks.
The filesystem on /dev/linuxvg/linuxos is now 1996800 blocks long.
- 重新挂载检查数据是否存在
[root@server8 /]# mount /dev/linuxvg/linuxos /mnt/
[root@server8 /]# cd /mnt/
[root@server8 mnt]# ls
boot.dump.1 lost+found
[root@server8 mnt]# ll -lh
total 11M
-rw-r--r--. 1 root root 11M 3月 17 17:29 boot.dump.1
drwx------. 2 root root 16K 3月 17 17:00 lost+found
- 检查现在的文件系统大小
[root@server8 mnt]# df -h /mnt/
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/linuxvg-linuxos 7.5G 79M 7.1G 2% /mnt
- 这个和事先执行的大小差不多
- 然后减小
PE
的数量
[root@server8 mnt]# pvdisplay
--- Physical volume ---
PV Name /dev/vdb1
VG Name linuxvg
PV Size 1.00 GiB / not usable 16.70 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 63
Free PE 0
Allocated PE 63
PV UUID tFCGUH-42Df-ReVb-4mJz-WohI-EyeK-Nc3vvh
[root@server8 mnt]# lvresize -l -63 /dev/linuxvg/linuxos
WARNING: Reducing active and open logical volume to 7.94 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce linuxos? [y/n]: y
Reducing logical volume linuxos to 7.94 GiB
Logical volume linuxos successfully resized
- 确认
/dev/vdb1
里面的PE
没有被使用,然后移除分区/dev/vdb1
[root@server8 mnt]# pvdisplay
--- Physical volume ---
PV Name /dev/vdb1
VG Name linuxvg
PV Size 1.00 GiB / not usable 16.70 MiB
Allocatable yes (but full)
PE Size 16.00 MiB
Total PE 63
Free PE 0
Allocated PE 63
PV UUID tFCGUH-42Df-ReVb-4mJz-WohI-EyeK-Nc3vvh
- 然后需要使用命令转移
PE
,需要转移到一个具有足够PE
来容纳的分区或者此案上面;
[root@server8 mnt]# pvmove /dev/vdb1 /dev/vdb4
/dev/vdb1: Moved: 3.2%
/dev/vdb1: Moved: 100.0%
- 然后执行
vgreduce
命令,来移除/dev/vdb1
[root@server8 mnt]# vgreduce linuxvg /dev/vdb1
Removed "/dev/vdb1" from volume group "linuxvg"
- 最后需要清除上面的
PV
信息
[root@server8 mnt]# pvremove /dev/vdb1
Labels on physical volume "/dev/vdb1" successfully wiped
- 如何使用LVM制作系统快照
- 快照:表示将当前的系统信息记录下来,如果数据发生改动,将原始的数据保留在快照区域,没有发生改动的数据,就由快照区域和文件系统区域共享,也就是只需要备份发生改变的数据,这个备份过程是很快速的;
- 这里使用
/dev/vdb1
来创建快照区域
[root@server8 mnt]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created
[root@server8 mnt]# vgextend linuxvg /dev/vdb1
Volume group "linuxvg" successfully extended
- 然后创建系统快照区域,并且需要指定快照卷的大小
[root@server8 mnt]# lvcreate -l 60 -s -n back_to /dev/linuxvg/linuxos
Logical volume "back_to" created
- 块照卷也是可以进行挂载的
[root@server8 /]# mount /dev/linuxvg/back_to /backup/
[root@server8 /]# cd backup/
[root@server8 backup]# ls
boot.dump.1 lost+found
- 这样就存在两个数据的访问入口,接下来,改变
/mnt
里面的内容,然后通过块照卷进行还原
[root@server8 /]# umount /backup/
[root@server8 mnt]# cp -r /etc/ .
[root@server8 mnt]# cp -r /boot/ .
[root@server8 mnt]# cp etc/ -r ./ets
[root@server8 mnt]# du -smh ./
93M ./
- 接下来进行数据的还原过程
[root@server8 mnt]# mount /dev/linuxvg/back_to /backup/
[root@server8 backup]# tar -jcf /back.1.tar.bz2 ./*
[root@server8 /]# umount /backup/
- 删除块照卷
[root@server8 /]# lvremove /dev/linuxvg/back_to
Do you really want to remove active logical volume back_to? [y/n]: y
Logical volume "back_to" successfully removed
- 首先卸载,然后进行重新格式化,在将打包的数据进行还原
[root@server8 /]# umount /mnt/
[root@server8 /]# mkfs.ext4 /dev/linuxvg/linuxos
[root@server8 /]# mount /dev/linuxvg/linuxos /mnt/
[root@server8 /]# tar -jxf /back.1.tar.bz2 -C /mnt/
[root@server8 mnt]# ls
boot.dump.1 lost+found
- 接下来正常删除上面创建的
LVM
设备 - 首先执行卸载操作
[root@server8 /]# umount /mnt/
- 然后删除
LV
设备
[root@server8 /]# lvremove /dev/linuxvg/linuxos
Do you really want to remove active logical volume linuxos? [y/n]: y
Logical volume "linuxos" successfully removed
- 然后修改状态为非活跃状态
[root@server8 /]# vgchange -an linuxvg
0 logical volume(s) in volume group "linuxvg" now active
- 然后移除
VG
设备
[root@server8 /]# vgremove linuxvg
Volume group "linuxvg" successfully removed
- 然后清除
PV
信息
[root@server8 /]# pvremove /dev/vdb{1,2,3,4}
Labels on physical volume "/dev/vdb1" successfully wiped
Labels on physical volume "/dev/vdb2" successfully wiped
Labels on physical volume "/dev/vdb3" successfully wiped
Labels on physical volume "/dev/vdb4" successfully wiped
[root@server8 /]# pvremove /dev/vdc1
Labels on physical volume "/dev/vdc1" successfully wiped
- 最后修改分区类型,或者删除这些暂时用不到的分区
[root@server8 /]# fdisk /dev/vdb
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): p
Disk /dev/vdb: 8589 MB, 8589934592 bytes
16 heads, 63 sectors/track, 16644 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xb357903f
Device Boot Start End Blocks Id System
/dev/vdb1 1 2082 1049296+ 8e Linux LVM
/dev/vdb2 2083 6244 2097648 8e Linux LVM
/dev/vdb3 6245 8326 1049328 8e Linux LVM
/dev/vdb4 8327 10408 1049328 8e Linux LVM
Command (m for help): d
Partition number (1-4): 1
Command (m for help): d
Partition number (1-4): 2
Command (m for help): d
Partition number (1-4): 3
Command (m for help): d
Selected partition 4
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@server8 /]# partx /dev/vdb
# 1: 0- -1 ( 0 sectors, 0 MB)
# 2: 0- -1 ( 0 sectors, 0 MB)
# 3: 0- -1 ( 0 sectors, 0 MB)
# 4: 0- -1 ( 0 sectors, 0 MB)
[root@server8 /]# fdisk /dev/vdc
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): p
Disk /dev/vdc: 4294 MB, 4294967296 bytes
16 heads, 63 sectors/track, 8322 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x76a24ee2
Device Boot Start End Blocks Id System
/dev/vdc1 1 8322 4194256+ 8e Linux LVM
Command (m for help): d
Selected partition 1
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.