KVM虚拟机快照备份

一.确认虚拟机镜像文件格式

# qemu-img info /home/img/VM1.img
image: VM1.img
file format: qcow2
virtual size: 20G (21474836480 bytes)
disk size: 1.7G

RAW格式是最原始的镜像格式,好处是速度快。但不支持很多新的功能。现在qcow2格式效率有很大提升了,而且还支持一些新的功能
1 更小的存储空间,即使是不支持holes的文件系统也可以(这下du -h和ls -lh看到的就一样了)
2 Copy-on-write support, where the image only represents changes made to an underlying disk image(这个特性SUN ZFS表现的淋漓尽致)
3 支持多个snapshot,对历史snapshot进行管理
4 支持zlib的磁盘压缩
5 支持AES的加密

二.注意要使用 KVM 的快照功能,虚拟机的硬盘一定要是 qcow2 格式,否则无法使用快照功能。想要启用快照功能,需要先转换镜像文件格式为qcow2。

# virsh shutdown VM1
先关掉VM
# qemu-img convert -f raw -O qcow2 VM1.img VM1qcow2.img
转换格式

三。常用快照命令
这里有一份libvirt官方的命令文档。完整,但说明不详细:http://wiki.libvirt.org/page/VM_lifecycle

具体示例:

1 列出快照:

[root@i3190 ~]#  virsh snapshot-list VM2
 Name                 Creation Time             State
------------------------------------------------------------
 snap0                2014-10-21 08:03:42 -0700 shutoff

2 创建快照

virsh snapshot-create-as VM1 snap0
virsh snapshot-create-as --domain VM1 --name snap1 --description "URL: aenes.com"

3查看快照配置

virsh snapshot-current VM1

4恢复快照

virsh snapshot-revert VM1 snap2

5删除快照

virsh snapshot-delete VM1 snap2

6获取帮助

virsh help snapshot

四。关于qemu-img snapshot -c和savevm
很多互相抄袭的教程里,都提到了使用qemu-img snapshot -c的命令来创建快照。但我自己测试的结果 ,不管虚拟机是运行中,还是关闭状态,这个命令创建的快照字节都是0。也就是说什么也没保存下来。对此,我还没有找到原因。但找到Red hat员工Kashyap Chamarthy的一篇文章。文章里提到virsh在不同情况下,会调用不同方式来保存快照。其中至少包括‘qemu-img snapshot -c‘,qemu的 ‘savevm‘和qemu的 ‘snapshot_blkdev‘这三种方式。所以看起来快照保存,还是使用virsh snapshot-create的方式比较好。

qemu-img snapshot相关命令格式:

qemu-img snapshot -c snap1 centos1-qcow2.img

qemu-img  snapshot -l centos1-qcow2.img

Snapshot list:
ID        TAG       VM SIZE      DATE       VM CLOCK
1         snap1      0              2011-07-21 23:17:38   00:00:00.000

恢复快照:

qemu-img  snapshot   -a   CentOS5.5_64bit_Qcow2_basesys.img    CentOS5.5_64bit_Qcow2.img

其他操作:

  'snapshot' is the name of the snapshot to create, apply or delete
  '-a' applies a snapshot (revert disk to saved state)
  '-c' creates a snapshot
  '-d' deletes a snapshot
  '-l' lists all snapshots in the given image



CentOS 7 下配置KVM

之前有文章写过这块的,CentOS 7 下面又有些不同,记录一下吧。
1.查cpu是否支持VT

egrep '(vmx|svm)' --color=always /proc/cpuinfo

2.检查内核模块是否加载

lsmod | grep kvm

3.查看Selinux状态

sestatus

如果是启用状态

# vi /etc/sysconfig/selinux
SELINUX=disabled
reboot

4.安装 KVM

yum install kvm libvirt python-virtinst qemu-kvm virt-viewer tunctl bridge-utils avahi dmidecode qemu-kvm-tools virt-manager qemu-img virt-install net-tools libguestfs-tools -y

