【Linux】创建逻辑卷管理(LVM)

LVM是对磁盘进行分区管理的机制。LVM有很多优点:在线扩容,跨磁盘分区......,缺点:管理相对麻烦。创建LVM的过程如下:

【Linux】创建逻辑卷管理(LVM)

LVM是基于普通分区或者整块硬盘来进行的。我们首先把这些存储转换为PV(physical volumn 物理卷),其次把PV加入到VG(volumn group 卷组),再从卷组中划分LV(logical volumn 逻辑卷),然后把LV格式化为文件系统,最后挂载使用。

实验描述:使用sdb、sdc两块硬盘配置LVM

实验过程:

一、配置PV物理卷

1.查看磁盘情况

[root@lgr ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes

255 heads, 63 sectors/track, 5221 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      104391   83  Linux

/dev/sda2              14         535     4192965   82  Linux swap / Solaris

/dev/sda3             536        5221    37640295   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sdc: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sdc doesn't contain a valid partition table

2.对sdb、sdc进行分区(我这里把sdb、sdc各只分了一个主分区)

[root@lgr ~]# fdisk /dev/sdb

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. 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)

Command (m for help): n

Command action

e   extended

p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-652, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-652, default 652):

Using default value 652

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.

[root@lgr ~]# fdisk /dev/sdc

Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel

Building a new DOS disklabel. 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)

Command (m for help): n

Command action

e   extended

p   primary partition (1-4)

p

Partition number (1-4): 1

First cylinder (1-652, default 1):

Using default value 1

Last cylinder or +size or +sizeM or +sizeK (1-652, default 652):

Using default value 652

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

3.分区 system格式为linux LVM

[root@lgr ~]# fdisk /dev/sdb

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): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@lgr ~]#

[root@lgr ~]# fdisk /dev/sdc

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): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

[root@lgr ~]#

[root@lgr ~]# fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes

255 heads, 63 sectors/track, 5221 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *           1          13      104391   83  Linux

/dev/sda2              14         535     4192965   82  Linux swap / Solaris

/dev/sda3             536        5221    37640295   83  Linux

Disk /dev/sdb: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

/dev/sdb1               1         652     5237158+  8e  Linux LVM

Disk /dev/sdc: 5368 MB, 5368709120 bytes

255 heads, 63 sectors/track, 652 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot      Start         End      Blocks   Id  System

/dev/sdc1               1         652     5237158+  8e  Linux LVM

4.创建物理卷PV

[root@lgr ~]# pvcreate /dev/sdb1

Physical volume "/dev/sdb1" successfully created

[root@lgr ~]# pvcreate /dev/sdc1

Physical volume "/dev/sdc1" successfully created

[root@lgr ~]# pvs

PV         VG   Fmt  Attr PSize PFree

/dev/sdb1       lvm2 a-   4.99G 4.99G

/dev/sdc1       lvm2 a-   4.99G 4.99G

[root@lgr ~]# pvdisplay

--- Physical volume ---

PV Name               /dev/sdb1

VG Name               vgdata

PV Size               4.99 GB / not usable 2.41 MB

Allocatable           yes

PE Size (KByte)       4096

Total PE              1278

Free PE               1278

Allocated PE          0

PV UUID               yGtlcJ-EVvu-ZRXA-RKhh-xGz7-Br8K-By2zdr

--- Physical volume ---

PV Name               /dev/sdc1

VG Name               vgdata

PV Size               4.99 GB / not usable 2.41 MB

Allocatable           yes

PE Size (KByte)       4096

Total PE              1278

Free PE               1278

Allocated PE          0

PV UUID               DzbU8c-3gZ7-8dup-mAH1-K4Vn-hHU9-Jfimtl

二、创建卷组VG

创建命令:vgcreate   VG名称   PV绝对路径

[root@lgr ~]# vgcreate vgdata /dev/sdb1 /dev/sdc1

Volume group "vgdata" successfully created

[root@lgr ~]# vgs

VG     #PV #LV #SN Attr   VSize VFree

vgdata   2   0   0 wz--n- 9.98G 9.98G

[root@lgr ~]# vgdisplay

--- Volume group ---

VG Name               vgdata

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               9.98 GB

PE Size               4.00 MB

Total PE              2556

Alloc PE / Size       0 / 0

Free  PE / Size       2556 / 9.98 GB

VG UUID               qR2txF-XLix-1BYn-ei9A-KFjZ-0kcA-4fITDn

三、创建逻辑卷LV

创建命令:lvcreate {选项} 卷组名

一般为:lvcreate -L 【size】 【lvname】【vgname】

创建一个5G的名为lv1的逻辑卷:

[root@lgr ~]# lvcreate -L 5G -n lv1 vgdata

Logical volume "lv1" created

[root@lgr ~]# lvs

LV   VG     Attr   LSize Origin Snap%  Move Log Copy%  Convert

lv1  vgdata -wi-a- 5.00G

[root@lgr ~]# lvdisplay

--- Logical volume ---

LV Name                /dev/vgdata/lv1

VG Name                vgdata

LV UUID                vcJTYO-IHkM-k7ju-x8C1-jmCR-pVKN-3dO5Dm

LV Write Access        read/write

LV Status              available

# open                 0

LV Size                5.00 GB

Current LE             1280

Segments               2

Allocation             inherit

Read ahead sectors     auto

- currently set to     256

Block device           253:0

四、格式化创建文件系统

[root@lgr ~]# mkfs.ext3 /dev/vgdata/lv1

mke2fs 1.39 (29-May-2006)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

655360 inodes, 1310720 blocks

65536 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=1342177280

40 block groups

32768 blocks per group, 32768 fragments per group

16384 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 21 mounts or

180 days, whichever comes first.  Use tune2fs -c or -i to override.

五、挂载逻辑卷

[root@lgr ~]# mkdir /u02

[root@lgr ~]# mount /dev/vgdata/lv1 /u02

[root@lgr ~]# df -h

Filesystem            Size  Used Avail Use% Mounted on

/dev/sda3              35G   13G   21G  38% /

/dev/sda1              99M   22M   73M  23% /boot

tmpfs                 995M     0  995M   0% /dev/shm

/dev/mapper/vgdata-lv1

5.0G  139M  4.6G   3% /u02

上一篇:C语言操作Redis总结


下一篇:oracle一视图性能问题