命令记不住

登陆

KVM 部署 Linux 和 Windows 虚拟机

KVM部署Linux和Windows虚拟机

1.使用命令行部署Linux虚拟机

1、使用纯字符console控制台安装Linux虚拟机


第1步,使用命令“chmod”修改vm-iso目录权限,如果不修改目录权限,后续安装虚拟机过程中挂载ISO文件可能会出现问题。

[root@localhost /]# chmod 777 vm-iso
[root@localhost /]# ll vm-iso
总用量 811008
-rw-r--r--. 1 root root 830472192 1月  11 2018 CentOS-7-x86_64-Minimal-1708.iso

第2步, 使用命令“mkdir”创建虚拟机目录并修改权限。

[root@localhost /]# mkdir /vm-centos7
[root@localhost /]# chmod 777 vm-centos7
[root@localhost /]# ll /vm-centos7
总用量 0

第3步,使用命令“qemu-img”创建虚拟机硬盘文件以及查看硬盘信息,可选项,如果未创建磁盘文件,安装虚拟机过程中可以创建。

[root@localhost vm-centos7]# qemu-img create -f raw centos7.raw 10G
Formatting 'centos7.raw', fmt=raw size=10737418240 
[root@localhost vm-centos7]# qemu-img info centos7.raw 
image: centos7.raw
file format: raw
virtual size: 10G (10737418240 bytes)
disk size: 0

第4步,使用命令“virt-install”安装虚拟机,这是一条相对比较长的命令,大家不要头晕,仔细看看参数解释其实就明白后面跟的参数就是虚拟机的各种硬件等配置,具体看下面的参数解释。

[root@localhost vm-centos7]# virt-install --name centos7 --vcpus 1 --ram 2048 --location=/vm-iso/CentOS-7-x86_64-Minimal-1708.iso --disk path=/vm-centos7/centos7.raw,size=10,format=raw --network bridge=virbr0 --os-type=linux --os-variant=rhel7 --extra-args='console=ttyS0' --noautoconsole --force

参数解释:
--name:指定虚拟机名称。
--vcpus:指定虚拟机使用的CPU数量。
--ram:指定虚拟机使用的内存大小,单位为MB。
--location:指定虚拟机安装操作系统调用的ISO路径。
--disk:指定虚拟机安装操作系统使用的硬盘。
--network:指定虚拟机使用的网络。
--os-type:指定虚拟机使用的操作系统,windows或linux。
--os-variant:指定虚拟机具体使用的操作系统版本,rhel7或windows 10等。
--extra-arg:指定安装时控制的工具,注意不能与vnc混用。
--noautoconsole:指定不使用自动console控制,如果不使用该参数,可能出现“ERROR unsupported format character '0xffffffe7) at index xx”等错误提示,但不会影响虚拟机安装。

第5步,执行命令后,如果参数设定没有问题,会出现下列提示,如果未出现“开始安装”或者其他提示,请检查参数设定是否正确。

开始安装......
搜索文件 .treeinfo...... | 354 B 00:00:00 
搜索文件 vmlinuz...... | 5.6 MB 00:00:00 
搜索文件 initrd.img...... | 46 MB 00:00:00 
域安装仍在进行。您可以重新连接
到控制台以便完成安装进程。
[root@localhost vm-centos7]#

第6步,虚拟机centos7已经开始安装,如何进行安装控制操作呢,可以通过命令行以及VNC工具连接到虚拟机进行控制,先使用命令行进行安装操作,使用命令“virsh console centos7”连接到虚拟机控制台,控制台是纯字符操作界面。

Starting installer, one moment...
anaconda 21.48.22.121-1 for CentOS 7 started.
 * installation log files are stored in /tmp during the installation
 * shell is available on TTY2
 * when reporting a bug add logs from /tmp as separate text/plain attachments
04:44:07 Not asking for VNC because we don't have a network

Installation
 1) [x] Language settings                 2) [!] Time settings
        (English (United States))                (Timezone is not set.)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 
[anaconda] 1:main* 2:shell  3:log  4:storage-lo> Switch tab: Alt+Tab | Help: F1

参数解释:

Language settings:设定安装语言。
Time settings:设定时区。
Installation source:设定安装源。
Software selection:选择安装的软件包。
Installation Destination:选择安装目的硬盘。
Kdump:内核崩溃转储机制,使用默认配置即可。
Network configuration:设定网络。
Root password:设定root用户密码。
User creation:创建新的用户。

注意:带 [!] 的参数是需要配置的,配置完成后按“b”键开始安装操作系统,如果参数没有设定正确,按“b”键不会安装操作系统,需要继续设定参数。

