lvm详解

假设此次添加的磁盘名称为sdb,并将此磁盘的所有空间都挂载到/data/下
1、	lsblk观察本机是否添加了空磁盘(因为空磁盘才能够进行分区和格式化,分区和格式化只能在空磁盘上进行,千万别弄错了,否则系统就挂掉,基本无法修复了);
2、	空白磁盘分区:fdisk /dev/sdb,分区完后lsblk看看是否出现了sdb1,如未出现,则执行partprobe刷新分区;
3、	创建物理卷PV:pvcreate /dev/sdb1,pvdisplay查看物理卷;
4、	创建卷组VG:vgcreate data_vg /dev/sdb1,vgdisplay查看卷组,找到自己创建的data_vg,并记住Total PE参数的数字;
5、	创建逻辑卷LV:lvcreate -l 262143 -n data_lv data_vg(262143是上面所说的Total PE参数的数字),lvdisplay查看逻辑卷;
6、	格式化:先df –Th查看本机的其他盘是什么文件格式(需格式化成跟/home、/boot等目录一样的格式)mkfs.xfs /dev/data_vg/data_lv
7、	挂载并设置开机自启:mount /dev/data_vg/data_lv /data,mount –a
vi /etc/fstab
/dev/mapper/data_vg-data_lv    /data            xfs     defaults        0 0

# 第一步: 添加新的硬盘。
# 第二步: 给某一磁盘进行分区
fdisk /dev/vdb
[root@test-node2 bin]# ll /dev/vdb*
brw-rw---- 1 root disk 253, 16 Apr 28 15:33 /dev/vdb
brw-rw---- 1 root disk 253, 17 Apr 28 15:58 /dev/vdb1

