Linux: LVM 创建

关于Linux 的逻辑卷知识,以前前接触过一些,今天再次温习下,主要是演练下 LVM 的创建过程及维护命令,LVM 理论部分和虚拟机增加硬盘部分略。

1 环境准备
虚拟机:Red Hat Enterprise Linux Server release 5.5
增加两块 IDE 硬盘,一块为 2GB,另一块为 1GB。

2查看新增硬盘情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Disk /dev/hda: 19.3 GB, 19327352832 bytes  
255 heads, 63 sectors/track, 2349 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/hda1 * 1 13 104391 83 Linux
/dev/hda2 14 144 1052257+ 82 Linux swap / Solaris
/dev/hda3 145 2349 17711662+ 83 Linux
Disk /dev/hdb: 2147 MB, 2147483648 bytes
16 heads, 63 sectors/track, 4161 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 1985 1000408+ 83 Linux
/dev/hdb2 1986 4161 1096704 83 Linux
Disk /dev/hdd: 1073 MB, 1073741824 bytes
16 heads, 63 sectors/track, 2080 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Device Boot Start End Blocks Id System
/dev/hdd1 1 2080 1048288+ 83 Linux

备注: /dev/hdb, /dev/hdd 就是新增的盘, /dev/hdb 为 2147 MB,/dev/hdd 为 1073 MB。

3 创建PV ( pvcreat )

1
2
3
4
5
6
7
[root@pgb ~]# pvcreate /dev/hdb1 /dev/hdd1  
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Physical volume "/dev/hdb1" successfully created
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Physical volume "/dev/hdd1" successfully created

备注:在创建 VG 之前,首先要将物理设备标识成 LVM 可识别的物理设备,另外 pvcreate 命令会清除设备上的所有数据。

4 查看 PV 信息

1
2
3
4
[root@pgb ~]# pvscan  
PV /dev/hdb1 lvm2 [976.96 MB]
PV /dev/hdd1 lvm2 [1023.72 MB]
Total: 2 [1.95 GB] / in use: 0 [0 ] / in no VG: 2 [1.95 GB]

备注:上面显示 /dev/hdb1, /dev/hdd1 已经创建成 PV 了。

5 创建VG ( vgcreate )

1
2
3
4
5
6
7
8
9
[root@pgb ~]# vgcreate vg01_pgdata /dev/hdb1 /dev/hdd1  
/dev/cdrom: open failed: Read-only file system
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
/dev/cdrom: open failed: Read-only file system
Attempt to close device '/dev/cdrom' which is not open.
Volume group "vg01_pgdata" successfully created

6 显示 VG 信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@pgb ~]# vgdisplay  
--- Volume group ---
VG Name vg01_pgdata
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 1.95 GB PE Size 4.00 MB
Total PE 499
Alloc PE / Size 0 / 0
Free PE / Size 499 / 1.95 GB
VG UUID B5pg8R-2AGm-6DEp-K7HK-TI1I-HC3h-gWx32m

备注: 逻辑卷 vg01_pgdata 创建成功,大小为 1.95 GB。

7 创建LV

1
2
[root@pgb lvm]# lvcreate -L 1G -n lv_pgdata1 vg01_pgdata  
Logical volume "lv_pgdata1" created

8 扫描 lv 逻辑单元

1
2
[root@pgb lvm]# lvscan  
ACTIVE '/dev/vg01_pgdata/lv_pgdata1' [1.00 GB] inherit

9 查看LV信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@pgb lvm]# lvdisplay /dev/vg01_pgdata/lv_pgdata1  
--- Logical volume ---
LV Name /dev/vg01_pgdata/lv_pgdata1
VG Name vg01_pgdata
LV UUID pXsfbX-MAdM-6FQ5-cUz6-IKXC-hRJM-sNQ9yp
LV Write Access read/write
LV Status available
# open 0
LV Size 1.00 GB
Current LE 256
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

备注:LV创建成功,大小为 1 GB,接下来可以格式化并挂载 LV 了。

10 格式化文件系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@pgb lvm]# mkfs -t ext3 -c /dev/vg01_pgdata/lv_pgdata1  
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
131072 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Checking for bad blocks (read-only test): done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

11 目录挂载

1
2
3
4
5
6
7
8
9
10
11
[root@pgb lvm]# mkdir -p /database/pgdata1
[root@pgb lvm]# mount -t ext3 /dev/vg01_pgdata/lv_pgdata1 /database/pgdata1
[root@pgb lvm]# chown -R postgres:postgres /database/pgdata1
[root@pgb lvm]# df -hv
Filesystem Size Used Avail Use% Mounted on
/dev/hda3 17G 9.8G 5.8G 64% /
/dev/hda1 99M 18M 76M 20% /boot
tmpfs 217M 0 217M 0% /dev/shm
none 217M 104K 217M 1% /var/lib/xenstored
/dev/mapper/vg01_pgdata-lv_pgdata1
1008M 34M 924M 4% /database/pgdata1

备注:到了这步, LVM 配置成功,目录挂载成功。

12 设置开机自动挂载
修改文件 /etc/fstab,增加以下行

1
/dev/vg01_pgdata/lv_pgdata1 /database/pgdata1 ext3 defaults 0 0

13 再次查看 VG

[root@pgb lvm]# vgdisplay  
 --- Volume group ---  
 VG Name vg01_pgdata  
 System ID  
 Format lvm2  
 Metadata Areas 2  
 Metadata Sequence No 2  
 VG Access read/write  
 VG Status resizable  
 MAX LV 0  
 Cur LV 1  
 Open LV 1  
 Max PV 0  
 Cur PV 2  
 Act PV 2  
 VG Size 1.95 GB  
 PE Size 4.00 MB  
 Total PE 499  
 Alloc PE / Size 256 / 1.00 GB  
 Free PE / Size 243 / 972.00 MB VG UUID B5pg8R-2AGm-6DEp-K7HK-TI1I-HC3h-gWx32m  

备注:分配了(Alloc PE) 1GB,还剩余 (Free PE) 972 MB。

14 常见 LVM 命令

功能 PV VG LV
搜索 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
列出 pvdisplay vgdisplay lvdisplay
增加 vgextend lvextend
减少 vgreduce lvreduce
删除 vgremove lvremove

最后推荐和张文升共同编写的《PostgreSQL实战》,本书基于PostgreSQL 10 编写,共18章,重点介绍SQL高级特性、并行查询、分区表、物理复制、逻辑复制、备份恢复、高可用、性能优化、PostGIS等,涵盖大量实战用例!

购买链接:https://item.jd.com/12405774.html

PostgreSQL实战
感谢支持!
0%