LXDコンテナをまとめて操作するスクリプト

## 目的
LXDコンテナを用いて、環境を配布したい時は複数のコンテナを立ち上げる必要がある。しかし、そのコンテナを立ち上げるのにいちいち手作業でやっていたら日が暮れてしまう。今回はその様な場合に使えるスクリプトを作ってみる

## スクリプト
### まとめてコピー

```sh
#!/bin/bash
for i in `seq 0 $3`
do
    lxc copy $1 $2-$i;
    lxc start $2-$i;
done
lxc list
```


#### 使い方
コピーする元のコンテナがある
```bash
root@shoma:/home/shoma# lxc list
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|    NAME     |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-test | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe2a:52a7 (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.50 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
```

コピー元、コピー先、個数を入力
```bash
$ ./lxdcopy.sh ubuntu-test ubuntu 10
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|    NAME     |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-0    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fea5:d00c (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.88 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-1    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe84:2caf (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.180 (eth0) |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-2    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:feb7:c37f (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.41 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-3    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fed1:1b6a (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.174 (eth0) |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-4    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe45:27e (eth0)  | CONTAINER | 1         |
|             |         | 10.107.73.217 (eth0) |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-5    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe68:feab (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.178 (eth0) |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-6    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fec3:b75b (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.164 (eth0) |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-7    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe61:983d (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.13 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-8    | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe77:3ca7 (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.7 (eth0)   |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-9    | RUNNING | 10.107.73.150 (eth0) | fd42:d70a:2761:a81b:216:3eff:fe84:3b4c (eth0) | CONTAINER | 1         |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-10   | RUNNING |                      | fd42:d70a:2761:a81b:216:3eff:fee4:5165 (eth0) | CONTAINER | 1         |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-test | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe2a:52a7 (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.50 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
```

### まとめて削除

```shell
#!/bin/bash
for i in `seq 0 $2`
do
    lxc stop $1-$i;
    lxc delete $1-$i;
done
lxc list
```

#### 使い方
消したいコンテナ名、個数を入力

```bash
$ ./lxddelete.sh ubuntu 10
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
|    NAME     |  STATE  |         IPV4         |                     IPV6                      |   TYPE    | SNAPSHOTS |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
| ubuntu-test | RUNNING | 172.17.0.1 (docker0) | fd42:d70a:2761:a81b:216:3eff:fe2a:52a7 (eth0) | CONTAINER | 1         |
|             |         | 10.107.73.50 (eth0)  |                                               |           |           |
+-------------+---------+----------------------+-----------------------------------------------+-----------+-----------+
```