【Linux系列教程】创建逻辑卷

一、添加2块硬盘

lv1

二、基本命令

1.PV物理卷

创建物理卷

# pvcreate 磁盘或者分区

查看物理卷

# pvscan

2.VG卷组

创建卷组

# vgcreate 卷组名 绑定硬盘或者分区

查看卷组

# vgscan
# vgdisplay 卷组名

3.LV逻辑卷

创建逻辑卷

# lvcreate -L 容量 -n 逻辑卷名 卷组名

列:# lvcreate -L +20G -n mysql db

查看逻辑卷

# lvscan

三、案列

/dev/db/mysql 30G ext4 /mysql

创建卷组db,创建逻辑卷mysql,分配30G的空间,格式化成ext4文件系统,并且把这个逻辑卷绑定到/mysql

1.添加2块硬盘省略(跳转第一步)

2.创建一个PV物理卷

[root@localhost ~]# lsblk	#查看系统硬盘情况
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0  476M  0 part /boot
└─sda2        8:2    0 19.5G  0 part 
  ├─centos-root
            253:0    0 17.5G  0 lvm  /
  └─centos-swap
            253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 	#发现我们刚刚添加的2块硬盘
sdc           8:32   0   20G  0 disk
[root@localhost ~]# pvcreate /dev/sdb /dev/sdc	#先把sdb和sdc创建一个PV物理卷
  Physical volume "/dev/sdb" successfully created.
  Physical volume "/dev/sdc" successfully created.
  
[root@localhost ~]# pvscan	#查看物理卷
  PV /dev/sda2   VG centos          lvm2 [19.53 GiB / 0    free]
  PV /dev/sdb                       lvm2 [20.00 GiB]
  PV /dev/sdc                       lvm2 [20.00 GiB]
  Total: 3 [59.53 GiB] / in use: 1 [19.53 GiB] / in no VG: 2 [40.00 GiB]

3.创建一个VG卷组

[root@localhost ~]# vgcreate db /dev/sdb /dev/sdc
  Volume group "db" successfully created
[root@localhost ~]# vgdisplay db	#查看db卷组
  --- Volume group ---
  VG Name               db
  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               39.99 GiB
  PE Size               4.00 MiB
  Total PE              10238
  Alloc PE / Size       0 / 0   
  Free  PE / Size       10238 / 39.99 GiB
  VG UUID               Wvv01t-agHC-gEa3-kV7Y-DEND-9IFt-gegH2K

4.创卷LV逻辑卷

[root@localhost ~]# lvcreate -L +30G -n mysql db	#创建一个名为mysql的30G大小的逻辑卷,卷组为db
  Logical volume "mysql" created.
[root@localhost ~]# lvscan
  ACTIVE            '/dev/db/mysql' [30.00 GiB] inherit
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [17.53 GiB] inherit

5.绑定mysql

[root@localhost ~]# mkdir /mysql	#先创建文件夹

[root@localhost ~]# mkfs -t ext4 /dev/db/mysql	#格式化刚刚创建的逻辑卷为ext4文件系统
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1966080 inodes, 7864320 blocks
393216 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2155872256
240 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000

Allocating group tables: 完成                            
正在写入inode表: 完成                            
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# vim /etc/fstab	#编辑配置文件,配置永久挂载

/dev/db/mysql   /mysql  ext4    defaults        0 0

[root@localhost ~]# mount -a

6验证是否成功挂载

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0  476M  0 part /boot
└─sda2        8:2    0 19.5G  0 part 
  ├─centos-root
            253:0    0 17.5G  0 lvm  /
  └─centos-swap
            253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
└─db-mysql  253:2    0   30G  0 lvm  /mysql
sdc           8:32   0   20G  0 disk 
└─db-mysql  253:2    0   30G  0 lvm  /mysql

[root@localhost ~]# df -hT | grep "mysql"
/dev/mapper/db-mysql    ext4       30G   45M   28G    1% /mysql

四、指定PE的方式创建逻辑卷

/dev/db/redis xfs /redis 800M

在db卷组下创建一个名为redis的逻辑卷,大小800M,文件系统为xfs,并且挂载到/redis

1.创建LV逻辑卷

因为我们上一题已经创建了一个名为db的卷组,所以直接用就可以了

  • -l 指定PE参数
[root@localhost ~]# lvcreate -l 200 -n redis db	
  Logical volume "redis" created.
#一个PE大小为4M,因为我们要创建800M的数据卷,所以上面的参数小l后面的数字为200
[root@localhost ~]# lvscan
  ACTIVE            '/dev/db/mysql' [30.00 GiB] inherit
  ACTIVE            '/dev/db/redis' [800.00 MiB] inherit
  ACTIVE            '/dev/centos/swap' [2.00 GiB] inherit
  ACTIVE            '/dev/centos/root' [17.53 GiB] inherit

2.格式化xfs

[root@localhost ~]# mkfs -t xfs /dev/db/redis
meta-data=/dev/db/redis          isize=512    agcount=4, agsize=51200 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0, sparse=0
data     =                       bsize=4096   blocks=204800, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=855, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

3.配置永久挂载

[root@localhost ~]# mkdir /redis
vim /etc/fstab

/dev/db/redis   /redis  xfs     defaults        0 0

[root@localhost ~]# mount -a

4.验证结果

[root@localhost ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0  476M  0 part /boot
└─sda2        8:2    0 19.5G  0 part 
  ├─centos-root
            253:0    0 17.5G  0 lvm  /
  └─centos-swap
            253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
└─db-mysql  253:2    0   30G  0 lvm  /mysql
sdc           8:32   0   20G  0 disk 
├─db-mysql  253:2    0   30G  0 lvm  /mysql
└─db-redis  253:3    0  800M  0 lvm  /redis
[root@localhost ~]# df -hT | grep "redis"
/dev/mapper/db-redis    xfs       797M   33M  765M    5% /redis