LVM创建及管理

1. LVM创建及管理

安装lvm

1
yum install -y lvm

查看磁盘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@template ~]# fdisk -l

Disk /dev/vda: 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: 0x000beec6

Device Boot Start End Blocks Id System
/dev/vda1 * 2048 2099199 1048576 83 Linux
/dev/vda2 2099200 20971519 9436160 8e Linux LVM

Disk /dev/vdb: 53.7 GB, 53687091200 bytes, 104857600 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 /dev/mapper/centos-root: 8585 MB, 8585740288 bytes, 16769024 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 /dev/mapper/centos-swap: 1073 MB, 1073741824 bytes, 2097152 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

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@template ~]# 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 0xda419f51.

Command (m for help): o
Building a new DOS disklabel with disk identifier 0x01d037f1.

Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p):
Using default response p
Partition number (1-4, default 1):
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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

Calling ioctl() to re-read partition table.
Syncing disks.

将磁盘添加到LVM PV

1
2
[root@template ~]# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.

列出所有PV

1
2
3
4
[root@template ~]# pvscan
PV /dev/vda2 VG centos lvm2 [<9.00 GiB / 0 free]
PV /dev/vdb1 lvm2 [<50.00 GiB]
Total: 2 [<59.00 GiB] / in use: 1 [<9.00 GiB] / in no VG: 1 [<50.00 GiB]

显示pv更多信息

1
2
3
4
5
6
7
8
9
10
11
12
[root@template ~]# pvdisplay /dev/vdb1
"/dev/vdb1" is a new physical volume of "<50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/vdb1
VG Name
PV Size <50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 4kmSEn-kBZG-vMJc-T03x-aWfO-dEes-5prCUL

创建卷组:使用尽可能多的PV来创建VG

1
2
[root@template ~]# vgcreate share /dev/vdb1
Volume group "share" successfully created

列出所有VG

1
2
3
4
[root@template ~]# vgscan
Reading volume groups from cache.
Found volume group "share" using metadata type lvm2
Found volume group "centos" using metadata type lvm2

显示vg更多信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@template ~]# vgdisplay share
--- Volume group ---
VG Name share
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 <50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 0 / 0
Free PE / Size 12799 / <50.00 GiB
VG UUID t9gVEp-02O6-IK40-oNxs-fdFC-nuA3-Y5NgNU

创建逻辑卷:使用VG创建任意数量的LV

1
2
[root@template ~]# lvcreate --size 49G --name vg_data share
Logical volume "vg_data" created.

列出所有LV

1
2
3
4
[root@template ~]# lvscan
ACTIVE '/dev/share/vg_data' [49.00 GiB] inherit
ACTIVE '/dev/centos/swap' [1.00 GiB] inherit
ACTIVE '/dev/centos/root' [<8.00 GiB] inherit

格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@template ~]# mkfs.ext4 /dev/share/vg_data
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
3211264 inodes, 12845056 blocks
642252 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2162163712
392 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, 7962624, 11239424

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

创建需要挂载到的目录

1
[root@template ~]# mkdir -p /data

挂载

1
[root@template ~]# mount /dev/mapper/share-vg_data /data

查看

