现在的位置: 首页 > 综合 > 正文

LVM详细操作说明

2017年10月28日 ⁄ 综合 ⁄ 共 20533字 ⁄ 字号 评论关闭

一、需求:
1、添加一个20G硬盘,并初始分区为如下:
/mysql 6G
/appstore 6G
/infoware 8G

2、全部使用LVM分区,并测试调整分区,比如将/mysql 扩大成8G,
(1)测试从相邻的/appstore中分割,(2)从/infoware中分割
注意:从后面的操作中可以看得到,最好不要缩小空间,否则可能会导致数据损坏,宁愿增加硬盘,本文作为测试,有详细的步骤可以说明为何不要采用减少某个分区的空间来增加给其他分区

3. 基本原理 (这些网络上有很多介绍了)

物理介质 The physical media
你应该感受“物理”这个单词有极大的延伸,虽然我们刚开始假设它仅仅是一个硬盘,或者一个分区。例如,/dev/hda,/dev/hda6,/dev/sda。你可以转换一个块设备上任何连续块到一个。。。
 
物理卷 Physical Volume (PV)
一个PV只不过是有许多管理数据添加在它里面的物理介质–一旦你添加了它,LVM就认为它是。。。的一个持有者。
 
物理分区 Physical Extents (PE)
物理分区真的象一些大的数据块,通常有几MB。PE可以分配给一个。。。
 
卷组 Volume Group (VG)
一个VG是由许多物理分区组成的(可能来自多个物理卷或硬盘)。虽然这可能容易让你认为一个VG就是由几个硬盘组成的(例如/dev/hda和/dev/sda),但是更确切的说,它包含由这些硬盘提供的许多PE。
 
>;从这个卷组,PE可以分配给一个。。。
 
逻辑卷 Logical Volume (LV)
Yes,我们最终到达某处。一个逻辑卷是我们工作的最终结果,这里是我们存储信息的地方。这等同于传统分区的想法。
象用通常的分区一样,在逻辑卷上你能代表性的创建一个。。。
 
文件系统 Filesystem
文件系统是你想它成为的形态:标准的 ext2,ReiserFS,NWFS,XFS,JFX,NTFS,等等。。。对Linux内核来说,在通常的分区和逻辑卷之间没有差别。
我试着画了一个ASCII图来使这些形象化。

一个物理卷,包含了许多物理分区:

+—–[ Physical Volume ]—–+
| PE | PE | PE | PE | PE | PE |
+—————————–+

一个卷组,包含了2个物理卷(PV)有6个物理分区:

+——[ Volume Group ]————-+
| +–[PV]——–+ +–[PV]——–+ |
| | PE | PE | PE | | PE | PE | PE | |
| +————–+ +————–+ |
+———————————–+

我们现在做更进一步扩展:

+——[ Volume Group ]————-+
| +–[PV]——–+ +–[PV]——–+ |
| | PE | PE | PE | | PE | PE | PE | |
| +–+—+—+—+ +-+—-+—-+–+ |
| | | | +—–/ | | |
| | | | | | | |
| +–+—+—+-+ +——+—-+–+ |
| | Logical | | Logical | |
| | Volume | | Volume | |
| | | | | |
| | /home | | /var | |
| +————+ +————–+ |
+———————————–+

这个向我们展示了两个文件系统,它跨越两个磁盘。/home文件系统包含4个物理分区,/var文件系统包含2个。

二、操作步骤:
1、分区(其实这一步可以省略,原因后面会分析到)

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
[root@test ~]# fdisk /dev/hdb
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-39560, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-39560, default 39560): +6000M
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  83  Linux
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (11628-39560, default 11628):       
Using default value 11628
Last cylinder or +size or +sizeM or +sizeK (11628-39560, default 39560): +6000M
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  83  Linux
/dev/hdb2           11628       23254     5860008   83  Linux
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (23255-39560, default 23255): 
Using default value 23255
Last cylinder or +size or +sizeM or +sizeK (23255-39560, default 39560):  ###直接回车,使用所有剩余的空间
Using default value 39560
 
