1、查看现在磁盘列表并格式化新增磁盘(也可不用格式化)
[root@localhost ~]# fdisk -l
Disk /dev/sdb: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 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: 0x00000000
[root@localhost ~]# mkfs.ext4 /dev/sdb
--------------------------------------------------------------------------------------------------------------------
2、创建PV物理卷
[root@localhost ~]# pvcreate /dev/sdb
Physical volume "/dev/sdb" successfully created
[root@localhost ~]# pvdisplay
"/dev/sdb" is a new physical volume of "50.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb
VG Name
PV Size 50.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID L8IPCU-dBPc-r3B1-5aRL-crTr-amaa-Qrui4y
[root@localhost ~]#
--------------------------------------------------------------------------------------------------------------------
3、创建一个VG卷组
[root@localhost ~]# vgcreate Vg_web /dev/sdb
Volume group "Vg_web" successfully created
#创建一个名称为Vg_web用于存放web站点目录的卷组
[root@localhost ~]# vgdisplay
--- Volume group ---
VG Name Vg_web
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 50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 0 / 0
Free PE / Size 12799 / 50.00 GiB
VG UUID b2PgAo-pFuj-MVh6-kSy6-PhLW-ByeJ-rIz8xJ
[root@localhost ~]#
#可更加需求更改PE大小,默认为4MB
#如:vgcreate Vg_web -s 16MB /dev/sdb
--------------------------------------------------------------------------------------------------------------------
4、创建LV逻辑卷并格式化
root@localhost ~]# lvcreate -n Lv_web -L 30G Vg_web
Logical volume "Lv_web" created
#也可以百分比形式建立,如:lvcreate -n LV_web -l 100%VG Vg_web
#创建LV逻辑卷可更加实际容量添加,满足需求即可
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/Vg_web/Lv_web
LV Name Lv_web
VG Name Vg_web
LV UUID iOWFFn-e6E3-cQIs-mNn8-W5WD-addZ-0Ay1SA
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-04-09 23:26:33 +0800
LV Status available
# open 0
LV Size 30.00 GiB
Current LE 7680
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
[root@localhost ~]#
--------------------------------------------------------------------------------------------------------------------
5、后期扩展或缩小
#逻辑卷和文件系统一起扩展和缩小可以降低错误率,逻辑卷缩小有风险慎重操作
#可用lvresize -r -L +20G /Vg_web/Lv_web参数
#也可用lvextend实现扩展lvreduce缩小
#拉伸测试:
[root@localhost ~]# lvextend -r -l 100%VG /dev/Vg_web/Lv_web
fsck from util-linux-ng 2.17.2
/dev/mapper/Vg_web-Lv_web: clean, 11/1966080 files, 167409/7864320 blocks
Extending logical volume Lv_web to 50.00 GiB
Logical volume Lv_web successfully resized
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/Vg_web-Lv_web to 13106176 (4k) blocks.
The filesystem on /dev/mapper/Vg_web-Lv_web is now 13106176 blocks long.
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/Vg_web/Lv_web
LV Name Lv_web
VG Name Vg_web
LV UUID iOWFFn-e6E3-cQIs-mNn8-W5WD-addZ-0Ay1SA
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-04-09 23:26:33 +0800
LV Status available
# open 0
LV Size 50.00 GiB
Current LE 12799
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
[root@localhost ~]#
#缩小测试:
(缩小前先卸载挂着目录)
[root@localhost ~]# mount /dev/Vg_web/Lv_web /mnt
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00 ext4 16G 4.6G 11G 31% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 508M 36M 446M 8% /boot
/dev/mapper/VolGroup-LogVol01 ext4 3.1G 72M 2.9G 3% /home
/dev/mapper/Vg_web-Lv_web ext4 53G 189M 50G 1% /mnt
[root@localhost ~]# umount /mnt/
[root@localhost ~]#lvresize -r -L 20G /dev/Vg_web/Lv_web
fsck from util-linux-ng 2.17.2
/dev/mapper/Vg_web-Lv_web: 11/3276800 files (0.0% non-contiguous), 251699/13106176 blocks
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/mapper/Vg_web-Lv_web to 5242880 (4k) blocks.
The filesystem on /dev/mapper/Vg_web-Lv_web is now 5242880 blocks long.
Reducing logical volume Lv_web to 20.00 GiB
[root@localhost ~]#
[root@localhost ~]# mount /dev/Vg_web/Lv_web /mnt
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00 ext4 16G 4.6G 11G 31% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 508M 36M 446M 8% /boot
/dev/mapper/VolGroup-LogVol01 ext4 3.1G 72M 2.9G 3% /home
/dev/mapper/Vg_web-Lv_web ext4 22G 181M 20G 1% /mnt
[root@localhost ~]#
[root@localhost ~]# lvdisplay
--- Logical volume ---
LV Path /dev/Vg_web/Lv_web
LV Name Lv_web
VG Name Vg_web
LV UUID iOWFFn-e6E3-cQIs-mNn8-W5WD-addZ-0Ay1SA
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-04-09 23:26:33 +0800
LV Status available
# open 1
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
[root@localhost ~]#
--------------------------------------------------------------------------------------------------------------------
6、建立LVM快照并测试
[root@localhost /]# mkdir /test
[root@localhost /]# touch /mnt/111 /mnt/222 /mnt/333
[root@localhost /]# lvcreate -s -L 5G -n lv_web_back /dev/Vg_web/Lv_web
Logical volume "lv_web_back" created
[root@localhost /]# lvdisplay
--- Logical volume ---
LV Path /dev/Vg_web/Lv_web
LV Name Lv_web
VG Name Vg_web
LV UUID iOWFFn-e6E3-cQIs-mNn8-W5WD-addZ-0Ay1SA
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-04-09 23:26:33 +0800
LV snapshot status source of
lv_web_back [active]
LV Status available
# open 1
LV Size 20.00 GiB
Current LE 5120
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:2
--- Logical volume ---
LV Path /dev/Vg_web/lv_web_back
LV Name lv_web_back
VG Name Vg_web
LV UUID RqUlC3-hqOA-ACxb-i6UP-eE3y-fq1X-wwpOIK
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2016-04-09 23:55:55 +0800
LV snapshot status active destination for Lv_web
LV Status available
# open 0
LV Size 20.00 GiB
Current LE 5120
COW-table size 5.00 GiB
COW-table LE 1280
Allocated to snapshot 0.00%
Snapshot chunk size 4.00 KiB
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:3
[root@localhost /]#
[root@localhost test]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00 ext4 16G 4.6G 11G 31% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 508M 36M 446M 8% /boot
/dev/mapper/VolGroup-LogVol01 ext4 3.1G 72M 2.9G 3% /home
/dev/mapper/Vg_web-Lv_web ext4 22G 181M 20G 1% /mnt
/dev/mapper/Vg_web-lv_web_back ext4 22G 181M 20G 1% /test
[root@localhost test]#
[root@localhost /]# mount /dev/Vg_web/lv_web_back /test/
[root@localhost /]# cd /test/
[root@localhost test]# ll
总用量 16
-rw-r--r--. 1 root root 0 4月 9 23:54 111
-rw-r--r--. 1 root root 0 4月 9 23:54 222
-rw-r--r--. 1 root root 0 4月 9 23:54 333
drwx------. 2 root root 16384 4月 9 23:27 lost+found
[root@localhost /]#
--------------------------------------------------------------------------------------------------------------------
7、快照还原测试
[root@localhost test]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00 ext4 16G 4.6G 11G 31% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 508M 36M 446M 8% /boot
/dev/mapper/VolGroup-LogVol01 ext4 3.1G 72M 2.9G 3% /home
/dev/mapper/Vg_web-Lv_web ext4 22G 181M 20G 1% /mnt
/dev/mapper/Vg_web-lv_web_back ext4 22G 181M 20G 1% /test
[root@localhost test]# cd /mnt/
[root@localhost mnt]# ll
总用量 16
-rw-r--r--. 1 root root 0 4月 19 03:13 111
-rw-r--r--. 1 root root 0 4月 19 03:13 222
-rw-r--r--. 1 root root 0 4月 19 03:13 333
drwx------. 2 root root 16384 4月 19 03:09 lost+found
[root@localhost mnt]# rm 111
#进入原逻Lv_web辑卷挂着目录删除和创建一些内容用于测试后期快照还原效果
[root@localhost mnt]# cd /
[root@localhost /]# umount /test/
[root@localhost /]# umount /mnt/
#还原前先要卸载
[root@localhost /]# lvconvert --merge /dev/mapper/Vg_web-lv_web_back
Merging of volume Lv_web_bak started.
Lv_web: Merged: 100.0%
Merge of snapshot into logical volume Lv_web has finished.
Logical volume "Lv_web_bak" successfully removed
[root@localhost /]#
#用lvconvert命令进行快照还原,还原后被还原的快照移除
--------------------------------------------------------------------------------------------------------------------
8、建立WEB站点目录并挂载到该目录查看恢复情况
[root@www /]# mkdir alidata
[root@www /]# chown wwwroot.wwwroot alidata/
[root@www /]# ll -d alidata/
drwxr-xr-x. 2 wwwroot wwwroot 4096 4月 11 20:30 alidata/
[root@www /]#
[root@localhost ~]# mount /dev/Vg_web/Lv_web /alidata/
[root@localhost ~]# df -TH
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-LogVol00 ext4 16G 4.6G 11G 31% /
tmpfs tmpfs 515M 0 515M 0% /dev/shm
/dev/sda1 ext4 508M 36M 446M 8% /boot
/dev/mapper/VolGroup-LogVol01 ext4 3.1G 72M 2.9G 3% /home
/dev/mapper/Vg_web-Lv_web ext4 22G 181M 20G 1% /alidata
[root@localhost ~]#
[root@localhost /]# cd alidata/
[root@localhost alidata]# ll
总用量 16
-rw-r--r--. 1 root root 0 4月 19 03:51 111
-rw-r--r--. 1 root root 0 4月 19 03:13 222
-rw-r--r--. 1 root root 0 4月 19 03:13 333
drwx------. 2 root root 16384 4月 19 03:09 lost+found
[root@localhost alidata]#
#文件已经被恢复
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###
技巧1、快照自动扩展方式:
[root@localhost alidata]# vim /etc/lvm/lvm.conf
snapshot_autoextend_threshold = 100
#快照自动扩展触发值(百分比形式计算,可更具自己需求变更)
snapshot_autoextend_percent = 20
#快照自动扩展增加值(百分比形式计算,可更具自己需求变更)
#默认意思快照达到100%的使用率时候再增加20%的扩展空间
-----------------------------------------------------------------------------------------------------------
技巧2、分区开机自动挂载
[root@localhost ~]# vim /etc/fstab
/dev/mapper/Vg_web-Lv_web /alidata ext4 defaults 0 0
#参数前为挂载点,后为挂载目录
[root@localhost ~]#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ###