热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

KVM虚拟机安装Windows7过程备忘

去年在一台备用服务器上装了个VirtualBox虚拟机,用headless模式跑Windows7,当做我的日常用系统,无论在哪都用RDP(Windows的远程桌面协议)远程登上,彻底的大内存、多CPU、千兆接入云桌面,非常舒服。VirtualBox是个很棒的虚拟机,无论图形界面还是命令

去年在一台备用服务器上装了个VirtualBox虚拟机,用headless模式跑Windows 7,当做我的日常用系统,无论在哪都用RDP(Windows的远程桌面协议)远程登上,彻底的大内存、多CPU、千兆接入云桌面,非常舒服。

VirtualBox是个很棒的虚拟机,无论图形界面还是命令行界面,都很好用。但间或死机(虚拟机停转,却把CPU占满),分越多的CPU速度越慢(可能是我后分的多CPU有关),以及虚拟磁盘配置不合理这三个原因,使我萌生了换虚拟机的念头。

这篇评测文章的结果是kvm的性能最好,此外听安天的王维工程师说只有kvm支持的spice协议是目前最快的远程桌面协议,有代码洁癖的我就当然选用它了。但这东西,比VirtualBox和VMWare都难配置太多了……必须得写下来备忘一些要点了。

kvm

kvm本身是个命令行程序,直接“kvm 参数”就能用。一开始就这么用,系统安装顺利,网络通畅。然后加bridged模式的网卡,结果一启动虚拟机就让主机网卡都断掉(甚至一度重启host系统都不能恢复,导致网络中心狠狠地重新收拾了一遍若干台交换机。对此麻烦,深表歉意)。查了无数资料,什么bridge、tun、tap的一通研究,各种脚本、手工试验,全无效。最后实在不想再跑机房了,就换用virt-manager建虚拟机。

virt-manager是libvirt的一个图形客户端,而libvirt是个通用的虚拟机管理库(支持kvm、xen、virtualbox、vmware等等)。virsh是libvirt的命令行客户端,用“virsh -c qemu:///system”可以在命令行管理virt-manager创建的虚拟机。

需要先在系统启动一个桥接网卡,才能在虚拟机里装bridged网卡。修改/etc/network/interfaces(debian)如下:

"language-shell shell"># Replace "auto eth0 ..." with following lines in /etc/network/interfaces
# The primary network interface
auto br0
iface br0 inet static
        address xxx.xxx.xxx.xxx
        netmask xxx.xxx.xxx.xxx
        network xxx.xxx.xxx.xxx
        broadcast xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers xxx.xxx.xxx.xxx
        dns-search domain.name
        bridge_ports    eth0
        bridge_stp      off
        bridge_maxwait  0
        bridge_fd       0

Windows 7最多只支持两个CPU socket,每个socket内可以有多个core。kvm缺省每个CPU模拟一个socket,必须修改虚拟机CPU的topology,才能使用超过一个CPU。

spice

virt-manager创建的虚拟机只要加了spice display,就可以用spice协议远程使用虚拟机的控制台。

Linux下的spice客户端叫spicec。

最重要的是Win7里必须安装spice的驱动和服务,才能让性能和功能达到最强。相关程序在这里下载:http://www.spice-space.org/download.html

qxl和virtio-serial驱动是必须安装的。如果安装时提示数字签名无效,驱动不生效,用管理员权限执行cmd.exe,运行下面两行命令并reboot(reboot后桌面会有“测试模式”字样,不影响使用):

"language-batchfile batchfile">bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS
bcdedit.exe -set TESTSIGNING ON

Windows guest agent服务也必须安装,才能同步剪贴板、屏幕分辨率。解开那个vdagent-xxxxx.zip,在管理员权限命令行执行“vdservice install”,然后reboot。

客户端加上?full-screen=auto-conf参数,就能全屏且同步分辨率了。Shift-F11切换全屏状态

spice性能确实比rdp好太多,看flash、gif基本不卡,视频音画基本同步,鼠标、键盘无延迟。最爽的,可以用招商银行大众版了!

安全的spice

spice缺省使用不安全连接。

用下面脚本创建必须的key

#!/bin/bash
SERVER_KEY=server-key.pem
# creating a key for our ca
if [ ! -e ca-key.pem ]; then
 openssl genrsa -des3 -out ca-key.pem 1024
fi
# creating a ca
if [ ! -e ca-cert.pem ]; then
 openssl req -new -x509 -days 1095 -key ca-key.pem -out ca-cert.pem  -subj "/C=IL/L=Raanana/O=Red Hat/CN=my CA"
fi
# create server key
if [ ! -e $SERVER_KEY ]; then
 openssl genrsa -out $SERVER_KEY 1024
fi
# create a certificate signing request (csr)
if [ ! -e server-key.csr ]; then
 openssl req -new -key $SERVER_KEY -out server-key.csr -subj "/C=IL/L=Raanana/O=Red Hat/CN=my server"
fi
# signing our server certificate with this ca
if [ ! -e server-cert.pem ]; then
 openssl x509 -req -days 1095 -in server-key.csr -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
fi
# now create a key that doesn't require a passphrase
openssl rsa -in $SERVER_KEY -out $SERVER_KEY.insecure
mv $SERVER_KEY $SERVER_KEY.secure
mv $SERVER_KEY.insecure $SERVER_KEY
# show the results (no other effect)
openssl rsa -noout -text -in $SERVER_KEY
openssl rsa -noout -text -in ca-key.pem
openssl req -noout -text -in server-key.csr
openssl x509 -noout -text -in server-cert.pem
openssl x509 -noout -text -in ca-cert.pem
# copy *.pem file to /etc/pki/libvirt-spice
if [[ ! -d "/etc/pki/libvirt-spice" ]]
then
 mkdir -p /etc/pki/libvirt-spice
fi
cp ./*.pem /etc/pki/libvirt-spice
# echo --host-subject
echo "your --host-subject is" \"`openssl x509 -noout -text -in server-cert.pem | grep Subject: | cut -f 10- -d " "`\"
echo "copy ca-cert.pem to %APPDATA%\spicec\spice_truststore.pem or ~/.spice/spice_truststore.pem in your clients"

根据提示记住?host-subject,拷贝ca-cert.pem到指定位置

关闭虚拟机,重新启动libvirtd(sudo /etc/init.d/libvirt-bin restart)

客户端用“spicec -h HOSTNAME -s TLS-PORT ?host-subject HOST-SUBJECT -w PASSWORD”连接

其它

虚拟机硬件配置很容易调整,导致Windows激活经常失效。在线激活失败时,选电话激活,根据提示打电话,跟客服mm稍加解释,就能顺利激活了。(此条只对正版Windows有效)

主要参考

SSLConnection ? Spice

QA:Testcase Virtualization Manually set spice listening port with TLS port set

WinQXL ? Spice

Networking ? KVM

manpages

(待补充:port forward)


推荐阅读
author-avatar
喂_早安学院_703
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有