您的位置:首页 > 运维架构 > Linux

Linux中创建LVM详细步骤

2015-05-13 19:02 302 查看
Linux中创建LVM详细步骤
1 什么事逻辑卷管理器LVM LVM: 将几个物理分区通过软件组合在一起,看起来像是独立的大硬盘,而要用这块大硬盘,就要将它分成可以使用的分区,然后格式化,挂载等PV物理卷:将物理磁盘或物理分区初始化成LVM能用的物理卷,分区中的类型是8e:Linux LVMVG逻辑卷组:将一个或者多个PV组合成一个大的磁盘,将其称之为组
LV逻辑卷:从VG中分割出来的分区,称为LV,LV被用来格式化后,进行挂载,设备代为: /dev/vgname/lvname
2 LVM结构图





演示实验:
1 创建一个100M的PV

系统中磁盘状态:
[root@desktop0 /]# fdisk -l
Disk /dev/sdb: 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
从sdb硬盘中添加一个分区:用作pv
[root@desktop0 /]#
[root@desktop0 /]# fdisk /dev/sdb
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.
Command (m for help): p
Disk /dev/sdb: 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: 0x541e3b32
Device Boot      Start         End      Blocks   Id  System
Command (m for help): n
Partition type:
p   primary (0 primary, 0 extended, 4 free)
e   extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +300M
Partition 1 of type Linux and of size 300 MiB is set
Command (m for help): p
Disk /dev/sdb: 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: 0x541e3b32
Device Boot       Start      End      Blocks   Id  System
/dev/sdb1      2048      616447      307200   83  Linux
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): p
Disk /dev/sdb: 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: 0x541e3b32
Device Boot     tart     End      Blocks   Id  System
/dev/sdb1         2048   616447      307200   8e  Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@desktop0 /]# partprobe
[root@desktop0 /]#
分区后:
[root@desktop0 /]# fdisk -l
Disk /dev/sdb: 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: 0x541e3b32
Device Boot Start End Blocks Id System
/dev/sdb1 2048 616447 307200 8e Linux LVM
创建pv物理卷:

[root@desktop0 /]#
[root@desktop0 /]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@desktop0 /]#
查看pv:
[root@desktop0 /]# pvdisplay
"/dev/sdb1" is a new physical volume of "300.00 MiB"
--- NEW Physical volume ---
PV Name               /dev/sdb1
VG Name
PV Size               300.00 MiB
Allocatable           NO
PE Size               0
Total PE              0
Free PE               0
Allocated PE          0
PV UUID               lUYnLJ-u6In-GAIA-b2YG-JHuX-AG6F-A4yVMR
创建vg逻辑卷组:
[root@desktop0 /]# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created
[root@desktop0 /]# vgdisplay
--- Volume group ---
VG Name               myvg
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               296.00 MiB
PE Size               4.00 MiB
Total PE              74
Alloc PE / Size            0 / 0
Free  PE / Size          74 / 296.00 MiB
VG UUID              nyc2Z4-zihI-jT9T-mvcF-ff8a-3Y0r-5Gkn7r
创建lv逻辑卷:mylv1:
[root@desktop0 /]# man lvcreate
[root@desktop0 /]# lvcreate -L 100M -n mylv1 myvg
Logical volume "mylv1" created


查看是否创建成功:
[root@desktop0 /]# lvdisplay
--- Logical volume ---
LV Path                /dev/myvg/mylv1
LV Name                mylv1
VG Name                myvg
LV UUID                VEdiDz-OLEH-Yua4-AQN9-2W26-nojO-KlpLaE
LV Write Access        read/write
LV Creation host, time desktop0.example.com, 2015-05-13 04:00:22 -0400
LV Status              available
# open                 0
LV Size                100.00 MiB
Current LE                25
Segments                  1
Allocation                 inherit
Read ahead sectors               auto
- currently set to              8192
Block device                253:0

[root@desktop0 /]# lvscan
ACTIVE            '/dev/myvg/mylv1' [100.00 MiB] inherit
[root@desktop0 /]#
查看mylv1分区是否成功:
[root@desktop0 /]# fdisk -l
………………
Disk /dev/mapper/myvg-mylv1: 104 MB, 104857600 bytes, 204800 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
格式化为ext3文件系统:

[root@desktop0 /]# mkfs.ext3 /dev/myvg/mylv1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
25688 inodes, 102400 blocks
5120 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1976 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729
Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done


