LXC（LXD）コマンド

# 0. 環境

|||
|:----|:----|
|端末|HP Elite c1030 Chromebook|
|ChomeOS|バージョン: 126.0.6478.222（Official Build） （64 ビット）|

# 1. `lxc version`：バージョンを表示
```shell_session
$ lxc version
Client version: 5.0.2
Server version: 5.0.2
```

# 2. `lxc list`：コンテナリスト表示
```shell_session
$ lxc list
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
|     NAME      |  STATE  |          IPV4          |                     IPV6                      |   TYPE    | SNAPSHOTS |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| a-web1        | STOPPED |                        |                                               | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| almalinux8    | STOPPED |                        |                                               | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| b-web1        | STOPPED |                        |                                               | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| c-web1        | STOPPED |                        |                                               | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| d-web1        | STOPPED |                        |                                               | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| penguin       | RUNNING | 192.168.39.1 (virbr1)  | 2400:2412:1cc4:c200:216:3eff:fe20:c0c4 (eth0) | CONTAINER | 0         |
|               |         | 192.168.122.1 (virbr0) |                                               |           |           |
|               |         | 172.17.0.1 (docker0)   |                                               |           |           |
|               |         | 100.115.92.205 (eth0)  |                                               |           |           |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| postgresql15  | RUNNING | 100.115.92.203 (eth0)  | 2400:2412:1cc4:c200:216:3eff:fe42:f2d9 (eth0) | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| vscode-server | RUNNING | 100.115.92.254 (eth0)  | 2400:2412:1cc4:c200:216:3eff:feb0:ee64 (eth0) | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
| wikijs        | RUNNING | 100.115.92.197 (eth0)  | 2400:2412:1cc4:c200:216:3eff:fea3:d21f (eth0) | CONTAINER | 0         |
+---------------+---------+------------------------+-----------------------------------------------+-----------+-----------+
```

# 3. `lxc remote list`：リモートサーバの表示
```shell_session
$ lxc remote list
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
|      NAME       |                   URL                    |   PROTOCOL    |  AUTH TYPE  | PUBLIC | STATIC | GLOBAL |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| canonical       | https://images.lxd.canonical.com         | simplestreams | none        | YES    | NO     | NO     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| images          | https://images.linuxcontainers.org       | simplestreams | none        | YES    | NO     | NO     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| local (current) | unix://                                  | lxd           | file access | NO     | YES    | NO     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| ubuntu          | https://cloud-images.ubuntu.com/releases | simplestreams | none        | YES    | YES    | NO     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| ubuntu-daily    | https://cloud-images.ubuntu.com/daily    | simplestreams | none        | YES    | YES    | NO     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
```

# 4. `lxc remote add <remote_name> <URL> [flags]`：LXDリモートサーバの追加
```shell_session
$ lxc remote add canonical https://images.lxd.canonical.com --protocol=simplestreams
Error: Remote canonical exists as <https://images.lxd.canonical.com>
```

# 5. `lxc image list [<remote>:] [<filter>...] [flags]`：LXCイメージの一覧
```shell_session
$ lxc image list canonical:almalinux/8/amd64
+----------------------+--------------+--------+-----------------------------------+--------------+-----------------+----------+------------------------------+
|        ALIAS         | FINGERPRINT  | PUBLIC |            DESCRIPTION            | ARCHITECTURE |      TYPE       |   SIZE   |         UPLOAD DATE          |
+----------------------+--------------+--------+-----------------------------------+--------------+-----------------+----------+------------------------------+
| almalinux/8 (3 more) | 1efc662ea421 | yes    | AlmaLinux 8 amd64 (20240808_0011) | x86_64       | CONTAINER       | 128.91MB | Aug 8, 2024 at 12:00am (UTC) |
+----------------------+--------------+--------+-----------------------------------+--------------+-----------------+----------+------------------------------+
| almalinux/8 (3 more) | 93290a46f387 | yes    | AlmaLinux 8 amd64 (20240808_0011) | x86_64       | VIRTUAL-MACHINE | 850.76MB | Aug 8, 2024 at 12:00am (UTC) |
+----------------------+--------------+--------+-----------------------------------+--------------+-----------------+----------+------------------------------+
```

# 6. `lxc launch [<remote>:]<image> [<name>]`：新規インスタンスの作成と起動
```shell_session
$ lxc launch canonical:almalinux/8/amd64 almalinux8-2
Creating almalinux8-2
Starting almalinux8-2
```

