WSL2でLXCしてVirtualBox的な仮想環境として使えるようにする

# 0. こんな人向け
1. GUIは必要としない
2. VirtualBoxで簡単なお試し環境をよく作って終わったら破棄する
3. WSL2の環境をあまり汚したくない
4. Dockerよりお手軽に使いたい

# 1. WSL2導入
公式の手順で導入
参考：[WSL のインストール \| Microsoft Learn](https://learn.microsoft.com/ja-jp/windows/wsl/install)

# 2. LXCインストール
```shell_session
# apt install -y lxd-installer
```

# 3. LXC設定
対話型で聞かれるのでお好みで設定を変える。
特になければEnter連打でよい。

```shell_session
# lxd init
Would you like to use LXD clustering? (yes/no) [default=no]:
Do you want to configure a new storage pool? (yes/no) [default=yes]:
Name of the new storage pool [default=default]:
Name of the storage backend to use (btrfs, ceph, dir, lvm, powerflex) [default=btrfs]:
Create a new BTRFS pool? (yes/no) [default=yes]:
Would you like to use an existing empty block device (e.g. a disk or partition)? (yes/no) [default=no]:
Size in GiB of the new loop device (1GiB minimum) [default=30GiB]: 100GiB
Would you like to connect to a MAAS server? (yes/no) [default=no]:
Would you like to create a new local network bridge? (yes/no) [default=yes]:
What should the new bridge be called? [default=lxdbr0]:
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]:
Would you like the LXD server to be available over the network? (yes/no) [default=no]:
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]:
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes
config: {}
networks:
- config:
    ipv4.address: auto
    ipv6.address: auto
  description: ""
  name: lxdbr0
  type: ""
  project: default
storage_pools:
- config:
    size: 100GiB
  description: ""
  name: default
  driver: btrfs
storage_volumes: []
profiles:
- config: {}
  description: ""
  devices:
    eth0:
      name: eth0
      network: lxdbr0
      type: nic
    root:
      path: /
      pool: default
      type: disk
  name: default
projects: []
cluster: null
```

# 4. リモート追加・起動

```shell_session
# lxc remote add canonical https://images.lxd.canonical.com --protocol=simplestreams
# lxc launch canonical:almalinux/8/amd64 almalinux8
```

# 5. コンテナに入る

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

# 6. コンテナにssh

```shell_session
# dnf install -y openssh-server
# systemctl enable --now sshd
# passwd
Changing password for user root.
New password:root
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:root
passwd: all authentication tokens updated successfully.
# exit
# lxc list
+------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|    NAME    |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| almalinux8 | RUNNING | 10.209.185.16 (eth0) | fd42:6162:6d45:da81:216:3eff:feaf:f31a (eth0) | CONTAINER | 0         |
+------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
# ssh root@10.209.185.16
The authenticity of host '10.209.185.16 (10.209.185.16)' can't be established.
ED25519 key fingerprint is SHA256:.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '10.209.185.16' (ED25519) to the list of known hosts.
root@10.209.185.16's password:
# cat /etc/os-release
NAME="AlmaLinux"
VERSION="8.10 (Cerulean Leopard)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="8.10"
PLATFORM_ID="platform:el8"
PRETTY_NAME="AlmaLinux 8.10 (Cerulean Leopard)"
ANSI_COLOR="0;34"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:almalinux:almalinux:8::baseos"
HOME_URL="https://almalinux.org/"
DOCUMENTATION_URL="https://wiki.almalinux.org/"
BUG_REPORT_URL="https://bugs.almalinux.org/"

ALMALINUX_MANTISBT_PROJECT="AlmaLinux-8"
ALMALINUX_MANTISBT_PROJECT_VERSION="8.10"
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.10"
SUPPORT_END=2029-06-01
# exit
```

# 7. 感想
GUIをポチポチしなくてよいし、ホストのWSL2環境を汚さないで色々試せる。とにかくお手軽で素早くフルOS環境が手に入るので、お試し環境としてとても使いやすい。デフォルトの設定だと特に何もせずともコンテナ同士の通信もできるため、なにかのクラスタを作る際のお試し環境としても重宝する。