DM设备的创建与管理

Posted ant_colonies

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DM设备的创建与管理相关的知识,希望对你有一定的参考价值。

DM(Device Mapper)即设备映射(逻辑设备)。

MD和DM是Linux内核上2种工作机制(实现逻辑设备)不同的模块。

Physical Volume(PV): 物理卷         底层
Volume Group(VG): 卷组              中间层
Logical Volume(LV):逻辑卷          第一层(可伸缩)
Physical Extend(PE):物理盘区       类似于RAID的chunk
Logical Extend(LE):逻辑盘区        PE在逻辑卷层面上的称谓

各层的管理命令:

pv
    pvcreate, pvremove, pvmove, pvdisplay, pvs, pvscan
vg
    vgcreate, vgremove, vgextend, vgreduce, vgdisplay, pvs, vgscan
lv
    lvcreate, lvremove, lvextend, lvreduce, lvresize, lvdisplay, lvs,lvscan

 

以下是实现过程(10G的卷组):

1、创建Linux LVM分区

[root@ant-colonies ~]# fdisk /dev/sda
...

Command (m for help): t
Partition number (1-13): 13
Hex code (type L to list codes): 8e
Changed system type of partition 13 to 8e (Linux LVM)
...

/dev/sda11           4210        4602     3156741   8e  Linux LVM
/dev/sda12           4603        5517     7349706   8e  Linux LVM
/dev/sda13           5518        6171     5253223+  8e  Linux LVM

[root@ant-colonies ~]# cat /proc/partitions
major minor  #blocks   name

...

   8       11    3156741 sda11
   8       12    7349706 sda12
   8       13    5253223 sda13

 

2、创建物理卷(Physical Volume)

[root@ant-colonies ~]# pvcreate /dev/sda{11,12}
  Physical volume "/dev/sda11" successfully created
  Physical volume "/dev/sda12" successfully created
[root@ant-colonies ~]# pvs
  PV              VG   Fmt  Attr  PSize PFree
  /dev/sda11         lvm2 ---  3.01g 3.01g
  /dev/sda12         lvm2 ---  7.01g 7.01g
  /dev/sda6  myvg lvm2 a--  7.01g 6.95g
  /dev/sda7  myvg lvm2 a--  5.01g 5.01g
[root@ant-colonies ~]# pvdisplay
...
"/dev/sda11" is a new physical volume of "3.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda11
  VG Name               
  PV Size               3.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               D0KinO-K7pw-rkIh-GsY1-dxDZ-d1MO-zBfjMZ
   
  "/dev/sda12" is a new physical volume of "7.01 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda12
  VG Name               
  PV Size               7.01 GiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               XJRRp1-awM3-qfDW-Qv46-ho2i-MS45-PmcxzG

  [root@ant-colonies ~]# pvscan
  PV /dev/sda6    VG myvg   lvm2 [7.01 GiB / 6.95 GiB free]
  PV /dev/sda7    VG myvg   lvm2 [5.01 GiB / 5.01 GiB free]
  PV /dev/sda11             lvm2 [3.01 GiB]
  PV /dev/sda12             lvm2 [7.01 GiB]
  Total: 4 [22.04 GiB] / in use: 2 [12.02 GiB] / in no VG: 2 [10.02 GiB]

 

3、创建卷组(Volume Group)

