Chinaunix首页 | 论坛 | 博客
  • 博客访问: 862854
  • 博文数量: 72
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1693
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-04 15:53
个人简介

主要从事Linux,云原生架构改造,服务网格,ELK,python,golang等相关技术。

文章分类

全部博文(72)

文章存档

2015年(52)

2014年(20)

分类: 系统运维

2014-09-17 14:16:49

       LVM是逻辑盘卷管理(Logical Volume Manager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。前面谈到,LVM是在磁盘分区和文件系统之间添加的一个逻辑层,来为文件系统屏蔽下层布局,提供一个抽象的盘卷,在盘卷上建立文件系统。物理卷(physical volume)物理卷就是指硬盘分区或从逻辑上与磁盘分区具有同样功能的设备(如RAID),是LVM的基本存储逻辑块,但和基本的物理存储介质(如分区、磁盘等)比较,却包含有与LVM相关的管理参数。

逻辑卷组的创建
1.在创建一个新的逻辑卷组的时候,我们首先要查看一下磁盘里面是否有可用的空间
[root@server10 ~]#fdisk -l    查看磁盘的使用情况
如果我们的磁盘被用完,可以手动加上去一块磁盘,然后做一下操作,开始创建我们想要的磁盘分区
[root@server10 ~]#fdisk /dev/sdb        //这里我们列举一个实例,假如加上去的是一个sdb设备


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes        //在这里我们可以查看到sdb这个设备的总空间大小为2G
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2f32c6b

   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-261, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-261, default 261): +1024M

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2f32c6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  83  Linux

Command (m for help): t       //这里我们去改变磁盘分区的类型
Selected partition 1
Hex code (type L to list codes): l    //列出所有的磁盘分区类型

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           39  Plan 9          82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      3c  PartitionMagic  83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       40  Venix 80286     84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      41  PPC PReP Boot   85  Linux extended  c7  Syrinx         
 5  Extended        42  SFS             86  NTFS volume set da  Non-FS data    
 6  FAT16           4d  QNX4.x          87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS       4e  QNX4.x 2nd part 88  Linux plaintext de  Dell Utility   
 8  AIX             4f  QNX4.x 3rd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    50  OnTrack DM      93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 51  OnTrack DM6 Aux 94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       52  CP/M            9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 54  OnTrackDM6      a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 55  EZ-Drive        a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            56  Golden Bow      a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    5c  Priam Edisk     a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 61  SpeedStor       a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 63  GNU HURD or Sys ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    64  Novell Netware  af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 65  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  70  DiskSecure Mult b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 75  PC/IX           bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 80  Old Minix       be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1
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/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2f32c6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  8e  Linux LVM

Command (m for help): wq        //保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
2.接下来我们对新建的这个分区进行lvm的创建
[root@server10 ~]# pvcreate /dev/sdb1 
  Physical volume "/dev/sdb1" successfully created
[root@server10 ~]# vgcreate vg0 /dev/sdb1 
  Volume group "vg0" successfully created
[root@server10 ~]# lvcreate -L 20M -n lv1 vg0
  Logical volume "lv1" created
[root@server10 ~]# mkfs.ext4 /dev/vg0/lv1 
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
5136 inodes, 20480 blocks
1024 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=20971520
3 block groups
8192 blocks per group, 8192 fragments per group
1712 inodes per group
Superblock backups stored on blocks: 
8193

Writing inode tables: done                            
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@server10 ~]# mount /dev/vg0/lv1 /sdba1/    //接下来对我们格式化好的分区进行挂载
[root@server10 ~]# df -h
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.1G   14G  23% /
tmpfs                495M  224K  495M   1% /dev/shm
/dev/sda1            194M   53M  132M  29% /boot
/dev/sr0             3.6G  3.6G     0 100% /mnt/redhat
/dev/mapper/vg0-lv1   20M  1.2M   18M   7% /sdba1        //到这里我们的整个分区的创建就算结束

