磁盘管理(五)

22.DM
1)综述
全称:Device Mapper设备映射
功能:dm和md功能有些是重叠的,同样支持逻辑设备,可以支持RAID(0,1),还支持LVM2。通常用md做RAID,用dm做LVM2
内核架构:
解释:与md一样dm也是由核心模块和子模块组成的。子模块拥有多种将物理设备组织成逻辑设备的方式
linear:线性设备,类似于JBOD,能够将多个磁盘上的空间装载拼接在一块,当作一个来使用
mirror:镜像。
snapshot:快照。比较小,因为里面只存储和原来的差异部分。所以相同部分用户直接访问原来的。差异部分访问快照的
multipath:多路径。主要用于对数据,系统可靠性,可用性要求非常高的场合。
场景:
主机与外接磁盘柜相连,如果这根线被老鼠咬断,数据无法访问,这在生产中是不被允许的。
如果接两个线路,数据如何转发呢?
多路径可以实现设备存储的寻路,让多根不同的线缆完成


2)LVM
理论:
@.PV:Physical Volume 物理卷(磁盘)
@.VG:Volume Group 卷组(类似于扩展分区)
i.只有把物理卷放入卷组中,卷组才可以将物理卷所提供的存储空间划分为块。
ii.也就是说卷组中的存储空间全是块,这个块不是文件系统块,而是叫做PE(Physical Extend 物理盘区)
iii.PE大小取决于卷组
@.LV:Logical Volume 逻辑卷(类似于逻辑分区)
i>在卷组汇总把多少个PE组成一个空间,这就是逻辑卷LV,只不过这里的PE就成了LE(Logical Extend)
ii>单个逻辑卷可以超出单个存储空间
iii>注意在穿件快照时必须在同一个卷组中预留出空间,而这个快照作为访问存储空间的另一个入口,第一个入口是逻辑卷
iv>支持镜像功能,防止物理磁盘损坏造成损失。

操作命令(添加3块10G硬盘)
PV:
pvcreate 创建PV
pvremove 用于删除一个存在的物理卷。使用pvremove指令删除物理卷时,他将LVM分区上的物理卷信息删除。
使其不再被视为一个物理卷
pvmove 用来将一块盘上的数据移到另一块盘,可以移动整块盘的数据,也可以移动指定extend范围的数据
pvscan 扫描PV
pvdisplay 显示PV

VG: 
vgcreate 创建vg
vgcreate VG_NAME  /PATH/TO/PV 
-s #:PE大小,默认是4M,他决定了单个逻辑卷最大容量为256GB,实际生产中如果容量大的话最好把PE调大
vgremove 移除vg
vgextend 扩展vg(加入新PV)
vgreduce 缩减vg(移走PV)
vgs(即vgscan) 扫描vg
vgdisplay 显示vg

LV:
lvcreate
lvcreate -n LV_NAME -L #G VG_NAME 
注意逻辑卷的名字用纯英文字符表示
lvremove
lvextend
lvreduce
lvresize
lvs(即lvscan)
lvdisplay

实验一:创建20G物理卷PV
准备工作:
[root@ns1 ~]# fdisk -l


Disk /dev/sda: 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: 0x000c417f


  Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          66      524288   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              66         327     2097152   82  Linux swap / Solaris
Partition 2 does not end on cylinder boundary.
/dev/sda3             327        2611    18349056   83  Linux


Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x00000000

Disk /dev/sdc: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x00000000

[root@ns1 ~]# fdisk /dev/sdb(sdc)


Command (m for help): n
Command action
  e   extended
  p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +10G
Value out of range.


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: 10.7 GB, 10737418240 bytes
。。。


  Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1305    10482381   8e  Linux LVM

Command (m for help): w       
The partition table has been altered!


Calling ioctl() to re-read partition table.
Syncing disks.

[root@ns1 ~]# cat /proc/partitions 

major minor  #blocks  name
备注:确认分区可用
  8        0   20971520 sda
  8        1     524288 sda1
  8        2    2097152 sda2
  8        3   18349056 sda3
  8       16   10485760 sdb
  8       17   10482381 sdb1
  8       32   10485760 sdc
  8       33   10482381 sdc1
  8       48   10485760 sdd

具体操作
创建物理卷PV
[root@ns1 ~]# pvcreate /dev/sdb1 /dev/sdc1
 Physical volume "/dev/sdb1" successfully created
 Physical volume "/dev/sdc1" successfully created
[root@ns1 ~]# pvs
 PV         VG   Fmt  Attr PSize  PFree 
 /dev/sdb1       lvm2 ---- 10.00g 10.00g
 /dev/sdc1       lvm2 ---- 10.00g 10.00g
注意:现在看到的容量实际中可能要小些,误差在20%内都是正常的,因为当中会保留一些元数据等内容(如文件目录)

