Linux LVM逻辑卷

一、逻辑卷
逻辑卷的工作原理:
逻辑卷(lv)是将几个磁盘分区或者块设备(pv,pv的id必须是8e的,pv可以位于不同的磁盘分区里,pv大小可以不一)组织起来形成一个大的扩展分区(vg,卷组,一个vg至少要包含一个pv。),该扩展分区不能直接用,需要将其划分成逻辑卷(lv)才能使用lv可以格式化成不同的文件系统,挂载后直接使用。 lv的扩展和缩减是不会影响原有数据的,但逻辑卷缩减的风险大于逻辑卷扩展的风险。逻辑卷可以支持快照功能。

1、将物理分区作成物理卷
新建磁盘并改变磁盘标签(8e)

[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): 
Using default response p
Partition number (2-4, default 2): 
First sector (1026048-20971519, default 1026048): 
Using default value 1026048
Last sector, +sectors or +size{K,M,G} (1026048-20971519, default 20971519): +500M
Partition 2 of type Linux and of size 500 MiB is set

Command (m for help): P

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6a675445

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux
/dev/vdb2         1026048     2050047      512000   83  Linux
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partprobe 

为方便查看逻辑卷的创建情况现在进行监控
新建目录用来挂载

[root@localhost ~]# mkdir /weixindata
[root@localhost ~]# 

对挂载目录进行监控

[root@localhost ~]# watch -n 1 'pvs;echo PV;vgs;echo VG;lvs;echo LV;df -h /weixindata'

由于未建立逻辑组逻辑卷所以此时监控到的逻辑组逻辑卷无数据
在这里插入图片描述

2、创建逻辑组逻辑卷
物理卷

[root@localhost ~]# pvcreate /dev/vdb2
  Physical volume "/dev/vdb2" successfully created
[root@localhost ~]# 

在这里插入图片描述
创建逻辑组

[root@localhost ~]# vgcreate weixingroup /dev/vdb2
  Volume group "weixingroup" successfully created
[root@localhost ~]# 

在这里插入图片描述
创建逻辑卷
格式:
lvcreate -L 逻辑卷大小 -n 卷组名 逻辑卷名称

[root@localhost ~]# lvcreate -L 50M -n weixindata weixingroup 
  Rounding up size to full physical extent 52.00 MiB
  Logical volume "weixindata" created
[root@localhost ~]# 

在这里插入图片描述
格式化逻辑卷

[root@localhost ~]# mkfs.xfs /dev/weixingroup/weixindata 
meta-data=/dev/weixingroup/weixindata isize=256    agcount=2, agsize=6656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=13312, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost ~]# 

挂载逻辑卷

