Linux LVM逻辑卷管理工具的使用方法

来源:网络整理时间:2024-04-28 09:32:52

摘要:Linux LVM逻辑卷管理工具的使用方法

Linux LVM逻辑卷管理工具的使用方法,以下是小编整理的详细信息,因信息具有时效性,仅供参考。

[info]1、基础概念[/info]

LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现。

Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间。普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不下某个文件时,这个文件因为受上层文件系统的限制,也不能跨越多个分区来存放,所以也不能同时放到别的磁盘上。而遇到出现某个分区空间耗尽时,解决的方法通常是使用符号链接,或者使用调整分区大小的工具,但这只是暂时解决办法,没有从根本上解决问题。随着Linux的逻辑卷管理功能的出现,这些问题都迎刃而解,用户在无需停机的情况下可以方便地调整各个分区大小。

PV (Physical Volumn 物理卷)

VG(Volumn Group 物理卷组)

LV(Logical Volumn 逻辑卷)

PE(Physical Extend 物理块 default 4M);

LE(Logical Extend 逻辑块)

PE:LE=1:1(Normal)

LVM各个组件的关系示意图

[info]1.5、预先需要将添加的硬盘进行分区[/info]

fdisk -l
fdisk /dev/sdb

[info]2、虚拟机上增加一块硬盘,并创建几个分区sdb1和sdb2[/info]

这里就不详细讲述创建分区的方法了。

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1000     8032468+  83  Linux
/dev/sdb2            1001        2000     8032500   83  Linux

[info]3、创建物理卷(PV)pvcreate,pvs[/info]

[root@oracle ~]# pvcreate /dev/sdb{1,2}
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created

[root@oracle ~]# pvs
  /dev/hdc: open failed: No medium found
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   99.88G    0
  /dev/sdb1             lvm2 --    7.66G 7.66G
  /dev/sdb2             lvm2 --    7.66G 7.66G

[info]4、创建卷组(VG) vgcreate,vgdisplay[/info]

[root@oracle ~]# vgdisplay
  --- 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               99.88 GB
  PE Size               32.00 MB
  Total PE              3196
  Alloc PE / Size       3196 / 99.88 GB
  Free  PE / Size       0 / 0   
  VG UUID               f8N3vv-ejHh-wU1x-zv96-jyiR-yFf5-2G7BwK

[root@oracle ~]# vgcreate -v VolGroup01 /dev/sdb{1,2}
    Wiping cache of LVM-capable devices
    Adding physical volume '/dev/sdb1' to volume group 'VolGroup01'
    Adding physical volume '/dev/sdb2' to volume group 'VolGroup01'
    Archiving volume group "VolGroup01" metadata (seqno 0).
    Creating volume group backup "/etc/lvm/backup/VolGroup01" (seqno 1).
  Volume group "VolGroup01" successfully created

