一、安装qemu
1、安装依赖
dnf install flex libslirp-devel libcap-ng-devel libattr-devel glib2-devel pixman-devel zlib-devel libcap-devel libattr-devel gcc gcc-c++ make git flex bison bzip2 pam-devel gnutls-devel perl -y #如果需要虚拟xp/win2003系统,显卡需要使用qxl,则安装以下包 dnf install spice-server spice-protocol -y
2、下载、编译
wget https://download.qemu.org/qemu-9.1.2.tar.xz tar xvf qemu-9.1.2.tar.xz cd qemu-9.1.2 ./configure --prefix=/usr/local/kvm --interp-prefix=/usr/local/kvm/gnemul --enable-kvm --enable-modules --enable-vnc --enable-virtfs --enable-auth-pam --target-list=x86_64-softmmu,i386-softmmu #接上,如需要虚拟xp/win2003,需添加--enable-spice make -j5 make install ln -sf /usr/local/kvm/bin/* /usr/local/bin/
二、创建、启动虚拟机
mkdir /usr/local/kvm/img qemu-img create -f qcow2 /usr/local/kvm/img/win7.qcow2 20G qemu-system-x86_64 -m 2048 -enable-kvm -machine usb=on -device usb-tablet -vnc :1 /usr/local/kvm/img/win7.qcow2 -cdrom /usr/local/os/win7.iso -boot d -device intel-hda -vga virtio -device virtio-net-pci,netdev=net0 -netdev user,id=net0,hostfwd=tcp::2222-:22,smb=/usr/local/share -nodefaults -smp 2,sockets=1 #-machine usb=on -device usb-tablet #解决vnc连接时鼠标不同步问题 #-vnc :1 #以主机的5901端口vnc连接虚拟机 #-device intel-hda或-device ich9-intel-hda #默认声卡,有的设备需要再加一个参数:-device hda-duplex #-device virtio-net-pci,netdev=net0 #设置网卡为virtio,也可以设置为rtl8139 #-netdev user,id=net0,hostfwd=tcp::2222-:22 #将虚拟机的22端口映射到主机2222端口上 #smb=/data/share #qemu内建的samba服务,将主机的/data/share目录设置为共享之后,虚拟机可以通过\\10.0.2.4\qemu来访问(完全共享) #-nodefaults #不创建默认设备,如:软盘、光驱等,使用此选项,必须加-vga选项,不然会提示没有显示设备 #-smp 4,sockets=1,cores=2 #设置cpu核心数,如:1个物理CPU(双核),4个逻辑CPU #-boot d #安装时使用,即D盘(光驱)启动
注:qemu旧版中有qemu-kvm模块存在,新版中需要使用kvm特性时候,在qemu-system-x86_64命令中使用--enable-kvm即可。
*使用virtio
linux系统和高版本windows安装时自带驱动或挂载光驱即可加载驱动,以下仅对winxp和win2003等作一说明:
方法一:安装时挂载一个光盘
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.229-1/virtio-win.iso qemu-system-i386 -m 2048 -enable-kvm -machine usb=on -device usb-tablet -vga qxl -drive file=/usr/local/kvm/img/xp.qcow2,format=qcow2,if=virtio,index=0 -drive file=/data/os/zh-hans_windows_xp_professional_with_service_pack_3_x86_cd_x14-80404.iso,media=cdrom,index=1 -drive file=/usr/local/kvm/img/virtio-win.iso,medis=cdrom,index=2 -device virtio-net-pci,netdev=net0 -netdev user,id=net0 -boot d --nodefaults
方法二:在已安装的系统上,需要先加载一个virtio模式的非系统硬盘和网卡等,进入系统后安装驱动,关机后再让系统硬盘和网卡以virtio模式挂载。
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.262-2/virtio-win.iso #创建一个虚拟硬盘 qemu-img create -f qcow2 /usr/local/kvm/img/virtio.qcow2 1G #挂载虚拟硬盘、网卡和驱动 qemu-system-i386 -m 2048 -enable-kvm -machine usb=on -device usb-tablet -vga qxl -drive file=/usr/local/kvm/img/xp.qcow2,format=qcow2 -drive file=/usr/local/kvm/img/virtio.qcow2,format=qcow2,if=virtio -cdrom /data/os/virtio-win.iso -device virtio-net-pci,netdev=net0 -netdev user,id=net0 --nodefaults #安装好驱动后,用以下参数重启即可 qemu-system-i386 -m 2048 -enable-kvm -machine usb=on -device usb-tablet -vga qxl -drive file=/usr/local/kvm/img/xp.qcow2,format=qcow2,if=virtio --nodefaults
*压缩qcow2
1、客户机空间清零
Windows系统:使用sdelete(下载:https://docs.microsoft.com/zh-cn/sysinternals/downloads/sdelete)
sdelete.exe -z c:
Linux系统:
dd if=/dev/zero of=zero rm -f zero
2、压缩qcow2
qemu-img convert -c -f qcow2 -O qcow2 -c old.qcow2 new.qcow2
三、安装libvirt
1、安装依赖
dnf install gnutls-devel libnl-devel libpciaccess-devel yajl-devel device-mapper-devel -y
2、下载、编译
wget https://download.libvirt.org/libvirt-10.8.0.tar.xz tar xvf libvirt-10.8.0.tar.xz cd libvirt-10.8.0 mkdir build cd build ../configure --prefix=/usr/local/kvm --without-vmware --without-vbox --without-openvz --without-esx --without-lxc --with-yajl --with-qemu #去掉vmware/vbox/openvz/esx/lxc支持,只支持qemu make -j5 make install ln -sf /usr/local/kvm/lib/pkgconfig/* /usr/lib64/pkgconfig/
3、配置
ln -sf /usr/local/kvm/bin/* /usr/local/bin/ ln -sf /usr/local/kvm/sbin/* /usr/local/bin/ cp src/libvirtd.service /lib/systemd/system/ cp src/libvirtd.socket /lib/systemd/system/ cp src/virtlogd.service /lib/systemd/system/ cp src/virtlogd.socket /lib/systemd/system/ cp src/virtlockd.service /lib/systemd/system/ cp src/virtlockd.socket /lib/systemd/system/ systemctl start libvirtd systemctl enable libvirtd
四、安装libvirt-php
1、下载、编译
wget https://download.libvirt.org/php/libvirt-php-0.5.8.tar.xz tar xvf libvirt-php-0.5.8.tar.xz cd libvirt-php-0.5.8 ./configure --with-php-config=/usr/local/php/bin/php-config --with-php-extensiondir=/usr/local/php/lib/php/extensions/no-debug-non-zts-20190902 --with-php-confdir=/usr/local/php/lib/php/extensions/ make -j5 make install
2、重启php,查看是否安装成功
systemctl restart php-fpm php -m|grep -i libvirt
© 2016-2024 清风的个人笔记