分区的扩展:
有时候我们会发现我们正在用的某个分区空间不够用,但是我们又不能重新去卸载,换到另外一个分区上面去,这个时候我们就要用到磁盘分区的扩展功能。
1.新创建一个sdb2逻辑分区,并更新分区表
[root@server10 ~]#fdisk /dev/sdb 
[root@server10 ~]# partx -d /dev/sdb            //这里这两条命令是更新某块设备的分区表
error deleting partition 1: BLKPG: Device or resource busy
error deleting partitions 3-256: BLKPG: No such device or address
[root@server10 ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
2.接下来对其进行初始化
[root@server10 ~]# pvcreate /dev/sdb2    
  dev_is_mpath: failed to get device for 8:18
  Physical volume "/dev/sdb2" successfully created
3.将PV加入VG组
[root@server10 ~]# vgextend vg0 /dev/sdb2
  Volume group "vg0" successfully extended
[root@server10 ~]# pvs
  PV         VG   Fmt  Attr PSize   PFree   
  /dev/sdb1  vg0  lvm2 a--    1.01g 1012.00m
  /dev/sdb2  vg0  lvm2 a--  516.00m  516.00m    //我们可以看到sdb2这个设备已经加入到vg0这个卷组里面了
[root@server10 ~]# vgs
  VG   #PV #LV #SN Attr   VSize VFree
  vg0    2   1   0 wz--n- 1.51g 1.49g
4、将VG组中的空闲空间划出100M到/sdba1分区所在的LV
[root@server10 ~]# lvextend -L +100M /dev/vg0/lv1 
  Extending logical volume lv1 to 120.00 MiB
  Logical volume lv1 successfully resized
5、使用resizefs2命令重新加载逻辑卷的大小才能生效
[root@server10 ~]# resize2fs /dev/vg0/lv1 
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg0/lv1 is mounted on /sdba1; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg0/lv1 to 122880 (1k) blocks.
The filesystem on /dev/vg0/lv1 is now 122880 blocks long.
[root@server10 ~]# df -h    
Filesystem           Size  Used Avail Use% Mounted on
/dev/sda2             20G  4.1G   14G  23% /
tmpfs                495M  224K  495M   1% /dev/shm
/dev/sda1            194M   53M  132M  29% /boot
/dev/sr0             3.6G  3.6G     0 100% /mnt/redhat
/dev/mapper/vg0-lv1  117M  1.5M  110M   2% /sdba1    
我们可以看到sdba1的空间由原来的20M增加到了117M证明扩展成功

LVM逻辑卷组的删除:
1.首先要卸载这个设备
[root@server10 ~]# umount /sdba1/
2.再对其所在的逻辑卷进行操作
[root@server10 ~]# lvremove /dev/
Display all 185 possibilities? (y or n)
[root@server10 ~]# lvremove /dev/vg0/lv1 
Do you really want to remove active logical volume lv1? [y/n]: y
  Logical volume "lv1" successfully removed
[root@server10 ~]# vgremove /dev/vg0
  Volume group "vg0" successfully removed
[root@server10 ~]# pvremove /dev/sdb2
  Labels on physical volume "/dev/sdb2" successfully wiped
[root@server10 ~]# pvremove /dev/sdb1
  Labels on physical volume "/dev/sdb1" successfully wiped
2.然后进入磁盘分区进行删除操作:
[root@server10 ~]# fdisk /dev/sdb


WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2f32c6b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  8e  Linux LVM
/dev/sdb2             133         198      530145   8e  Linux LVM

Command (m for help): d
Partition number (1-4): 1

Command (m for help): d
Selected partition 2

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xd2f32c6b

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

接下来我们介绍一下lvm做快照:
1.首先我们要查看VG的大小
[root@server10 ~]# vgs
  VG   #PV #LV #SN Attr   VSize  VFree 
  vg0    1   1   0 wz--n- 28.00m  8.00m
  vg1    1   0   0 wz--n- 36.00m 36.00m
[root@server10 ~]# lvcreate -L 20M -n lv2 -s /dev/vg0/lv1
接下来我们就可以对这个快照进行使用了。






阅读(3010) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~