Command (m for help): w  ##保存操作
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

##查看

1
2
3
4
5
6
7
8
9
10
11
12
[root@test ~]# fdisk -l
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  83  Linux
/dev/hdb2           11628       23254     5860008   83  Linux
/dev/hdb3           23255       39560     8218224   83  Linux

2、将/dev/hdb 上面的三个分区设置成LVM

1)首先设置/dev/hdb1和/dev/hdb2和/dev/hdb3的分区类型为0×8e,它表示”Linux LVM”。请注意你的fdisk的版本可能仍不认识这种类型,因此它显示为“Unknown”:

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
[root@test ~]# fdisk /dev/hdb
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  83  Linux
/dev/hdb2           11628       23254     5860008   83  Linux
/dev/hdb3           23255       39560     8218224   83  Linux
 
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): L ## 列出所有的格式,可以看到LVM是8e
 
 0  Empty           1e  Hidden W95 FAT1 80  Old Minix       bf  Solaris        
 1  FAT12           24  NEC DOS         81  Minix / old Lin c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          82  Linux swap / So c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  83  Linux           c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     84  OS/2 hidden C:  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   85  Linux extended  da  Non-FS data    
 6  FAT16           42  SFS             86  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4d  QNX4.x          87  NTFS volume set de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 88  Linux plaintext df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM       e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi ee  EFI GPT        
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a5  FreeBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a6  OpenBSD         f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       a9  NetBSD          f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  8e  Linux LVM
/dev/hdb2           11628       23254     5860008   83  Linux
/dev/hdb3           23255       39560     8218224   83  Linux
 
## /dev/hdb2和/dev/hdb3做同样的操作
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

###通常,不需要重启,但有些计算机却要求。因此如果下面的例子不工作,试试重启。

2)接着我们创建物理卷(PV),如下:

1
2
3
4
5
6
[root@test ~]# pvcreate /dev/hdb1
  Physical volume "/dev/hdb1" successfully created
[root@test ~]# pvcreate /dev/hdb2
  Physical volume "/dev/hdb2" successfully created
[root@test ~]# pvcreate /dev/hdb3
  Physical volume "/dev/hdb3" successfully created

3)我们再将这3个物理卷(PV)加到一个叫做“test”的卷组(VG)中:

1
2
[root@test ~]# vgcreate test /dev/hdb1 /dev/hdb2 /dev/hdb3
  Volume group "test" successfully created

4)现在我们有一个空的卷组(VG),让我们来检查一下:

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
[root@test ~]# vgdisplay -v test
    Using volume group(s) on command line
    Finding volume group "test"
  --- Volume group ---
  VG Name               test
  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               19.01 GB
  PE Size               4.00 MB
  Total PE              4866
  Alloc PE / Size       0 / 0   
  Free  PE / Size       4866 / 19.01 GB
  VG UUID               cQ4Rmz-SIQJ-jVsU-IxpP-pKCk-ns3U-0oUo4C
 
  --- Physical volumes ---
  PV Name               /dev/hdb1     
  PV UUID               JYiZkd-DhU1-NsGe-tbE9-bc2k-yUnn-IxS544
  PV Status             allocatable
  Total PE / Free PE    1430 / 1430
 
  PV Name               /dev/hdb2     
  PV UUID               454aEC-e0kI-6smG-kxVc-VS5J-KhdF-ecXBcj
  PV Status             allocatable
  Total PE / Free PE    1430 / 1430
 
  PV Name               /dev/hdb3     
  PV UUID               iUDVOR-mVnx-91MW-a82Y-UeNV-tXWJ-qjRfWe
  PV Status             allocatable
  Total PE / Free PE    2006 / 2006

5) 这里的许多数据–大多数现在都容易理解了。我们看到没有逻辑卷(LV)定义,因此我们要补上它。我们将在“test”卷组(PV)中创建一个6000MB的逻辑卷,叫做“mysql”:

1
2
[root@test ~]# lvcreate -L 6000M -n mysql test
  Logical volume "mysql" created

