LVM 管理

创建LVM

  • 划分分区,并指定适合的LVM的ID
    [root@centos6 rf]# fdisk/dev/sdc使用fdisk划分一个分区;也可直接拿块新硬盘直接创建LVM逻辑卷
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x3ca76d2c.
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)

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 查看/dev/sdc硬盘的分区情况;由于是新硬盘所以没有分区表

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x3ca76d2c

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n 表示创建一个分区
Command action
   e   extended
   p   primary partition (1-4)
p   表示主分区  MBR分区表最多只允许4个主分区或3个主分区加一个扩展分区,可以扩展分区创建多个逻辑分区(e|p) e表示扩展分区;p表示主分区
Partition number (1-4): 1   分区号
First cylinder (1-2610, default 1):   指定开始的柱面这里如果不指定表示选择的是默认柱面,扩展分区不要让系统默认否则会出错
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G  创建的大小可以指定柱面 也可以指定空间的大小

Command (m for help): t 调整分区的ID
Selected partition 1
Hex code (type L to list codes): l 查看系统默认支持的所有分区ID

 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 这里选择8e 8e表示LVM
Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p 查看分区情况

Disk /dev/sdc: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x3ca76d2c

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  8e  Linux LVM
Command (m for help): w保存退出
The partition table has been altered!

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

  • 注意 这里此磁盘分区已同步到内存中centos6新增磁盘同步分区表的
  • partx -a /dev/DEVICE 或者 kpartx -a /dev/DEVICE -f: force
  • centos6删除分区表同步的方法
  • partx -d --nr M-N /dev/DEVICE
  • CentOS 5 ,7: 增加分区或删除分区 使用partprobe

分区已划分创建LVM

  • 创建物理卷
  • [root@centos6 rf]# pvcreate /dev/sdc1
  • Physical volume "/dev/sdc1" successfully created
  • [root@centos6 rf]# pvdisplay 查看系统上所有的物理卷的详细信息
"/dev/sdc1" is a new physical volume of "10.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdc1
  VG Name                PV Size               10.00 GiB
  Allocatable           NO
   PE Size               0   
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               n4NV1m-sr2p-A5K0-ckl4-681D-ah7g-HtYjmo
  • [root@centos6 rf]# pvs 显示系统上物理卷的信息
 PV         VG   Fmt  Attr PSize  PFree 
 /dev/sdc1       lvm2 ---- 10.00g 10.00g
  • [root@centos6 rf]# vgcreate -s 10M vg0 /dev/sdc1 创建卷组 -s 指定PE大小 可以省略,省略表示选择默认值 后面跟的是卷组的名字 指定从哪个物理卷划分
  • Volume group "vg0" successfully created
  • [root@centos6 rf]# vgdisplay 查看卷组相关的信息
--- Volume group ---
 VG Name               vg0   卷组名称
 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               10.00 GiB 卷组总大小
 PE Size               10.00 MiB PE大小
 Total PE              1024 PE个数
 Alloc PE / Size       0 / 0   
 Free  PE / Size       1024 / 10.00 GiB
 VG UUID               ZujzGf-nNnJ-LfZb-ge6F-fiAP-MtHW-EepPhI
  • [root@centos6 rf]# vgs 查看系统中的卷组
VG   #PV #LV #SN Attr   VSize  VFree 
vg0    1   0   0 wz--n- 10.00g 10.00g

[root@centos6 rf]# lvcreate -L 10G -n cx vg0 创建逻辑卷 -L 表示指定空间大小 -n 表示指定逻辑卷的名字 后面跟卷组名字表示从哪个卷组中划分逻辑分区

  Logical volume "cx" created.

[root@centos6 rf]# mkfs.ext4 /dev/vg0/cx 创建文件系统

mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done                            
Creating journal (32768 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@centos6 rf]# mkdir /cx 创建一个目录做挂载点
  • [root@centos6 rf]# vim /etc/fstab 编辑 /etc/fstab添加“UUID=d7ab49ba-156c-4ca4-8e57-d2c6e8dd3076 /cx ext4 defaults 1 0
  • [root@centos6 ~]# blkid /dev/vg0/cx 查看UUID
/dev/vg0/cx: UUID="d7ab49ba-156c-4ca4-8e57-d2c6e8dd3076" TYPE="ext4"
  • [root@centos6 rf]# mount -a 挂载/etc/fstab 文件中未挂载的设备
    [root@centos6 rf]# mount 查看已挂载的设备
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw,usrquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/vg0-cx on /cx type ext4 (rw)

扩展LVM分区

  • 1如果卷组里还有多余的空间,可以直接扩展逻辑卷;如果没有多余的空间就需要先扩展卷组
  • 我这里先用一块新硬盘扩展物理卷再扩展卷组最后扩展逻辑分区
  • [root@centos6 ~]# pvcreate /dev/sdd # 扩展物理卷 ;如果有多余的物理卷,可以直接扩展卷组
  Physical volume "/dev/sdd" successfully created

[root@centos6 ~]# pvs 查看物理卷PV的信息

  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sdc1  vg0  lvm2 a--u 10.00g     0 
  /dev/sdd        lvm2 ---- 60.00g 60.00g

