lvm基本概念

物理存储设备(Physical Media):指系统的存储设备文件,比如 /dev/sda、/dev/sdb 等。
PV(物理卷 Physical Volume):指硬盘分区或者从逻辑上看起来和硬盘分区类似的设备(比如 RAID 设备)。
VG(卷组 Volume Group):类似于非 LVM 系统中的物理硬盘,一个 LVM 卷组由一个或者多个 PV(物理卷)组成。
LV(逻辑卷 Logical Volume):类似于非 LVM 系统上的磁盘分区,LV 建立在 VG 上,可以在 LV 上建立文件系统。

lvm优缺点

LVM 的优点如下:
文件系统可以跨多个磁盘,因此大小不再受物理磁盘的限制。
可以在系统运行状态下动态地扩展文件系统大小。
可以以镜像的方式冗余重要数据到多个物理磁盘上。
可以很方便地导出整个卷组,并导入到另外一台机器上。
LVM 也有一些缺点:
在从卷组中移除一个磁盘的时候必须使用 reducevg 命令(这个命令要求root权限,并且不允许在快照卷组中使用)。
当卷组中的一个磁盘损坏时,整个卷组都会受影响。
因为增加了一个逻辑层,存储的性能会受影响。

安装lvm工具

1
yum -y install lvm2

操作指令

1
2
3
4
5
6
7
8
9
10
11
12
13
创建物理卷 :pvcreate 设备名1  {设备名2 ... ...}
创建卷组 :vgcreate 卷组名 物理卷名1 物理卷名2
创建逻辑卷 :lvcreate -L 容量大小 -n 逻辑卷名 卷组名
删除逻辑卷:lvremove 逻辑卷的绝对路径
删除卷组:vgremove 卷组名
删除物理卷:pvremove 物理卷设备名
减少卷组大小 :vgreduce 卷组名 物理卷设备名
增加卷组大小 :vgextend 卷组名 物理卷设备名
扩展逻辑卷大小 :lvextend -L +大小 /dev/卷组名/逻辑卷名
减少逻辑卷大小 :lvreduce -L -大小 /dev/卷组名/逻辑卷名
显示物理卷:pvdisplay 物理卷设备名
显示卷组:vgdisplay 卷组名
显示逻辑卷:lvdisplay 逻辑卷名。

磁盘配额

Linux磁盘限额的特点:

作用范围:针对指定的文件系统(分区)
限制对象:用户账号、组账号
限制类型:磁盘容量、文件数量
限制方法:软限制、硬限制

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
1、挂载一块硬盘:(120G)
创建分区:
[root@localhost 桌面]# fdisk –l //查看当前挂载的硬盘情况
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00023cf9

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 2576 20480000 83 Linux
/dev/sda3 2576 5125 20480000 83 Linux
/dev/sda4 5125 13055 63691776 5 Extended
/dev/sda5 5126 7675 20480000 83 Linux
/dev/sda6 7675 7936 2097152 82 Linux swap / Solaris
/dev/sda7 7937 13055 41111552 83 Linux

Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

[root@localhost 桌面]# fdisk /dev/sdb //进入交互式界面
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x16d0d17d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-15665, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-15665, default 15665): +20G

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (2613-15665, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-15665, default 15665): +20G

Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (5225-15665, default 5225):
Using default value 5225
Last cylinder, +cylinders or +size{K,M,G} (5225-15665, default 15665): +20G

Command (m for help): p

Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x16d0d17d

Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 83 Linux
/dev/sdb2 2613 5224 20980890 83 Linux
/dev/sdb3 5225 7836 20980890 83 Linux

2、改8e(逻辑卷类型)
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)

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

Command (m for help): p

Disk /dev/sdb: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x16d0d17d

Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 8e Linux LVM
/dev/sdb2 2613 5224 20980890 8e Linux LVM
/dev/sdb3 5225 7836 20980890 8e Linux LVM

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

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

3、创建物理卷:
[root@www ~]# pvcreate /dev/sdb{1,2,3}
Physical volume "/dev/sdb1" successfully created
Physical volume "/dev/sdb2" successfully created
Physical volume "/dev/sdb3" successfully created
[root@www ~]# pvdisplay (查看)
"/dev/sdb1" is a new physical volume of "20.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 20.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ISJqbg-Bbsn-SmaH-96dq-YRLf-KvZ1-aGC8PB

"/dev/sdb2" is a new physical volume of "20.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb2
VG Name
PV Size 20.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID PXryjG-r2gN-d6vY-T0yp-kAGo-ywOo-W7upqv

"/dev/sdb3" is a new physical volume of "20.01 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 20.01 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID OjRATX-tGQJ-rrei-bCFu-HAmf-XdrS-5OcrdU

4、创建卷组:
[root@www ~]# vgcreate juanzu /dev/sdb1 /dev/sdb2 /dev/sdb3
//创建了一个叫juanzu的卷组
Volume group "juanzu" successfully created

[root@www ~]# vgdisplay (查看卷组)
--- Volume group ---
VG Name juanzu
System ID
Format lvm2
Metadata Areas 3
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 3
Act PV 3
VG Size 60.02 GiB
PE Size 4.00 MiB
Total PE 15366
Alloc PE / Size 0 / 0
Free PE / Size 15366 / 60.02 GiB
VG UUID wM41Ud-Nqq6-LaI4-11lG-9P7h-AClr-F9GfU1

5、创建逻辑卷:
[root@www ~]# lvcreate -L 30G -n luojijuan juanzu
//从卷组sb中划出30G创建了一个逻辑卷luojijuan
Logical volume "luojijuan" created
[root@www ~]# lvdisplay
--- Logical volume ---
LV Path /dev/juanzu/luojijuan
LV Name luojijuan
VG Name juanzu
LV UUID 7zXdd7-1xFz-Uq82-g3mB-MyfW-0uf3-97SCag
LV Write Access read/write
LV Creation host, time www, 2016-10-18 21:41:17 +0800
LV Status available
# open 0
LV Size 30.00 GiB
Current LE 7680
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