6)Ok,到这里,我们创建一个文件系统:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@test ~]# mkfs -t ext3 /dev/test/mysql
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
768544 inodes, 1536000 blocks
76800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1572864000
47 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736
 
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

7)建立新分区卷标:

1
[root@test ~]# tune2fs –L/mysql /dev/test/mysql  (卷标自己根据需要定)

8)加载新分区

1
2
[root@test ~]# mkdir /mysql
[root@test ~]# mount –t ext3 /dev/test/mysql /mysql

9)我们做完了!让我们回顾我们的卷组(VG),它现在充满了一些东西:

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
[root@test ~]# vgdisplay test -v
    Using volume group(s) on command line
    Finding volume group "test"
  --- Volume group ---
  VG Name               test
  System ID             
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               19.01 GB
  PE Size               4.00 MB
  Total PE              4866
  Alloc PE / Size       1500 / 5.86 GB
  Free  PE / Size       3366 / 13.15 GB
  VG UUID               cQ4Rmz-SIQJ-jVsU-IxpP-pKCk-ns3U-0oUo4C
 
  --- Logical volume ---
  LV Name                /dev/test/mysql
  VG Name                test
  LV UUID                7ew2xI-kIA4-RLma-Icn8-5Zqv-fvrU-pF4bfF
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                5.86 GB
  Current LE             1500
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
 
  --- Physical volumes ---
  PV Name               /dev/hdb1     
  PV UUID               JYiZkd-DhU1-NsGe-tbE9-bc2k-yUnn-IxS544
  PV Status             allocatable
  Total PE / Free PE    1430 / 1430
 
  PV Name               /dev/hdb2     
  PV UUID               454aEC-e0kI-6smG-kxVc-VS5J-KhdF-ecXBcj
  PV Status             allocatable
  Total PE / Free PE    1430 / 1430
 
  PV Name               /dev/hdb3     
  PV UUID               iUDVOR-mVnx-91MW-a82Y-UeNV-tXWJ-qjRfWe
  PV Status             allocatable
  Total PE / Free PE    2006 / 506

10)进行到这里,觉得先前的分区成3个部分完全没有必要,因为后来是建立在整个LVM上的,这样说来前面的可以不用分3个,只需要分一个分区即可,我们来测试一下

11)先卸载掉已经加载的/mysql分区

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
[root@test ~]# umount /mysql
 
[root@test ~]# fdisk /dev/hdb
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       11627     5859976+  8e  Linux LVM
/dev/hdb2           11628       23254     5860008   8e  Linux LVM
/dev/hdb3           23255       39560     8218224   8e  Linux LVM
 
Command (m for help): d   ## 删除分区
Partition number (1-4): 1
 
Command (m for help): d
Partition number (1-4): 2
 
Command (m for help): d
Selected partition 3
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-39560, default 1): 
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-39560, default 39560): ## 直接回车使用全部分区
Using default value 39560
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       39560    19938208+  83  Linux
 
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
 
Command (m for help): p
 
Disk /dev/hdb: 20.4 GB, 20416757760 bytes
16 heads, 63 sectors/track, 39560 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1               1       39560    19938208+  8e  Linux LVM
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

12)接着我们创建物理卷(PV),如下:

1
2
[root@test ~]# pvcreate /dev/hdb1
  Can't initialize physical volume "/dev/hdb1" of volume group "test" without -ff

### 这个时候之所以出现这个提示,是因为,我们先前创建的LVM没有卸载掉
卸载卷的方法:
卸载物理卷:pvremove PVDEVICE
卸载卷组:vgremove VGNAME
卸载逻辑卷:lvremove LVDEVICE
卸载的顺序:先逻辑然后卷组最后物理卷
卸载前别忘了备份

1
2
3
4
5
6
7
8
[root@test ~]# vgremove test
Do you really want to remove volume group "test" containing 1 logical volumes? [y/n]: y
Do you really want to remove active logical volume mysql? [y/n]: y
  Logical volume "mysql" successfully removed
  Volume group "test" successfully removed
 
