linux之lvm学习 (转载)

5月20号 更新:

 今天把我本子上的 vmware上重新整了下,虚拟一个Linux ,实地演练下LVM

1.装好linux系统, 我给虚拟机开辟了一个 /dev/hda 硬盘,3G空间,为了实验LVM,我有给虚拟机添加了一块硬盘

/dev/hdb 2G 空间

注意: 我在虚拟机中装Linux的时候,遇到了 系统找不到 硬盘的情况,开始以为 光盘有问题,在网上重新下载了ISO包,安装,问题依旧,最后才发现我这个版本的vmware 对硬盘支持的类型只能是 IDE 的,改了 硬盘类型 就OK了

2. 使用 fdisk -l 查看磁盘 情况,可以看到 hdb 的类型是 linux

3. 将 hdb 从linux 改成 linux LVM,一定要使用 w 保存退出,再次查看 已经是linux lvm了

   fdisk /dev/hdb ->m -> t -> 3 -> 8e -> w

[root@xhu-vm ~]# fdisk -l

Disk /dev/hda: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 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         391     3036285   8e  Linux LVM

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/hdb3               1        4161     2097112+  8e  Linux LVM
 

4.创建 PV

pvcreate/dev/hdb

 查看创建好的pv
[root@xhu-vm ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/hda2
  VG Name               VolGroup00
  PV Size               2.88 GB / not usable 0  
  Allocatable           yes
  PE Size (KByte)       32768
  Total PE              92
  Free PE               1
  Allocated PE          91
  PV UUID               el1YY6-k7dU-V7mA-Uvyk-TNuH-Dnsm-yRBxDC
  
  --- NEW Physical volume ---
  PV Name               /dev/hdb
  VG Name              
  PV Size               2.00 GB
  Allocatable           NO
  PE Size (KByte)       0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               r1uwvi-NhI1-bVS5-qfZQ-GR8M-VNkX-UW0Ajr
 

5.创建卷组

[root@xhu-vm ~]# vgcreate VolGroup01 /dev/hdb
  Volume group "VolGroup01" successfully created
 

查看卷组

[root@xhu-vm ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/hdb
  VG Name               VolGroup01
  PV Size               2.00 GB / not usable 0  
  Allocatable           yes
  PE Size (KByte)       4096
  Total PE              511
  Free PE               511
  Allocated PE          0
  PV UUID               r1uwvi-NhI1-bVS5-qfZQ-GR8M-VNkX-UW0Ajr
  
  --- Physical volume ---
  PV Name               /dev/hda2
  VG Name               VolGroup00
  PV Size               2.88 GB / not usable 0  
  Allocatable           yes
  PE Size (KByte)       32768
  Total PE              92
  Free PE               1
  Allocated PE          91
  PV UUID               el1YY6-k7dU-V7mA-Uvyk-TNuH-Dnsm-yRBxDC
 

或者这么查看

[root@xhu-vm ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup01
  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               2.00 GB
  PE Size               4.00 MB
  Total PE              511
  Alloc PE / Size       0 / 0  
  Free  PE / Size       511 / 2.00 GB
  VG UUID               bVxufu-AZWK-ifl2-e8OQ-AH7k-oMuR-Dkn5Cm
  
  --- Volume group ---
  VG Name               VolGroup00
  System ID            
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               2.88 GB
  PE Size               32.00 MB
  Total PE              92
  Alloc PE / Size       91 / 2.84 GB
  Free  PE / Size       1 / 32.00 MB
  VG UUID               jUbO2f-SWru-9BXD-Kp16-AGbp-0pQE-fw5YJ2
 

6. 激活卷组

[root@xhu-vm ~]# vgchange -a y VolGroup01
  0 logical volume(s) in volume group "VolGroup01" now active
[root@xhu-vm ~]#
 

7.创建逻辑卷(152M)

[root@xhu-vm ~]# lvcreate -L150  -nLogVol02  VolGroup01
  /dev/cdrom: open failed: Read-only file system
  Rounding up size to full physical extent 152.00 MB
  Logical volume "LogVol02" created
[root@xhu-vm ~]#
 

8.格式化文件系统 + mount 到 /data/wwwroot

[root@xhu-vm ~]# mkdir /data/wwwroot
[root@xhu-vm ~]# mkfs -V -t ext3 /dev/VolGroup01/LogVol02
mkfs version 2.12a (Dec 21 2004)
mkfs.ext3 /dev/VolGroup01/LogVol02
mke2fs 1.35 (28-Feb-2004)
max_blocks 159383552, rsv_groups = 19456, rsv_gdb = 256
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
38912 inodes, 155648 blocks
7782 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
19 block groups
8192 blocks per group, 8192 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
        8193, 24577, 40961, 57345, 73729

Writing inode tables: done                           
inode.i_blocks = 3074, i_size = 67383296
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@xhu-vm ~]# mount  /dev/VolGroup01/LogVol02 /data/wwwroot
[root@xhu-vm ~]#
 

9. 拷贝一个文件到 /data/wwwroot

[root@xhu-vm wwwroot]# df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       2418944   1464868    831196  64% /
/dev/hda1               101086      9028     86839  10% /boot
none                    128020         0    128020   0% /dev/shm
/dev/mapper/VolGroup01-LogVol02
                        150733     11554    131397   9% /data/wwwroot
 

注意:我创建的LogVol02 有152M,当你他的 mount点 /data/wwwroot 拷贝数据的时候,超过152M会报错,再次操作 /data/wwwroot 的时候会说 file system read only. 只要你增加了 这个lv的容量就万事OK了,可见LVM的好处

10.增加这个LV增加容量100M

lvextend-L+100/dev/VolGroup01/LogVol02

lvs看看现在的容量

11.减少LV的容量减少100M

lvreduce-L-100/dev/VolGroup01/LogVol02

12.在VolGroup01继续建立一个LVLogVol03

 lvcreate -L150  -nLogVol03  VolGroup01

参考:

Linux添加硬盘并分区格式化

Linux系统新加物理盘实现LVM管理

虚拟机环境中linux系统增加磁盘空间

linux之lvm学习

硬盘分区如何设定大小,如何规划,再好的规划也有出差错的时候,人算不如天算.

为了让我们脱离那可悲的分区与分区修改工具,请跟我来认识一下LVM-逻辑卷管理(logical volume manager).

首先先把基本术语定义讲一下:

LVM是linux对磁盘分区进行管理的一种机制.LVM是建立在磁盘与分区之上的一个逻辑层,以提高磁盘管理的灵活性.

在LVM内有三个基本术语PV(PhysicalVolume物理卷)VG(VolumeGroup卷组)LV(LogicalVolume逻辑卷)

三者的关系为,先建立PV,根据PV组合或扩展形成VG,根据用户需求使用VG卷的总量去建立相应的LV卷(个人理解)

一.创建PV的命令与方法

1.查看磁盘分区情况:

[root@localhost /]# fdisk -l /dev/hdb

Disk /dev/hdb: 21.4 GB, 21474836480 bytes

15heads,63sectors/track,44384cylinders

Units = cylinders of 945 * 512 = 483840 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/hdb11100004724968+83Linux

/dev/hdb21000120000472500083Linux

/dev/hdb32000130000472500083Linux

/dev/hdb43000144384679644083Linux

共计4个分区请大家看ID位置代码:83system:linux,我们需要更改ID代码为8e此为LVM卷分区标签

分区系统号更改方法如下:

[root@localhost/]#fdisk/dev/hdb

Command(mforhelp):t

Partitionnumber(1-4):1

Hexcode(typeLtolistcodes):8e

Changedsystemtypeofpartition1to8e(LinuxLVM)

此时系统ID已经更改为LVM,我们再用FDISK命令查看一下磁盘情况

[root@localhost /]# fdisk -l /dev/hdb

Disk /dev/hdb: 21.4 GB, 21474836480 bytes

15heads,63sectors/track,44384cylinders

Units = cylinders of 945 * 512 = 483840 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/hdb11100004724968+8eLinuxLVM

/dev/hdb2100012000047250008eLinuxLVM

SYStem类型:LinuxLVM,此时我们可以创建PV了,我创建了二个PV,通过pvdisplay命令可以查看创建的PV卷的情况.

[root@localhost/]#pvcreate/dev/hdb1/dev/hdb2

Physicalvolume"/dev/hdb1"successfullycreated

Physicalvolume"/dev/hdb2"successfullycreated

查看PV卷详细信息:

[root@localhost/]#pvdisplay

"/dev/hdb1"isanewphysicalvolumeof"4.51GB"

---NEWPhysicalvolume---

PVName/dev/hdb1

VGName

PVSize4.51GB

AllocatableNO

PESize(KByte)0

TotalPE0

FreePE0

AllocatedPE0

PVUUIDgSv31K-01SU-1Vbm-HF26-MR5W-wgBw-1EeHb0

"/dev/hdb2"isanewphysicalvolumeof"4.51GB"

---NEWPhysicalvolume---

PVName/dev/hdb2

VGName

PVSize4.51GB

AllocatableNO

PESize(KByte)0

TotalPE0

FreePE0

AllocatedPE0

PVUUIDODEuTv-iMqs-Wzxq-HMyB-yUw2-Lnwn-5JdlSp

我们此时再回看查看一下磁盘分区的情况.

[root@localhost /]# fdisk -l /dev/hdb

Disk /dev/hdb: 21.4 GB, 21474836480 bytes

15heads,63sectors/track,44384cylinders

Units = cylinders of 945 * 512 = 483840 bytes

   Device Boot      Start         End      Blocks   Id  System

/dev/hdb11100004724968+8eLinuxLVM

/dev/hdb2100012000047250008eLinuxLVM

/dev/hdb3200013000047250008eLinuxLVM

/dev/hdb4300014438467964408eLinuxLVM

PV创建完成,相应的命令如下:pvcreate(PV创建)pvdisplay(PV信息查看)pvremove(pv卷删除)

pvremove /dev/hdb1 (删除命令例)

参考:

合理规划您的硬盘分区

http://hi.baidu.com/sinyo/blog/item/d793be3e866682f9828b13c6.html

http://blog.csdn.net/steelren/archive/2007/12/12/1931363.aspx

利用lvm增加linux根分区的容量

http://www.opsers.org/base/linux-root-partition-using-lvm-the-capacity-to-increase.html

相关推荐