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
Logical volume "lv1_sp" created
[root@station122 mnt]#
[root@station122 mnt]# 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 owi-ao 20.00m
lv1_sp vg1 swi-a- 8.00m lv1 0.15
[root@station122 mnt]# mkdir /mnt/lv1_sp
[root@station122 mnt]# mount /dev/vg1/lv1_sp /mnt/lv1_sp/ #挂载快照,注意我们并没有为该快照创建文件系统
[root@station122 mnt]# 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
/dev/mapper/vg1-lv1_sp
20M 1.2M 18M 7% /mnt/lv1_sp
[root@station122 mnt]# cd /boot/
[root@station122 boot]# ll -h
total 18M
-rw-r--r--. 1 root root 96K Sep 1 2010 config-2.6.32-71.el6.x86_64
drwxr-xr-x. 3 root root 1.0K Mar 28 10:00 efi
drwxr-xr-x. 2 root root 1.0K Mar 28 10:03 grub
-rw-r--r--. 1 root root 12M Mar 28 10:02 initramfs-2.6.32-71.el6.x86_64.img
drwx------. 2 root root 12K Mar 28 09:55 lost+found
-rw-r--r--. 1 root root 157K Sep 1 2010 symvers-2.6.32-71.el6.x86_64.gz
-rw-r--r--. 1 root root 2.2M Sep 1 2010 System.map-2.6.32-71.el6.x86_64
-rwxr-xr-x. 1 root root 3.7M Sep 1 2010 vmlinuz-2.6.32-71.el6.x86_64
[root@station122 boot]# cp vmlinuz-2.6.32-71.el6.x86_64 /mnt/lv1/ #模拟系统运行,往lvm写入3.7M数据
[root@station122 boot]# 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 4.8M 14M 27% /mnt/lv1
/dev/mapper/vg1-lv1_sp
20M 1.2M 18M 7% /mnt/lv1_sp
[root@station122 boot]# lvs #快照已占用46.09%
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 owi-ao 20.00m
lv1_sp vg1 swi-ao 8.00m lv1 46.09
[root@station122 boot]# cat /mnt/lv1_sp/passwd | wc -l #模拟快照备份,此处仍可以读取
20
[root@station122 boot]# cp initramfs-2.6.32-71.el6.x86_64.img /mnt/lv1/ #再往lvm写入一个12M的文件,超出快照大小8M
[root@station122 boot]# 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 17M 1.8M 91% /mnt/lv1
/dev/mapper/vg1-lv1_sp
20M 1.2M 18M 7% /mnt/lv1_sp
[root@station122 boot]# lvs #快照占用100%
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 owi-ao 20.00m
lv1_sp vg1 Swi-Io 8.00m lv1 100.00
[root@station122 boot]# cd /mnt/lv1 #lvm还有空间
[root@station122 lv1]# du -sh
16M .
[root@station122 boot]# cat /mnt/lv1_sp/passwd | wc -l #lvm快照已不可读取,有时仍可读取,不知何故?
cat: /mnt/lv1_sp/passwd: Input/output error
0
[root@station122 lv1_sp]# du -sh #至快照目录执行du命令会报错
du: cannot access `./.vmlinuz-2.6.32-71.el6.x86_64.hmac': Input/output error
3.0K
小结: 如果在备份期间,数据变换量大于快照容量,可能会导致备份失败。