Raspbian stretch(stable) のLXC用テンプレートを作ってみた件


# やりたいこと
RaspberryPi3 Medel B にLXC/LXDでコンテナ環境を構築し、コンテナのubuntu16.04からRaspberryPiのI2Cインターフェースに接続しようとしたらできなかったので、raspbianのイメージからコンテナを作ればいいんじゃね？ということで作ってみた。

結果、raspbianのイメージを作ってコンテナを作ることは出来たものの、RaspberryPiのI2Cインターフェースには接続出来ませんでした。。。

# 環境
- RaspberryPi3 Medel B
- raspbian stretch lite 2017-09-07

# LXC用のテンプレートを作る

## debootstrap をインストール
```shell-session
pi@raspberrypi:~ $ 
pi@raspberrypi:~ $ sudo apt-get install debootstrap
pi@raspberrypi:~ $
```

## rootユーザーでテンプレート作成
リポジトリは https://www.raspbian.org/RaspbianMirrors から日本国内のサーバーを指定する
- http://ftp.jaist.ac.jp/raspbian 
- http://ftp.tsukuba.wide.ad.jp/Linux/raspbian/raspbian/ 
- http://ftp.yz.yamagata-u.ac.jp/pub/linux/raspbian/raspbian/


```shell-session
pi@raspberrypi:~ $
pi@raspberrypi:~ $ sudo su -
root@raspberrypi:~#
root@raspberrypi:~# cd /var/tmp/
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# mkdir /var/tmp/raspbian-stretch
root@raspberrypi:/var/tmp# 
root@raspberrypi:/var/tmp# debootstrap --arch armhf stable /var/tmp/raspbian-stretch/ http://ftp.jaist.ac.jp/raspbian/
W: Cannot check Release signature; keyring file not available /usr/share/keyrings/debian-archive-keyring.gpg
I: Retrieving InRelease
I: Retrieving Packages
I: Validating Packages
I: Resolving dependencies of required packages...
　：
　：
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF /var/tmp/raspbian-stretch/
合計 80
drwxr-xr-x 20 root root 4096 10月 10 23:40 ./
drwxrwxrwt  3 root root 4096 10月 10 23:25 ../
drwxr-xr-x  2 root root 4096 10月 10 23:39 bin/
drwxr-xr-x  2 root root 4096  7月 28 07:17 boot/
drwxr-xr-x  4 root root 4096 10月 10 23:35 dev/
drwxr-xr-x 53 root root 4096 10月 10 23:40 etc/
drwxr-xr-x  2 root root 4096  7月 28 07:17 home/
drwxr-xr-x 10 root root 4096 10月 10 23:38 lib/
drwxr-xr-x  2 root root 4096 10月 10 23:33 media/
drwxr-xr-x  2 root root 4096 10月 10 23:33 mnt/
drwxr-xr-x  2 root root 4096 10月 10 23:33 opt/
drwxr-xr-x  2 root root 4096  7月 28 07:17 proc/
drwx------  2 root root 4096 10月 10 23:33 root/
drwxr-xr-x  4 root root 4096 10月 10 23:33 run/
drwxr-xr-x  2 root root 4096 10月 10 23:40 sbin/
drwxr-xr-x  2 root root 4096 10月 10 23:33 srv/
drwxr-xr-x  2 root root 4096  7月 28 07:17 sys/
drwxrwxrwt  2 root root 4096 10月 10 23:40 tmp/
drwxr-xr-x 10 root root 4096 10月 10 23:33 usr/
drwxr-xr-x 11 root root 4096 10月 10 23:33 var/
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# mksquashfs /var/tmp/raspbian-stretch raspbian-stretch.squashfs -comp xz
Parallel mksquashfs: Using 4 processors
Creating 4.0 filesystem on raspbian-stretch.squashfs, block size 131072.
　：
　：
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF
合計 128912
drwxrwxrwt  3 root root      4096 10月 10 23:41 ./
drwxr-xr-x 12 root root      4096 10月  4 09:34 ../
drwxr-xr-x 20 root root      4096 10月 10 23:40 raspbian-stretch/
-rw-r--r--  1 root root 131993600 10月 10 23:43 raspbian-stretch.squashfs
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
```

