wslでnerves その15

# 概要
wsl(wsl2じゃない)でnervesやってみる。
qemu(x86_64エミュレータ、ラズパイじゃない)でやってみた。
練習問題やってみた。

# 練習問題
ファイルシステムを使って、プログラムを永続化せよ。

# 方針
/dataにプログラムをセーブする。
Code.eval_fileで、ロードする。


# 実行結果

```
$ ssh -p 10022 localhost
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
████▄▄    ▐███
█▌  ▀▀██▄▄  ▐█
█▌  ▄▄  ▀▀  ▐█   N  E  R  V  E  S
█▌  ▀▀██▄▄  ▐█
███▌    ▀▀████
hello_nerves 0.1.0 (7b5b34a4-8bc2-5144-5f98-58be2e2464a2) x86_64 x86_64
  Uptime       : 1 minutes and 52 seconds
  Clock        : 2023-01-19 23:18:53 UTC

  Firmware     : Valid (A)               Applications : 38 started
  Memory usage : 57 MB (50%)             Part usage   : 0 MB (0%)
  Hostname     : nerves-3456             Load average : 1.38 0.47 0.17

  eth0         : 10.0.2.15/24, fec0::5054:ff:fe12:3456/64, fe80::5054:ff:fe12:3456/64

Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html

Toolshed imported. Run h(Toolshed) for more info.
iex(1)> ls "/"
bin       boot      data      dev       etc       lib       lib64     media
mnt       opt       proc      root      run       sbin      share     srv
sys       tmp       usr       var
iex(2)> ls "-la /"
No such file or directory -la /
iex(3)> ls "/ -la"
No such file or directory / -la
iex(4)> cmd("ls -la /")
drwxr-xr-x    4 root     root           120 Dec 18 19:56 var
drwxr-xr-x    6 root     root            75 Dec 18 19:56 usr
drwxrwxrwt    2 root     root           100 Jan 19 23:18 tmp
dr-xr-xr-x   12 root     root             0 Jan 19 23:16 sys
drwxr-xr-x    3 root     root            29 Jan 18 07:20 srv
drwxr-xr-x    5 root     root            65 Dec 18 19:56 share
drwxr-xr-x    2 root     root           967 Dec 18 19:56 sbin
drwxr-xr-x    2 root     root            60 Jan 19 23:18 run
drwxr-xr-x    5 root     root          1024 Jan 18 07:42 root
dr-xr-xr-x   94 root     root             0 Jan 19 23:16 proc
drwxr-xr-x    2 root     root             3 Dec 18 19:56 opt
drwxr-xr-x    2 root     root             3 Dec 18 19:56 mnt
drwxr-xr-x    2 root     root             3 Dec 18 19:56 media
lrwxrwxrwx    1 root     root             3 Dec 18 19:56 lib64 -> lib
drwxr-xr-x    4 root     root           475 Dec 18 19:56 lib
drwxr-xr-x    6 root     root           386 Jan 18 07:21 etc
drwxr-xr-x    6 root     root          2460 Jan 19 23:18 dev
lrwxrwxrwx    1 root     root             4 Dec 18 19:56 data -> root
drwxr-xr-x    3 root     root            42 Dec 18 19:56 boot
drwxr-xr-x    2 root     root           686 Dec 18 19:56 bin
drwxr-xr-x   20 1000     1000           270 Jan 18 07:20 ..
drwxr-xr-x   20 1000     1000           270 Jan 18 07:20 .
0
iex(5)> ls "/data"
.cache           .nerves_time     lost+found       nerves_ssh
iex(6)> cmd("ls -la /data")
lrwxrwxrwx    1 root     root             4 Dec 18 19:56 /data -> root
0
iex(7)> ls -la "/data"
** (CompileError) iex:7: undefined function la/1 (there is no such import)

iex(7)> cmd("ls /data")
lost+found
nerves_ssh
0
iex(8)> cmd("ls /data -la")
lrwxrwxrwx    1 root     root             4 Dec 18 19:56 /data -> root
0
iex(9)> ls "-la" "/data"
** (SyntaxError) iex:9:10: syntax error before: "/data"

iex(9)> ls "/data" "-la"
** (SyntaxError) iex:9:12: syntax error before: "-la"

iex(9)> cmd("ls /root -la")
drwxr-xr-x    5 root     root          1024 Jan 18 07:42 .
drwxr-xr-x    3 root     root          1024 Jan 18 07:42 .cache
-rw-r--r--    1 root     root             0 Jan 19 23:18 .nerves_time
drwx------    2 root     root         12288 Jan 18 07:26 lost+found
drwxr-xr-x   20 1000     1000           270 Jan 18 07:20 ..
drwxr-xr-x    3 root     root          1024 Jan 18 07:26 nerves_ssh
0
iex(10)> s = "defmodule M do\n def add(a, b) do\n a + b\n end\n end"
"defmodule M do\n def add(a, b) do\n a + b\n end\n end"
iex(11)> :ok = File.write("/data/M.exs", s)
:ok
iex(12)> ls "/data"
.cache           .nerves_time     M.exs            lost+found       nerves_ssh
iex(13)> {:ok, code} = File.read("/data/M.exs")
{:ok, "defmodule M do\n def add(a, b) do\n a + b\n end\n end"}
iex(14)> Code.eval_string(code)
{{:module, M,
  <<70, 79, 82, 49, 0, 0, 5, 56, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 154,
    0, 0, 0, 16, 8, 69, 108, 105, 120, 105, 114, 46, 77, 8, 95, 95, 105, 110,
    102, 111, 95, 95, 10, 97, 116, 116, ...>>, {:add, 2}}, []}
iex(15)> M.add(5, 4)
9
iex(16)> exit
Connection to localhost closed.

```

```
$ ssh -p 10022 localhost
Interactive Elixir (1.13.4) - press Ctrl+C to exit (type h() ENTER for help)
████▄▄    ▐███
█▌  ▀▀██▄▄  ▐█
█▌  ▄▄  ▀▀  ▐█   N  E  R  V  E  S
█▌  ▀▀██▄▄  ▐█
███▌    ▀▀████
hello_nerves 0.1.0 (7b5b34a4-8bc2-5144-5f98-58be2e2464a2) x86_64 x86_64
  Uptime       : 12 minutes and 46 seconds
  Clock        : 2023-01-19 23:46:21 UTC

  Firmware     : Valid (A)               Applications : 38 started
  Memory usage : 57 MB (50%)             Part usage   : 0 MB (0%)
  Hostname     : nerves-3456             Load average : 0.00 0.27 0.28

  eth0         : 10.0.2.15/24, fec0::5054:ff:fe12:3456/64, fe80::5054:ff:fe12:3456/64

Nerves CLI help: https://hexdocs.pm/nerves/iex-with-nerves.html

Toolshed imported. Run h(Toolshed) for more info.
iex(1)> ls "/data"
.cache           .nerves_time     M.exs            lost+found       nerves_ssh
iex(2)> Code.eval_file("/data/M.exs")
{{:module, M,
  <<70, 79, 82, 49, 0, 0, 5, 64, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 154,
    0, 0, 0, 16, 8, 69, 108, 105, 120, 105, 114, 46, 77, 8, 95, 95, 105, 110,
    102, 111, 95, 95, 10, 97, 116, 116, ...>>, {:add, 2}}, []}
iex(3)> M.add(5, 4)
9
iex(4)> exit
Connection to localhost closed.



```

以上。