5.启动libvirt服务

systemctl start libvirtd
systemctl enable libvirtd

6.查看kvm服务是否正常,目前没任何虚拟机,所以没有内容显示

virsh -c qemu:///system list

7.为虚拟机创建网桥
首先备份一下网卡设置

mv /etc/sysconfig/network-scripts/ifcfg-enp3s0 /root/ifcfg-enp3s0.bak

创建一个文件 /etc/sysconfig/network-scripts/ifcfg-br0

DEVICE=br0
TYPE=Bridge
BOOTRPOTO=static
IPADDR=208.66.77.146
NETMASK=255.255.255.248
GATEWAY= 208.66.77.145
ONBOOT=yes

修改 /etc/sysconfig/network-scripts/ifcfg-enp3s0为

DEVICE=enp3s0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
BRIDGE=br0

重启网络

/etc/init.d/network restart

创建镜像文件

qemu-img create -f qcow2 /home/img/VM1.img 20G

安装虚拟机

virt-install \
--name VM5 \
--ram 1024 \
--vcpus=8 \
--disk path=/home/img/VM5.img,size=20,format=qcow2,bus=virtio \
--accelerate \
--cdrom /root/iso/CentOS-6.6-x86_64-bin-DVD1.iso \
--graphics vnc,listen=0.0.0.0,port=5924, \
--network bridge=br0,model=virtio  \
--force  --autostart \
--noautoconsole

Virsh语法参考:

Autostart                         自动开始一个域
Create                            从一个 XML 文件创建一个域
Define                            从一个 XML 文件定义(但不开始)一个域
edit                              编辑某个域的 XML 配置
shutdown                          关闭一个域
start                             开始一个(以前定义的)非活跃的域
reboot                            重新启动一个域
suspend                           挂起一个域
resume                            重新恢复一个域
vncdisplay                        vnc 显示
undefine                          删除虚拟机,只是删除VM的配置文件,并不删除虚拟磁盘文件

查看虚拟机列表。

virsh -c qemu:///system list

重启虚拟机

virsh reboot VM1

虚拟机宿主机开机自启动

virsh autostart VM1

取消

virsh autostart --disable VM1

删除虚拟机

virsh undefine VM1
rm -rf /home/img/VM1.img
rm -rf /etc/libvirt/qemu/VM1.xml
rm -rf /etc/libvirt/qemu/autostart/VM1.xml

虚拟机的配置文件在这里

