lxdで仮想マシンを簡単に立ち上げる

# lxdのインストール

[lxd公式サイト](https://linuxcontainers.org/ja/lxd/getting-started-cli/)を参考にしてlxdで仮想マシンを起動できるようにしていきます。

```sh
$ sudo snap install lxd
lxd 5.3-924be6a from Canonical✓ installed
$ sudo lxd --version
5.3
```

# 初期設定

ここではすべてデフォルトの設定にします。

```sh
$ sudo 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, dir, lvm, zfs, ceph) [default=zfs]: 
Create a new ZFS 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 GB of the new loop device (1GB minimum) [default=30GB]: 
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]: 
```

# lxcのイメージサーバーを表示する



```sh
$ lxc remote list
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
|      NAME       |                   URL                    |   PROTOCOL    |  AUTH TYPE  | PUBLIC | STATIC | GLOBAL |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
| 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     |
+-----------------+------------------------------------------+---------------+-------------+--------+--------+--------+
```

# lxcのイメージを検索する

```sh
lxc image list <remote image server> <search word>
```

```sh
$ lxc image list images: ubuntu amd64 jammy
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
|             ALIAS             | FINGERPRINT  | PUBLIC |             DESCRIPTION             | ARCHITECTURE |      TYPE       |   SIZE   |         UPLOAD DATE          |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
| ubuntu/jammy (7 more)         | 27cf23ac9849 | yes    | Ubuntu jammy amd64 (20220607_07:42) | x86_64       | VIRTUAL-MACHINE | 246.88MB | Jun 7, 2022 at 12:00am (UTC) |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
| ubuntu/jammy (7 more)         | 114ba63a4b29 | yes    | Ubuntu jammy amd64 (20220607_07:42) | x86_64       | CONTAINER       | 112.27MB | Jun 7, 2022 at 12:00am (UTC) |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
| ubuntu/jammy/cloud (3 more)   | 420a2ebc96fa | yes    | Ubuntu jammy amd64 (20220607_07:42) | x86_64       | VIRTUAL-MACHINE | 272.19MB | Jun 7, 2022 at 12:00am (UTC) |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
| ubuntu/jammy/cloud (3 more)   | c178ca6f9098 | yes    | Ubuntu jammy amd64 (20220607_07:42) | x86_64       | CONTAINER       | 129.03MB | Jun 7, 2022 at 12:00am (UTC) |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
| ubuntu/jammy/desktop (3 more) | a3042d3e54d9 | yes    | Ubuntu jammy amd64 (20220608_09:17) | x86_64       | VIRTUAL-MACHINE | 953.02MB | Jun 8, 2022 at 12:00am (UTC) |
+-------------------------------+--------------+--------+-------------------------------------+--------------+-----------------+----------+------------------------------+
```

# lxcコンテナを起動する



```sh
$ lxc launch images:ubuntu/jammy/desktop ubuntu2204-desktop-vm-00 --vm
Creating ubuntu2204-desktop-vm-00
Starting ubuntu2204-desktop-vm-00                     
$ lxc list
+--------------------------+---------+------------------------+-------------------------------------------------+-----------------+-----------+
|           NAME           |  STATE  |          IPV4          |                      IPV6                       |      TYPE       | SNAPSHOTS |
+--------------------------+---------+------------------------+-------------------------------------------------+-----------------+-----------+
| ubuntu2204-desktop-vm-00 | RUNNING | 10.86.163.111 (enp5s0) | fd42:f181:9c81:dde3:216:3eff:fe2f:a94a (enp5s0) | VIRTUAL-MACHINE | 0         |
+--------------------------+---------+------------------------+-------------------------------------------------+-----------------+-----------+
```

# 起動した仮想マシンにユーザを追加し、管理者権限を追加する。

```sh
$ lxc exec ubuntu2204-desktop-vm-00 -- adduser higma
$ lxc exec ubuntu2204-desktop-vm-00 -- usermod -aG sudo higma
```

# 作成したユーザでログインする

```sh
$ lxc exec ubuntu2204-desktop-vm-00 -- su --login higma
```

# GUIコンソールでログインする

```sh
$ lxc console ubuntu2204-desktop-vm-00 --type=vga
```