Virtual machines (VMs) came into existence in the 1960s. Here in this blog, we will answer the following questions: What is a VM? Why people need VMs? What role are VMs playing in blockchain projects?

What Is A VM?

So, what on earth is a VM? When first showed up in the computer world, VM is originally defined as an duplicate of a real computer machine.

But VM actually has different kinds. The first is called System VMs(also full virtualization VMs), it provide a substitute for a real machine. We can run full unmodified guest OS, like Linux or Windows, on top of it. Real world examples are VirtualBox and VMware server, to name two most popular ones among developers. The second kind is called Process VMs, which are designed to execute computer programs in a platform-independent environment. The well-known JVM is of this kind, on top of which we can run programs written in Java.

The concept of host comes hand in hand with VM. Remember VM is still software, we need to run it on top of a real computer. The physical, “real-world” hardware running the VM is generally referred to as the ‘host’.

To conclude, A VM is an OS(in case of System VM) or application(in case of Process VM) environment that runs on a host.

Why People Need VMs ?

Now we know what a VM is, let’s talk about why people need it.

The first use case. When my hardware CPU has a A architecture, while I need to run a program only works on a CPU of B architecture, VM will come in handy.

The second use case. When I can only afford one host, but need to run several OS at the same time. Say I have Linux install on my hardware machine, now I want to run a Linux server to host my web app, and a Windows Desktop to test my web app. Then I can pick a VM software, say Virtual Box, have it installed on my Linux platform, then fire two VM instance to run the other two OS I need.

The third case. I want to write my program once and have it run on all kinds of platforms. To avoid writing and compiling my program for different platform OS, I need a layer on top of each OS to eliminate the differences. Then VM is a great tool for the job. Say people only write their Java program once, and then they can have faith to run the program on any platform with JVM installed.

VMs are widely used in real work, and we have not all the use case listed yet.

Blockchain VMs

VMs are also used in blockchains.

If you are in the blockchain filed, there is a good chance you’ve already heard of Ethereum VM (EVM). It is the runtime environment for running programs, namely smart contracts, in Ethereum blockchain. People can run their program, written in a language called Solidity, on EVM. This reminds us of JVM just mentioned, both JVM and EVM are Process VMs.

And the Nervos CKB blockchain also has its own VM, named CKB-VM. CKB-VM takes a really different approach to run smart contracts. Instead of providing support only for program written in particular high-level language, CKB-VM is a System-VM imitating a RISC-V CPU hardware. In case you don’t know about RISC-V , it is a open source CPU architecture showing a lot of potential recent years. Now we can do our smart contracts in any high-level programming language we want, then have it compiled to binary for RISC-V CPU and run the program. Isn’t this cool?

Now I hope I made how VMs works on blockchains a bit more clearer for you.

Conclusion

That is all I have to say about VM. To recap, there are two kinds of VMs, one is System VM, the other Process VM. EVM is mostly a Process VM, while CKB-VM a System VM.

Ref:

以下是中文版:

虚拟机(VM)于20世纪60年代诞生。 在这篇博客中,我们将回答以下问题:什么是VM? 为什么人们需要虚拟机?虚拟机在区块链项目中扮演什么角色?

什么是虚拟机 VM ?

那么,虚拟机到底是什么? 最早出现在计算机世界中时,VM 的定义是真实计算机的副本。

但VM实际上有不同的种类。 第一种称为 System VM ,系统虚拟机(也称为完全虚拟化虚拟机),它可以替代硬件机器。我们可以在上面运行未经修改的操作系统,如 Linux 或 Windows 。 VirtualBox 和 VMware Server 就属于这类虚拟机,也是大家比较熟悉的。第二种是 Process VM,进程 VM ,它被设计为在独立于平台的环境中执行计算机程序。 众所周知的 JVM 就是这种类型,我们可以在 JVM 上运行 Java 程序。

Host 是一个跟 VM 紧密相关的概念。VM 毕竟是软件,所以必须要有真正的机器来运行它。上面运行了 VM 的真实机器,就叫做 Host ,宿主机。

总而言之,VM 个运行在真实机器上的软件,提供操作系统(在系统VM的情况下)或应用程序(在进程 VM的情况下)的运行环境。

VM 有什么用?

什么是 VM 聊完了,下面来聊 VM 能干些什么事。

第一个使用场景。当我的硬件 CPU 是 A 架构的,但是我要运行的程序只能在 B 架构的 CPU 上运行, VM 就会派上用场。

第二个使用场景。当我只能买得起一台计算机,但是需要同时运行多个操作系统。比如,我在我的硬件机器上安装了Linux 桌面版,现在我想要运行 Linux 服务器来托管我的 Web 应用,还需要一个 Windows 来测试 Web 应用。这样,我就可以选择一个可以安装到 Linux 上的 VM 软件,比如 Virtual Box,把它安装到我的电脑上,然后启动两个 VM 实例就可以运行我需要的另外两个操作系统了。

第三个使用场景。让我的程序可以在各种平台上运行。 为了不用给每个平台都写一个版本的代码并且专门编译一下, 我需要在每个操作系统之上添加一层来磨平平台差异,这时候 VM 就可以作为这一层来使用。比如,我只要编写一次 Java 程序,然后就可以在安装了 JVM 的任何平台上运行程序了。

VM 在实际工作中被广泛使用,以上其实还不是所有情形。

区块链和 VM

VM 在区块链领域也很常用。

如果你也搞区块链,所以大概率是已经知道以太坊虚拟机(EVM)了。EVM 可以让我们在以太坊上运行智能合约。 大家可以在 EVM 上运行用一种名为 Solidity 的语言编写的程序。这让我们想起了刚才提到的 JVM ,JVM 和 EVM 都是进程 VM。

Nervos CKB 区块链也有自己的 VM,名为 CKB-VM 。CKB-VM 运行智能合约的方式跟 EVM 不一样。CKB-VM不为特定高级语言编写的程序提供支持,而是一个模仿 RISC-V CPU 硬件的 System VM。[RISC-V](https://zhuanlan.zhihu.com/p/53565685)是一个开源 CPU 架构。 这种方式的好处是,可以用我们想要的任何高级编程语言写智能合约,然后将它编译为 RISC-V CPU 的二进制格式,就可以运行了。挺 Cool 的。

现在对区块链上的虚拟机原理是不是清晰了一些了呢。

总结

关于 VM ,主体内容就是这些了。总结一下,VM 分为两种,系统 VM 和进程 VM 。 EVM 可以归类为进程 VM ,CKB-VM 则属于系统 VM 。

参考: