8.1 HOST上的设置
虚拟机提供了ssh进入的方式, 如果我们想要以virsh console方式进入虚拟机, 需要在host机器上的vm1.xml做些更改:
# virsh edit vm1
进入vim界面, 在<devices>…</devices>里面添加如下内容(注意, 如果已经存在就不要加了):
- <devices>
- …
- <serial type=‘pty’>
- <source path=‘/dev/pts/2’/>
- <target port=‘0’/>
- </serial>
- <console type=‘pty’ tty=‘/dev/pts/2’>
- <source path=‘/dev/pts/2’/>
- <target port=‘0’/>
- </console>
- </devices>
8.2 GUEST上的设置使virsh console可以登录虚拟机
1) ubuntu
在虚拟机vm1中, 需要创建一个文件”/etc/init/ttyS0.conf”, 内容如下(#注释部分可以忽略):
- # ttyS0.conf – getty
- # This service maintains a getty on ttyS0 from the point the system is
- # started until it is shut down again.
- start on stopped rc RUNLEVEL=[2345]
- stop on runlevel [!2345]
- respawn
- exec /sbin/getty -L 115200 ttyS0 vt220
这个”/etc/init/ttyS0.conf”可以在创建虚拟机的最后阶段, 作为一个启动脚本加进去.
2) rhel6.4或centos6.5
How to activate the serial console in CentOS 6. Useful for logging into a virtual guest via ‘virsh console’.
/etc/init/ttyS0.conf:
- # ttyS0 – agetty for rhel6
- stop on runlevel [016]
- start on runlevel [345]
- instance ttyS0
- respawn
- pre-start exec /sbin/securetty ttyS0
- exec /sbin/agetty /dev/ttyS0 115200 vt100-nav
3) rhel5.4
# 添加ttyS0的安全许可,允许root登录:
- echo “ttyS0” >> /etc/securetty
- console=ttyS0
- S0:12345:respawn:/sbin/agetty ttyS0 115200
8.3 virsh console
重启虚拟机后,进入 vm1
- # virsh console vm1
多敲几次回车, 敲入登录的用户名和密码.
退出虚拟机回到HOST按组合键:
Ctrl+]
9 为虚拟机添加启动脚本
创建一个脚本文件: firstboot_vm.sh
- # Set time zone
- cp /usr/share/zoneinfo/Asia/Harbin /etc/localtime
- # Set proxy server
- # replaceyourProxyHostIP by your true IP, if not, comment below line
- echo ‘Acquire::http::proxy “http://yourProxyHostIP:3142”;’ >> /etc/apt/apt.conf
- # ttyS0 -getty
- #
- # Thisservice maintains a getty on ttyS0 from the point the system is
- # starteduntil it is shut down again.
- echo “start on stopped rc RUNLEVEL=[2345]” > /etc/init/ttyS0.conf
- echo “stop on runlevel [!2345]” >> /etc/init/ttyS0.conf
- echo “respawn” >> /etc/init/ttyS0.conf
- echo “exec /sbin/getty -L 115200 ttyS0 vt220” >> /etc/init/ttyS0.conf
在创建虚拟机时指定它, 参考 “4.1 创建虚拟机的脚本: create_vm.sh”:
- –firstboot=firstboot_vm.sh
一般我们认为,windows系统下的acpi是默认开启的,但是本次我测试win2003虚拟机,发现默认情况下通过“virsh shutdown 虚拟机名称”无法正常关闭虚拟机
解决方法:
在虚拟机里面修改组策略配置:
1
2
3
|
“gpedit.msc”--“Computer Configuration\Windows Settings\Security Settings\Local Policies\Security Options\" Shutdown: Allow system to be shut down without having to log on” |
修改注册表信息:
然后运行ShutdownWarningDialogTimeout.reg
内容如下:
1
2
3
|
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows] "ShutdownWarningDialogTimeout" =dword:00000001 |
再次测试,发现问题得到解决。
Linux下只需安装acpid并启动该服务即可.