kvm installing windows virtual machine

Author: SRE operation and maintenance blog

Blog address: https://www.cnsre.cn/

Article address: https://www.cnsre.cn/posts/211108848062/

Related topics: https://www.cnsre.cn/tags/kvm/

Preparation before installation

Verify that the CPU supports KVM

If there is vmx (Intel) or svm(AMD) in the result, it indicates that the CPU supports.

egrep '(vmx|svm)' /proc/cpuinfo

Close SELinux

Change SELinux=enforcing in / etc/sysconfig/selinux to SELinux=disabled

Install some of the most basic services

Optional, because I have just installed CentOS, so for the convenience of the following, first install some necessary tools

yum install epel-release net-tools vim unzip zip wget ftp -y

Install KVM and its dependencies

yum install qemu-kvm libvirt virt-install bridge-utils -y

Verify installation results

The following figure shows that the has been successfully installed

lsmod | grep kvm

Turn on kvm service

And set it to start automatically

systemctl start libvirtd systemctl enable libvirtd

View status operation results

As shown in the figure below, it indicates that the operation is good

systemctl status libvirtd

systemctl is-enabled libvirtd

Configure bridge mode

First, back up a copy of the network card configuration file in the / etc / sysconfig / network scripts / directory

Create ifcfg-br0 file

The IP address of the created br0 file should be consistent with the IP address of the physical network card. If you command ipconfig to view the physical network card, the IP address will not be displayed

The contents are as follows:

[root@bogon ~]*# vim /etc/sysconfig/network-scripts/ifcfg-br0*

DEVICE=br0
BOOTPROTO=none
DEFROUTE=yes
ONBOOT=yes
TYPE=Bridge
IPV4_FAILURE_FATAL=yes
IPADDR=192.168.1.130
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=221.6.4.66
DELAY=0
USERCE=no

Modify the original network card configuration

vim /etc/sysconfig/network-scripts/ifcfg-eno1s
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="YES"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eno1"
UUID="bb40d726-8d67-4187-90c3-eb61e1b42d61"
DEVICE="eno1"
ONBOOT="yes"
IPADDR="192.168.1.130"
NETAMSK=255.255.255.0
GATEWAY="192.168.1.254"
DNS1="221.6.4.66"
IPV6_PRIVACY="no"
BRIDGE=br0

service network restart

systemctl restart network

Use ifconfig to verify the operation results. There is an additional network card br0. Now you can use 192.168.1.130 to access the host computer.

Installing virtual machines

Preparing the operating system installation image file

In this article, you will use CentOS 7.2, which is the same as the host environment, and put the file in the / home/iso directory

Mount U SB flash disk

[root@nkgtsv01 ~]# fdisk -l
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

disk /dev/sda: 2995.0 GB, 2994995200000 Bytes, 5849600000 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte
 Disk label type: gpt
Disk identifier: 434CB2EB-C42E-4B9D-A76B-B759709EC159


#         Start          End    Size  Type            Name
 1         2048       206847    100M  EFI System      EFI System Partition
 2       206848       616447    200M  Microsoft basic
 3       616448   5849597951    2.7T  Linux LVM

disk /dev/mapper/centos-root: 214.7 GB, 214748364800 Bytes, 419430400 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte


disk /dev/mapper/centos-swap: 17.2 GB, 17179869184 Bytes, 33554432 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte


disk /dev/mapper/centos-home: 322.1 GB, 322122547200 Bytes, 629145600 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte


disk /dev/mapper/centos-tmp: 107.4 GB, 107374182400 Bytes, 209715200 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte


disk /dev/mapper/centos-data: 2333.2 GB, 2333249372160 Bytes, 4557127680 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 4096 byte
I/O size(minimum/optimum): 4096 byte / 4096 byte


disk /dev/sdb: 31.0 GB, 31004295168 Bytes, 60555264 sectors
Units = a sector of 1 * 512 = 512 bytes
 Sector Size (logic/Physics): 512 byte / 512 byte
I/O size(minimum/optimum): 512 byte / 512 byte
 Disk label type: dos
 Disk identifier: 0 xcad4ebea            Hard disk to mount

   equipment Boot      Start         End      Blocks   Id  System
