kvm console连接和shutdown虚拟机

8.1 HOST上的设置 虚拟机提供了s.

8.1 HOST上的设置

虚拟机提供了ssh进入的方式, 如果我们想要以virsh console方式进入虚拟机, 需要在host机器上的vm1.xml做些更改:

# virsh edit vm1

进入vim界面, 在<devices>…</devices>里面添加如下内容(注意, 如果已经存在就不要加了):

 

  1. <devices>
  2.   …
  3.   <serial type=‘pty’>
  4.     <source path=‘/dev/pts/2’/>
  5.     <target port=‘0’/>
  6.   </serial>
  7.   <console type=‘pty’ tty=‘/dev/pts/2’>
  8.     <source path=‘/dev/pts/2’/>
  9.     <target port=‘0’/>
  10.   </console>
  11. </devices>

 

8.2 GUEST上的设置使virsh console可以登录虚拟机

1) ubuntu

在虚拟机vm1中, 需要创建一个文件”/etc/init/ttyS0.conf”, 内容如下(#注释部分可以忽略):

 

  1. # ttyS0.conf – getty
  2. # This service maintains a getty on ttyS0 from the point the system is
  3. # started until it is shut down again.
  4. start on stopped rc RUNLEVEL=[2345]
  5. stop on runlevel [!2345]
  6. respawn
  7. 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:

  1. # ttyS0 – agetty for rhel6
  2. stop on runlevel [016]
  3. start on runlevel [345]
  4. instance ttyS0
  5. respawn
  6. pre-start exec /sbin/securetty ttyS0
  7. exec /sbin/agetty /dev/ttyS0 115200 vt100-nav

3) rhel5.4

# 添加ttyS0的安全许可,允许root登录:

  1. echo “ttyS0” >> /etc/securetty
# 在 /etc/grub.conf 文件中为内核添加参数, 让内核把输出定向至 ttyS0。
  1. console=ttyS0
# 在 /etc/inittab 中添加 agetty, 在inittab里加一个ttyS0在系统启动时会生成一个ttyS0来接收内核的数据。
  1. S0:12345:respawn:/sbin/agetty ttyS0 115200

 

8.3 virsh console

重启虚拟机后,进入 vm1

  1. # virsh console vm1

 

多敲几次回车, 敲入登录的用户名和密码.

退出虚拟机回到HOST按组合键:

Ctrl+]

9 为虚拟机添加启动脚本

创建一个脚本文件: firstboot_vm.sh

  1. # Set time zone
  2. cp /usr/share/zoneinfo/Asia/Harbin /etc/localtime
  3. # Set proxy server
  4. # replaceyourProxyHostIP by your true IP, if not, comment below line
  5. echo ‘Acquire::http::proxy “http://yourProxyHostIP:3142”;’ >> /etc/apt/apt.conf
  6. # ttyS0 -getty
  7. #
  8. # Thisservice maintains a getty on ttyS0 from the point the system is
  9. # starteduntil it is shut down again.
  10. echo “start on stopped rc RUNLEVEL=[2345]” > /etc/init/ttyS0.conf
  11. echo “stop on runlevel [!2345]” >> /etc/init/ttyS0.conf
  12. echo “respawn” >> /etc/init/ttyS0.conf
  13. echo “exec /sbin/getty -L 115200 ttyS0 vt220” >> /etc/init/ttyS0.conf

 

在创建虚拟机时指定它, 参考 “4.1 创建虚拟机的脚本: create_vm.sh”:

  1. –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并启动该服务即可.