Home > Web Front-end > JS Tutorial > Take you through a detailed explanation of JavaScript-based operating systems

Take you through a detailed explanation of JavaScript-based operating systems

黄舟
Release: 2017-03-09 14:47:11
Original
1513 people have browsed it

Take you through a detailed explanation of the JavaScript-based operating system:

I think most people have heard of Node.js, but have you heard that Have you tried NodeOS? That’s right, NodeOS, an operating system written in Node.js. Well, to be honest, NodeOS uses the Linux kernel to handle various low-level tasks, such as hardware communication and so on, but other than that, it uses Node.js. The development of NodeOS started two years ago. The creator's purpose was simple. He was just curious, "Is it possible to create an operating system using only Node.js?"

基于 JavaScript 的操作系统你听说过吗?

Is it possible to create an operating system using only Node.js?

What about this idea?

We've seen Node.js develop so rapidly in the past few years, so why don't we make it even cooler? For example, use it to make an operating system.

User-autonomous independent file system

NodeOS introduces an interesting feature: all users have an independent file system, and they complete various tasks in a simple file system. Because their "home directory" is actually the root of their own file system, they can install software packages into the system without any super permissions, and they don't need to configure anything, because the software packages are installed by default. Installed in their own home directory. In addition, this also provides good security. If a hacker breaks into an account, he can only access the part where the user is located. The end result is that the hacker cannot affect the entire system.

Node.js and NPM

You can think about it, if an operating system uses Node.js, it means that all packages available in NPM are also NodeOS packages. . At the time of writing, there are over 210,000 packages and growing every second. It wouldn’t be surprising if NodeOS has a million applications in a few years.

Based on the Linux kernel

This may not seem like much, Linux is the operating system used by most servers. Because NodeOS is based on the Linux kernel, you can use applications developed for other Linux distributions with minimal modifications.

Bad News

I would very much like NodeOS to be a finished work, but it is not yet. It still lacks some key features necessary for a server operating system. For example, the complete BASH tool set is missing, including ps, tail, nano, grep, etc. To make matters worse, you can't use it as a desktop operating system because it doesn't have a GUI. Of course, you can implement some of the missing functionality with just a little JavaScript, but it's too bad that none of it is there by default right now.

Okay, can I try NodeOS?

Use Docker to experience

The easiest and fastest way to experience NodeOS is as follows:

  1. A computer running Mac OSX or Linux, maybe Windows works too, but I haven't tried it.

  2. Docker.

After you have Docker installed, running a NodeOS instance is easy. You only need to execute the following command, and Docker will complete all this magic for you:

sudo docker run -t -i nodeos/nodeos
Copy after login

基于 JavaScript 的操作系统你听说过吗?

The easiest and fastest way to experience NodeOS It's through Docker.

When you run the above command, Docker will automatically download the NodeOS image from the warehouse and install it into a virtual environment. Once installed, an SSH session will open to NodeOS.

What if you don’t need docker?

In some cases you may not be able to use Docker to experience it, or you may want to experience the latest version of NodeOS. At the time of writing this article, the NodeOS image was generated two months ago, and the development version was updated six days ago. So, if you wish to use the latest version, you should start from the source code. This isn't too difficult, but it does take some time. What you need:

  1. A computer running Linux. You can compile it on OS X, but cross-platform compilation takes a lot of time, and the same goes for Windows.

  2. Linux compilation and build-related tools (make, g++, gcc, autoconf).

  3. Qemu.

  4. It really takes a lot of time.

If everything is ready, you can start compiling from the source code:

  1. Download the project source code: bash git clone git@github .com:NodeOS/NodeOS.git.

  2. Compile using the following commands: cd NodeOS and npm install.

  3. 我逐字引用了其官方文档的话:“拿上爆米花去看场电影吧,不开玩笑,真的。”,是的,它需要很多时间,做些有意思的事情去吧。

  4. 执行 bash npm start 来在 Qemu 中运行 NodeOS。

可以工作了吗?

当安装完成后,我们可以通过在 NodeOS 的 shell 中执行 ls命令来看看它是否工作了。输出类似如下:

[ 'etc', 'lib', 'lib64', 'root', 'bin', 'sys', 'usr', 'share', 'proc' ]
Copy after login

如果显示如上,说明一些基本的命令可以工作了。但是如果我们想要知道网卡地址呢?在 Linux 下,这个命令是 ifconfig ,让我们试试:

command not found: ifconfig
Copy after login

看起来没有 ifconfig 命令。这是因为 NodeOS 默认没有 ifconfig命令。现在怎么办?很简单,NodeOS 有一个集成的包管理器(类似 apt 或 yum) ,叫做 npkg,它是基于 Node 的 NPM 的,很容易使用。可以通过如下命令很方便的安装 ifconfig :

npkg install bin-ifconfig
Copy after login

如果一切正常, ifconfig 命令现在就可以在 shell 中使用了。我们再次试着执行一下,输出类似如下:(我替换了其中的 MAC 地址):

eth0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
  ether 01:23:45:67:89:ab 
  inet6 f0cd::ef01:0203:0405:181%en1 prefixlen 64 scopeid 0x5 
  inet 192.168.0.21 netmask 0xffffff00 broadcast 192.168.0.21
  nd6 options=1<PERFORMNUD>
  media: autoselect
  status: active
Copy after login

如果你的输出也类似如上,那说明它可以工作了。你已经成功地安装了你的第一个 NodeOS 应用: ifconfig。

它是可以工作了,然而我们可以用这个操作系统做什么呢?

如果我们只能拿这个用 Node.js 写的操作系统做到你在 Ubuntu 或其它 Linux 发行版上一样的(或更少的)事情,那它有什么价值?其实,整个事情中最有趣的地方是所有的东西都是 Node.js 开发的。这意味着我们可以只需要使用 Node.js 就可以开发我们的应用了。比如,NodeOS 里没有默认实现的 man 命令,它用于显示其它命令的帮助信息。不用担心,实现它很简单。

基于 JavaScript 的操作系统你听说过吗?

使用 Node.js 构建一个 NodeOS 应用

首先让我们来安装一个叫做 Hipster 的文本编辑器,以便我们可以创建和编辑文件。执行如下命令: npm install -g hipster@0.15.0。这个文本编辑器很简单,除了用作文本编辑之外啥也干不了,不过对于我们来说足够了。

用 Hipster 创建文件很简单,运行 hip filename即可,如: hip package.json。要保存文件请按下 Ctrl + s ,退出按下 Ctrl + q。

在这里,我们使用了一个 NodeOS 的主开发人员所开发的代码,我自己并没有真的去开发这个应用。我们例子中的原始代码可以在 node-bin-man Git 仓库中找到。

让我们回过头来创建我们的第一个 NodeOS 应用。像每个 Node.js 应用(或 NPM 包)一样,我们从创建一个 package.json 文件开始,内容如下:

{
  "name": "bin-man",
  "version": "0.0.1",
  "description": "Format and display manual pages",
  "bin": {
    "man": "man.js"
  },
  "repository": "https://github.com/groundwater/node-bin-man",
  "author": "groundwater",
  "license": "MIT",
  "dependencies": {
    "blessed": "~0.0.22"
  }
}
Copy after login

这些参数 name、version、 author、 repository、 license和 description 是其意自明的。这个 bin 集合是一个 JSON 的键值对对象,包含了命令名及其关联的 JavaScript 文件。在我们的例子中, man 命令关联到 man.js文件。而 dependencies集合包含了这个应用所需要的 NPM 包的列表。在我们的例子中,代码的作者包含了 Blessed 包,这是一个类 curses 的库,可以让 Node.js 支持高级终端界面的 API。

现在我们进入了主要的部分,实际的代码。

#!/usr/bin/env node
Copy after login