[root@www ~]# lvdisplay /dev/juanzu/luojijuan (单独看一个逻辑卷)
--- Logical volume ---
LV Path /dev/juanzu/luojijuan
LV Name luojijuan
VG Name juanzu
LV UUID 7zXdd7-1xFz-Uq82-g3mB-MyfW-0uf3-97SCag
LV Write Access read/write
LV Creation host, time www, 2016-10-18 21:41:17 +0800
LV Status available
# open 0
LV Size 30.00 GiB
Current LE 7680
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

6、给逻辑卷扩容
[root@www ~]# lvextend -L +10G /dev/juanzu/luojijuan
//给逻辑卷luojijuan扩容10G,由30G扩充到了40G
Extending logical volume s11 to 40.00 GiB
Logical volume s11 successfully resized
[root@www ~]# lvdisplay /dev/juanzu/luojijuan
--- Logical volume ---
LV Path /dev/juanzu/luojijuan
LV Name luojijuan
VG Name juanzu
LV UUID 7zXdd7-1xFz-Uq82-g3mB-MyfW-0uf3-97SCag
LV Write Access read/write
LV Creation host, time www, 2016-10-18 21:41:17 +0800
LV Status available
# open 0
LV Size 40.00 GiB
Current LE 10240
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0

7、格式化
[root@www ~]# mkfs.ext4 /dev/juanzu/luojijuan
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10485760 blocks
524288 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=4294967296
320 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

正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

8、挂载
[root@www ~]# mkdir /aaa 创建/aaa目录
[root@www ~]# mount /dev/juanzu/luojijuan /aaa 挂载
[root@www ~]# resize2fs /dev/juanzu/luojijuan 操作系统重新识别逻辑卷大小
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 10485760 blocks long. Nothing to do!

9、验证:逻辑卷挂载成功
[root@www ~]# mount
/dev/sda7 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda2 on /home type ext4 (rw)
/dev/sda3 on /tmp type ext4 (rw)
/dev/sda5 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/mapper/juanzu-luojijuan on /aaa type ext4 (rw)

10、重新挂载并添加配额功能:用户配额、组配额
[root@www ~]# mount -o remount,usrquota,grpquota /dev/juanzu/luojijuan /aaa
//重新挂载,并且为用户和组添加磁盘配额属性
[root@www ~]# mount
/dev/sda7 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda2 on /home type ext4 (rw)
/dev/sda3 on /tmp type ext4 (rw)
/dev/sda5 on /usr type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/mapper/juanzu-luojijuan on /aaa type ext4 (rw,usrquota,grpquota)

11、给aaa设置最大权限,方便后面写入(测试用,允许用户写入数据)
[root@www ~]# chmod 777 /aaa
[root@www ~]# vim /etc/fstab(在文件中添加如下内容)
/dev/juanzu/luojijuan /aaa ext4 defaults,usrquota,grpquota 0 0
[root@localhost ~]# setenforce 0{关闭SELinux进程(沙盒),系统的安全守护进程}
[root@www ~]# ll /aaa(确认里面没有配置文件)

12、检测磁盘配额设置并生成配额文件:
[root@www ~]# quotacheck -augcv
quotacheck: Your kernel probably supports journaled quota but you are not using it. Consider switching to journaled quota to avoid running quotacheck after an unclean shutdown.
quotacheck: Scanning /dev/mapper/sb-s11 [/aaa] done
quotacheck: Cannot stat old user quota file /aaa/aquota.user: 没有那个文件或目录. Usage will not be substracted.
quotacheck: Cannot stat old group quota file /aaa/aquota.group: 没有那个文件或目录. Usage will not be substracted.
quotacheck: Cannot stat old user quota file /aaa/aquota.user: 没有那个文件或目录. Usage will not be substracted.
quotacheck: Cannot stat old group quota file /aaa/aquota.group: 没有那个文件或目录. Usage will not be substracted.
quotacheck: Checked 2 directories and 0 files
quotacheck: Old file not found.
quotacheck: Old file not found.

13、已经生成配额文件
[root@www ~]# ll /aaa
总用量 32
-rw-------. 1 root root 6144 10月 18 22:46 aquota.group
-rw-------. 1 root root 6144 10月 18 22:46 aquota.user
drwx------. 2 root root 16384 10月 18 22:25 lost+found

14、创建测试用户,并为用户设置磁盘配额(限制为可以创建3个文件,3个以上则不允许创建)
[root@www ~]# useradd zhangsan
[root@www ~]# edquota -u zhangsan
Disk quotas for user zhangsan (uid 501):
Filesystem blocks soft hard inodes soft hard
/dev/mapper/juanzu-luojijuan 0 0 0 0 0 3

15、启用磁盘配额:
[root@www ~]# quotaon -ugv /aaa
/dev/mapper/sb-s11 [/aaa]: group quotas turned on
/dev/mapper/sb-s11 [/aaa]: user quotas turned on

16、验证:
[root@www ~]# su zhangsan 切换到测试用户zhangsan
[zhangsan@www root]$ cd /aaa 切换到逻辑卷挂载目录
[zhangsan@www aaa]$ touch 1.txt
[zhangsan@www aaa]$ touch 2.txt
[zhangsan@www aaa]$ touch 3.txt
[zhangsan@www aaa]$ touch 4.txt 证明超出磁盘配额,无法创建第4个文件
dm-0: write failed, user file limit reached.
touch: 无法创建"4.txt": 超出磁盘限额