# 在 OpenWRT 上布建 Container (LXC)

Container 是一種虛擬運算的技術，和 Virtual Machine (虛擬機) 的技術不同，Container 只進行部分的虛擬化 (例如: 使用者權限、硬碟空間、記憶體區間，等)，並且，所有的 Container 都使用相同的作業系統核心，以減少虛擬化的負擔。Container 技術中，最廣為人知的應該是 docker 的技術，我們可以以下圖來說明 Container 和 VM 的差別:

![來自: https://www.docker.com/resources/what-container](/files/-LaVip6U23IMtWX0qeEY)

圖中，我們可以看到，所有的 Container 都共用相同的作業系統，而 docker 則用以見控並排程系統資源予上層的 Container (App) 來使用，同理，一個 Container 就像是作業系統上的一支程式般，可以輕量的在 Linux 系統上執行。考慮到 Container 輕量的特性，因此，也可以在 OpenWRT 這種嵌入式的 Linux 系統上使用，而在 OpenWRT 上已有實作的 Container 套件稱為 LXC (LinuX Container)。

首先，我們先透過 opkg 安裝 lxc-checkconfig，此套件能夠檢查此裝置是否可以支援 lxc 的相關功能， opkg 將自動抓取相依的套件，包含: lxc、lxc-common。

```
# opkg install  lxc-checkconfig
Installing lxc-checkconfig (2.1.1-1) to root...
Downloading http://download.gl-inet.com/releases/packages-3.x/ar71xx/packages/lxc-checkconfig_2.1.1-1_mips_24kc.ipk
Installing lxc (2.1.1-1) to root...
Downloading http://download.gl-inet.com/releases/packages-3.x/ar71xx/packages/lxc_2.1.1-1_mips_24kc.ipk
Installing lxc-common (2.1.1-1) to root...
Downloading http://download.gl-inet.com/releases/packages-3.x/ar71xx/packages/lxc-common_2.1.1-1_mips_24kc.ipk
Configuring lxc.
Configuring lxc-common.
Configuring lxc-checkconfig.
```

之後透過指令進行檢查:

```
# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
lxc-checkconfig: unable to retrieve kernel configuration

Try recompiling with IKCONFIG_PROC, installing the kernel headers,
or specifying the kernel configuration path with:
  CONFIG=<path> lxc-checkconfig
```

結果顯示此 OpenWRT 在編譯時，並沒有將 IKCONFIG\_PROC 的功能開啟，因此，不支援 LXC 的功能。要解決此問題的方法必須重新編譯 OpenWRT 的 kernel，這部分的設定可以參考第二篇[參考資料](http://www.gnuton.org/blog/2016/02/lxc-on-openwrt/)。考慮到這可能要重刷目前所使用的系統，因此先擱置，以後若有機會會更新這一部分的進度。

順便補充說明一下，目前已經有人實作出 LXC 在 OpenWRT 上的執行環境，並成立一個專屬的開發平台稱為: [Turris Omnia](https://omnia.turris.cz/en/)，此實驗平台也是建立在 OpenWRT 之上，並提供和 LuCi (OpenWRT 的網頁介面) 整合的 LXC 運算服務，若有興趣在 WiFi AP 上玩 LXC 的功能，可以參考。

參考資料:

* <https://openwrt.org/packages/pkgdata/lxc>
* <http://www.gnuton.org/blog/2016/02/lxc-on-openwrt/>
* <https://omnia.turris.cz/en/>
* <https://pellin.be/2018/03/10/lxc-ubuntu-container/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openwrt-nctu.gitbook.io/project/openwrt-functions/openwrt-lxc.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