挂载/dev/myvg/mylv1 to /mnt/mylvtest:
[root@desktop0 /]# mkdir /mnt/mylvtest
[root@desktop0 /]# mount /dev/myvg/mylv1  /mnt/mylvtest/
查看挂载:
[root@desktop0 /]# mount | grep "/mnt/mylvtest"
/dev/mapper/myvg-mylv1 on /mnt/mylvtest type ext3 (rw,relatime,seclabel,data=ordered)
[root@desktop0 /]#
查看挂载情况:
[root@desktop0 /]# df -hT
Filesystem         Type     Size  Used Avail Use% Mounted on
/dev/sda1         xfs      10G  3.1G  7.0G  31% /
devtmpfs          devtmpfs     899M     0  899M   0% /dev
tmpfs           tmpfs     914M  140K  914M   1% /dev/shm
tmpfs           tmpfs     914M   17M  897M   2% /run
tmpfs           tmpfs     914M     0  914M   0% /sys/fs/cgroup
/dev/mapper/myvg-mylv1    ext3      93M  1.6M   85M   2% /mnt/mylvtest
[root@desktop0 /]#
[root@desktop0 /]# cd /mnt/mylvtest/
[root@desktop0 mylvtest]# ls
lost+found
[root@desktop0 mylvtest]# touch {1..4}.txt
[root@desktop0 mylvtest]# ls
1.txt  2.txt  3.txt  4.txt  lost+found
[root@desktop0 mylvtest]# pwd
/mnt/mylvtest
------------成功创建一个100M的lv,并且挂载在/mnt/mylvtest下-----------------

-=*************扩容lv卷***********************
卸载挂载:
[root@desktop0 /]# umount /dev/myvg/mylv1


增加100M:
[root@desktop0 mylvtest]# lvextend -L +100M /dev/myvg/mylv1
Extending logical volume mylv1 to 200.00 MiB
Logical volume mylv1 successfully resized
验证:
[root@desktop0 mylvtest]# lvscan
ACTIVE            '/dev/myvg/mylv1' [200.00 MiB] inherit
验证:发觉没有刷新
[root@desktop0 mylvtest]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda1                10G  3.1G  7.0G  31% /
devtmpfs                899M     0  899M   0% /dev
tmpfs                   914M  140K  914M   1% /dev/shm
tmpfs                   914M   17M  897M   2% /run
tmpfs                   914M     0  914M   0% /sys/fs/cgroup
/dev/mapper/myvg-mylv1   93M  1.6M   85M   2% /mnt/mylvtest
刷新lv: resize2fs
[root@desktop0 mylvtest]# resize2fs /dev/myvg/mylv1  ##刷新lv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv1 is mounted on /mnt/mylvtest; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/myvg/mylv1 is now 204800 blocks long.
挂载:
[root@desktop0 /]# mount /dev/myvg/mylv1 /mnt/mylvtest
验证:已刷新
[root@desktop0 mylvtest]# df -h
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda1                10G  3.1G  7.0G  31% /
devtmpfs                899M     0  899M   0% /dev
tmpfs                   914M  140K  914M   1% /dev/shm
tmpfs                   914M   17M  897M   2% /run
tmpfs                   914M     0  914M   0% /sys/fs/cgroup
/dev/mapper/myvg-mylv1  190M  1.6M  178M   1% /mnt/mylvtest
[root@desktop0 mylvtest]#
-----------------------------------------------------
************************缩减lv卷容量****************
卸载挂载:
[root@desktop0 /]# umount /dev/myvg/mylv1
校验分区:
[root@desktop0 mnt]# e2fsck -f /dev/myvg/mylv1
e2fsck 1.42.9 (28-Dec-2013)
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
/dev/myvg/mylv1: 11/55296 files (0.0% non-contiguous), 12900/217088 blocks
指定缩小后的大小:
[root@desktop0 mnt]# resize2fs /dev/myvg/mylv1 100M
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/myvg/mylv1 to 102400 (1k) blocks.
The filesystem on /dev/myvg/mylv1 is now 102400 blocks long.
缩小lv容量:
[root@desktop0 mnt]# lvreduce -L 100M /dev/myvg/mylv1
WARNING: Reducing active logical volume to 100.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv1? [y/n]: Y
Reducing logical volume mylv1 to 100.00 MiB
Logical volume mylv1 successfully resized
[root@desktop0 mnt]#
验证:
[root@desktop0 mnt]# lvdisplay
--- Logical volume ---
LV Path                /dev/myvg/mylv1
LV Name                mylv1
VG Name                myvg
LV UUID                TmZGYo-Xm6P-RgyQ-w9bl-JfCh-GLbv-bbzLTK
LV Write Access        read/write
LV Creation host, time desktop0.example.com, 2015-05-13 07:37:48 -0400
LV Status              available
# open                 0
LV Size                100.00 MiB
Current LE             25
Segments               1
Allocation             inherit
Read ahead sectors     auto
- currently set to     8192
Block device           253:0

[root@desktop0 mnt]#
重新挂载:
[root@desktop0 mnt]# mount  /dev/myvg/mylv1  /mnt/mylvtest/
验证:
[root@desktop0 mnt]# df -hT
Filesystem                Type     Size  Used Avail Use% Mounted on
/dev/sda1                xfs      10G  3.1G  7.0G  31% /
devtmpfs                 devtmpfs  899M     0  899M   0% /dev
tmpfs                  tmpfs     914M  140K  914M   1% /dev/shm
tmpfs                  tmpfs     914M   17M  897M   2% /run
tmpfs                  tmpfs     914M     0  914M   0% /sys/fs/cgroup
/dev/mapper/myvg-mylv1 ext3    93M  1.6M   85M   2% /mnt/mylvtest


本文出自 “天道酬勤” 博客,请务必保留此出处http://luzhi1024.blog.51cto.com/8845546/1651053
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: