LVM逻辑卷管理

实验:
本实验在虚拟机运行,运行时需要增加一块硬盘分区:
分区:
fdisk /dev/sdb
Disk /dev/sdb: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        2610    20964793+   5  Extended
/dev/sdb5               1         244     1959867   8e  Linux LVM
/dev/sdb6             245         488     1959898+  8e  Linux LVM
/dev/sdb7             489         732     1959898+  8e  Linux LVM
/dev/sdb8             733         976     1959898+  8e  Linux LVM
 
加入物理卷:
pvcreate /dev/sdb
sdb   sdb1  sdb5  sdb6  sdb7  sdb8 
[root @localhost ~]# pvcreate /dev/sdb{5,6,7,8}
  Physical volume "/dev/sdb5" successfully created
  Physical volume "/dev/sdb6" successfully created
  Physical volume "/dev/sdb7" successfully created
  Physical volume "/dev/sdb8" successfully created
 
查看物理卷信息:
pvdisplay
"/dev/sdb8" is a new physical volume of "1.87 GB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb8        #实际分区设备名称
  VG Name                                       #因为尚未分配出去所以空白
  PV Size               1.87 GB                #容量
  Allocatable           NO                    #是否已被分配,结果是NO
  PE Size (KByte)       0                       #在此PV内的PE大小
  Total PE              0                        #共分出几个PE
  Free PE               0                        #没被LV用掉的PE
  Allocated PE          0                    #尚可分配出去的PE容量
  PV UUID               q5NEcF-WC6V-XoH5-sSRk-EcE2-dSRk-oelHhJ
 
新建VG卷:
vgcreate -s 16M vgyi /dev/sdb{5,6,7}            #PE的大小为16M
 Volume group "vgyi" successfully created
 
查看VG:
pvscan
  PV /dev/sdb5   VG vgyi            lvm2 [1.86 GB / 1.86 GB free]
  PV /dev/sdb6   VG vgyi            lvm2 [1.86 GB / 1.86 GB free]
  PV /dev/sdb7   VG vgyi            lvm2 [1.86 GB / 1.86 GB free]
  PV /dev/sda2   VG VolGroup00      lvm2 [19.88 GB / 0    free]
  PV /dev/sdb8                      lvm2 [1.87 GB]                                    #sdb8没有加入VG
  Total: 5 [27.32 GB] / in use: 4 [25.45 GB] / in no VG: 1 [1.87 GB]
 
 
vgdisplay
  --- Volume group ---
  VG Name               vgyi
  System ID            
  Format                lvm2
  Metadata Areas        3
  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                3
  Act PV                3
  VG Size               5.58 GB            #整体的VG容量有这么大
  PE Size               16.00 MB            #内部每个PE的大小
  Total PE              357                    #总共的PE数量共有这么多
  Alloc PE / Size       0 / 0  
  Free  PE / Size       357 / 5.58 GB
  VG UUID               pW0cZ4-4YYv-aKIf-PNCo-G2gi-joPu-MxMOsp
 
往VG里添加一个PV:
vgextend vgyi /dev/sdb8
  Volume group "vgyi" successfully extended
 
建立LV:
lvcreate -L +5.58G -n lvyi vgyi
  Rounding up size to full physical extent 5.59 GB
  Logical volume "lvyi" created
也可用PE方式创建:
lvcreate -l 357 -n lvpure vgyi

  Logical volume "lvpure" created
 
查看LV容量:
lvdisplay
  --- Logical volume ---
  LV Name                /dev/vgyi/lvpure
  VG Name                vgyi
  LV UUID                fE5ZQ9-nShY-wEh8-jb0s-Mku4-YwmW-ZycS7J
  LV Write Access        read/write
  LV Status              available
  # open                 0
  LV Size                5.58 GB            #lv的总容量
  Current LE             357
  Segments               3
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
 
挂载和格式化:
mkfs -t ext3 /dev/vgyi/lvpure
mkdir /dd
mount /dev/vgyi/lvpure /dd
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       19G  3.1G   15G  18% /
/dev/sda1              99M   12M   82M  13% /boot
tmpfs                1014M     0 1014M   0% /dev/shm
/dev/mapper/vgyi-lvpure
                      5.5G  140M  5.1G   3% /dd
cp -a /etc /var/log /dd        #放点东西在里做验证

放大LV容量:
再在添加的磁盘上创建一个容量一样的分区:
fdisk /dev/sdb
/dev/sdb9             977        1220     1959898+  8e  Linux LVM
新建新的PV:
pvcreate /dev/sdb9

 Physical volume "/dev/sdb9" successfully created
pvscan
  PV /dev/sdb5   VG vgyi            lvm2 [1.86 GB / 0    free]
  PV /dev/sdb6   VG vgyi            lvm2 [1.86 GB / 0    free]
  PV /dev/sdb7   VG vgyi            lvm2 [1.86 GB / 0    free]
  PV /dev/sdb8   VG vgyi            lvm2 [1.86 GB / 1.86 GB free]
  PV /dev/sda2   VG VolGroup00      lvm2 [19.88 GB / 0    free]
  PV /dev/sdb9                      lvm2 [1.87 GB]
 
加大VG:
vgextend vgyi /dev/sdb9
  Volume group "vgyi" successfully extended
vgdisplay
  --- Volume group ---
  VG Name               vgyi
  System ID            
  Format                lvm2
  Metadata Areas        5
  Metadata Sequence No  8
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                1
  Open LV               1
  Max PV                0
  Cur PV                5
  Act PV                5
  VG Size               9.30 GB
  PE Size               16.00 MB
  Total PE              595
  Alloc PE / Size       357 / 5.58 GB
  Free  PE / Size       238 / 3.72 GB
  VG UUID               pW0cZ4-4YYv-aKIf-PNCo-G2gi-joPu-MxMOsp
 
加大LV:
lvresize -L +2G /dev/vgyi/lvpure
  Extending logical volume lvpure to 7.58 GB
  Logical volume lvpure successfully resized
#这样就增加了,lvresize的语法基本上通过-l跟-L来增加
#要增加用+,要减少用-
 
查看lv:
lvdisplay
  --- Logical volume ---
  LV Name                /dev/vgyi/lvpure
  VG Name                vgyi
  LV UUID                fE5ZQ9-nShY-wEh8-jb0s-Mku4-YwmW-ZycS7J
  LV Write Access        read/write
  LV Status              available
  # open                 1
  LV Size                7.58 GB
  Current LE             485
  Segments               5
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2
 
df -h /dd
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgyi-lvpure
                      5.5G  140M  5.1G   3% /dd
#lv卷的容量还是没有增加,用以下方法
dumpe2fs /dev/vgyi/lvpure
resize2fs /dev/vgyi/lvpure
df -h /dd
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgyi-lvpure
                      7.5G  141M  7.0G   2% /dd
#容量就增加了,查看下磁盘里的东西
ll /dd
total 40
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  14 root root  4096 May 19 12:49 log
drwx------   2 root root 16384 May 19 12:32 lost+found
#东西都还在
 
缩小LV容量:
e2fsck -f /dev/vgyi/lvpure
resize2fs /dev/vgyi/lvpure 5G
#“resize2fs /dev/vgyi/lvpure 5G 重新分配lv卷的大小,如果你写1G那么你的卷挂载上只有1G了
#反过来说你写10G那么它就只有10G大。
mount /dev/vgyi/lvpure /dd
df -h /dd
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vgyi-lvpure
                      5.0G  240M  4.6G   5% /dd
 
 
LVM的系统快照:
lvcreate -L 1G -s -n lvbb /dev/vgyi/lvpure
  Logical volume "lvbb" created
#-s是创建快照的意思
lvdisplay
--- Logical volume ---
  LV Name                /dev/vgyi/lvbb
  VG Name                vgyi
  LV UUID                9fBl0m-a8qI-KcQn-30hW-botf-5CnF-zusgKQ
  LV Write Access        read/write
  LV snapshot status     active destination for /dev/vgyi/lvpure
  LV Status              available
  # open                 0
  LV Size                7.58 GB                    #被快照的园LV容量
  Current LE             485
  COW-table size         1.00 GB                #实际容量
  COW-table LE           64
  Allocated to snapshot  0.00%
  Snapshot chunk size    4.00 KB
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:3
 
mkdir /dd1
mount /dev/vgyi/lv
lvbb    lvpure 
mount /dev/vgyi/lvbb /dd1
df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                       19G  3.1G   15G  18% /
/dev/sda1              99M   12M   82M  13% /boot
tmpfs                1014M     0 1014M   0% /dev/shm
/dev/mapper/vgyi-lvpure
                      5.0G  240M  4.6G   5% /dd
/dev/mapper/vgyi-lvbb
                      5.0G  240M  4.6G   5% /dd1
 
umount /dd1/
ll /dd
total 40
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  14 root root  4096 May 19 12:49 log
drwx------   2 root root 16384 May 19 12:32 lost+found
 