[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
[root@localhost ~]# 

在这里插入图片描述
3、扩展逻辑卷

逻辑卷扩展前大小为60M,现在扩展到80M

[root@localhost ~]# lvextend -L 80M /dev/weixingroup/weixindata 
  Extending logical volume weixindata to 80.00 MiB
  Logical volume weixindata successfully resized
[root@localhost ~]# 

在这里插入图片描述

拉伸文件设备

[root@localhost ~]# xfs_growfs /dev/weixingroup/weixindata 
meta-data=/dev/mapper/weixingroup-weixindata isize=256    agcount=2, agsize=6656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=13312, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 13312 to 20480
[root@localhost ~]# 

在这里插入图片描述
4、VG最大只能扩大100M,若超过100M,可以给逻辑组添加
(1)新建磁盘改变标签为(Lvm)

[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): 
Using default response p
Partition number (3,4, default 3): 
First sector (2050048-20971519, default 2050048): 
Using default value 2050048
Last sector, +sectors or +size{K,M,G} (2050048-20971519, default 20971519): +300M
Partition 3 of type Linux and of size 300 MiB is set

Command (m for help): t
Partition number (1-3, default 3): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x6a675445

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux
/dev/vdb2         1026048     2050047      512000   8e  Linux LVM
/dev/vdb3         2050048     2664447      307200   8e  Linux LVM

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

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# 

(2)将新创建的逻辑组添加进物理卷组

[root@localhost ~]# pvcreate /dev/vdb3
  Physical volume "/dev/vdb3" successfully created
[root@localhost ~]# 

在这里插入图片描述

(3)扩展逻辑组
未扩展之前逻辑组大小为496m(图20)

在这里插入图片描述

[root@localhost ~]# vgextend weixingroup /dev/vdb3
  Volume group "weixingroup" successfully extended
[root@localhost ~]# 

在这里插入图片描述

(4)扩展逻辑卷

[root@localhost ~]# lvextend -L 200M /dev/weixingroup/weixindata 
  Extending logical volume weixindata to 200.00 MiB
  Logical volume weixindata successfully resized
[root@localhost ~]# 

在这里插入图片描述

(5)格式化文件系统

[root@localhost ~]# xfs_growfs /dev/weixingroup/weixindata
meta-data=/dev/mapper/weixingroup-weixindata isize=256    agcount=4, agsize=6656 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=20480, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 20480 to 51200
[root@localhost ~]# 

在这里插入图片描述
5、缩减逻辑卷
将设备格式化成xfs后是不能缩减的,只能扩展,所以需重新格式化逻辑卷的类型
先缩减软件再缩减硬件
(1)重新格式化

[root@localhost ~]# umount /weixindata/
[root@localhost ~]# mkfs.ext4 /dev/weixingroup/weixindata 
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
51200 inodes, 204800 blocks
10240 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=33816576
25 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done 

(2)重新挂载

[root@localhost ~]# mount /dev/weixingroup/weixindata  /weixindata/
[root@localhost ~]# 

(3)扩大逻辑卷

[root@localhost ~]# lvextend -L 300M /dev/weixingroup/weixindata 
  Extending logical volume weixindata to 300.00 MiB
  Logical volume weixindata successfully resized
[root@localhost ~]# 

在这里插入图片描述
(3)扩展文件系统

[root@localhost ~]# resize2fs /dev/weixingroup/weixindata 
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/weixingroup/weixindata is mounted on /weixindata; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/weixingroup/weixindata is now 307200 blocks long.

[root@localhost ~]# 

在这里插入图片描述
(4)缩减文件系统(缩减时需卸掉挂载
先缩减软件再缩减硬件

[root@localhost ~]# umount /weixindata/
[root@localhost ~]# e2fsck -f /dev/weixingroup/weixindata 
e2fsck 1.42.9 (28-Dec-2013)
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/weixingroup/weixindata: 11/77824 files (0.0% non-contiguous), 15987/307200 blocks
[root@localhost ~]# resize2fs /dev/weixingroup/weixindata 50M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/weixingroup/weixindata to 51200 (1k) blocks.
The filesystem on /dev/weixingroup/weixindata is now 51200 blocks long.

重新挂载

[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
[root@localhost ~]# 

缩减硬件

[root@localhost ~]# lvreduce -L 50M /dev/weixingroup/weixindata 
  Rounding size to boundary between physical extents: 52.00 MiB
  WARNING: Reducing active and open logical volume to 52.00 MiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce weixindata? [y/n]: y
  Reducing logical volume weixindata to 52.00 MiB
  Logical volume weixindata successfully resized
[root@localhost ~]# 

6、取出物理卷分区(现在物理卷分区有2和3,现在取出2)

(1)将2的信息同步到3
[root@localhost ~]# pvmove /dev/vdb2 /dev/vdb3
  /dev/vdb2: Moved: 15.4%
  /dev/vdb2: Moved: 100.0%
[root@localhost ~]# 

(2)将2移出逻辑卷组

[root@localhost ~]# vgreduce weixingroup /dev/vdb2
  Removed "/dev/vdb2" from volume group "weixingroup"
[root@localhost ~]# 

(3)将2移动出物理卷

[root@localhost ~]# pvremove /dev/vdb2
  Labels on physical volume "/dev/vdb2" successfully wiped
[root@localhost ~]# 

7、LVM快照
快照的工作原理:
保存数据某一时刻的状态。快照刚刚建立的时候,它的大小是空的。快照是访问原文件的另一条路径。快照是在数据改变之前将数据原封不动的保存到快照空间里面。注意数据变化的幅度不能超过给定快照空间的大小,否则快照将崩溃。

快照的作用:
对某一时刻数据状态的备份提供另一条访问路径,通过快照来备份数据到其它位置,备份完成后,快照就没用了,卸载和移除该快照卷。快照空间的大小取决于数据改变的频度和所需要使用的时间长度,即有限的时间内原文件数据改变的空间大小。

(1)建立快照(在原挂载建立文件)

[root@localhost ~]# mount /dev/weixingroup/weixindata /weixindata/
[root@localhost ~]# df
Filesystem                         1K-blocks    Used Available Use% Mounted on
/dev/vda1                           10473900 3160456   7313444  31% /
devtmpfs                              469344       0    469344   0% /dev
tmpfs                                 484932      80    484852   1% /dev/shm
tmpfs                                 484932   12832    472100   3% /run
tmpfs                                 484932       0    484932   0% /sys/fs/cgroup
/dev/mapper/weixingroup-weixindata     45289    1038     40838   3% /weixindata

新建文件

[root@localhost ~]# cd /weixindata/
[root@localhost weixindata]# touch file{1..5}
[root@localhost weixindata]# ls
file1  file2  file3  file4  file5  lost+found
[root@localhost weixindata]# 
[root@localhost ~]# lvcreate -L 20M -n  weixin_back -s /dev/weixingroup/weixindata
  Logical volume "weixin_back" created
[root@localhost ~]# 

(2)文件验证

[root@localhost ~]# mount /dev/weixingroup/weixin_back /weixindata/
[root@localhost ~]# ls /weixindata/
file1  file2  file3  file4  file5  lost+found
[root@localhost ~]# 

(3)删除文件,重新快照后仍能看见文件

[root@localhost ~]# ls /weixindata/
file1  file2  file3  file4  file5  lost+found
[root@localhost ~]# rm -fr /weixindata/*
[root@localhost ~]# ls /weixindata/
[root@localhost ~]# 

(4)重新快照

[root@localhost ~]# umount /weixindata/
[root@localhost ~]# lvremove /dev/weixingroup/weixin_back 
Do you really want to remove active logical volume weixin_back? [y/n]: y
  Logical volume "weixin_back" successfully removed
[root@localhost ~]# lvcreate -L 20M -n xing -s /dev/weixingroup/weixindata
  Logical volume "xing" created
[root@localhost ~]# mount /dev/weixingroup/xing /weixindata/
[root@localhost ~]# ls /weixindata/
file1  file2  file3  file4  file5  lost+found
[root@localhost ~]# 

8、LVM排错
11
(1)磁盘在工作时误删磁盘
之前将2移除,现在将2加入

[root@localhost ~]# vgextend weixingroup /dev/vdb2
  Physical volume "/dev/vdb2" successfully created
  Volume group "weixingroup" successfully extended
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize   VFree  
  weixingroup   2   2   1 wz--n- 592.00m 520.00m
[root@localhost ~]# 

(2)删除2
删除2以后再次查看时出错(将分区2删除(d))

[root@localhost ~]# partprobe 
[root@localhost ~]# vgs
  PV VGBvQB-njGH-vJUs-1n1k-RMti-nIrO-AGrk4R not recognised. Is the device missing?
  PV VGBvQB-njGH-vJUs-1n1k-RMti-nIrO-AGrk4R not recognised. Is the device missing?
  VG          #PV #LV #SN Attr   VSize   VFree  
  weixingroup   2   2   1 wz-pn- 592.00m 520.00m

(3)处理错误

vgreduce --removemissing weixingroup

[root@localhost ~]# vgreduce --removemissing  weixingroup 
  PV VGBvQB-njGH-vJUs-1n1k-RMti-nIrO-AGrk4R not recognised. Is the device missing?
  PV VGBvQB-njGH-vJUs-1n1k-RMti-nIrO-AGrk4R not recognised. Is the device missing?
  Wrote out consistent volume group weixingroup
[root@localhost ~]# vgs
  VG          #PV #LV #SN Attr   VSize  VFree 
  weixingroup   1   2   1 wz--n- 96.00m 24.00m
[root@localhost ~]# 

22/配置文件错误

[root@localhost ~]# vim /etc/fstab 
[root@localhost ~]# 
内容写错了
/dev/mappe/disk  /mnt xfs defaults 0 0

重启动
reboot
会提示输入密码
输入密码修改配置文件
/etc/fstab

修改保存后 exit后reboot

9、删除LVM

[root@localhost ~]# lvremove /dev/weixingroup/weixindata 
Do you really want to remove active logical volume weixindata? [y/n]: y
  Logical volume "weixindata" successfully removed
[root@localhost ~]# vgremove weixingroup 
  Volume group "weixingroup" successfully removed
[root@localhost ~]# pvremove /dev/vdb3
  Labels on physical volume "/dev/vdb3" successfully wiped
[root@localhost ~]# pvs
[root@localhost ~]# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0xbcd9cec2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux
/dev/vdb3         2050048     2254847      102400   8e  Linux LVM

Command (m for help): d
Partition number (1,3, default 3): 
Partition 3 is deleted

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partprobe 
[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     512000 vdb1
[root@localhost ~]# 

10、改变pe的值
(1)查看pv (pvdisplay)

[root@localhost ~]# pvcreate /dev/vdb1
WARNING: xfs signature detected on /dev/vdb1 at offset 0. Wipe it? [y/n] y
  Wiping xfs signature on /dev/vdb1.
  Physical volume "/dev/vdb1" successfully created
[root@localhost ~]# pvdisplay
  "/dev/vdb1" is a new physical volume of "500.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               500.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PDBR3c-pBoy-jJNp-uzb4-7fER-ouEZ-wG9LIH

(2)设置pv的值

[root@localhost ~]# vgcreate -s 16M vg0 /dev/vdb1
  Volume group "vg0" successfully created
[root@localhost ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/vdb1
  VG Name               vg0
  PV Size               500.00 MiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               16.00 MiB
  Total PE              31
  Free PE               31
  Allocated PE          0
  PV UUID               PDBR3c-pBoy-jJNp-uzb4-7fER-ouEZ-wG9LIH
   
[root@localhost ~]# 

(3)查看默认pv值

[root@localhost ~]# vgremove vg0 
  Volume group "vg0" successfully removed
[root@localhost ~]# pvdisplay
  "/dev/vdb1" is a new physical volume of "500.00 MiB"
  --- NEW Physical volume ---
  PV Name               /dev/vdb1
  VG Name               
  PV Size               500.00 MiB
  Allocatable           NO
  PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               PDBR3c-pBoy-jJNp-uzb4-7fER-ouEZ-wG9LIH
[root@localhost ~]# vgcreate vg0 /dev/vdb1
Volume group "vg0" successfully created
[root@localhost ~]# pvdisplay
--- Physical volume ---
PV Name               /dev/vdb1
VG Name               vg0
PV Size               500.00 MiB / not usable 4.00 MiB
Allocatable           yes 
PE Size               4.00 MiB   ##默认值为4
Total PE              124
Free PE               124
Allocated PE          0
PV UUID               PDBR3c-pBoy-jJNp-uzb4-7fER-ouEZ-wG9LIH

[root@localhost ~]# 

11、改变分区类型为gpt
(1)改变类型

[root@localhost ~]# parted  /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help     ##查看帮助                                                         
align-check TYPE N                        check partition N for TYPE(min|opt) alignment
help [COMMAND]                           print general help, or help on COMMAND
mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
mkpart PART-TYPE [FS-TYPE] START END     make a partition
name NUMBER NAME                         name partition NUMBER as NAME
print [devices|free|list,all|NUMBER]     display the partition table, available
      devices, free space, all found partitions, or a particular partition
quit                                     exit program
rescue START END                         rescue a lost partition near START and END
rm NUMBER                                delete partition NUMBER
select DEVICE                            choose the device to edit
disk_set FLAG STATE                      change the FLAG on selected device
disk_toggle [FLAG]                       toggle the state of FLAG on selected device
set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
unit UNIT                                set the default unit to UNIT
version                                  display the version number and copyright
      information of GNU Parted
(parted) mklabel                                                          
New disk label type?  (Tab键)                                                    
aix    amiga  bsd    dvh    gpt    loop   mac    msdos  pc98   sun    
New disk label type? gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk
will be lost. Do you want to continue?  ##是否摧毁原磁盘分区数据
Yes/No? yes                                                               
(parted) q                                                                
Information: You may need to update /etc/fstab.

(2)创建新的分区查看类型

[root@localhost ~]# fdisk /dev/vdb                                        
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): n
Partition number (1-128, default 1):  
First sector (34-20971486, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +100M
Created partition 1


Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt ##类型为gpt
 Start          End    Size  Type            Name
 1         2048       206847    100M  Linux filesyste 

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

猜你喜欢

转载自blog.csdn.net/wuludan0217/article/details/84025305
今日推荐