这个部分叫做释伴(shebang)。NodeOS 实际上并不需要它,但是它用于告诉操作系统如何执行下面的代码。在这里的意思是,它告诉系统下面的每行代码都需要通过 /usr/bin/env node命令来解释执行。

var fs = require(&#39;fs&#39;);
var blessed = require(&#39;blessed&#39;);
Copy after login

像在 Node.js 中一样, require() 函数加载选定的包到内存中,并将其保存为特定的变量。

var arg = process.argv[2] || &#39;bin-man&#39;;
Copy after login

man 命令的标准行为是如果没有指定要查看的命令时,就显示它自己的帮助信息。在我们的代码示例中也一样:如果没有给出第二个参数(第一个参数是 man 本身),那么该参数的默认值是 bin-man。

var path = process.env.HOME + "/lib/node_modules/" + arg + "/README.md";

try{
  var readme = fs.readFileSync(path, &#39;utf-8&#39;);
}catch(e){
  console.log(&#39;No README.md for Package &#39;,arg);
  process.exit(-1);
}
Copy after login

在这里,程序检查给定的应用是否有一个 readme 文件。在 NodeOS 中,每个应用的安装路径是其主目录(/)下的 lib/node_modules。如果 README.md 文件存在,就将其内容保存到 readme变量中。否则,显示一个错误信息并退出。

// Create a screen object.
var screen = blessed.screen();

var box = blessed.box({
  content: readme,
  alwaysScroll:true,
  scrollable: true,
});

// Append our box to the screen.
screen.append(box);
Copy after login

Blessed 有一个非常简单的 API,要显示一个文件的内容很容易,只需要创建一个 box ,然后载入内容即可。

screen.key([&#39;escape&#39;, &#39;q&#39;, &#39;C-c&#39;], function(ch, key) {
  return process.exit(0);
});
Copy after login

现在,让我们找个退出 man 应用的方法。我们组合了 escape、 q 或 emacs 风格的 C-c 来退出应用。

screen.key([&#39;space&#39;,&#39;f&#39;,&#39;j&#39;,&#39;n&#39;], function(ch, key) {
  box.scroll(box.height);
  screen.render();
});

screen.key([&#39;down&#39;], function(ch, key) {
  box.scroll(1);
  screen.render();
});

screen.key([&#39;up&#39;], function(ch, key) {
  box.scroll(-1);
  screen.render();
});

screen.key([&#39;b&#39;,&#39;k&#39;,&#39;p&#39;], function(ch, key) {
  box.scroll(-box.height);
  screen.render();
});
Copy after login

我们使用方向键来上滚和下滚,用 space、 f、 j 或 n 向下翻页,b、 k 或 p 向上翻页。

box.focus();
screen.render();
Copy after login

最后,我们让应用将输入焦点放到 box ,我们在这里创建和渲染所有内容。

把上面编辑的这个文件存放到 /lib/node_modules/bin-man 目录下(名字是 man.js),并加一个简单的 README.md ,类似如下:

# Man

Author: @groundwater

## Install

npkg install bin-man

## Usage

"
Usage: man PKGNAME

Display a packages README.md file
"
Copy after login

我们已经基本完成了我们的第一个 NodeOS 定制应用。最后剩下一小步了,我们需要创建一个 NodeOS 应用需要的配置文件。很简单,把它创建到 /etc/bin-man/config.json ,内容只是一个空的 JSON 对象: {}。

现在我们可以试试我们的新应用了。在 NodeOS 中运行 man ,它将展示我们之前创建的 readme 文件。

总结

如你所见,在 NodeOS 中实现任何东西都很简单,你只需要懂得 Node.js 即可。

NodeOS 很有潜力,我认为当实现了更多的功能之后它会成为一个伟大的操作系统。目前仍然需要很多工作,但是在整个Node.js 生态系统兴盛发展的形势下,万一哪天它很快地成为一个流行的操作系统也没什么好惊奇的。

你怎么看?发表你的评论让我们知道。

The above is the detailed content of Take you through a detailed explanation of JavaScript-based operating systems. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template