Why bother with KVM virtual machine
Recently, due to company affairs, I don’t have much time to write, so I will use a technical article to make up for it.
The thing is like this, our company has a small thing that is developed based on an embedded system and does not belong to the Android or iOS platform. Well, what does this mean? This means that the language for developing this little thing is C language, not Java, Objective-C or Swift. So we need a Cross Compiler, also known as a cross-compilation environment, to compile this code.
In fact, the iOS App compiled by Xcode also belongs to the category of cross-compilation.
However, this cross-compilation environment is only for Windows platform! In fact, it seems that apart from GCC, the most useful cross-compilation environments are those under Windows. It is estimated that many students who play embedded systems should know that the Keil and IAR series are commonly used in the industry now. Unfortunately, both are Windows-specific and there are no macOS or Linux versions.
Why does Windows make me so unhappy? Because usually in order to better improve work efficiency, except when the industry application software that we have to use must run under Windows, we only use Mac instead of Windows. So, we basically don’t have idle and wealthy Windows hosts, no! There is no such thing at all. There is no such thing as "basically no".
On the server side, don’t even talk to me about Windows Server. As a crazy user of macOS and Ubuntu, it is impossible for me to run a Windows Server on Bare metal. We only run Linux on the server and are not interested in anything else.
Then the question is, we have an automated compilation system, which is also known as CI system (Continuous integration), using Atlassian's family bucket: Bitbucket, Bamboo, JIRA and Confluence, etc., all running on In a Docker virtualization container on the Linux platform.
Now we want to automate the compiler for this little thing, so as to better release manpower and make it easier to track and manage the content of each Release in the future. The first problem to solve is: we do not have a Windows server.
Since virtualization is so popular now, let’s use the existing Ubuntu server to virtualize a Windows. Okay, let’s go!
What is KVM
The full name of KVM is: Kernel-based Virtual Machine. In a simple sentence, it is a virtualization management system based on the Linux kernel.
This feature has been integrated since Linux kernel version 2.6.20. Simply understand that Docker is virtualization at the application layer, while KVM is virtualization at the system layer.
The difference is that the content of Docker virtualization must share the kernel with the Host host, which means that it can only run Linux-type systems.
KVM virtualizes the entire host, so it can install different operating systems and is not limited to Linux itself. When I have a fit later, I'll see if I can virtualize macOS on Ubuntu. In this way, there is no need to get a separate Mac host to automatically compile the iOS App.
Install KVM
I am using the Ubuntu 16.04 LTS version here. Considering long-term use and stability, I basically only choose the LTS version. The other versions are not much different, so just refer to it.
The first step is to install the KVM-related package files. Because my server is all command line and the X desktop is not installed, I added the --no-install-recommends parameter. Otherwise it will install packages like virt-viewer, which have X11 and many graphics libraries among their dependencies, none of which are needed. But if you enable the desktop system, you do not need to add this parameter.
$ sudo apt-get install --no-install-recommends qemu-kvm qemu-utils libvirt-bin virtinst cpu-checker
Let’s verify if everything is OK
$ kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
Very good, everything goes well, let's get another bridge network. In fact, this is not necessary, it depends on your usage scenario.
KVM will create a virbr0 bridge network by itself, but this is a NAT network and there is no way to communicate with other hosts in the LAN, so don’t be lazy and build a bridge network yourself.
The reference configuration is as follows:
$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The bridged network interface
auto br0
iface br0 inet static
address 1.2.3.4
netmask 255.255.255.0
gateway 1.2.3.1
dns-nameservers 1.2.3.1
bridge_ports enp9s0
bridge_stop off
bridge_fd 0
bridge_maxwait 0
Restart the network and verify the bridge status:
$ sudo systemctl restart networking
$ sudo brctl show
bridge name bridge id STP enabled interfaces
br0 8000.f079593874d9 no enp9s0
virbr0 8000.525400087ef2 yes virbr0-nic
OK, everything is normal and you can start creating a virtual host.
Create virtual host
KVM has only completed the first step, we still need to create a virtual host before we can continue.
Before we start, we need to prepare a few things:
1.Windows installation image
2.Virtio ISO and floppy disk image
3.VNC client (included with macOS)
I am using Virtio 0.1.126-2 version here. The operating system version is: Ubuntu 16.04.1 LTS.
Everything is ready, use the virt-install command to help create a virtual machine:
virt-install \
--name win10 \
--memory 2048 \
--vcpus sockets=1,cores=1,threads=2 \
--cdrom=/path/to/windows_10.iso \
--os-variant=win8.1 \
--disk /path/to/win10/win10.qcow2,bus=virtio,size=40 \
--disk /path/to/virtio/virtio-win-0.1.126_amd64.vfd,device=floppy \
--network bridge=br0,model=virtio \
--graphics vnc,password=Passw0rd,port=5910 \
--hvm \
--virt-type kvm
Basically the configuration information is all above, the virtual information machine configuration is as follows:
2G memory
1 CPU, 1 core, 2 threads
1 CDROM (Windows installation disc)
40G hard disk (system disk)
1 floppy drive (Virtio driver)
Open a VNC remote desktop on port 5910
If you enter the command correctly, you should get a similar feedback message:
Starting install...
Creating domain...
Domain installation still in progress. Waiting for installation to complete.
To put it simply, the virtual machine has been created, but because there is no graphical interface, there is no way to proceed. Once we have VNC connected, we can continue. This prompt will only appear once. After the system is installed, this startup waiting situation will not occur.
Install Windows 10
Although when we created the virtual machine, we specified the use of port 5910 to provide VNC remote desktop. However, this port cannot be accessed directly because it is bound to 127.0.0.1 by default, which means it can only be accessed locally. We need to first map the remote local port to the local port.
ssh -L 5910:127.0.0.1:5910 server
Then under macOS, ⌘ Space calls out Spotlight, and then enter:
vnc://127.0.0.1:5910
Enter the password in the pop-up window: Passw0rd
Then you can happily install the Windows system. If the hard disk cannot be recognized, you need to manually load the driver and select drive A and the corresponding Windows directory. Similarly, if the network card does not have a driver, the same method applies.
Attached is a screenshot of the installation completion:
The above is the detailed content of Guide to Configuring and Installing Windows Operating Systems in KVM for Ubuntu. For more information, please follow other related articles on the PHP Chinese website!