[root@centos6 ~]# vgextend vg0 /dev/sdd 使用vgextend 扩展卷组(vg) 后面跟卷组名 指定把那个物理卷扩展到卷组

  Volume group "vg0" successfully extended
  • [root@centos6 ~]# vgdisplay
  --- Volume group ---
  VG Name               vg0  卷组名字
  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               1
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               69.99 GiB 卷组总大小
  PE Size               10.00 MiB PE的大小
  Total PE              7167  PE个数
  Alloc PE / Size       1024 / 10.00 GiB  已分配的给逻辑卷(lv)使用的空间
  Free  PE / Size       6143 / 59.99 GiB  未分配空间
  VG UUID               ZujzGf-nNnJ-LfZb-ge6F-fiAP-MtHW-EepPhI

[root@centos6 ~]# lvextend -r -l +1000 /dev/vg0/cx 扩展逻辑卷并同步文件系统-r 表示同步文件系统

  Size of logical volume vg0/cx changed from 10.00 GiB (1024 extents) to 19.77 GiB (2024 extents).
  Logical volume cx successfully resized.
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/mapper/vg0-cx is mounted on /cx; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 2
Performing an on-line resize of /dev/mapper/vg0-cx to 5181440 (4k) blocks.
The filesystem on /dev/mapper/vg0-cx is now 5181440 blocks long.
注意扩展逻辑卷无需取消挂载;建议提前备份数据

缩减逻辑卷

  • 先取消挂载
  • [root@centos6 ~]# umount /dev/vg0/cx
  • 检查文件系统完整性,这一步必须否则系统不让缩
  • [root@centos6 ~]# e2fsck -f /dev/vg0/cx
e2fsck 1.41.12 (17-May-2010)
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/vg0/cx: 11/1302528 files (0.0% non-contiguous), 121551/5181440 blocks
  • 缩减文件系统到15G
    [root@centos6 ~]# resize2fs /dev/vg0/cx 15G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg0/cx to 3932160 (4k) blocks.
The filesystem on /dev/vg0/cx is now 3932160 blocks long.
  • 缩减逻辑卷到15G,注意这里缩减的大小与文件系统大小一致
  • [root@centos6 ~]# lvreduce -L 15G /dev/vg0/cx
 WARNING: Reducing active logical volume to 15.00 GiB.
 THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg0/cx? [y/n]: y  提醒用户缩减空间可能会损坏数据
 Size of logical volume vg0/cx changed from 19.77 GiB (2024 extents) to 15.00 GiB (1536 extents).
 Logical volume cx successfully resized.

缩减逻辑卷成

创建逻辑卷快照

  • [root@centos6 ~]# mount -a 挂载文件系统
  • [root@centos6 ~]# mount
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda6 on /home type ext4 (rw,usrquota)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/vg0-cx on /cx type ext4 (rw)

拷贝数据

  • [root@centos6 ~]# cp /etc/passwd /cx/
  • [root@centos6 ~]# ls /cx/
lost+found  passwd
  • 创建快照
  • [root@centos6 ~]# lvcreate -L 1G -s -n cx-date -p r /dev/vg0/cx
  Rounding up size to full physical extent 1.01 GiB
  Logical volume "cx-date" created.
  • [root@centos6 ~]# mkdir ll
  • [root@centos6 ~]# mount /dev/vg0/cx-date ll
mount: block device /dev/mapper/vg0-cx--date is write-protected, mounting read-only  挂载快照
  • [root@centos6 ~]# ls l 查看快照
lost+found  passwd
  • [root@centos6 ~]# rm -rf /cx/passwd 删除源文件的部分数据
    取消逻辑卷与快照的挂载
  • [root@centos6 ~]# umount /cx/
  • [root@centos6 ~]# umount ll/
  • 恢复快照 注意恢复快照会被删除
  • [root@centos6 ~]# lvconvert --merge /dev/vg0/cx-date
    Merging of volume cx-date started.
    cx: Merged: 100.0%
    Merge of snapshot into logical volume cx has finished.
    Logical volume "cx-date" successfully removed
    
  • 快照被删除
  • [root@centos6 ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/vg0/cx
  LV Name                cx
  VG Name                vg0
  LV UUID                YqujVe-SSvL-BDFZ-TuPu-YMTg-lXTe-DnlubY
  LV Write Access        read/write
  LV Creation host, time centos6.chenxi.com, 2017-06-24 08:52:01 +0800
  LV Status              available
  # open                 0
  LV Size                15.00 GiB
  Current LE             1536
  Segments               2
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

查看是否成功恢复

  • [root@centos6 ~]# mount -a
  • [root@centos6 ~]# cat /cx/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin......

迁移LVM到其他主机,先取消挂载

  • 迁移前改个名字 如果目标主机没有重复名字的卷组就不用更改
  • vgrename 原名 新名字
  • lvrename 原名 新名字 改逻辑卷的名字
  • umount /cx/取消挂载
  • pvmove /dev/sdc1 迁移PE空间,迁移前应先查看卷组PE空间是否够sdc1 PE空间使用,如果不够应先扩展卷组空间
  • vgchange -an vg0 禁用卷组
  • vgexport vg0 导出卷组
  • 拔出对应的硬盘拔出
    在目标计算机上
  • 在新系统中安装旧硬盘,并vgimport vg0.
  • vgchange –ay vg0 启用卷组
  • mount 挂载

你可能感兴趣的:(LVM 管理)