[root@ns1 ~]# pvdisplay //或查看指定信息   pvdisplay /dev/sdb1(sdc1)
 "/dev/sdb1" is a new physical volume of "10.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sdb1
 VG Name               
 PV Size               10.00 GiB
 Allocatable           NO
 PE Size               0   
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               c0SjkO-zo2W-KAVa-aso7-3fwI-P2tX-cxV3w4
  
 "/dev/sdc1" is a new physical volume of "10.00 GiB"
 --- NEW Physical volume ---
 PV Name               /dev/sdc1
 VG Name               
 PV Size               10.00 GiB
 Allocatable           NO
 PE Size               0   
 Total PE              0
 Free PE               0
 Allocated PE          0
 PV UUID               fi5PXO-N6PO-5R1I-zbeg-cCl2-BqxG-MtY07O
备注:显示的更加详细,但是要注意里面的Free PE为0,是因为目前没有把物理卷加入卷组

[root@ns1 ~]# pvscan
 PV /dev/sdb1                      lvm2 [10.00 GiB]
 PV /dev/sdc1                      lvm2 [10.00 GiB]
 Total: 2 [19.99 GiB] / in use: 0 [0   ] / in no VG: 2 [19.99 GiB]


创建卷组VG:
[root@ns1 ~]# vgcreate zygvg /dev/sdb1 /dev/sdc1
 Volume group "zygvg" successfully created
[root@ns1 ~]# vgs
 VG    #PV #LV #SN Attr   VSize  VFree 
 zygvg   2   0   0 wz--n- 19.98g 19.98g
[root@ns1 ~]# vgdisplay zygvg
 --- Volume group ---
 VG Name               zygvg
 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               19.98 GiB
 PE Size               4.00 MiB
 Total PE              5116
 Alloc PE / Size       0 / 0   
 Free  PE / Size       5116 / 19.98 GiB
 VG UUID               PWTKwA-aQGA-sK71-YLtG-3XH1-cwi6-EZzWh2
备注:可以发现PE Size有数据了:PE Size               4.00 MiB

调整PE大小:
[root@ns1 ~]# vgremove zygvg
 Volume group "zygvg" successfully removed
[root@ns1 ~]# vgcreate -s 8M zygvg /dev/sdb1 /dev/sdc1
 Volume group "zygvg" successfully created


[root@ns1 ~]# vgdisplay zygvg
 --- Volume group ---
 VG Name               zygvg
 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               19.98 GiB
 PE Size               8.00 MiB
 Total PE              2558
 Alloc PE / Size       0 / 0   
 Free  PE / Size       2558 / 19.98 GiB
 VG UUID               EfwblR-KKxy-Hcbt-dza6-oato-H2x0-KMgMvR
备注:发现PE Size               8.00 MiB
缩减VG的过程就是拿掉PV的过程,如果PV有数据则要先把PV中的数据先移走pvmove,之后再拿掉 PV,过程如下:
[root@ns1 ~]# pvmove /dev/sdc1
 No data to move for zygvg
[root@ns1 ~]# vgreduce zygvg /dev/sdc1
 Removed "/dev/sdc1" from volume group "zygvg"
[root@ns1 ~]# pvscan
 PV /dev/sdb1   VG zygvg           lvm2 [9.99 GiB / 9.99 GiB free]
 PV /dev/sdc1                      lvm2 [10.00 GiB]
 Total: 2 [19.99 GiB] / in use: 1 [9.99 GiB] / in no VG: 1 [10.00 GiB]
备注:发现/dev/sdc1不属于任何vg了
擦除/dev/sdc1的数据
[root@ns1 ~]# pvremove /dev/sdc1
 Labels on physical volume "/dev/sdc1" successfully wiped
[root@ns1 ~]# pvs
 PV         VG    Fmt  Attr PSize PFree
 /dev/sdb1  zygvg lvm2 a--u 9.99g 9.99g
备注:发现/dev/sdc1已经不存在了
扩展VG容量
[root@ns1 ~]# pvcreate /dev/sdc1
 Physical volume "/dev/sdc1" successfully created
[root@ns1 ~]# vgextend zygvg /dev/sdc1
 Volume group "zygvg" successfully extended
[root@ns1 ~]# vgs
 VG    #PV #LV #SN Attr   VSize  VFree 
 zygvg   2   0   0 wz--n- 19.98g 19.98g
备注:发现PV有两个,卷组大小也发生了变化

创建逻辑卷LV
[root@ns1 ~]# lvcreate -L 50M -n testlv zygvg
 Rounding up size to full physical extent 56.00 MiB
 Logical volume "testlv" created.
备注:因为50M不能被8M整除,所以给出的结果要比50M大
[root@ns1 ~]# lvs
 LV     VG    Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 testlv zygvg -wi-a----- 56.00m
[root@ns1 ~]# lvdisplay /dev/zygvg/testlv 
 --- Logical volume ---
 LV Path                /dev/zygvg/testlv
 LV Name                testlv
 VG Name                zygvg
 LV UUID                kVd8BQ-DAW6-u4xn-1LQC-DmnO-9NCS-bu2tIx
 LV Write Access        read/write
 LV Creation host, time ns1.zyg, 2018-04-19 15:05:27 +0800
 LV Status              available
 # open                 0
 LV Size                56.00 MiB
 Current LE             7
 Segments               1
 Allocation             inherit
 Read ahead sectors     auto
 - currently set to     256
 Block device           253:0