## metadata.yaml と必要なテンプレートを作ってtar.gzでアーカイブする

```shell-session
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# mkdir templates
root@raspberrypi:/var/tmp# cat << EOF > templates/cloud-init-meta.tpl 
#cloud-config
instance-id: {{ container.name }}
local-hostname: {{ container.name }}
{{ config_get("user.meta-data", "") }}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# cat << EOF > templates/cloud-init-network.tpl
{% if config_get("user.network-config", "") == "" %}version: 1
config:
    - type: physical
      name: eth0
      subnets:
          - type: {% if config_get("user.network_mode", "") == "link-local" %}manual{% else %}dhcp{% endif %}
            control: auto{% else %}{{ config_get("user.network-config", "") }}{% endif %}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# cat << EOF > templates/cloud-init-user.tpl
{{ config_get("user.user-data", properties.default) }}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# cat << EOF > templates/cloud-init-vendor.tpl
{{ config_get("user.vendor-data", properties.default) }}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# cat << EOF > templates/hostname.tpl
{{ container.name }}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# cat << EOF > metadata.yaml
architecture: "armv7l"
creation_date: 1505837412
properties:
    architecture: "armv7l"
    description: "Raspbian Stretch (2017-09-07)"
    os: "raspbian"
    release: "stretch"
templates:
    /etc/hostname:
        when:
            - create
            - copy
        template: hostname.tpl
    /var/lib/cloud/seed/nocloud-net/meta-data:
        when:
            - create
            - copy
        template: cloud-init-meta.tpl
    /var/lib/cloud/seed/nocloud-net/network-config:
        when:
            - create
            - copy
        template: cloud-init-network.tpl
    /var/lib/cloud/seed/nocloud-net/user-data:
        when:
            - create
            - copy
        template: cloud-init-user.tpl
        properties:
            default: |
                #cloud-config
                {}
    /var/lib/cloud/seed/nocloud-net/vendor-data:
        when:
            - create
            - copy
        template: cloud-init-vendor.tpl
        properties:
            default: |
                #cloud-config
                {}
EOF
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF
合計 128920
drwxrwxrwt  4 root root      4096 10月 11 00:40 ./
drwxr-xr-x 12 root root      4096 10月  4 09:34 ../
-rw-r--r--  1 root root      1046 10月 11 00:38 metadata.yaml
drwxr-xr-x 20 root root      4096 10月 10 23:40 raspbian-stretch/
-rw-r--r--  1 root root 131993600 10月 10 23:43 raspbian-stretch.squashfs
drwxr-xr-x  2 root root      4096 10月 10 23:46 templates/
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# tar zcf meta-raspbian-stretch.tar.gz metadata.yaml templates/
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF
合計 128924
drwxrwxrwt  4 root root      4096 10月 11 00:40 ./
drwxr-xr-x 12 root root      4096 10月  4 09:34 ../
-rw-r--r--  1 root root       763 10月 11 00:40 meta-raspbian-stretch.tar.gz
-rw-r--r--  1 root root      1046 10月 11 00:38 metadata.yaml
drwxr-xr-x 20 root root      4096 10月 10 23:40 raspbian-stretch/
-rw-r--r--  1 root root 131993600 10月 10 23:43 raspbian-stretch.squashfs
drwxr-xr-x  2 root root      4096 10月 10 23:46 templates/
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
```

## lxc image import する
```shell-session
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc image list
+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF /var/snap/lxd/common/lxd/images/
合計 8
drwx------  2 root root 4096 10月 11 00:41 ./
drwxr-xr-x 14 root root 4096 10月  5 23:17 ../
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc image import meta-raspbian-stretch.tar.gz raspbian-stretch.squashfs --alias raspbian/stretch
Image imported with fingerprint: 056f3c57bf177f7c42cd8180fd2049b00e0a76c735657a770608167b74709e6b
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc image list
+------------------+--------------+--------+-------------------------------+--------+----------+------------------------------+
|      ALIAS       | FINGERPRINT  | PUBLIC |          DESCRIPTION          |  ARCH  |   SIZE   |         UPLOAD DATE          |
+------------------+--------------+--------+-------------------------------+--------+----------+------------------------------+
| raspbian/stretch | 056f3c57bf17 | no     | Raspbian Stretch (2017-09-07) | armv7l | 125.88MB | Oct 10, 2017 at 3:43pm (UTC) |
+------------------+--------------+--------+-------------------------------+--------+----------+------------------------------+
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ls -laF /var/snap/lxd/common/lxd/images/
合計 128912
drwx------  2 root root      4096 10月 11 00:43 ./
drwxr-xr-x 14 root root      4096 10月  5 23:17 ../
-rw-------  1 root root       763 10月 11 00:43 056f3c57bf177f7c42cd8180fd2049b00e0a76c735657a770608167b74709e6b
-rw-------  1 root root 131993600 10月 11 00:43 056f3c57bf177f7c42cd8180fd2049b00e0a76c735657a770608167b74709e6b.rootfs
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
```