rm rm -rf /dd/log/
cp -a /boot /lib /sbin /dd
dd/  dd1/
cp -a /boot /lib /sbin /dd
ll /dd
total 64
drwxr-xr-x   4 root root  4096 Dec 27  2009 boot
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  13 root root  4096 May 19 12:48 lib
drwx------   2 root root 16384 May 19 12:32 lost+found
drwxr-xr-x   2 root root 12288 May 19 12:49 sbin
 
mount /dev/vgyi/lvbb /dd1
df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      19679908   3244092  15420008  18% /
/dev/sda1               101086     12058     83809  13% /boot
tmpfs                  1037728         0   1037728   0% /dev/shm
/dev/mapper/vgyi-lvpure
                       5161216    357596   4646336   8% /dd
/dev/mapper/vgyi-lvbb
                       5161216    245728   4758204   5% /dd1
 
tar zcfv ddd.tar.gz /dd1/*

#把备份区的东西压缩出来
umount /dd1
lvremove /dev/vgyi/lvbb
Do you really want to remove active logical volume lvbb? [y/n]: y
  Logical volume "lvbb" successfully removed
umount /dd                                #卸载分区
mkfs -t ext3 /dev/vgyi/lvpure     #格式化分区
mount /dev/vgyi/lvpure /dd/    #重新挂载
tar zxfv ddd.tar.gz -C /dd/        #吧刚才备份分区的压缩包解压到挂载目录
ll /dd/
total 20
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  14 root root  4096 May 19 12:49 log
drwx------   2 root root  4096 May 19 12:32 lost+found
东西又回来了。
 
快照实验二:
lvcreate -s -L 1.5G -n lvdd /dev/vgyi/lvpure
  Logical volume "lvdd" created
umount /dd/
mount /dev/vgyi/lvdd /dd1/
df -h /dev/vgyi/lvdd
Filesystem            Size  Used Avail Use% Mounted on
-                    1014M   88K 1014M   1% /dev
rm -rf /dd1/log/ /dd1/etc/
cp -a /boot/ /lib /sbin /dd1/
ll /dd1/
total 36
drwxr-xr-x  4 root root  4096 Dec 27  2009 boot
drwxr-xr-x 13 root root  4096 May 19 12:48 lib
drwx------  2 root root  4096 May 19 12:32 lost+found
drwxr-xr-x  2 root root 12288 May 19 12:49 sbin                #跟原本的数据有差异了
mount /dev/vgyi/lvpure /dd/
ll /dd/
total 20
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  14 root root  4096 May 19 12:49 log
drwx------   2 root root  4096 May 19 12:32 lost+found
#原本的数据没有改变
 
还原快照区域的数据,跟主文件信息相同
umount /dd1/
lvremove /dev/vgyi/lvdd
Do you really want to remove active logical volume lvdd? [y/n]: y
  Logical volume "lvdd" successfully removed
lvcreate -s -L 1.5G -n lvdd /dev/vgyi/lvpure
  Logical volume "lvdd" created
mount /dev/vgyi/lvdd /dd1/
ll /dd1/
total 20
drwxr-xr-x 102 root root 12288 May 19 12:49 etc
drwxr-xr-x  14 root root  4096 May 19 12:49 log
drwx------   2 root root  4096 May 19 12:32 lost+found
#这样快照区域的数据又复原了
 
LVM的关闭:
umount /dd1/
umount /dd
lvremove /dev/vgyi/lvdd
Do you really want to remove active logical volume lvdd? [y/n]: y
  Logical volume "lvdd" successfully removed
[root @localhost ~]# lvremove /dev/vgyi/lvpure
Do you really want to remove active logical volume lvpure? [y/n]: y
  Logical volume "lvpure" successfully removed
vgchange -a n vgyi
  0 logical volume(s) in volume group "vgyi" now active
[root @localhost ~]# vgremove vgyi
  Volume group "vgyi" successfully removed
pvremove /dev/sdb{5,6,7,8,9}
  Labels on physical volume "/dev/sdb5" successfully wiped
  Labels on physical volume "/dev/sdb6" successfully wiped
  Labels on physical volume "/dev/sdb7" successfully wiped
  Labels on physical volume "/dev/sdb8" successfully wiped
  Labels on physical volume "/dev/sdb9" successfully wiped
#如果在/etc/fstab下面写的还有开机启动LVM的话,把那条信息删除
#再把磁盘的类型改为82就行了
#创建顺序:PV--VG--LV
 

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