格式化与挂载LV:
[root@ns1 ~]# mke2fs -j /dev/zygvg/testlv
[root@ns1 ~]# mount /dev/zygvg/testlv /mnt/
[root@ns1 ~]# ls /mnt/
lost+found
[root@ns1 ~]# mount
/dev/sda3 on / type ext4 (rw)
显示部分结果:/dev/mapper/zygvg-testlv on /mnt type ext3 (rw)
备注:挂载的真正目录是/dev/mapper/zygvg-testlv
[root@ns1 ~]# ls -l /dev/mapper/
总用量 0
crw-rw---- 1 root root 10, 58 4月  19 14:30 control
lrwxrwxrwx 1 root root      7 4月  19 15:07 zygvg-testlv -> ../dm-0

实验二:
1.扩展逻辑卷
原则:先扩展逻辑卷物理边界,再扩展文件系统(逻辑)边界
1)扩展逻辑卷物理便捷命令:lvextend(如果是xfs和reiserfs文件系统会用其他命令了)
格式:lvextend —L [+]# /PATH/TO/LV
备注:+代表扩展了(添加)多少M(G,T,P,E),不带+代表扩展到(变为)多少M(G,T,P,E)。

2)扩展文件系统(逻辑)便捷命令:resize2fs(如果是xfs和reiserfs文件系统会用其他命令)
格式:resize2fs -p  /PATH/TO/LV

3)案例要求
创建一个由两个物理卷组成的大小为2G的卷组zygvg
要求其PE大小为16M
要此卷组中创建一个大小为5G 的逻辑卷lv1
此逻辑卷要能在开机后自动挂载到/users目录,且支持acl功能(实现时可能略有差距)




实施:
[root@ns1 ~]# lvcreate -L 2G -n testlv zygvg
 Logical volume "testlv" already exists in volume group "zygvg"
[root@ns1 ~]# mke2fs -j /dev/zygvg/testlv 
mke2fs 1.41.12 (17-May-2010)
/dev/zygvg/testlv 已经挂载;will not make a 文件系统 here!
[root@ns1 ~]# mkdir /users
[root@ns1 ~]# mount /dev/zygvg/testlv /users/
[root@ns1 ~]# ls /users/
lost+found
[root@ns1 ~]# vim /etc/fstab
备注:实现开机自动挂载,具备acl功能,在此文件末尾添加如下代码
/dev/zygvg/testlv       /users                  ext3    defaults,acl    0 0
[root@ns1 ~]# mount -a
[root@ns1 ~]# mount
显示部分内容:
/dev/mapper/zygvg-testlv on /users type ext3 (rw)
备注:此时发现没有具备acl功能,重启系统,再次mount 
[root@ns1 ~]# mount
/dev/mapper/zygvg-testlv on /users type ext3 (rw,acl)
[root@ns1 ~]# cp /etc/inittab /users/
[root@ns1 ~]# cat /users/inittab 
# inittab is only used by upstart for the default runlevel.
#
# ADDING OTHER CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# System initialization is started by /etc/init/rcS.conf
#
# Individual runlevels are started by /etc/init/rc.conf
#
# Ctrl-Alt-Delete is handled by /etc/init/control-alt-delete.conf
#
# Terminal gettys are handled by /etc/init/tty.conf and /etc/init/serial.conf,
# with configuration in /etc/sysconfig/init.
#
# For information on how to write upstart event handlers, or how
# upstart works, see init(5), init(8), and initctl(8).
#
# Default runlevel. The runlevels used are:
#   0 - halt (Do NOT set initdefault to this)
#   1 - Single user mode
#   2 - Multiuser, without NFS (The same as 3, if you do not have networking)
#   3 - Full multiuser mode
#   4 - unused
#   5 - X11
#   6 - reboot (Do NOT set initdefault to this)

id:3:initdefault:
备注:可以使用
[root@ns1 ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              18G  2.1G   15G  13% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             488M   33M  430M   8% /boot
/dev/mapper/zygvg-testlv
  55M  5.0M   47M  10% /users
[root@ns1 ~]# vgs
 VG    #PV #LV #SN Attr   VSize  VFree 
 zygvg   2   1   0 wz--n- 19.98g 19.93g
备注:保证有足够空间做扩展
[root@ns1 ~]# lvextend -L 5G /dev/zygvg/testlv 
 Size of logical volume zygvg/testlv changed from 56.00 MiB (7 extents) to 5.00 GiB (640 extents).
 Logical volume testlv successfully resized.
[root@ns1 ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              18G  2.1G   15G  13% /
tmpfs                 499M     0  499M   0% /dev/shm
/dev/sda1             488M   33M  430M   8% /boot
/dev/mapper/zygvg-testlv
  55M  5.0M   47M  10% /users
[root@ns1 ~]# lvs
 LV     VG    Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
 testlv zygvg -wi-ao---- 5.00g 
备注:这里显示的是5G,由此可见目前仅仅是扩展了物理边界,没有扩展逻辑边界
[root@ns1 ~]# cat /users/inittab
备注:文件仍然可以使用,所以在线扩容很方便,风险极低,很实用。

猜你喜欢

转载自blog.csdn.net/gang926/article/details/80073502