[root@test ~]# pvremove /dev/hdb1
  Labels on physical volume "/dev/hdb1" successfully wiped

继续
创建物理卷(PV):

1
2
[root@test ~]# pvcreate /dev/hdb1
  Physical volume "/dev/hdb1" successfully created

创建做“test”的卷组(VG):

1
2
[root@test ~]# vgcreate test /dev/hdb1
  Volume group "test" successfully created

这里出现了一个很奇怪的问题,发现使用vgdisplay查看的时候,VG size才5.59G,也就是好像上次所分的/dev/hdb1 6000M的大小

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@test ~]# vgdisplay -v test
    Using volume group(s) on command line
    Finding volume group "test"
  --- Volume group ---
  VG Name               test
  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               5.59 GB
  PE Size               4.00 MB
  Total PE              1430
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1430 / 5.59 GB
  VG UUID               xfGIKf-BDjE-jWUd-9alx-wXmL-K00x-7tOw2W
 
  --- Physical volumes ---
  PV Name               /dev/hdb1     
  PV UUID               n1KYH6-K5Wc-dk18-1M3K-UB11-09Bw-bQ3X3I
  PV Status             allocatable
  Total PE / Free PE    1430 / 1430

使用
fdisk /dev/hdb1
fdisk /dev/hdb2
fdisk /dev/hdb3

均能看到上次所分的大小,很奇怪
重启后使用fdisk查看正常了
卸载掉后重新创建

1
2
3
4
[root@test ~]# vgremove /dev/test
  Volume group "test" successfully removed
[root@test ~]# pvremove /dev/hdb1
  Labels on physical volume "/dev/hdb1" successfully wiped

创建物理卷(PV):

1
2
[root@test ~]# pvcreate /dev/hdb1
  Physical volume "/dev/hdb1" successfully created

创建做“test”的卷组(VG):

1
2
[root@test ~]# vgcreate test /dev/hdb1
  Volume group "test" successfully created

这次使用vgdisplay 查看的结果正常了

创建逻辑卷

1
2
[root@test ~]# lvcreate -L 6000M -n mysql test
  Logical volume "mysql" created

Ok,到这里,我们创建一个文件系统:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@test ~]# mkfs -t ext3 /dev/test/mysql
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
768544 inodes, 1536000 blocks
76800 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1572864000
47 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736
 
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

建立新分区卷标:

1
[root@test ~]# tune2fs –L/mysql /dev/test/mysql  (卷标自己根据需要定)

加载新分区

1
2
[root@test ~]# mkdir /mysql
[root@test ~]# mount –t ext3 /dev/test/mysql /mysql

同样创建/appstore,/infoware

在创建/infoware分区的时候,由于是最后一个分区,为了避免空间的浪费,需要将所有的空间全部进行分配
再使用lvcreate -L 6000M -n mysql test 这样的命令显然是不可以的
查看lvcreate的帮助的时候得知有个-l的参数
-l|–extents LogicalExtentsNumber

使用vgdisplay可以看到
Total PE / Free PE 4867 / 1867
还有1867个PE(物理分区)没有使用

1
2
[root@test ~]# lvcreate -l+1867 -n infoware test
  Logical volume "infoware" created

然后使用vgdisplay可以看到全部PE都用完了

然后就是建立卷标,建立分区,mount了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  236M  4.9G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-mysql
                      5.8G  140M  5.4G   3% /mysql
/dev/mapper/test-appstore
                      5.8G  140M  5.4G   3% /appstore
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware

最后就是修改fstab文件,将上述的分区写入即可

三、测试分割和移动

##说明:最好不要缩小空间,否则可能会导致数据损坏,宁愿增加硬盘。本文作为测试,有详细的步骤可以说明为何不要采用减少某个分区的空间来增加给其他分区

1、相邻的分割
目标:从appstore中分割1.5G到mysql之中
步骤:
1)、首先取消挂载

1
2
[root@test ~]# umount /appstore
[root@test ~]# umount /mysql