# 第三步: 对磁盘分区创建物理卷pv
[root@machine-for-class ~]# pvcreate /dev/sdb1 /dev/sdb1
Physical volume "/dev/sdb1" successfully created.
# 第四步: 创建卷组vg,并将物理卷加入卷组
[root@machine-for-class ~]# vgcreate vg01 /dev/sdb1
#查看vg
[root@machine-for-class ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <19.51g 0
vg01 2 0 0 wz--n- 96.00m 96.00m
[root@machine-for-class ~]# vgdisplay
--- Volume group ---
VG Name vg01
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 96.00 MiB
PE Size 4.00 MiB
Total PE 24
Alloc PE / Size 0 / 0
Free PE / Size 24 / 96.00 MiB
VG UUID sEwqUc-wJvm-aWbt-KQpE-wkA1-mg6q-4GXJns
# 第五步: 创建逻辑卷lv,将卷组所有空间给逻辑卷
[root@machine-for-class ~]# lvcreate -n lv01 -L +96M vg01    	建议这样写:lvcreate -n lv01 -l +100%FREE  vg01
#lvcreate -n 设置LV名称
#lvcreate -L 设置LV大小(以字节为单位)
#lvcreate -l 设置LV大小(以区块数为单位)以及托管此逻辑卷的卷组的名称
WARNING: xfs signature detected on /dev/locvg/lvgfs at offset 0. Wipe it? [y/n]: y
  Wiping xfs signature on /dev/locvg/lvgfs.
  Logical volume "lvgfs" created.
[root@test-node2 data]# lvs
  LV    VG    Attr       LSize    Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lvgfs locvg -wi-a----- <100.00g    
  


# 第六步: 格式化-创建文件系统,挂载至 /data 目录,如果创建逻辑卷的时候制定了lv名称。就用对应的目录。如果没有指定。默认是mapper目录
[root@machine-for-class ~]# mkfs.xfs /dev/vg01/lv01
# mkfs.xfs /dev/mapper/locvg-lvgfs
meta-data=/dev/vg01/lv01 isize=512 agcount=4, agsize=6144 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=24576, imaxpct=25

meta-data=/dev/mapper/locvg-lvgfs isize=512    agcount=4, agsize=6553344 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=26213376, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=12799, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
#挂载到/data目录。注意:如果data目录下面有数据,请先断开对应服务。在mv移走data下面所有的数据。然后挂载lvm。挂载完再mv回来。重启服务。
[root@test-node2 data]# mkdir /data
[root@test-node2 data]# mount /dev/mapper/locvg-lvgfs /data
[root@test-node2 data]# echo /dev/mapper/locvg-lvgfs /data xfs defaults 0 0 >> /etc/fstab
[root@test-node2 data]# df -h



#案例2

#查看挂载情况
[root@flyby-mongo ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        7.8G     0  7.8G   0% /dev
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           7.8G  8.6M  7.8G   1% /run
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1        40G  2.4G   35G   7% /
tmpfs           1.6G     0  1.6G   0% /run/user/0

#查看磁盘
[root@flyby-mongo ~]# fdisk -l

Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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: 0x000a0687

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83886079    41942016   83  Linux

Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 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


#磁盘分区
[root@flyby-mongo ~]# 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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x45543ac1.

Command (m for help): t
No partition is defined yet!

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-419430399, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): 
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set

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

Calling ioctl() to re-read partition table.
Syncing disks.
       
[root@flyby-mongo ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0   40G  0 disk 
└─vda1 253:1    0   40G  0 part /
vdb    253:16   0  200G  0 disk 
└─vdb1 253:17   0  200G  0 part 


[root@flyby-mongo ~]# mkfs -t xfs /dev/vdb1
meta-data=/dev/vdb1              isize=512    agcount=4, agsize=13107136 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=52428544, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=25599, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@flyby-mongo ~]# yum install -y lvm2

#对磁盘分区创建物理卷pv
[root@flyby-mongo ~]# 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.
  
#创建卷组vg,并将物理卷加入卷组
[root@flyby-mongo ~]# vgcreate vg01 /dev/vdb1
  Volume group "vg01" successfully created
  
#查看vg
[root@flyby-mongo ~]# vgs
  VG   #PV #LV #SN Attr   VSize    VFree   
  vg01   1   0   0 wz--n- <200.00g <200.00g
[root@flyby-mongo ~]# vgdisplay
  --- Volume group ---
  VG Name               vg01
  System ID             
  Format                lvm2
  Metadata Areas        1
  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                1
  Act PV                1
  VG Size               <200.00 GiB
  PE Size               4.00 MiB
  Total PE              51199
  Alloc PE / Size       0 / 0   
  Free  PE / Size       51199 / <200.00 GiB
  VG UUID               WfpvBf-x3SY-HA0j-iKyS-EmmD-07Jb-7SUFid

#创建逻辑卷lv,将卷组所有空间给逻辑卷
[root@flyby-mongo ~]# lvcreate -n lv01 -l +100%FREE  vg01
  Logical volume "lv01" created.
  
#格式化-创建文件系统,挂载至 /data 目录,如果创建逻辑卷的时候制定了lv名称。就用对应的目录。如果没有指定。默认是mapper目录
[root@flyby-mongo ~]# mkdir /data
[root@flyby-mongo ~]# mkfs.xfs /dev/vg01/lv01 
meta-data=/dev/vg01/lv01         isize=512    agcount=4, agsize=13106944 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=52427776, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=25599, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

#挂载到/data目录。注意:如果data目录下面有数据,请先断开对应服务。在mv移走data下面所有的数据。然后挂载lvm。挂载完再mv回来。重启服务。
[root@flyby-mongo ~]# mount /dev/mapper/vg01-lv01  /data
[root@flyby-mongo ~]# echo /dev/mapper/vg01-lv01  /data xfs defaults 0 0 >> /etc/fstab
[root@flyby-mongo ~]# df -h
Filesystem             Size  Used Avail Use% Mounted on
devtmpfs               7.8G     0  7.8G   0% /dev
tmpfs                  7.8G     0  7.8G   0% /dev/shm
tmpfs                  7.8G  8.6M  7.8G   1% /run
tmpfs                  7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1               40G  2.6G   35G   7% /
tmpfs                  1.6G     0  1.6G   0% /run/user/0
/dev/mapper/vg01-lv01  200G   33M  200G   1% /data

posted @ 2023-06-12 10:53  安生丶  阅读(58)  评论(0编辑  收藏  举报