1
2
3
4
5
6
7
8
9
10
[root@template ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 5.7G 0 5.7G 0% /dev
tmpfs 5.7G 0 5.7G 0% /dev/shm
tmpfs 5.7G 8.6M 5.7G 1% /run
tmpfs 5.7G 0 5.7G 0% /sys/fs/cgroup
/dev/mapper/centos-root 8.0G 1.8G 6.3G 22% /
/dev/vda1 1014M 231M 784M 23% /boot
tmpfs 1.2G 0 1.2G 0% /run/user/0
/dev/mapper/share-vg_data 49G 53M 46G 1% /data

开机自动挂载

1
2
3
[root@template ~]# vim /etc/fstab

/dev/mapper/share-vg_data /data ext4 defaults 0 0

增加100G到/data

1
2
3
4
5
6
7
8
[root@vmhost2 ~]# lvextend --size +500G --resizefs /dev/mapper/VGsdb1T-vm_main
Size of logical volume VGsdb1T/vm_main changed from 400.00 GiB (102400 extents) to 900.00 GiB (230400 extents).
Logical volume VGsdb1T/vm_main successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/VGsdb1T-vm_main is mounted on /data; on-line resizing required
old_desc_blocks = 50, new_desc_blocks = 113
The filesystem on /dev/mapper/VGsdb1T-vm_main is now 235929600 blocks long.

查看

1
df -h

删除卷组VG

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@vmhost2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <9.00g 0
datavg 1 1 0 wz--n- <100.00g 0
share 1 0 0 wz--n- <100.00g <100.00g


[root@vmhost2 ~]# vgremove share
Volume group "share" successfully removed

[root@vmhost2 ~]# vgs
VG #PV #LV #SN Attr VSize VFree
centos 1 2 0 wz--n- <9.00g 0
datavg 1 1 0 wz--n- <100.00g 0

参数详情中文版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[root@VMhost4 yum.repos.d]# fdisk /dev/sdb

欢迎使用 fdisk (util-linux 2.32.1)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。

设备不包含可识别的分区表。
创建了一个磁盘标识符为 0xc4b0420b 的新 DOS 磁盘标签。

命令(输入 m 获取帮助):m

帮助:

DOS (MBR)
a 开关 可启动 标志
b 编辑嵌套的 BSD 磁盘标签
c 开关 dos 兼容性标志

常规
d 删除分区
F 列出未分区的空闲区
l 列出已知分区类型
n 添加新分区
p 打印分区表
t 更改分区类型
v 检查分区表
i 打印某个分区的相关信息

杂项
m 打印此菜单
u 更改 显示/记录 单位
x 更多功能(仅限专业人员)

脚本
I 从 sfdisk 脚本文件加载磁盘布局
O 将磁盘布局转储为 sfdisk 脚本文件

保存并退出
w 将分区表写入磁盘并退出
q 退出而不保存更改

新建空磁盘标签
g 新建一份 GPT 分区表
G 新建一份空 GPT (IRIX) 分区表
o 新建一份的空 DOS 分区表
s 新建一份空 Sun 分区表


Hex 代码(输入 L 列出所有代码):L

0 空 24 NEC DOS 81 Minix / 旧 Linu bf Solaris
1 FAT12 27 隐藏的 NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 隐藏 或 In c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux 扩展 c7 Syrinx
5 扩展 41 PPC PReP Boot 86 NTFS 卷集 da 非文件系统数据
6 FAT16 42 SFS 87 NTFS 卷集 db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux 纯文本 de Dell 工具
8 AIX 4e QNX4.x 第2部分 8e Linux LVM df BootIt
9 AIX 可启动 4f QNX4.x 第3部分 93 Amoeba e1 DOS 访问
a OS/2 启动管理器 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad 休 ea Rufus 对齐
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS fs
f W95 扩展 (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
10 OPUS 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
11 隐藏的 FAT12 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC
12 Compaq 诊断 5c Priam Edisk a9 NetBSD f1 SpeedStor
14 隐藏的 FAT16 <3 61 SpeedStor ab Darwin 启动 f4 SpeedStor
16 隐藏的 FAT16 63 GNU HURD 或 Sys af HFS / HFS+ f2 DOS 次要
17 隐藏的 HPFS/NTF 64 Novell Netware b7 BSDI fs fb VMware VMFS
18 AST 智能睡眠 65 Novell Netware b8 BSDI swap fc VMware VMKCORE
1b 隐藏的 W95 FAT3 70 DiskSecure 多启 bb Boot Wizard 隐 fd Linux raid 自动
1c 隐藏的 W95 FAT3 75 PC/IX bc Acronis FAT32 L fe LANstep
1e 隐藏的 W95 FAT1 80 旧 Minix be Solaris 启动 ff BBT
-------------本文结束感谢您的阅读-------------