2)、执行lvresize -L-1.5G /dev/test/appstore

  即可给刚才的appstore分区减少1.5GB空间

  执行lvresize -L+1.5G /dev/test/mysql

  即可给刚才的mysql分区增加1.5GB空间

  修改之后重新挂载

  mount /dev/test/appstore /appstore
  mount /dev/test/mysql /mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  217M  4.9G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware
/dev/mapper/test-appstore
                      5.8G  140M  5.4G   3% /appstore
/dev/mapper/test-mysql
                      5.8G  140M  5.4G   3% /mysql

好奇怪,怎么分区大小都没改变呢?使用vgdisplay查看得到/mysql和/appstore 的lv size已经更改掉了

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
[root@test ~]# vgdisplay -v test
    Using volume group(s) on command line
    Finding volume group "test"
  --- Volume group ---
  VG Name               test
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  6
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               19.01 GB
  PE Size               4.00 MB
  Total PE              4867
  Alloc PE / Size       4867 / 19.01 GB
  Free  PE / Size       0 / 0   
  VG UUID               YRS7CW-saYm-yid2-QVD2-FfrF-j9n0-Zirelb
 
  --- Logical volume ---
  LV Name                /dev/test/mysql
  VG Name                test
  LV UUID                PM4OAk-212K-iRBZ-k9xK-RIzu-0f1N-A1yVln
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.36 GB
  Current LE             1884
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
 
  --- Logical volume ---
  LV Name                /dev/test/appstore
  VG Name                test
  LV UUID                gCN8PK-7cfm-8ATM-EcE2-KR5P-hs5b-jPNlkp
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                4.36 GB
  Current LE             1116
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:4
 
  --- Logical volume ---
  LV Name                /dev/test/infoware
  VG Name                test
  LV UUID                Z9wcWf-bkAS-5brz-pmyQ-Hdp6-bCqn-xwPUkB
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.29 GB
  Current LE             1867
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:5
 
  --- Physical volumes ---
  PV Name               /dev/hdb1     
  PV UUID               E8kn73-nmZE-J3Os-e99x-L13J-1OIV-qQOJpT
  PV Status             allocatable
  Total PE / Free PE    4867 / 0

重启后还是没变???

注意:这里有个很严重的问题,就是lvresize貌似只能增加,而不能减少,如果减少的话可能会导致数据损坏,所以这里一定要慎重 *****

现在需要resize相应的ext3文件系统来利用LV中的可用空间。

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
[root@test ~]# resize2fs -p /dev/test/mysql 
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/test/mysql is mounted on /mysql; on-line resizing required
Performing an on-line resize of /dev/test/mysql to 1929216 (4k) blocks.
The filesystem on /dev/test/mysql is now 1929216 blocks long.
 
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  216M  5.0G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-mysql
                      7.3G  142M  6.8G   3% /mysql
/dev/mapper/test-appstore
                      5.8G  140M  5.4G   3% /appstore
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware
 
[root@test ~]# resize2fs -p /dev/test/appstore 
resize2fs 1.39 (29-May-2006)
Filesystem at /dev/test/appstore is mounted on /appstore; on-line resizing required
On-line shrinking from 1536000 to 1142784 not supported.

## 这里不支持刚才lvresize减少的appstore分区

那么该如何减少文件系统呢?
下面让我们来一步一步的看下

减小LVM中的文件系统必须离线操作(处于umount装态),要减小文件系统和LV:

Unmount相应的文件系统
运行磁盘检查确保卷的完整
减小文件系统
减小LV

1)、首先查看空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  216M  5.0G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-mysql
                      7.3G  142M  6.8G   3% /mysql
/dev/mapper/test-appstore
                      5.8G  140M  5.4G   3% /appstore
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware

2)、卸载文件系统

1
2
[root@test ~]# cd /
[root@test /]# umount /appstore

2.1) 检查文件系统是否有错误

1
2
3
4
5
6
[root@test /]# e2fsck -f /dev/test/appstore 
e2fsck 1.39 (29-May-2006)
The filesystem size (according to the superblock) is 1536000 blocks
The physical size of the device is 1142784 blocks
Either the superblock or the partition table is likely to be corrupt!
Abort<y>?