[root@ant-colonies ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   1   0 wz--n- 12.02g 11.96g
[root@ant-colonies ~]# vgcreate MYVG /dev/sda{11,12}
  Volume group "MYVG" successfully created
[root@ant-colonies ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  MYVG   2   0   0 wz--n- 10.02g 10.02g
  myvg   2   1   0 wz--n- 12.02g 11.96g

[root@ant-colonies ~]# vgdisplay MYVG
  --- 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               10.02 GiB
  PE Size               4.00 MiB
  Total PE              2564
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2564 / 10.02 GiB
  VG UUID               8mp6sm-1Wj4-p82x-d7LG-OWSt-8i2q-BZmGKA

 

4、删除卷组创建指定PE大小的卷组

[root@ant-colonies ~]# vgremove MYVG
  Volume group "MYVG" successfully removed
[root@ant-colonies ~]# vgcreate -s 8M MYVG /dev/sda{11,12}
  Volume group "MYVG" successfully created
[root@ant-colonies ~]# vgdisplay MYVG
  --- 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               10.02 GiB
  PE Size               8.00 MiB
  Total PE              1282
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1282 / 10.02 GiB
  VG UUID               RI4sJU-OI6d-CSva-HBes-gfzP-MMu8-E2QPWu

 

5、移除物理卷成员“三部曲

[root@ant-colonies ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  MYVG   2   0   0 wz--n- 10.02g 10.02g
  myvg   2   1   0 wz--n- 12.02g 11.96g
[root@ant-colonies ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sda11 MYVG lvm2 a--  3.01g 3.01g
  /dev/sda12 MYVG lvm2 a--  7.01g 7.01g
  /dev/sda6  myvg lvm2 a--  7.01g 6.95g
  /dev/sda7  myvg lvm2 a--  5.01g 5.01g

   1)将要移除的物理卷/dev/sda11的数据移动到同组其他物理卷
[root@ant-colonies ~]# pvmove /dev/sda11
  No data to move for MYVG

   2)将/dev/sda11从卷组中移除
[root@ant-colonies ~]# vgreduce MYVG /dev/sda11
  Removed "/dev/sda11" from volume group "MYVG"
[root@ant-colonies ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  MYVG   1   0   0 wz--n-  7.01g  7.01g
  myvg   2   1   0 wz--n- 12.02g 11.96g
[root@ant-colonies ~]# pvs
  PV              VG    Fmt  Attr PSize PFree
  /dev/sda11          lvm2 ---  3.01g 3.01g
  /dev/sda12 MYVG lvm2 a--  7.01g 7.01g
  /dev/sda6   myvg lvm2 a--  7.01g 6.95g
  /dev/sda7   myvg lvm2 a--  5.01g 5.01g

    3)将物理卷/dev/sda11移除
[root@ant-colonies ~]# pvremove /dev/sda11
  Labels on physical volume "/dev/sda11" successfully wiped
[root@ant-colonies ~]# pvs
  PV         VG   Fmt  Attr PSize PFree
  /dev/sda12 MYVG lvm2 a--  7.01g 7.01g
  /dev/sda6  myvg lvm2 a--  7.01g 6.95g
  /dev/sda7  myvg lvm2 a--  5.01g 5.01g

 

6、添加新的物理卷到卷组

[root@ant-colonies ~]# pvcreate /dev/sda13
  Physical volume "/dev/sda13" successfully created
[root@ant-colonies ~]# vgextend MYVG /dev/sda13
  Volume group "MYVG" successfully extended
[root@ant-colonies ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  MYVG   2   0   0 wz--n- 12.02g 12.02g
  myvg   2   1   0 wz--n- 12.02g 11.96g
[root@ant-colonies ~]# pvdisplay /dev/sda13
  --- Physical volume ---
  PV Name               /dev/sda13
  VG Name               MYVG
  PV Size               5.01 GiB / not usable 2.10 MiB
  Allocatable           yes
  PE Size               8.00 MiB                      #与卷组的PE大小一致
  Total PE              641
  Free PE               641
  Allocated PE          0
  PV UUID               DJyRRk-NnZb-dqHt-pqUy-T83d-SkQO-e4S4U2

 

7、创建逻辑卷(Logical Volume)

[root@ant-colonies ~]# lvcreate -L 50M -n TESTLV MYVG
  Rounding up size to full physical extent 56.00 MiB
  Logical volume "TESTLV" created.
[root@ant-colonies ~]# lvs
  LV          VG      Attr             LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  TESTLV  MYVG  -wi-a-----    56.00m                                                    
  testlv     myvg   -wi-a-----    56.00m   
[root@ant-colonies ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/MYVG/TESTLV
  LV Name                TESTLV
  VG Name                MYVG
  LV UUID                r41vKB-XplG-PCwV-q8C4-gaAT-Rxh1-ydSQFh
  LV Write Access        read/write
  LV Creation host, time ant-colonies, 2017-01-20 18:15:31 +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:1
...

8、格式化

[root@ant-colonies ~]# mke2fs -j /dev/MYVG/TESTLV
...

 

9、挂载

[root@ant-colonies ~]# mount /dev/MYVG/TESTLV  /user
[root@ant-colonies ~]# cp /etc/inittab /user
[root@ant-colonies ~]# cd /user
[root@ant-colonies user]# ls -l
total 14
-rw-r--r--. 1 root root   884 Jan 21 00:03 inittab
drwx------. 2 root root 12288 Jan 20 23:12 lost+found
[root@ant-colonies user]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/MYVG-TESTLV on /user type ext3 (rw)


    说明/dev/mapper/MYVG-TESTLV 和 /dev/MYVG/TESTLV均为/dev/dm-1的字符链接。

 

10、卸载

[root@ant-colonies user]# lvremove /dev/mapper/MYVG-TESTLV
  Logical volume MYVG/TESTLV contains a filesystem in use.
[root@ant-colonies /]# umount /user
[root@ant-colonies /]# lvremove /dev/mapper/MYVG-TESTLV
Do you really want to remove active logical volume TESTLV? [y/n]: y
  Logical volume "TESTLV" successfully removed                               # 意味着数据也被删除

[root@ant-colonies ~]# lvs
  LV     VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv myvg -wi-a----- 56.00m     

 

再次创建、格式化并挂载逻辑卷

[root@ant-colonies ~]# lvcreate -L 50M -n TESTLV MYVG

[root@ant-colonies ~]# mke2fs -j -b 4096 -E stride=16 /dev/mapper/MYVG-TESTLV

...

Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=16 blocks, Stripe width=0 blocks
14336 inodes, 14336 blocks
...

[root@ant-colonies ~]# mount /dev/mapper/MYVG-TESTLV /user
[root@ant-colonies ~]# ls -l /user
total 16
drwx------. 2 root root 16384 Jan 21 00:24 lost+found

[root@ant-colonies ~]# cp /etc/inittab /user
[root@ant-colonies ~]# cd /user
[root@ant-colonies user]# ls -l
total 24
-rw-r--r--. 1 root root   884 Jan 21 00:29 inittab
drwx------. 2 root root 16384 Jan 21 00:24 lost+found

 

11、扩展逻辑卷

[root@ant-colonies user]# pvs
  PV             VG     Fmt  Attr PSize PFree
  /dev/sda12 MYVG lvm2 a--  7.01g 6.95g
  /dev/sda13 MYVG lvm2 a--  5.01g 5.01g
  /dev/sda6  myvg lvm2 a--  7.01g 6.95g
  /dev/sda7  myvg lvm2 a--  5.01g 5.01g
[root@ant-colonies user]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree
  MYVG   2   1   0 wz--n- 12.02g 11.96g
  myvg   2   1   0 wz--n- 12.02g 11.96g
[root@ant-colonies user]# lvs
  LV     VG            Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  TESTLV MYVG -wi-ao---- 56.00m                                                    
  testlv myvg -wi-a----- 56.00m       
[root@ant-colonies user]# lvextend -L 5G /dev/mapper/MYVG-TESTLV
  Size of logical volume MYVG/TESTLV changed from 56.00 MiB (7 extents) to 5.00 GiB (640 extents).
  Logical volume TESTLV successfully resized
[root@ant-colonies user]# resize2fs -p /dev/mapper/MYVG-TESTLV
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/MYVG-TESTLV is mounted on /user; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/mapper/MYVG-TESTLV to 1310720 (4k) blocks.
The filesystem on /dev/mapper/MYVG-TESTLV is now 1310720 blocks long.
[root@ant-colonies user]# lvs
  LV     VG   Attr                LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  TESTLV MYVG -wi-ao----  5.00g                                                    
  testlv myvg -wi-a----- 56.00m

[root@ant-colonies user]# ls -l
total 24
-rw-r--r--. 1 root root   884 Jan 21 00:29 inittab
drwx------. 2 root root 16384 Jan 21 00:24 lost+found

 

12、缩减逻辑卷(至2G)

[root@ant-colonies ~]# df -Ph                                          # 查看逻辑卷大小
Filesystem               Size  Used Avail Use% Mounted on
/dev/sda2                9.8G  2.1G  7.2G  23% /
tmpfs                    491M     0  491M   0% /dev/shm
/dev/sda1                190M   36M  145M  20% /boot
/dev/mapper/MYVG-TESTLV  5.0G  4.2M  4.7G   1% /user

[root@ant-colonies ~]# umount /user                                        # 卸载
[root@ant-colonies ~]# e2fsck -f /dev/mapper/MYVG-TESTLV      # 强行检查逻辑卷
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-TESTLV: 12/573440 files (0.0% non-contiguous), 19073/1310720 blocks
[root@ant-colonies ~]# resize2fs /dev/mapper/MYVG-TESTLV 2G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/MYVG-TESTLV to 524288 (4k) blocks.
The filesystem on /dev/mapper/MYVG-TESTLV is now 524288 blocks long.
[root@ant-colonies ~]# lvreduce -L -3G /dev/mapper/MYVG-TESTLV
  WARNING: Reducing active logical volume to 2.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce TESTLV? [y/n]: y
  Size of logical volume MYVG/TESTLV changed from 5.00 GiB (640 extents) to 2.00 GiB (256 extents).
  Logical volume TESTLV successfully resized

[root@ant-colonies ~]# mount /dev/mapper/MYVG-TESTLV /user
[root@ant-colonies ~]# cd /user
[root@ant-colonies user]# ls -l
total 24
-rw-r--r--. 1 root root   884 Jan 21 00:29 inittab                #文件未发生改变
drwx------. 2 root root 16384 Jan 21 00:24 lost+found

 

13、创建快照

[root@ant-colonies user]# lvcreate -L 50M -n TESTLV-SNAP -s -p r /dev/mapper/MYVG-TESTLV
  Rounding up size to full physical extent 56.00 MiB
  Logical volume "TESTLV-SNAP" created.
[root@ant-colonies user]# lvs
  LV          VG   Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  TESTLV      MYVG owi-aos--- 2.00g                                         
  TESTLV-SNAP MYVG sri-a-s--- 56.00m      TESTLV 0.02                                   
  testlv      myvg -wi-a----- 56.00m                                                    
[root@ant-colonies user]# mount /dev/mapper/MYVG-TESTLV /mnt
[root@ant-colonies user]# cd /mnt
[root@ant-colonies mnt]# ls -l
total 24
-rw-r--r--. 1 root root   884 Jan 21 00:29 inittab
drwx------. 2 root root 16384 Jan 21 00:24 lost+found
[root@ant-colonies mnt]# wc -l inittab
26 inittab
[root@ant-colonies mnt]#  (之后的操作,在系统中修改数据,快照也发生改变,原因不明?????)

以上是关于DM设备的创建与管理的主要内容,如果未能解决你的问题,请参考以下文章

Linux块设备加密之dm-crypt分析

LVM逻辑卷管理

dm-verity简介 ——

lvm 逻辑卷 和 网络管理

lvm 逻辑卷管理器

逻辑卷管理器LVM