第7步,输入数字2进入Time settings设置,可以选择手动设定时区也可以配置NTP服务器,根据实际情况进行设定即可,输入数字2进入时区设定。

Time settings
Timezone: not set
NTP servers:not configured
 1)  Set timezone  #设置时区
 2)  Configure NTP servers  #配置NTP服务器
  Please make your choice from above ['q' to quit | 'c' to continue |
  'r' to refresh]: 1

第8步,选择需要设定的时区,输入数字2选择Asia。

Timezone settings
Available regions
 1)  Europe                 6)  Pacific               10)  Arctic
 2)  Asia                   7)  Australia             11)  US
 3)  America                8)  Atlantic              12)  Etc
 4)  Africa                 9)  Indian
 5)  Antarctica
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]:2

第9步,选择时区所在城市,输入数字64选择Shanghai。

1)  Aden                  29)  Hong_Kong             56)  Pontianak
 2)  Almaty                30)  Hovd                  57)  Pyongyang
……(省略)
 9)  Baghdad               37)  Karachi               64)  Shanghai
……(省略)
Please select the timezone.
Use numbers or type names directly [b to region list, q to quit]:64

第10步,时区配置完成后回到主配置界面,可以看到Time settings选项由原来未配的 [!]状态变为[x]状态,说明已配置,同时下面会显示配置相关参数“Asia/Shanghai timezone”。

Installation
 1) [x] Language settings                 2) [x] Time settings
        (English (United States))                (Asia/Shanghai timezone)
 3) [x] Installation source               4) [x] Software selection
        (Local media)                            (Minimal Install)
 5) [!] Installation Destination          6) [x] Kdump
        (No disks selected)                      (Kdump is enabled)
 7) [ ] Network configuration             8) [!] Root password
        (Not connected)                          (Password is not set.)
 9) [!] User creation
        (No user will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: 

第11步,基本配置选项操作方式与Time settings基本一致,配置完成后界面如下。

Installation
1) [x] Language settings                 2) [x] Time settings
        (English (United States))                (Asia/Shanghai timezone)
 3) [!] Installation source               4) [!] Software selection
        (Processing...)                          (Processing...)
 5) [x] Installation Destination          6) [x] Kdump
        (Automatic partitioning                  (Kdump is enabled)
        selected)                         8) [x] Root password
 7) [x] Network configuration                    (Password is set.)
        (Wired (eth0) connected)
 9) [x] User creation
        (User admin will be created)
  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]:

第12步,输入“b”键后开始操作系统的安装,安装完成后操作系统会重新启动。

  Please make your choice from above ['q' to quit | 'b' to begin installation |
  'r' to refresh]: b

Progress
Setting up the installation environment
Creating disklabel on /dev/vda
Creating xfs on /dev/vda1
Creating lvmpv on /dev/vda2
Creating swap on /dev/mapper/centos-swap
……(省略)
Installing elfutils-default-yama-scope (100/299)
Installing elfutils-libs (101/299)
Installing freetype (102/299)
……(省略)
Installing iwl6000-firmware (299/299)
Performing post-installation setup tasks
Installing boot loader
Performing post-installation setup tasks
Configuring installed system
Writing network configuration
.reating users
Running post-installation scripts
Use of this product is subject to the license agreement found at /usr/share/centos-release/EULA
Installation complete.  Press return to quit
dracut Warning: Killing all remaining processes
Rebooting.
[ 1728.665719] Restarting system.
[root@localhost vm-centos7]#

第13步,使用命令“virsh start”启动虚拟机,再使用“virsh console”命令连接到虚拟机控制台。

[root@localhost vm-centos7]# virsh start centos7
域 centos7 已开始
[root@localhost vm-centos7]# virsh list
 Id    名称                         状态
 10    centos7                        running

[root@localhost vm-centos7]# virsh console centos7
连接到域 centos7
换码符为 ^]
CentOS Linux 7 (Core)   
Kernel 3.10.0-693.el7.x86_64 on an x86_64
kvm-centos login: root    #登录到虚拟机
[   24.879010] random: crng init done
Password: 
[root@kvm-centos ~]# uname –a   #查看Linux版本信息
Linux kvm-centos 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@kvm-centos ~]# 

第14步,使用命令“ip addr”以及PING命令查看网络的连通性,注意IP地址目前为自动获取的地址,可以通过KVM主机访问外部网络,使用ctrl+]可以从虚拟机console控制台退回到KVM主机命令行界面。