/etc/libvirt/qemu/*.xml

一个一个安装虚拟机太麻烦,我们来克隆吧!
首先关闭虚拟机

virsh shutdown VM1
virsh -c qemu:///system list
virt-clone -o VM1 -n VM2 -f /home/img/VM2.img --connect=qemu:///system
chown qemu.qemu VM05.img

修改一些配置,把 vnc 的端口号修改一下,避免两个产生冲突,并记录一下这里面的 MAC 地址备用。

virsh edit VM2

先启动 VM2,目前两个虚拟机还不能同时启动。

virsh start VM2
virsh -c qemu:///system list

连接VNC

rm -rf /etc/udev/rules.d/70-persistent-net.rules
rm -rf /etc/udev/rules.d/70-persistent-cd.rules
vi /etc/network/interfaces
hwaddress ether 52:54:00:4b:04:bb 添加mac地址

/etc/init.d/networking restart
virsh reboot VM2
virsh start VM1

也使用virt-edit命令直接在宿主机修改虚拟机文件

virt-edit VM2 /etc/network/interfaces
virt-edit VM2 /etc/hostname
virt-edit VM2 /etc/hosts


FreeRADIUS 配置认证记录入数据库

修改 radpostauth 的表结构:

mysql> drop table radpostauth;
mysql> CREATE TABLE radpostauth (
       id int(11) NOT NULL auto_increment,
       username varchar(64) NOT NULL default '',
       pass varchar(64) NOT NULL default '',
       nasipaddress varchar(15) NOT NULL default '',
       clientipaddress varchar(100) NOT NULL default '',
       reply varchar(32) NOT NULL default '',
       replymessage varchar(100) NOT NULL default '',
       authdate timestamp NOT NULL,
       PRIMARY KEY  (id)
       ) ENGINE = INNODB;

Freeradius 3.x 下修改raddb/mods-config/sql/main/mysql/queries.conf

post-auth {
     # Write SQL queries to a logfile. This is potentially useful for bulk inserts
     # when used with the rlm_sql_null driver.
     # logfile = ${logdir}/post-auth.sql
     query = "\
             INSERT INTO ${..postauth_table} \
                     (username, pass, reply, authdate) \
             VALUES ( \
                     '%{SQL-User-Name}', \
                     '%{%{User-Password}:-%{Chap-Password}}', \
                     '%{Calling-Station-Id}', \
                     '%{reply:Packet-Type}', \
                     '%S')"
}

修改为

post-auth {
     # Write SQL queries to a logfile. This is potentially useful for bulk inserts
     # when used with the rlm_sql_null driver.
     # logfile = ${logdir}/post-auth.sql
     query = "\
             INSERT INTO ${..postauth_table} \
                     (username, nasipaddress, clientipaddress, reply, authdate) \
             VALUES ( \
                     '%{SQL-User-Name}', \
                     '%{Client-IP-Address}', \
                     '%{Calling-Station-Id}', \
                     '%{reply:Packet-Type}', \
                     '%S')"
 }

Freeradius 2.x 下
修改/usr/local/radius/etc/raddb/sql/mysql/dialup.conf

postauth_query = "INSERT INTO ${postauth_table} \
                      (username, pass, reply, authdate) \
                      VALUES ( \
                      '%{User-Name}', \
                      '%{%{User-Password}:-%{Chap-Password}}', \
                      '%{reply:Packet-Type}', '%S')"

修改为

postauth_query = "INSERT INTO ${postauth_table} \
                      (username, pass, nasipaddress, clientipaddress, reply, authdate) \
                      VALUES ( \
                      '%{User-Name}', \
                      '%{%{User-Password}:-%{Chap-Password}}', \
                      '%{Client-IP-Address}', \
                      '%{Calling-Station-Id}', \
                      '%{reply:Packet-Type}', \
                      '%S')"

登陆的错误信息到数据库
在/usr/local/radius/etc/raddb/modules/attr_rewrite最末添加如下

attr_rewrite sanereplymessage {
        attribute = Reply-Message
        searchin = reply
        searchfor = "\n|\r"
        replacewith = ""
        ignore_case = no
        new_attribute = no
        max_matches = 10
        append = no
}

修改/usr/local/radius/etc/raddb/sites-enabled/default,在sql前面增加sanereplymessage

Post-Auth-Type REJECT {
 ..
 sanereplymessage
 sql
 ..
 }

那么/usr/local/radius/etc/raddb/sql/mysql/dialup.conf就要修改为如下

postauth_query = "INSERT INTO ${postauth_table} \
                      (username, pass, nasipaddress, clientipaddress, reply, replymessage, authdate) \
                      VALUES ( \
                      '%{User-Name}', \
                      '%{%{User-Password}:-%{Chap-Password}}', \
                      '%{Client-IP-Address}', \
                      '%{Calling-Station-Id}', \
                      '%{reply:Packet-Type}', \
                      '%{Module-Failure-Message}-%{reply:Reply-Message}', \
                      '%S')"

当然需要先在psotauth表里添加相应的字段先.
因为 NAS-IP-Address 很多时候都是 "127.0.0.1" .所以替换dialup.conf中的%{NAS-IP-Address}替换为%{Client-IP-Address}
添加字段

alter table radpostauth add column replymessage varchar(100) after reply;