## 看,到这里就会发现刚才使用lvresize减少的appstore的文件系统出错了

## 郁闷的是无法使用e2fsck和fsck修复 -_-!!

## 尝试着恢复到以前的状态,即/mysql还是5.4G时候的情况

2.2)、卸载文件系统

1
[root@test /]# umount /dev/test/mysql

2.3)、减小文件系统到5.4GB

1
2
3
4
5
6
[root@test /]# resize2fs /dev/test/mysql 5.4G
resize2fs 1.39 (29-May-2006)
resize2fs: bad filesystem size - 5.4G
[root@test /]# resize2fs /dev/test/mysql 6000M
resize2fs 1.39 (29-May-2006)
Please run 'e2fsck -f /dev/test/mysql' first.

## 提示需要先进行检查,实际操作中应先用e2fsck进行检查,再做resize2fs操作
2.4)、检查文件系统是否有错误

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@test /]# e2fsck -f /dev/test/mysql 
e2fsck 1.39 (29-May-2006)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/mysql: 11/964768 files (9.1% non-contiguous), 66459/1929216 blocks
 
[root@test /]# resize2fs /dev/test/mysql 6000M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/test/mysql to 1536000 (4k) blocks.
The filesystem on /dev/test/mysql is now 1536000 blocks long.

2.5)、减小LV到5.4GB

1
2
3
4
5
6
7
[root@test /]# lvresize -L 5.4G /dev/test/mysql
  Rounding up size to full physical extent 5.40 GB
  WARNING: Reducing active logical volume to 5.40 GB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mysql? [y/n]: y
  Reducing logical volume mysql to 5.40 GB
  Logical volume mysql successfully resized

2.6)、校验LV的新大小.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@test /]# lvdisplay /dev/test/mysql
  --- Logical volume ---
  LV Name                /dev/test/mysql
  VG Name                test
  LV UUID                PM4OAk-212K-iRBZ-k9xK-RIzu-0f1N-A1yVln
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.40 GB
  Current LE             1383
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3

2.7)、重新挂载文件系统,并校验新大小

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@test /]# mount /dev/test/mysql /mysql
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  217M  4.9G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware
/dev/mapper/test-mysql
                      5.8G  140M  5.4G   3% /mysql

3、修复/appstore分区,将刚才从mysql上分下来的多余空间调整到appstore中(同上面的那次增加mysql空间一样的步骤)
3.1)、使用vgdisplay查看多余的空间为 PE 501

1
[root@test ~]# lvresize -l+501 /dev/test/appstore

3.2)、resize2fs -p /dev/test/appstore
提示需要使用e2fsck
3.3)、使用e2fsck -f /dev/test/appstore
发现了很多错误,修复
3.4)、resize2fs -p /dev/test/appstore
3.5)、使用vgdisplay -v test 查看lv size是否正确
3.6)、挂载
[root@test ~]# mount /dev/test/appstore /appstore
3.7)、查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@test ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol002
                      7.6G  1.9G  5.3G  27% /
/dev/mapper/VolGroup00-LogVol003
                      5.4G  217M  4.9G   5% /var
/dev/hda1             190M   18M  163M  10% /boot
tmpfs                 189M     0  189M   0% /dev/shm
/dev/mapper/test-infoware
                      7.2G  145M  6.7G   3% /infoware
/dev/mapper/test-mysql
                      5.8G  140M  5.4G   3% /mysql
/dev/mapper/test-appstore
                      6.3G  142M  5.8G   3% /appstore

## 需要再次说明的是,最好不要缩小空间,否则可能会导致数据损坏,宁愿增加硬盘

参考资料:

http://www.hudong.com/wiki/LVM

http://polygun2000.spaces.live.com/blog/cns!182B490BAC7D9686!459.entry

http://www.tcpdump.com/kb/os/linux/lvm-resizing-guide/intro.html

抱歉!评论已关闭.