LVM

1.      创建LVM

1.         准备物理卷sdb1
2.         vgscan,初始化LVM(这个命令只运行就可以吗?)
重复做实验时,提示如下
[root@ninglianjie_app_1 ~]# vgscan
  Reading all physical volumes.  This may take a while...
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  No volume groups found
3.         指定物理卷
[root@ninglianjie_app_1 ~]# pvcreate  /dev/sdb1
  Wiping software RAID md superblock on /dev/sdb1
  Physical volume "/dev/sdb1" successfully created
4.         创建卷组
[root@ninglianjie_app_1 ~]# vgcreate  VG1 /dev/sdb1
  Volume group "VG1" successfully created
5.         增加卷组大小
[root@ninglianjie_app_1 ~]# vgextend VG1 /dev/sdc1
  /dev/cdrom: open failed: 只读文件系统
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  Volume group "VG1" successfully extended
6.         查看卷组状态
[root@ninglianjie_app_1 ~]# vgdisplay
  --- Volume group ---
  VG Name               VG1
  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               9.86 GB
  PE Size               4.00 MB
  Total PE              2525
  Alloc PE / Size       125 / 500.00 MB
  Free  PE / Size       2400 / 9.38 GB
  VG UUID               RMT3aA-XRau-coeV-H40J-72TX-h0eM-bvu0g5
7.         减小卷组大小(注意卸载的设备,不能被分到逻辑卷)
[root@ninglianjie_app_1 ~]# vgreduce VG1 /dev/sdc1
  Removed "/dev/sdc1" from volume group "VG1"
 

2.      创建逻辑卷组

8.         创建逻辑卷组(调整逻辑卷的大小要解除挂载)
[root@ninglianjie_app_1 ~]# lvcreate -n LV1 -L 500M VG1
  /dev/cdrom: open failed: 只读文件系统
  Logical volume "LV1" already exists in volume group "VG1"
[root@ninglianjie_app_1 ~]# mkfs.ext3 /dev/VG1/LV1
9.         增加逻辑卷(先增大逻辑卷,再增大逻辑卷的文件系统)
[root@ninglianjie_app_1 ~]# lvextend -L +200M /dev/VG1/LV1
  Extending logical volume LV1 to 700.00 MB
  Logical volume LV1 successfully resized
[root@ninglianjie_app_1 ~]# e2fsck -f /dev/VG1/LV1
[root@ninglianjie_app_1 ~]# resize2fs /dev/VG1/LV1
10.     减小逻辑卷(先缩小文件系统,再缩小逻辑卷)
[root@ninglianjie_app_1 ~]# e2fsck -f /dev/VG1/LV1
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/VG1/LV1: 11/89664 files (9.1% non-contiguous), 7102/179200 blocks
[root@ninglianjie_app_1 ~]# resize2fs /dev/VG1/LV1 400M  
 *调整后为400M
resize2fs 1.39 (29-May-2006)
Resizing the filesystem on /dev/VG1/LV1 to 102400 (4k) blocks.
The filesystem on /dev/VG1/LV1 is now 102400 blocks long.
[root@ninglianjie_app_1 ~]# e2fsck -f /dev/VG1/LV1
[root@ninglianjie_app_1 ~]# lvreduce -L -100M /dev/VG1/LV1 
*以减的大小为主
  WARNING: Reducing active logical volume to 600.00 MB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce LV1? [y/n]: y
  Reducing logical volume LV1 to 600.00 MB
  Logical volume LV1 successfully resized

3.      LVM快照

11.     创建快照
[root@ninglianjie_app_1 ~]# lvcreate -L 4M -s -n kuaizhao1 /dev/VG1/LV1
  Logical volume "kuaizhao1" created
12.     快照文件
[root@ninglianjie_app_1 data1]# find / -name kuaizhao1
/dev/VG1/kuaizhao1
 
[root@ninglianjie_app_1 data1]#
[root@ninglianjie_app_1 data1]#
[root@ninglianjie_app_1 data1]# cd /dev/VG1/
[root@ninglianjie_app_1 VG1]# ls
kuaizhao1  LV1
[root@ninglianjie_app_1 VG1]# ll
总计 0
lrwxrwxrwx 1 root root 25 11-09 19:19 kuaizhao1 -> /dev/mapper/VG1-kuaizhao1
13.     挂载逻辑卷快照
[root@ninglianjie_app_1 VG1]# mount /dev/mapper/VG1-kuaizhao1 /data2
14.     卸载逻辑卷快照
[root@ninglianjie_app_1 data2]# cd
[root@ninglianjie_app_1 ~]# umount /dev/mapper/VG1-kuaizhao1 /data2
umount: /dev/mapper/VG1-kuaizhao1: not mounted
umount: /dev/mapper/VG1-kuaizhao1: not mounted
[root@ninglianjie_app_1 ~]# lvremove /dev/mapper/VG1-kuaizhao1
Do you really want to remove active logical volume "kuaizhao1"? [y/n]: y
  Logical volume "kuaizhao1" successfully removed

4.      移动卷

15.     移动物理卷的数据,到新加入的物理卷上
[root@ninglianjie_app_1 ~]# pvcreate /dev/sdc1        
*指定物理卷
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  Physical volume "/dev/sdc1" successfully created
[root@ninglianjie_app_1 ~]# vgextend VG1 /dev/sdc1     
*加入卷组
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  Volume group "VG1" successfully extended
[root@ninglianjie_app_1 ~]# pvmove /dev/sdb1 /dev/sdc1  
*移动卷内数据
  /dev/sdb1: Moved: 100.0%
16.     将数据迁移到其他系统
17.     导出卷组
[root@ninglianjie_app_1 ~]# umount /data1              
*卸载卷
[root@ninglianjie_app_1 ~]# vgchange -a n VG1         
* -a改变状态、n不生效
  0 logical volume(s) in volume group "VG1" now active
[root@ninglianjie_app_1 ~]# vgexport VG1
  /dev/cdrom: open failed: 只读文件系统
  Volume group "VG1" successfully exported
通过以上三个命令,若在真实环境中,则可以关机,拔硬盘。
18.     导入卷组
[root@ninglianjie_app_1 ~]# pvscan                   
*扫描物理卷
  /dev/cdrom: open failed: 只读文件系统
  Attempt to close device '/dev/cdrom' which is not open.
  PV /dev/sdb1    is in exported VG VG1 [1.87 GB / 1.87 GB free]
  PV /dev/sdc1    is in exported VG VG1 [8.00 GB / 7.41 GB free]
  Total: 2 [9.86 GB] / in use: 2 [9.86 GB] / in no VG: 0 [0   ]
[root@ninglianjie_app_1 ~]# vgimport VG1             
*导入卷组
  Volume group "VG1" successfully imported
[root@ninglianjie_app_1 ~]# vgchange -a y VG1         
*设置为生效
  1 logical volume(s) in volume group "VG1" now active
[root@ninglianjie_app_1 ~]# mount /dev/VG1/LV1 /data1