[root@kvm-centos ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:43:5c:3b brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.229/24 brd 192.168.122.255 scope global dynamic eth0
       valid_lft 3547sec preferred_lft 3547sec
    inet6 fe80::583e:4057:f346:a0a/64 scope link 
       valid_lft forever preferred_lft forever
[root@kvm-centos ~]# ping 119.6.6.6
PING 119.6.6.6 (119.6.6.6) 56(84) bytes of data.
64 bytes from 119.6.6.6: icmp_seq=1 ttl=250 time=6.19 ms
64 bytes from 119.6.6.6: icmp_seq=2 ttl=250 time=6.52 ms
……(省略)
--- 119.6.6.6 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 4.580/6.965/11.150/2.208 ms
[root@kvm-centos ~]# 
[root@localhost vm-centos7]#

至此,在KVM主机上使用命令行成功部署第一台Linux虚拟机,虚拟机已经能够正常使用。

2.使用vnc控制台安装Linux虚拟机

除了使用纯字符界面安装虚拟机外,也可以配置启用vnc server,然后通过vnc viewer客户端工具连接到虚拟机更加直观看到虚拟机的安装过程,使用vnc连接虚拟机需要先在到KVM主机上配置vnc server。

第1步,使用命令“yum install tigervnc tigervnc-serve”在KVM主机上安装vnc server。

[root@localhost /]# yum install tigervnc tigervnc-server  

第2步,配置vnc服务,使用命令复制[email protected] 文件,复制生成新的文件vncserver@:1.service。

[root@localhost /]# cp /lib/systemd/system/[email protected] /lib/systemd/system/vncserver@:1.service

第3步,使用命令“vncpasswd”配置vnc连接密码。

[root@localhost ~]# vncpasswd
Password:
Verify:
Would you like to enter a view-only password (y/n)? y``
Password:
Verify:
[root@localhost ~]#

第4步,使用命令“systemctl start vncserver@:1.service”启动服务,启动后使用命令“systemctl status vncserver@:1.service”查看服务状态是否处于“active (running)”状态。

[root@localhost ~]# systemctl start vncserver@:1.service
[root@localhost ~]# systemctl status vncserver@:1.service

3.使用vnc部署Windows虚拟机

Windows操作系统由于一些特殊性,不能像Linux可以使用纯字符界面进行部署,对于在KVM主机上使用的Windows虚拟机推荐使用vnc viewer客户端工具连接到虚拟机进行后续的安装, R2(生产环境用于服务器端)以及Windows 7(生产环境用于虚拟桌面端)两种Windows虚拟机。

1、准备工作

  • (1)准备Windows操作系统ISO文件并上传至KVM主机。
  • (2)安装好vncserver,确保vnc viewer可以连接到虚拟机(注意不是KVM主机)。

2、使用vnc部署Windows 2012 R2虚拟机
使用命令部署Windows 2012 R2虚拟机命令与部署Linux虚拟机基本一致,主要是引导方式的变化。

第1步,使用命令“virt-install”安装虚拟机,详细参数解释如下。注意安装Windows虚拟机使用了boot引导参数以及cdrom路径。

root@kvm-server:/home/admin# virt-install  --name win2012 --vcpus 2 --ram 4096 --boot cdrom --cdrom=/vm-iso/cn_windows_server_2012_r2.iso --disk path=/vm-win/win2012.raw,size=20,format=raw  --network bridge=virbr0 --os-type=windows  --vnc --vncport=5900 --vnclisten=0.0.0.0 --noautoconsole --force  
Starting install...
Allocating 'win2012.raw'                                                                       |  20 GB  00:00:00   
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

参数解释:
--name:指定虚拟机名称。
--vcpus:指定虚拟机使用的CPU数量。
--ram:指定虚拟机使用的内存大小,单位为MB。
--boot:指定虚拟机使用什么引导安装。
--cdrom:指定虚拟机安装操作系统时使用cdrom的路径。
--disk:指定虚拟机安装操作系统使用的硬盘。
--network:指定虚拟机使用的网络。
--os-type:指定虚拟机使用的操作系统,windows或linux。
--os-variant:指定虚拟机具体使用的操作系统版本,rhel7或windows 10等。
--vnc:指定安装使用vnc进行控制。
--vncport:指定vnc使用的端口。
--vnclisten=0.0.0.0:指定vnc侦听的IP地址。
--noautoconsole:指定不使用自动console控制,如果不使用该参数,可能出现“ERROR    unsupported format character '0xffffffe7) at index xx”等错误提示,但不会影响虚拟机安装。

第2步,使用vnc viewew客户端工具连接到虚拟机,如图3-2-1所示,安装过程与在物理服务器安装Windows操作系统一样,系统开始载入安装文件。

第3步,进入Windows 2012 R2操作系统安装界面。

第4步,开始安装Windows 2012 R2操作系统。

第5步,完成Windows 2012 R2操作系统安装,查看计算机相关信息。

第6步,查看Windows 2012 R2操作系统网络连通性,通过图3-2-5可以看到,网络连接正常。

第7步,使用命令“virsh list”查看虚拟机运行状态,目前为运行状态。

root@kvm-server:/home/admin# virsh list 
 Id    Name                           State
 8     win2012                        running

4.使用命令部署Windows 7虚拟机

使用命令部署Windows 7虚拟机命令与部署Windows 2012 R2虚拟机基本一致,主要注意在安装过程中Windows 7驱动加载问题。

第1步,使用命令“virt-install”安装虚拟机,详细参数解释如下。注意安装Windows虚拟机使用了boot引导参数以及cdrom路径。

root@kvm-server:/home/admin# virt-install  --name win7 --vcpus 2 --ram 4096 --boot cdrom --cdrom=/vm-iso/WIN7_MSDN.iso --disk path=/vm-win/win7.raw,size=20,format=raw  --network bridge=virbr0 --os-type=windows  --vnc --vncport=5901 --vnclisten=0.0.0.0 --noautoconsole --force  
Starting install...
Allocating 'win7.raw'                                                                       |  20 GB  00:00:00   
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

参数解释:
--name:指定虚拟机名称。
--vcpus:指定虚拟机使用的CPU数量。
--ram:指定虚拟机使用的内存大小,单位为MB。
--boot:指定虚拟机使用什么引导安装。
--cdrom:指定虚拟机安装操作系统时使用cdrom的路径。
--disk:指定虚拟机安装操作系统使用的硬盘。
--network:指定虚拟机使用的网络。
--os-type:指定虚拟机使用的操作系统,windows或linux。
--os-variant:指定虚拟机具体使用的操作系统版本,rhel7或windows 10等。
--vnc:指定安装使用vnc进行控制。
--vncport:指定vnc使用的端口。
--vnclisten=0.0.0.0:指定vnc侦听的IP地址。
--noautoconsole:指定不使用自动console控制,如果不使用该参数,可能出现“ERROR    unsupported format character '0xffffffe7) at index xx”等错误提示,但不会影响虚拟机安装。

第2步,使用vnc viewew客户端工具连接到虚拟机,如图3-2-6所示,安装过程与在物理服务器安装Windows操作系统一样,系统开始载入安装文件。

第3步,进入Windows 7操作系统安装界面。

第4步,提示一下,早期的版本安装Windows 7操作系统过程中会出现无法识别硬盘的问题,需要手动加载KVM虚拟化专用的virtio驱动,新的版本测试了CentOS以及Ubuntu系统均无需再加载,直接识别到硬盘。

第5步,开始安装Windows 7操作系统。

第6步,完成Windows 7操作系统安装,查看计算机相关信息,通过图3-2-10可以看到虚拟机使用的硬盘为QEMU虚拟出来的硬盘。

第7步,查看Windows 7操作系统网络连通性,通过图3-2-11可以看到,网络连接正常。

第8步,使用命令“virsh list”查看虚拟机运行状态,目前为运行状态。

root@kvm-server:/vm-win# virsh list 
 Id    Name                           State
 8     win2012                        running
12 win7 running

4、使用命令部署Windows虚拟机常见问题的处理

(1)部署过程中不能识别硬盘问题

早期的版本安装Windows 操作系统过程中会出现无法识别硬盘的问题,需要手动加载KVM虚拟化专用的virtio驱动,新的版本测试了CentOS以及Ubuntu系统均无需再加载,直接识别到硬盘,如果使用老版系统遇到这样的问题可以下载virtio驱动解决,目前最新的版本是2018年8月发布的驱动,可以访问https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.160-1 / 进行下载,如图3-2-12所示。
![1]

(2)部署过程中鼠标漂移问题

在使用vnc viewer连接虚拟机进行部署Windows操作系统的时候,可能会出现vnc控制台与外部鼠标漂移的情况,特别是在老的版本中经常出现,一般情况可以通过添加usb鼠标解决,修改虚拟机配置xml配置文件即可.

至此,在KVM主机成功部署Windows2012 R2以及Windows 7两台虚拟机。

[1] https://opsxdev.com/opsxdevcom/vitio-win.png

推荐阅读