/dev/sdb4   *         256    60555263    30277504    7  HPFS/NTFS/exFAT
[root@nkgtsv01 ~]# mkdir -p /mnt/udisk
[root@nkgtsv01 ~]# ls
anaconda-ks.cfg
[root@nkgtsv01 ~]# ls /mnt/
udisk  usb
[root@nkgtsv01 ~]# mount -t ntfs-3g /dev/sdb4 /mnt/udisk/
mount: Unknown file system type“ ntfs-3g"

[root@nkgtsv01 ~]# yum list *ntfs*
Plug in loaded: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.sjtu.edu.cn
 * epel: mirrors.ustc.edu.cn
 * extras: ftp.sjtu.edu.cn
 * updates: ftp.sjtu.edu.cn
 Installable packages
ntfs-3g.x86_64                              2:2017.3.23-1.el7                        epel
ntfs-3g-devel.x86_64                        2:2017.3.23-1.el7                        epel
ntfsprogs.x86_64                            2:2017.3.23-1.el7                        epel

already installed:
  ntfs-3g.x86_64 2:2017.3.23-1.el7

complete!
[root@nkgtsv01 ~]#yum install ntfs-3g.x86_64 -y

[root@nkgtsv01 ~]# mount -t ntfs-3g /dev/sdb4 /mnt/udisk/
The disk contains an unclean file system (0, 0).
The file system wasn't safely closed on Windows. Fixing.

[root@nkgtsv01 ~]#
[root@nkgtsv01 ~]# ls /mnt/
udisk  usb
[root@nkgtsv01 ~]# ls /mnt/udisk/
CentOS-7.2-x86_64-DVD-1611.iso  CentOS-7-x86_64-DVD-1708.iso  maven_storey2.zip
[root@bogon data]# mkdir -p /data/iso
[root@bogon data]# ls
iso  kvm-bak  network
[root@nkgtsv01 ~]# mount  -o loop /mnt/udisk/CentOS-7-x86_64-DVD-1708.iso /data/iso/
mount: /dev/loop0 Write protected, will mount as read-only

Create a directory where virtual machine files are stored

mkdir -p /data/kvm-images

{{< alert theme="warning" dir="ltr" >}}
⚠️ be careful

About virtio win driver

{{< /alert >}}

Virtio win driver is mainly used for windows virtual machine in kvm. Virtio win provides yum source to install rpm package. Virtio driver will be installed in / usr / share / virtio win directory by default and can be shared with windows virtual machine in kvm.

sudo wget https://fedorapeople.org/groups/virt/virtio-win/virtio-win.repo -O /etc/yum.repos.d/virtio-win.repo
# Add repo

The repo file provides two different repositories:

  • virtio-win-stable
    By default, the warehouse version and RHEL version are released together, which means that they have been tested and stable.

  • virtio-win-latest
    The warehouse provides the latest build versions. These versions may have various problems, and users should bear their own risks. The warehouse is disable d by default. If you want to update from the stable version to the latest version, you can use the following command:

    sudo yum --enablerepo=virtio-win-latest update virtio-win
    

The files after installation will be stored in / user/share, as follows:

/usr/share/virtio-win/*.iso: ISO Image, including all drivers. Details will be described later
/usr/share/virtio-win/*.vfd: be used for Windows XP Lower VFD Floppy drive image
/usr/share/virtio-win/drivers: from VFD Files extracted from floppy drive image
/usr/share/guest-agent/*.msi: QEMU Guest Agent 32bit And 64 bit Installation package

The ISO image content is:

NetKVM/: Virtio Network driven
viostor/: Virtio Block drive
vioscsi/: Virtio SCSI drive
viorng/: Virtio RNG drive
vioser/: Virtio Serial Drivers 
Balloon/: Virtio Memory balloon driver
qxl/: be used for Windows 7 And previous versions QXL Graphics card driver. (virtio-win-0.1.103-1 And later versions will be created)
qxldod/: be used for Windows 8 And later versions QXL Graphics card driver. (virtio-win-0.1.103-2 And later versions will be created)
pvpanic/: QEMU pvpanic Device drive (virtio-win-0.1.103-2 And later versions will be created)
guest-agent/: QEMU Guest Agent 32bit And 64 bit Installation package
qemupciserial/: QEMU PCI Serial device driver

Or you can get it from one below

Stable virtio win ISO

Stable virtio win x86 floppy

Stable virtio win AMD64 floppy

Latest virtio win ISO

Latest virtio win x86 floppy

Latest virtio win AMD64 floppy

Latest QEMU GA files

All download directories

Change log

Creating virtual machines using virt install

[root@bogon data]# virt-install --virt-type kvm --name=njvm2k8 --ram=1024 --vcpus=1 --os-type=windows --accelerate --cdrom=/home/kvm_data/iso/cn_windows_server_2008_r2_617396.iso --disk path=/usr/share/virtio-win/virtio-win.iso,device=cdrom --disk path=/home/kvm_data/img/njvm2k8.qcow2,bus=virtio --network bridge=br0 --graphics vnc,listen=0.0.0.0,password=123456,port=5920 --noautoconsole
 

WARNING  Unable to connect to graphics console: not installed virt-viewer. Please install 'virt-viewer' Software package.
WARNING  There is no console to start the client. The default is --wait -1
 Start installation......
Allocating 'njvm2k8.img'                                   | 200 GB  00:00:00
 Deleting disk 'njvm2k8.img'                                |    0 B  00:00:00
 Domain installation failed. You can restart your domain by running the following command:
'virsh start virsh --connect qemu:///system start njvm2k8'
Otherwise, restart the installation

Ignore the mistakes in it and go on

Command meaning

virt-install 
--name=Client name  
--ram Memory (1024) 1 G 
--vcpus= cpu1 
--os-type= Client system type  
--os-variant= win2k8 
--disk path= Client file storage path ,
size=Client hard disk size 200 
--accelerate 
--cdrom  (Mirror file path) 
--vnc --vncport=vnc Remote port 5910 
--network bridge=Network card bridging br0,model=virtio 
--noautoconsole

Open port 5910 on the firewall

Because the port used by VNC is 5910 by default, the firewall needs to be turned on. If the firewall is turned off, please ignore it

 firewall-cmd --zone=public --add-port=5910/tcp --permanent              

View and open the newly created virtual machine

virsh list --all
virsh start njvm2k8-win

Linking virtual machines using VNC

vncviewer download address

Use VNC to enter the host IP address plus the port number, enter the virtual machine name, and then open

{{< alert theme="warning" dir="ltr" >}}
⚠️ be careful

Hard disk not found during installation

{{< /alert >}}

If the hard disk cannot be found during reinstallation, please perform the following operations

Mount win image

virsh change-media njvm2k8  hdb /usr/share/virtio-win/virtio-win.iso    

Then, cancel the attached image, and mount the system image to the system again for partition installation

Restart the virtual machine after installation

After installation, the windows virtual machine will be restarted

We check the status and start the virtual machine

Wait for the installation to complete

After starting up and setting the password, we entered the new windows operating system

Link with vnc

{{< alert theme="warning" dir="ltr" >}}
⚠️ be careful
Mount floppy drive (hard disk drive, network card drive, etc.) Note: the network card drive should be mounted after it is installed in the system, otherwise it will be prompted that the CD image cannot be found when installing the system
{{< /alert >}}

Execute the command in the host

virsh change-media njvm2k8  hdb /usr/share/virtio-win/virtio-win.iso 

After the VMTools tool is installed, use the following command to uninstall the mounted VMTools tool:

a.If it is Linux virtual machine,Execute the following command to uninstall VMTools tool:

virsh change-media [virtual machine ID] --eject [vmtools-linux.iso [full file path]

b.If it is Windows virtual machine,Execute the following command to uninstall VMTools tool:

virsh change-media [virtual machine ID] --eject [vmtools-windows.iso [full file path]

Mount the drive image file to the njvm2k8 host

Link with vnc

Author: SRE operation and maintenance blog

Blog address: https://www.cnsre.cn/

Article address: https://www.cnsre.cn/posts/211108848062/

Related topics: https://www.cnsre.cn/tags/kvm/

Tags: Windows CentOS kvm

Posted on Sun, 07 Nov 2021 23:59:08 -0500 by DWilliams