# Building the MoodleBox

* [MoodleBox 官網](https://moodlebox.net/en/)
* [MoodleBox Github](https://github.com/martignoni/moodlebox)
* [MoodleBox-Bpi Github](https://github.com/GXiangCo/moodlebox/tree/BananaPi)

MoodleBox一開始就是設定在Raspberry Pi 3上執行運作，讓使用者可以隨身帶著一個Raspberry Pi 3及行動電源就可以使用Moodle教學平台，MoodleBox官網有做動的img檔可供使用者下載，然後燒錄到Raspberry Pi 3，或者可以透過手動建置的方式，在剛開始官方所提供的是一步一步的指令去修改設定，接著又轉換成使用script自動安裝(github中切換到[Branch:https](https://github.com/GXiangCo/moodlebox/tree/https))，現在則改用Ansbile自動化組態管理把Moodle安裝到Raspberry Pi 3中。

在官方的Github中有以下10點的安裝步驟

To build a MoodleBox from scratch with this script, you need a Raspberri Pi 3 (Wi-Fi!) and follow these instructions.

1. Clone Rasbpian Stretch Lite on your microSD card.
2. Create a`ssh`file on the`boot`partition, e.g. using`touch ssh`
3. Insert the microSD card into your Raspberry
4. Connect your Raspberry to your Ethernet network and boot it.
5. [Install Ansible](http://docs.ansible.com/intro_installation.html) on your computer.
6. [Clone this repository](https://github.com/martignoni/moodlebox.git) to your local drive.
7. Create a`keys`directory in the repository folder and copy your public key into it, under the name`id_rsa.pub`.
8. Get the IP address of your RaspberryPi and change it in the`hosts.yml`file. Do not change anything else, unless you know what you're doing. You're on your own.
9. Run`ansible-playbook moodlebox.yml`from the repository folder.
10. Wait 30–50 minutes, depending on your SD card and Internet bandwidth. You're done.

因為目前是要使用MoodleBox安裝在BananaPi M64上，而官方是預設安裝於Raspberry Pi 3上所部分安裝內容會不同，以下我們依順說明

### 1. Clone Rasbpian Stretch Lite on your microSD card.

在Raspberry Pi 3的話就是直接到官方網站下載[Raspian Stretch Lite](https://www.raspberrypi.org/downloads/raspbian/)(<https://www.raspberrypi.org/downloads/raspbian/>)，這邊要改用Banana Pi M64所以要到BananaPi官方網站下載[BPI-M64](http://forum.banana-pi.org/c/BPI-M64/M63image)(<http://forum.banana-pi.org/c/BPI-M64/M63image>)，目前測試使用ubuntu作業系統，如下圖官網上列出最新的為「BPI-M64 new image:2017-08-14-ubuntu-16.04-mate-......」開頭的檔案，但使用後會有些問題，最後在Gitbook中找到<https://bananapi.gitbooks.io/bpi-m64/content/en/linuximageforbpi-m64.html>，有新的版本「[2018-01-05-ubuntu-16.04-mate-desktop-v1.0-bpi-m64-sd-emmc.img.zip](https://drive.google.com/file/d/1GJcUPzeHtK6THk7_3ke5uevDqc-NzRF3/view?usp=sharing)」，2018-01-05這個版本在燒錄好開機後就有ssh可以連線，下載完後Mac作業系統可以使用[Etcher](https://etcher.io)軟體燒錄到sd卡中，Windows作業系統可以使用[Win32DiskImager](https://sourceforge.net/projects/win32diskimager/)燒錄。

![](https://2618987405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdydZPliXiG2wnHDyCm%2F-Ldyd_Wjw5Jz5h3MZr8t%2F-LdydcpK-oc5XtWm5N5w%2Fbpi-download2.png?generation=1556903017137091\&alt=media)

### 2. Create a ssh file on thebootpartition, e.g. using touch ssh

此動作在Raspberry Pi 3，燒到sd卡後，要先執行指令「touch ssh」產生ssh檔案開機後會啟動ssh服務，而在BananaPi M64燒錄「[2018-01-05-ubuntu-16.04-mate-desktop-v1.0-bpi-m64-sd-emmc.img.zip](https://drive.google.com/file/d/1GJcUPzeHtK6THk7_3ke5uevDqc-NzRF3/view?usp=sharing)」此版本，開機就有啟動ssh服務，所以可以跳過此步驟。

### 3. Insert the microSD card into your Raspberry

IMG檔燒錄完成後，接著就是把microSD卡插入設備的microSD插槽，Raspberry Pi 3和Banana Pi M64這兩台設備的使用方式有點不用，Raspberry Pi 3直接把microSD卡推到最底就可以，取出的方式是直接拔出microSD，而Banana Pi M64的插槽有自動彈回的功能，所以在microSD卡推到最底時，還要再用力按壓一下，會自動卡住，而取出的時後，也是要再按壓一下後會自動彈回，再拔出microSD卡即可。

### 4. Connect your Raspberry to your Ethernet network and boot it

Raspberry Pi 3及Banana Pi M64都內建RJ-45網路介面，這個步驟只要把網路線線上，及插上電源，會自動開機。

### 5. [Install Ansible](http://docs.ansible.com/intro_installation.html) on your computer.

目前Ansible測試於Mac系統，請參考[Mac 安裝 Ansible](https://gxiangco.gitbook.io/moodlebox-on-banana-pi/chapter1) 的安裝步驟

### 6. [Clone this repository](https://github.com/martignoni/moodlebox.git) to your local drive.

在本機電腦中安裝好Ansible後，接著就是從GitHub上Clone Repository(復製儲存庫)，moodlebox官方的github網址「<https://github.com/martignoni/moodlebox>」，git的網址「<https://github.com/martignoni/moodlebox.git>」，如果安裝於Raspberry Pi 3請直接clone官方的設定檔即可，要注意的就是記得使用[RASPBIAN STRETCH LITE](https://www.raspberrypi.org/downloads/raspbian/)，因為Ansible自動化配置文件是依raspbian作業系統所撰寫的，要直接安裝於Banana Pi M64是不能使用的，所以這邊有另外依Banana Pi M64上的「[2018-01-05-ubuntu-16.04-mate-desktop-v1.0-bpi-m64-sd-emmc.img](https://drive.google.com/file/d/1GJcUPzeHtK6THk7_3ke5uevDqc-NzRF3/view?usp=sharing)」作業系統客製一個版本，目前也是放在github上，所以執行的方式是相同的，只是git網址要修改成「<https://github.com/GXiangCo/moodlebox-bpi.git>」

打開終端機，然後切換到要存放的路徑，輸入指令「git clone [https://github.com/GXiangCo/moodlebox-bpi.git」](https://github.com/GXiangCo/moodlebox-bpi.git%E3%80%8D)

```
allen-mba:github allen$ git clone https://github.com/GXiangCo/moodlebox-bpi.git
Cloning into 'moodlebox-bpi'...
remote: Counting objects: 113, done.
remote: Compressing objects: 100% (80/80), done.
remote: Total 113 (delta 1), reused 113 (delta 1), pack-reused 0
Receiving objects: 100% (113/113), 95.57 KiB | 207.00 KiB/s, done.
Resolving deltas: 100% (1/1), done.
```

切換路徑到moodlebox-bpi，檢查是否有檔案

```
allen-mba:github allen$ cd moodlebox-bpi/
allen-mba:moodlebox-bpi allen$ ls
CHANGELOG.md        README.md        hosts.yml        rootaccount.yml
CODE_OF_CONDUCT.md    ansible.cfg        library            setup.cfg
CONTRIBUTING.md        buildmoodlebox.yml    moodlebox.yml        util
LICENSE.md        default.config.yml    roles
allen-mba:moodlebox-bpi allen$
```

### 7. Create a`keys`directory in the repository folder and copy your public key into it, under the name`id_rsa.pub`

這個步驟主要讓Raspberry Pi 3或Banana Pi M64可以免輸入密碼登入，讓Ansible執行安裝過程中不用再輸入密碼，首先在moodlebox-bpi目錄中建立一個名稱為keys的資料夾

```
allen-mba:moodlebox-bpi allen$ pwd
/Users/allen/github/moodlebox-bpi
allen-mba:moodlebox-bpi allen$ mkdir keys
allen-mba:moodlebox-bpi allen$ ls
CHANGELOG.md        README.md        hosts.yml        roles
CODE_OF_CONDUCT.md    ansible.cfg        keys            rootaccount.yml
CONTRIBUTING.md        buildmoodlebox.yml    library            setup.cfg
LICENSE.md        default.config.yml    moodlebox.yml        util
allen-mba:moodlebox-bpi allen$
```

使用ssh-keygen指令產生公開金鑰，產生過程會問要儲存的路徑及密碼，都不設定，用預設值即可，注意一下預設存放的路徑，一般都是在使用者帳號下的「.ssh」資料夾中，ssh-keygen產生過程如下：

```
allen-mba:~ allen$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/allen/.ssh/id_rsa):
Created directory '/Users/allen/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/allen/.ssh/id_rsa.
Your public key has been saved in /Users/allen/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:nCpzaCPq6waI3KTpm0sq+Yfoxku/ENVcL2gKX2ZLJrE allen@allen-mba.local
The key's randomart image is:
+---[RSA 2048]----+
|   .   .         |
|    = o .        |
| . E @ . .       |
|  +.X ....       |
|+.=o .  S        |
|++.. . .         |
|+*..* o          |
|B*=o.=           |
|X@*+.            |
+----[SHA256]-----+
```

執行完ssh-keygen後，預設檔名為id\_rsa，會產生兩個檔案id\_rsa及id\_rsa.pub於「使用者目錄/.ssh」中，確認一下是否有產生所要的檔案，如下：

```
allen-mba:~ allen$ ls -al ~/.ssh/
total 16
drwx------   4 allen  staff   128  3  7 15:28 .
drwxr-xr-x+ 53 allen  staff  1696  3  7 15:27 ..
-rw-------   1 allen  staff  1675  3  7 15:28 id_rsa
-rw-r--r--   1 allen  staff   403  3  7 15:28 id_rsa.pub
```

idrsa為私鑰，這就如登入的密碼，此檔案是放在本機電腦中，而id\_rsa.pub為公鑰，此檔案要放在遠端的電腦中認證用，所以要把id\_rsa.pub檔案複製到所建立的「keys」資料夾中，在執行Ansible時，會把此檔案放到遠端的設備中，最後檢查keys資料夾中是否有id\_rsa.pub檔

```
allen-mba:~ allen$ cp ~/.ssh/id_rsa.pub ~/github/moodlebox-bpi/keys/
allen-mba:~ allen$ ls ~/github/moodlebox-bpi/keys/
id_rsa.pub
```

### 8. Get the IP address of your RaspberryPi and change it in the`hosts.yml`file. Do not change anything else, unless you know what you're doing. You're on your own.

接著要設定moodlebox專案中的「hosts.yml」檔，此檔案主要是設定要連線的設備ip及登入的帳號及密碼，原始內容如下：

```
all:
  hosts:
    moodlebox:
      ansible_host: '192.168.1.212'
      ansible_user: 'pi'
      ansible_password: 'raspberry'
```

目前moodlebox-bpi專案中的檔案內容如下，因為Banana Pi系統的預設密碼為「bananapi」，如同官方文件的說明，只要修改設備的ip，就是hosts.yml中的ansible\_host參數，其他如果不知道或沒必要請不要修改

```
all:
  hosts:
    moodlebox:
      ansible_host: '192.168.0.6'
      ansible_user: 'pi'
      ansible_password: 'bananapi'
```

如果Raspberry Pi 3及Banana Pi M64在沒有外接螢幕的狀況下要如何連線並且取得設備的ip呢？接下來就試範當設備燒錄好後，接上網路線並且開機後要如何連線取得設備ip，注意本機電腦和遠端的設備需要在同一個網段下。

Raspberry Pi 3

在終端機中執行指令「ssh <pi@raspberrypi.local>」，預設密碼為「raspberry」

```
allen-mba:~ allen$ ssh pi@raspberrypi.local
The authenticity of host 'raspberrypi.local (fe80::5f74:cb72:5324:c40b%en0)' can't be established.
ECDSA key fingerprint is SHA256:rvM07WHr9gcI+KOY1sr7+m3IpvakmgMRnpGt6tP2i8w.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'raspberrypi.local,fe80::5f74:cb72:5324:c40b%en0' (ECDSA) to the list of known hosts.
pi@raspberrypi.local's password:
Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Wed Mar  7 09:23:27 2018

SSH is enabled and the default password for the 'pi' user has not been changed.
This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.
```

使用「ifconfig」指令查詢

```
pi@raspberrypi:~ $ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.3  netmask 255.255.255.0  broadcast 192.168.0.255
        inet6 fe80::5f74:cb72:5324:c40b  prefixlen 64  scopeid 0x20<link>
        ether b8:27:eb:b6:d3:08  txqueuelen 1000  (Ethernet)
        RX packets 26122  bytes 3209257 (3.0 MiB)
        RX errors 0  dropped 64  overruns 0  frame 0
        TX packets 278  bytes 37615 (36.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wlan0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether b8:27:eb:e3:86:5d  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
```

使用「ip addr」指令查詢

```
pi@raspberrypi:~ $ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:b6:d3:08 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.3/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::5f74:cb72:5324:c40b/64 scope link
       valid_lft forever preferred_lft forever
3: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether b8:27:eb:e3:86:5d brd ff:ff:ff:ff:ff:ff
```

Banana Pi M64

在終端機中執行指令「ssh <pi@bpi-iot-ros-ai.local>」，預設密碼為「bananapi」

```
allen-mba:~ allen$ ssh pi@bpi-iot-ros-ai.local
The authenticity of host 'bpi-iot-ros-ai.local (fe80::84e0:eaff:fef2:36e2%en0)' can't be established.
ECDSA key fingerprint is SHA256:5sZdACIsvP5cxP3bob+z563JmKelc5liBZfAz89eGHM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'bpi-iot-ros-ai.local,fe80::84e0:eaff:fef2:36e2%en0' (ECDSA) to the list of known hosts.
pi@bpi-iot-ros-ai.local's password:
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 3.10.105-BPI-M64-Kernel aarch64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

116 packages can be updated.
0 updates are security updates.

*** System restart required ***
Last login: Sat Oct 21 16:21:23 2017 from 192.168.88.162
```

使用「ifconfig」指令查詢

```
pi@bpi-iot-ros-ai:~$ ifconfig
eth0      Link encap:Ethernet  HWaddr 86:e0:ea:f2:36:e2
          inet addr:192.168.0.6  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::84e0:eaff:fef2:36e2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:155140 errors:0 dropped:0 overruns:0 frame:0
          TX packets:73494 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:205165419 (205.1 MB)  TX bytes:5171402 (5.1 MB)
          Interrupt:114

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:343 errors:0 dropped:0 overruns:0 frame:0
          TX packets:343 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:30160 (30.1 KB)  TX bytes:30160 (30.1 KB)

wlan0     Link encap:Ethernet  HWaddr b0:f1:ec:0c:f0:66
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
```

使用「ip addr」指令查詢

```
pi@bpi-iot-ros-ai:~$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: dummy0: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN group default
    link/ether 9a:99:17:d9:e5:34 brd ff:ff:ff:ff:ff:ff
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 86:e0:ea:f2:36:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.6/24 brd 192.168.0.255 scope global dynamic eth0
       valid_lft 465sec preferred_lft 465sec
    inet6 fe80::84e0:eaff:fef2:36e2/64 scope link
       valid_lft forever preferred_lft forever
4: sit0: <NOARP> mtu 1480 qdisc noop state DOWN group default
    link/sit 0.0.0.0 brd 0.0.0.0
5: wlan0: <NO-CARRIER,BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state DORMANT group default qlen 1000
    link/ether b0:f1:ec:0c:f0:66 brd ff:ff:ff:ff:ff:ff
```

### 9. Run`ansible-playbook moodlebox.yml`from the repository folder.

將目錄切換到moodlebox專案目錄，然後執行以下的指令開始執行安裝作業，注意，Banana Pi M64需要在最後面加入「--ask-sudo-pass」

Raspberry Pi 3

```
ansible-playbook moodlebox.yml
```

Banana Pi M64

```
ansible-playbook moodlebox.yml --ask-sudo-pass
```

### 10. Wait 30–50 minutes, depending on your SD card and Internet bandwidth. You're done.

整個安裝過程大約30\~50分鐘，這個時間會因為使用的microSD卡讀寫的速度與網路頻寬的速度而有所不同。

* [Banana Pi M64使用Ansible安裝過程](https://gxiangco.gitbook.io/moodlebox-on-banana-pi/chapter3)
* [Raspberry Pi 3 使用Ansible安裝過程](https://gxiangco.gitbook.io/moodlebox-on-banana-pi/chapter4)

## 進入Moodle平台

以上十個步驟完成後，會自動重開機，此時就可以拿出行動裝置或有無線網卡的電腦進行連線，打開無線網路設定會看到一個SSID為「MoodleBox」的連接點，點選後預設密碼為「moodlebox」，連線完後打開瀏覽器，輸入以下的網址就可以進入moodle平台 ，預預管理者帳號：「admin」，密碼：「Moodlebox4$」

* Raspberry Pi 3預設網址： <http://moodlebox.home>
* Banana Pi M64網址：<http://moodlebox.bpi>

## SSH進入Moodlebox

如果要ssh到安裝好的MoodleBox時，連線的網址會不同，被修改成moodlebox開頭，另外在未連MoodleBox的wifi時，都要用\[「moodlebox.local」，連wifi後的網址後面原本在Raspberry Pi 3原本是用「.home」而Banana PI M64修改成「.bpi」，連線網址及帳號密碼，請參照下方的對照表

### 連線網址對照表

| 設備連線網址         | 官方預設值                | 安裝MoodleBox     | 連moodlebox wifi後 |
| -------------- | -------------------- | --------------- | ---------------- |
| Raspberry Pi 3 | raspberrypi.local    | moodlebox.local | moodlebox.home   |
| Banana Pi M64  | bpi-iot-ros-ai.local | moodlebox.local | moodlebox.bpi    |

### 連線的帳號密碼對照表

| 設備連線帳號 / 密碼    | 官方預設值            | 安裝MoodleBox               |
| -------------- | ---------------- | ------------------------- |
| Raspberry Pi 3 | pi  /  raspberry | moodlebox  /  Moodlebox4$ |
| Banana Pi M64  | pi  /  bananapi  | moodlebox  /  Moodlebox4$ |

連線後的畫面會多一個樹梅派的logo，正常連線後會看到如下圖的畫面

![](https://2618987405-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LdydZPliXiG2wnHDyCm%2F-Ldyd_Wjw5Jz5h3MZr8t%2F-LdydcpUhY8iuziyUWjU%2Fssh_moodlebox_local.png?generation=1556903017054783\&alt=media)