[root@oracle ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup01
  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               15.32 GB
  PE Size               4.00 MB
  Total PE              3922
  Alloc PE / Size       0 / 0   
  Free  PE / Size       3922 / 15.32 GB
  VG UUID               pGE9tH-k5oz-Qt20-I8JZ-tuFb-qMca-OsVLTV

[info]5、删除(VG) vgremove[/info]

[root@oracle ~]# vgremove VolGroup01
  Volume group "VolGroup01" successfully removed

通过 -s 调整VG的PE大小
[root@oracle ~]# vgcreate -v -s 32M VolGroup01 /dev/sdb{1,2}
    Wiping cache of LVM-capable devices
    Adding physical volume '/dev/sdb1' to volume group 'VolGroup01'
    Adding physical volume '/dev/sdb2' to volume group 'VolGroup01'
    Archiving volume group "VolGroup01" metadata (seqno 0).
    Creating volume group backup "/etc/lvm/backup/VolGroup01" (seqno 1).
  Volume group "VolGroup01" successfully created

[root@oracle ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup01
  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               15.31 GB
  PE Size               32.00 MB
  Total PE              490
  Alloc PE / Size       0 / 0   
  Free  PE / Size       490 / 15.31 GB
  VG UUID               pFJKA1-METh-vMph-9PbB-1fNC-sxV0-e9axc7

[info]6、创建逻辑卷(LVM)lvcreate[/info]

   lvcreate [options]  -n 逻辑卷名  卷组名
   其中的常用可选项有:
   "-i Stripes :采用交错(striped)方式创建LV,其中Stripes指卷组中PV的数量。
   "-I Stripe_size :采用交错方式时采用的块大小(单位为KB),Stripe_size必须为2的指数:2N ,N=2,3...9。
   "-l LEs :指定LV的逻辑块数。
   "-L size :指定LV的大小,其后可以用K、M、G表示KB、MB、GB。
   "-s :创建一已存在LV的snapshot卷。
   "-n name :为LV指定名称。

   创建普通LV: 

    lvcreate -L 5G -n lv01 vg01  创建一个名为lv01的5G 虚拟分区在虚拟硬盘vg01上   /dev/vg01/lv01

    lvcreate -i 4 -I 8 -L 4G -n lv02  vg01  [-i 是VG中组成PV的分区个数,-I 块大小,-L LV的大小]

   创建快照LV:

    lvcreate -s -i 4 -I 16 -L 2G -n lv03_snapshot vg01


[root@oracle ~]# lvcreate -L 5G -n lv01 VolGroup01
  Logical volume "lv01" created
[root@oracle ~]# lvdisplay
  --- Logical volume ---
  LV Name                /dev/VolGroup01/lv01
  VG Name                VolGroup01
  LV UUID                B2nmqM-NFjv-zFso-JG2l-X2rc-OVzO-9bF9WJ
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.00 GB
  Current LE             160
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

[info]7、创建文件系统[/info]

[root@oracle ~]# mke2fs  -j /dev/VolGroup01/lv01
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 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[info]8、挂载[/info]

[root@oracle /]# mkdir /u02
[root@oracle /]# mount /dev/VolGroup01/lv01 /u02
[root@oracle /]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      97445468  29006156  63409424  32% /
/dev/sda1               101086     12070     83797  13% /boot
tmpfs                  3276800         0   3276800   0% /dev/shm
/dev/mapper/VolGroup01-lv01
                       5160576    141440   4756992   3% /u02

[info]9、增、减LV大小: lvextend[/info]

[root@oracle /]# umount /u02
[root@oracle /]# lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 95.94G                                     
  LogVol01 VolGroup00 -wi-ao  3.94G                                     
  lv01     VolGroup01 -wi-a-  5.00G                                     
[root@oracle /]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               15.31 GB
  PE Size               32.00 MB
  Total PE              490
  Alloc PE / Size       160 / 5.00 GB
  Free  PE / Size       330 / 10.31 GB
  VG UUID               pFJKA1-METh-vMph-9PbB-1fNC-sxV0-e9axc7

[root@oracle /]# lvextend  -L +100M /dev/VolGroup01/lv01       
  Rounding up size to full physical extent 128.00 MB
  Extending logical volume lv01 to 5.12 GB
  Logical volume lv01 successfully resized
[root@oracle /]# lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 95.94G                                     
  LogVol01 VolGroup00 -wi-ao  3.94G                                     
  lv01     VolGroup01 -wi-a-  5.12G                                     

[root@oracle /]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup01
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               15.31 GB
  PE Size               32.00 MB
  Total PE              490
  Alloc PE / Size       164 / 5.12 GB
  Free  PE / Size       326 / 10.19 GB
  VG UUID               pFJKA1-METh-vMph-9PbB-1fNC-sxV0-e9axc7

[info]10、扩充VG[/info]

[root@oracle /]# fdisk /dev/sdb

The number of cylinders for this disk is set to 3916.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help): p

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1000     8032468+  83  Linux
/dev/sdb2            1001        2000     8032500   83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2001-3916, default 2001):
Using default value 2001
Last cylinder or +size or +sizeM or +sizeK (2001-3916, default 3916): 3000     

Command (m for help): p

Disk /dev/sdb: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1000     8032468+  83  Linux
/dev/sdb2            1001        2000     8032500   83  Linux
/dev/sdb3            2001        3000     8032500   83  Linux

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@oracle ~]# pvcreate /dev/sdb3
  Physical volume "/dev/sdb3" successfully created

将新分区加入到物理卷组中
[root@oracle ~]# vgextend VolGroup01 /dev/sdb3
  Volume group "VolGroup01" successfully extended

查看物理卷组信息
[root@oracle ~]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   99.88G     0
  /dev/sdb1  VolGroup01 lvm2 a-    7.66G  2.53G
  /dev/sdb2  VolGroup01 lvm2 a-    7.66G  7.66G
  /dev/sdb3  VolGroup01 lvm2 a-   14.66G 14.66G

[info]11、扩充LV逻辑卷空间[/info]

查看逻辑卷组信息
[root@oracle ~]# lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 95.94G                                     
  LogVol01 VolGroup00 -wi-ao  3.94G                                     
  lv01     VolGroup01 -wi-a-  5.12G              

扩展逻辑卷lv01 500M空间
[root@oracle ~]# lvextend  -L +500M /dev/VolGroup01/lv01
  Rounding up size to full physical extent 512.00 MB
  Extending logical volume lv01 to 5.62 GB
  Logical volume lv01 successfully resized