※`TYPE:VIRTUAL-MACHINE`をインストールしたい場合は以下
```shell_session
$ lxc launch canonical:ubuntu/noble/desktop ubuntu2404 --vm
```

# 7. `lxc exec <name> bash`：インスタンスに入る
※「bash」部分を他のコマンドに変えるとコンテナに入らずにコマンドだけ実行可能

```shell_session
$ lxc exec almalinux8 bash
[root@almalinux8 ~]# 
```

# 8. `lxc start [<name>]`：インスタンスの起動
```shell_session
$ lxc start almalinux8-2
```

# 9. `lxc start [<name>]`：インスタンスの停止
```shell_session
$ lxc stop almalinux8-2
```

# 10. `lxc delete [<name>]`：インスタンスの削除
```shell_session
$ lxc delete almalinux8-2
Error: The instance is currently running, stop it first or pass --force
$ lxc delete almalinux8-2 -f
```

# 11. `lxc network list`：LXCネットワークの一覧表示
```shell_session
$ lxc network list
+--------+----------+---------+-------------------+------+-------------+---------+---------+
|  NAME  |   TYPE   | MANAGED |       IPV4        | IPV6 | DESCRIPTION | USED BY |  STATE  |
+--------+----------+---------+-------------------+------+-------------+---------+---------+
| eth0   | physical | NO      |                   |      |             | 0       |         |
+--------+----------+---------+-------------------+------+-------------+---------+---------+
| lxdbr0 | bridge   | YES     | 100.115.92.193/26 | none |             | 10      | CREATED |
+--------+----------+---------+-------------------+------+-------------+---------+---------+
```

# 12. `lxc network show [<name>]`:対象のLXCネットワーク設定表示
```shell_session
$ lxc network show lxdbr0
config:
  ipv4.address: 100.115.92.193/26
  ipv4.dhcp.expiry: infinite
  ipv4.dhcp.ranges: 100.115.92.192-100.115.92.255
  ipv6.address: none
  raw.dnsmasq: |-
    resolv-file=/run/resolv.conf
    dhcp-authoritative
    no-ping
    addn-hosts=/etc/arc_host.conf
description: ""
name: lxdbr0
type: bridge
used_by:
- /1.0/instances/a-web1
- /1.0/instances/almalinux8
- /1.0/instances/b-web1
- /1.0/instances/c-web1
- /1.0/instances/d-web1
- /1.0/instances/penguin
- /1.0/instances/postgresql15
- /1.0/instances/vscode-server
- /1.0/instances/wikijs
- /1.0/profiles/default
managed: true
status: Created
locations:
- none
```

# 13. `lxc network set <name> ipv4.address=<IP Address>/<CIDR> ipv4.dhcp.ranges=<IP Range start>-<IP Range end>`：ネットワークアドレス範囲の変更
※起動中コンテナがある場合、再起動で変更適用
```shell_session
$ lxc network set lxdbr0 ipv4.address=100.115.92.193/26 ipv4.dhcp.ranges=100.115.92.192-100.115.92.255
```

# 14. `lxc publish <name> --alias <alias>`：イメージの取得
```shell_session
$ lxc publish penguin --alias penguin.20240812 --force
Instance published with fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

# 15. `lxc image export <alias> <path>`：イメージのエスクスポート
※実行後は
`mv $LXD_CONF/penguin.20240812.tar.gz /mnt/shared/removable/sd_card/Backup`
などして好きな場所に保存できる。
```shell_session
$ lxc image export penguin.20240812 $LXD_CONF/penguin.20240812
Image exported successfully!    
```

# 16. `lxc image import <path> --alias <alias>`：イメージのインポート
```shell_session
$ lxc image import /mnt/shared/removable/sd_card/Backup/penguin.20240812.tar.gz --alias penguin.20240812
Image imported with fingerprint: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

# 17. `lxc init <alias> <name>`：イメージからインスタンスを作成
```
$ lxc init penguin.20240812 penguin
Creating penguin
```

# 参考リンク
* [Canonical LXD](https://documentation.ubuntu.com/lxd/en/latest/)
* [Crostini LXC コンテナバックアップとリストア ~ 不安定な Dev Channel でもくじけない \#Chromebook \- Qiita](https://qiita.com/weakboson/items/77b34704556178cf6f09)

# 備考
おおよそ備忘録。