qemu版本:2.4.0.1
libvmi版本:0.10.1
libvirt版本:2.4.0
1、安装qemu
准备工作:
sudo apt-get update -ysudo apt-get install git make patch tar -ysudo apt-get install qemu-kvm -y#提前安装virt-viewer的原因是如果后装virt-viewer会对libvirt产生覆盖,使libvirt安装失败sudo apt-get install vnc4server virt-viewer -ysudo apt-get install zlib1g-dev libglib2.0-dev libtool autoconf flex bison -y
安装qemu,这里会覆盖上面的qemu-kvm中的一些关键包。
wget http://download.qemu-/qemu-2.4.0.1.tar.xztar xvfJ qemu-2.4.0.1.tar.xz#下面这几步的目的是最新的libvmi里有对qemu2.4.0.1的补丁,虽然我们不使用新版的libvmi但是需要里面的patch。git clone /libvmi/libvmicd libvmicp libvmi/tools/qemu-kvm-patch/kvm-qemu-2.4.0.1-libvmi.patch qemu-2.4.0.1/sudo apt-get install zlib1g-dev libglib2.0-dev libtool autoconf flex bison -ycd qemu-2.4.0.1patch -p1 < ./kvm-qemu-2.4.0.1-libvmi.patch./configuremake -j16sudo make installldconfig
2、安装libvirt
在安装前先删除系统自带的libvirt,一般在/usr/lib/下,不确定的可以查找一下:
find / -name libvirt-admin*find / -name libvirt-lxc*find / -name libvirt-qemu*find / -name libvirt.so*
找到删除即可,然后安装:
sudo apt-get install libyajl-dev libxml2-dev libdevmapper-dev libpciaccess-dev libnl-3-dev libnl-route-3-dev uuid python-urlgrabber python-libxml2-ywget /repo/pkgs/libvirt/libvirt-2.4.0.tar.xz/53ebd616857333fc3ce4dea3db986488/libvirt-2.4.0.tar.xztar xvfJ libvirt-2.4.0.tar.xzcd libvirt-2.4.0#./autogen.sh --system非常重要,可以让新安装的版本和系统发行版有相同路径,这样可以覆盖发行版,不会产生混淆./autogen.sh --system --localstatedir=/var --sysconfdir=/etc --enable-compile-warnings=errormake -j64 && make install && ldconfig#使libvirt和python绑定sudo apt-get install python-dev -ygit clone /libvirt/libvirt-pythoncd libvirt-pythonpython setup.py build && sudo python setup.py install#安装虚拟机管理工具git clone /rlaager/python-virtinstcd python-virtinstpython setup.py build && sudo python setup.py install
完成上面两步安装后,启动libvirtd服务
libvirtd -d
检查是否安装成功:
如果出现和pid file有关的问题,尝试用下面解决方法:
3、安装libvmi
sudo apt-get install python-pip libffi-dev python-docopt libfuse-dev libjson-c-dev check -ypip install pkgconfigpip install futurewget /libvmi/libvmi/archive/v0.10.1.tar.gztar -zxvf v0.10.1.tar.gzcd libvmi-0.10.1./autogen.sh./configure --enable-xen=0makesudo make installsudo ldconfig#libvmi绑定python接口cd ./tools/pyvmipython setup.py build && python setup.py install
4、 搭建网桥
sudo apt-get install bridge-utils
修改网络配置文件
vi /etc/network/interfaces
如果固定ip,添加
auto eth0 iface eth0 inet manual auto br0 iface br0 inet static address 192.168.200.130 network 192.168.200.0 netmask 255.255.255.0 broadcast 192.168.200.255 gateway 192.168.200.1 dns-nameservers 8.8.8.8 bridge_ports eth0 bridge_stp off bridge_fd 0 bridge_maxwait 0
如果dhcp,添加
auto eth0 iface eth0 inet dhcp auto br0 iface br0 inet dhcp bridge_ports eth0 bridge_stp off bridge_fd 0bridge_maxwait 0
以上是只有一个eth0网卡的情况下,如果有多个,以此类推即可。
然后reboot
就可以了
搭建完成后测试下:
4、安装虚拟机
mkdir /var/data/images/mkdir /var/data/isoes#将镜像放到/var/data/isoes下,/var/data/images下存放虚拟机磁盘qemu-img create -f qcow2 /var/data/images/ubuntu1204.qcow2 20G#具体参数就不介绍了,注意虚拟机内存不要太大,2G即可,否则后面libvmi将内存溢出。virt-install --virt-type=kvm --name=u1204 --ram=2048 --vcpus=1 --hvm --cdrom=/var/data/isoes/ubuntu-12.04-server-amd64.iso --network=bridge=br0,model=virtio --graphics vnc,listen=0.0.0.0 --disk path=/var/data/images/ubuntu1204.qcow2,size=20,bus=virtio,format=qcow2
安装虚拟机直到成功
可以用virsh list --all
查看安装完成的虚拟机。
5、配置libvmi
虚拟机安装成功后,需要配置libvmi.conf
touch /etc/libvmi.conf
如何配置libvmi.conf在LibVMI Installation Instructions有介绍。依次完成就好,没有坑。
配置完成后,可以测试一下:
文件pslist.py:
#!/usr/bin/env pythonimport pyvmiimport sysdef get_os_params(vmi):ostype = vmi.get_ostype()if ostype.lower() == "windows":return get_windows_params(vmi)elif ostype.lower() == "linux":return get_linux_params(vmi)else:print("Sorry, {} ostype is not supported in this example yet.".format(ostype))exit(1)def get_linux_params(vmi):tasks_offset = vmi.get_offset("linux_tasks")name_offset = vmi.get_offset("linux_name") - tasks_offsetpid_offset = vmi.get_offset("linux_pid") - tasks_offsetlist_head = vmi.translate_ksym2v("init_task")return (tasks_offset, name_offset, pid_offset, list_head)def get_windows_params(vmi):tasks_offset = vmi.get_offset("win_tasks")name_offset = vmi.get_offset("win_pname") - tasks_offsetpid_offset = vmi.get_offset("win_pid") - tasks_offsetlist_head = vmi.read_addr_ksym("PsInitialSystemProcess")return (tasks_offset, name_offset, pid_offset, list_head)def processes(vmi):tasks_offset, name_offset, pid_offset, list_head = get_os_params(vmi)next_process = vmi.read_addr_va(list_head + tasks_offset, 0)list_head = next_processwhile True:procname = vmi.read_str_va(next_process + name_offset, 0)pid = vmi.read_32_va(next_process + pid_offset, 0)next_process = vmi.read_addr_va(next_process, 0)if (pid < 1<<16):yield pid, procnameif (list_head == next_process):breakdef main(argv):vmi = pyvmi.init(argv[1], "complete")if vmi.get_access_mode() == 'file':print("Process listing for File {}".format(vmi.get_name()))else:print("Process listing for VM {}".format(vmi.get_name()))for pid, procname in processes(vmi):print "[%5d] %s" % (pid, procname)if __name__ == "__main__":main(sys.argv)
执行python pylist.py [vm_name]
,这里vm_name是我们在第4步中安装虚拟机设置的name,而且切记在第4步中虚拟机内存不要开太大,建议2G即可。如果正常输出,则以上所有步骤都搭建成功!
之后可以使用libvmi的github提供的使用volatility来进行虚拟机自省。
参考:
libvirt官网:/
libvmi官网:/
libvmi最新github项目:/libvmi/libvmi