[root@oracle ~]# pvs
  PV         VG         Fmt  Attr PSize  PFree
  /dev/sda2  VolGroup00 lvm2 a-   99.88G     0
  /dev/sdb1  VolGroup01 lvm2 a-    7.66G  2.03G
  /dev/sdb2  VolGroup01 lvm2 a-    7.66G  7.66G
  /dev/sdb3  VolGroup01 lvm2 a-   14.66G 14.66G

[root@oracle ~]# lvs
  LV       VG         Attr   LSize  Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao 95.94G                                     
  LogVol01 VolGroup00 -wi-ao  3.94G                                     
  lv01     VolGroup01 -wi-a-  5.62G 

挂载文件系统,查看a.txt仍在,说明扩展文件系统空间时,文件不会丢失                        
[root@oracle ~]# mount /dev/VolGroup01/lv01 /u02
[root@oracle ~]# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      97445468  29005128  63410452  32% /
/dev/sda1               101086     12070     83797  13% /boot
tmpfs                  3276800         0   3276800   0% /dev/shm
/dev/mapper/VolGroup01-lv01
                       5160576    141448   4756984   3% /u02
[root@oracle ~]# cd /u02
[root@oracle u02]# ll
total 24
-rw-r--r-- 1 root root    21 Jun 26 08:41 a.txt
drwx------ 2 root root 16384 Jun 26 08:39 lost+found

[info]12、缩小LV逻辑卷空间[/info]

[root@oracle /]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       93G   28G   61G  32% /
/dev/sda1              99M   12M   82M  13% /boot
tmpfs                 3.2G     0  3.2G   0% /dev/shm
/dev/mapper/VolGroup01-lv01
                      5.0G  139M  4.6G   3% /u02
[root@oracle /]# umount /u02

检查文件系统
[root@oracle /]# e2fsck -f  -y /dev/VolGroup01/lv01
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
/dev/VolGroup01/lv01: 12/655360 files (8.3% non-contiguous), 55938/1310720 blocks

重新定义文件系统大小,空间调整到300M
[root@oracle /]# resize2fs /dev/VolGroup01/lv01 300M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VolGroup01/lv01 to 76800 (4k) blocks.
The filesystem on /dev/VolGroup01/lv01 is now 76800 blocks long.

resize2fs /dev/mapper/VolGroup-lv_root 6105088


再次检查文件系统
[root@oracle /]# e2fsck -f /dev/VolGroup01/lv01     
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
/dev/VolGroup01/lv01: 12/49152 files (8.3% non-contiguous), 34994/76800 blocks

重新定义lv大小,调整到300M,这个要大于文件系统重新定义的空间                    
[root@oracle /]# lvreduce  -L 300 /dev/VolGroup01/lv01
  Rounding up size to full physical extent 320.00 MB
  WARNING: Reducing active logical volume to 320.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv01? [y/n]: y
  Reducing logical volume lv01 to 320.00 MB
  Logical volume lv01 successfully resized

查看逻辑卷信息,已经调整到300M
[root@oracle /]# lvs
  LV       VG         Attr   LSize   Origin Snap%  Move Log Copy%  Convert
  LogVol00 VolGroup00 -wi-ao  95.94G                                     
  LogVol01 VolGroup00 -wi-ao   3.94G                                     
  lv01     VolGroup01 -wi-a- 320.00M                                     
[root@oracle /]#

重新挂载文件系统,a.txt没有丢失,成功缩小了文件系统。
[root@oracle /]# mount /dev/VolGroup01/lv01 /u01
[root@oracle /]# cd u01
[root@oracle u01]# ls
a.txt

【微语】不要选择那个更好的人,而要选择那个能把你变得更好的人。

展开全文
热门推荐
内蒙古NCRE考试网上报名系统入口http;//www1.nm.zsks.cn/ncrebm04-23
http;//fzzs.hit.edu.cn/web/哈工大附中小升初报名系统04-28
baiyin.safetree.com.cn白银学校安全教育平台入口04-17
http;//tgbm.gzsedu.cn/Login贵州省特岗教师网上报名04-23
61.139.44.131/scwb自贡高考志愿填报04-23
梧州市学校安全教育平台https://wuzhou.xueanquan.com/04-27
http://cg.gxeea.cn:8668/广西成人高考录取结果查询04-27
A man enters a store to buy milk. He walks out of the store04-16
三明安全教育平台登录入口https://sanming.safetree.com.cn/04-24
dezhou.safetree.com.cn德州市学校安全教育平台04-23