## コンテナ起動！

```shell-session
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc launch raspbian/stretch raspbian
Creating raspbian
Starting raspbian
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc list
+------------+---------+----------------------+------------------------------------------+------------+-----------+
|    NAME    |  STATE  |         IPV4         |                   IPV6                   |    TYPE    | SNAPSHOTS |
+------------+---------+----------------------+------------------------------------------+------------+-----------+
| raspbian   | RUNNING |                      | 240f:1c:908c:1:216:3eff:fe7f:1e23 (eth0) | PERSISTENT | 0         |
+------------+---------+----------------------+------------------------------------------+------------+-----------+
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc exec raspbian /sbin/dhclient eth0
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# lxc list
+------------+---------+----------------------+------------------------------------------+------------+-----------+
|    NAME    |  STATE  |         IPV4         |                   IPV6                   |    TYPE    | SNAPSHOTS |
+------------+---------+----------------------+------------------------------------------+------------+-----------+
| raspbian   | RUNNING | 192.168.3.203 (eth0) | 240f:1c:908c:1:216:3eff:fe7f:1e23 (eth0) | PERSISTENT | 0         |
+------------+---------+----------------------+------------------------------------------+------------+-----------+
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp#
root@raspberrypi:/var/tmp# ping -c 3 192.168.3.203
PING 192.168.3.203 (192.168.3.203) 56(84) bytes of data.
64 bytes from 192.168.3.203: icmp_seq=1 ttl=64 time=0.404 ms
64 bytes from 192.168.3.203: icmp_seq=2 ttl=64 time=0.129 ms
64 bytes from 192.168.3.203: icmp_seq=3 ttl=64 time=0.160 ms

--- 192.168.3.203 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2040ms
rtt min/avg/max/mdev = 0.129/0.231/0.404/0.122 ms
root@raspberrypi:/var/tmp# 
root@raspberrypi:/var/tmp# lxc exec raspbian /bin/bash
root@raspbian:~#
root@raspbian:~# cat /etc/debian_version
9.1
root@raspbian:~#
root@raspbian:~# cat /etc/issue
Raspbian GNU/Linux 9 \n \l
root@raspbian:~#
root@raspbian:~# apt-get install lsb-release
root@raspbian:~# lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 9.1 (stretch)
Release:        9.1
Codename:       stretch
root@raspbian:~#
root@raspbian:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
73: eth0@if74: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 00:16:3e:7f:1e:23 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 192.168.3.203/24 brd 192.168.3.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 240f:1c:908c:1:216:3eff:fe7f:1e23/64 scope global mngtmpaddr dynamic
       valid_lft 269sec preferred_lft 269sec
    inet6 fe80::216:3eff:fe7f:1e23/64 scope link
       valid_lft forever preferred_lft forever
root@raspbian:~#
```

# I2Cインターフェイスにアクセスできるか確認 → 残念。。。

