lvm快照有两大用途,一是用来克隆虚拟机,例如做xen虚拟机时,可先新建一台完整虚拟机,假设大小为10G,然后我们可以在这台10G的虚拟机上创建1个3G的快照,接着通过这个3G的快照启动虚拟机,做完实验后,可以直接删除快照而保持原来完整虚拟机的纯净。
lvm快照的第二大用途是实时备份(moment-in-time),即为了保持系统的一致性,我们先做一个快照冻结当前系统状态,这样快照里面的内容可暂时保持不变,系统本身继续运行,通过备份快照来实现不中断服务的的备份。
本文实验基于快照的热备。
[root@station122 md]# fdisk -l /dev/sdb
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0xbfedc746
Device Boot Start End Blocks Id System
/dev/sdb1 1 13055 104857568+ 5 Extended
/dev/sdb5 1 131 1048576+ 83 Linux
/dev/sdb6 131 262 1048576+ 83 Linux
/dev/sdb7 262 392 1048576+ 83 Linux
/dev/sdb8 392 523 1048576+ 83 Linux
/dev/sdb9 523 653 1048576+ 83 Linux
/dev/sdb10 653 784 1048576 83 Linux
/dev/sdb11 784 2089 10485760 83 Linux
[root@station122 md]# pvcreate /dev/sdb5 #创建pv
Wiping software RAID md superblock on /dev/sdb5
Physical volume "/dev/sdb5" successfully created
[root@station122 md]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 VolGroup lvm2 a- 79.51g 0
/dev/sdb5 lvm2 a- 1.00g 1.00g
[root@station122 md]# vgcreate vg1 /dev/sdb5 #创建vg
Volume group "vg1" successfully created
[root@station122 md]# vgs
VG #PV #LV #SN Attr VSize VFree
VolGroup 1 3 0 wz--n- 79.51g 0
vg1 1 0 0 wz--n- 1020.00m 1020.00m
[root@station122 md]# lvcreate -L 20M -n lv1 vg1 #创建lv
Logical volume "lv1" created
[root@station122 md]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_home VolGroup -wi-ao 28.54g
lv_root VolGroup -wi-ao 50.00g
lv_swap VolGroup -wi-ao 992.00m
lv1 vg1 -wi-a- 20.00m
[root@station122 md]# mkfs.ext3 /dev/vg1/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 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@station122 md]# mkdir /mnt/lv1
[root@station122 md]# mount /dev/vg1/lv1 /mnt/lv1/ #挂载文件系统
[root@station122 md]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
50G 884M 46G 2% /
tmpfs 120M 0 120M 0% /dev/shm
/dev/sda1 485M 29M 431M 7% /boot
/dev/mapper/VolGroup-lv_home
29G 172M 27G 1% /home
/dev/sr0 3.2G 3.2G 0 100% /media
/dev/mapper/vg1-lv1 20M 1.2M 18M 7% /mnt/lv1
[root@station122 lv1]# rm /mnt/lv1/* -rf
[root@station122 mnt]# cp /etc/passwd lv1/ #拷贝一个文件做测试用
[root@station122 mnt]# ll lv1/
total 2
-rw-r--r--. 1 root root 902 Jul 16 19:09 passwd
[root@station122 mnt]# lvcreate -L 5M -s -n lv1_sp /dev/vg1/lv1 #创建lvm快照
Rounding up size to full physical extent 8.00 MiB