```shell-session
root@raspbian:~#
root@raspbian:~# apt-get install i2c-tools
root@raspbian:~#
root@raspbian:~# i2cdetect -y 1
Error: Could not open file `/dev/i2c-1' or `/dev/i2c/1': No such file or directory
root@raspbian:~# i2cdetect -y 0
Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory
root@raspbian:~#
root@raspbian:~#
root@raspbian:~# ls /dev
console  fd  full  fuse  initctl  log  lxd  mqueue  net  null  ptmx  pts  random  shm  stderr  stdin  stdout  tty  urandom  zero
root@raspbian:~#
root@raspbian:~#
root@raspbian:~# lsmod
Module                  Size  Used by
appletalk              31421  0
psnap                   2787  1 appletalk
rose                   44651  0
netrom                 44835  0
ax25                   63043  2 rose,netrom
i2c_bcm2708             5994  0
binfmt_misc             7988  1
veth                    5878  0
bridge                108641  0
stp                     2430  1 bridge
llc                     5903  3 psnap,bridge,stp
cmac                    3239  1
bnep                   12051  2
hci_uart               20020  1
btbcm                   7916  1 hci_uart
bluetooth             365511  24 hci_uart,bnep,btbcm
fuse                   99603  5
ip6table_filter         2159  1
ip6t_MASQUERADE         1308  1
nf_nat_masquerade_ipv6     3794  1 ip6t_MASQUERADE
ip6table_nat            2310  1
nf_conntrack_ipv6       9161  1
nf_defrag_ipv6         15756  1 nf_conntrack_ipv6
nf_nat_ipv6             6407  1 ip6table_nat
ip6_tables             13434  2 ip6table_filter,ip6table_nat
iptable_filter          2197  1
xt_CHECKSUM             1525  1
xt_tcpudp               2526  13
iptable_mangle          2047  1
ipt_MASQUERADE          1363  1
nf_nat_masquerade_ipv4     3123  1 ipt_MASQUERADE
xt_comment              1171  19
iptable_nat             2320  1
nf_conntrack_ipv4       8890  1
nf_defrag_ipv4          1824  1 nf_conntrack_ipv4
nf_nat_ipv4             6071  1 iptable_nat
nf_nat                 18854  4 nf_nat_masquerade_ipv6,nf_nat_ipv6,nf_nat_masquerade_ipv4,nf_nat_ipv4
nf_conntrack          104800  7 nf_conntrack_ipv6,nf_conntrack_ipv4,nf_nat_masquerade_ipv6,nf_nat_ipv6,nf_nat_masquerade_ipv4,nf_nat_ipv4,nf_nat
squashfs               33761  2
brcmfmac              223048  0
brcmutil                9092  1 brcmfmac
cfg80211              543091  1 brcmfmac
rfkill                 20851  4 bluetooth,cfg80211
snd_bcm2835            24427  0
snd_pcm                98501  1 snd_bcm2835
snd_timer              23968  1 snd_pcm
snd                    70032  3 snd_timer,snd_bcm2835,snd_pcm
bcm2835_gpiomem         3940  0
i2c_bcm2835             7167  0
uio_pdrv_genirq         3923  0
fixed                   3285  0
uio                    10204  1 uio_pdrv_genirq
i2c_dev                 6913  0
ip_tables              13161  3 iptable_mangle,iptable_filter,iptable_nat
x_tables               20578  10 xt_comment,iptable_mangle,ip_tables,iptable_filter,xt_tcpudp,ipt_MASQUERADE,xt_CHECKSUM,ip6table_filter,ip6t_MASQUERADE,ip6_tables
ipv6                  409035  95 nf_conntrack_ipv6,nf_nat_masquerade_ipv6,nf_defrag_ipv6,nf_nat_ipv6,bridge
root@raspbian:~#
```


# 参考
- [LXD 2.0: イメージの管理 [5/12] @Qiita](https://qiita.com/m-shibata/items/606f7465d83f296ceea0#%E7%8B%AC%E8%87%AA%E3%81%AE%E3%82%A4%E3%83%A1%E3%83%BC%E3%82%B8%E3%81%AE%E4%BD%9C%E6%88%90)
- [CLIマジック：debootstrapによるDebian GNU/Linuxのインストール](https://mag.osdn.jp/06/09/19/0151206)
- [D.3. Unix/Linux システムからの Debian GNU/Linux のインストール / D.3.3. debootstrap の実行](https://www.debian.org/releases/stable/amd64/apds03.html.ja)
- [SquashFSをマウントするまで](https://qiita.com/stc1988/items/41d9da92ea02fc3d15a3)
- [SquashFSの圧縮形式](https://qiita.com/stc1988/items/5a2eb4ba9335840c8fe0)
- [RHEL6 squashfsの作り方](http://nopipi.hatenablog.com/entry/2015/05/05/164350)