目次

開発環境用コンテナのセットアップ(Ubuntu以外のディストリビューション使用の場合)

Ubuntu 以外のディストリビューションを利用している場合でも、クロスコンパイラなどの導入を考えると
Ubuntu の環境を構築するのがお勧めです。

仮想マシンを作成するとリソースを消費しますので、コンテナ(LXC1) 2))でUbuntuの環境を作成する方法を紹介します。

ホストの環境は Debian(Jessie) を想定しています。

emdebian のリポジトリを追加し、クロスコンパイラを導入する方法 3)もあるようです。


Ubuntu コンテナの作成

LXC パッケージの導入

LXC を使用するためのパッケージを導入します。

root@jessie:~# apt-get install lxc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  debootstrap dh-python libapparmor1 libmpdec2 libpython3-stdlib libpython3.4-minimal libpython3.4-stdlib
  libseccomp2 python3 python3-minimal python3.4 python3.4-minimal rsync
Suggested packages:
  lua5.2 python3-doc python3-tk python3-venv python3.4-venv python3.4-doc binutils binfmt-support
The following NEW packages will be installed:
  debootstrap dh-python libapparmor1 libmpdec2 libpython3-stdlib libpython3.4-minimal libpython3.4-stdlib
  libseccomp2 lxc python3 python3-minimal python3.4 python3.4-minimal rsync
0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,822 kB of archives.
After this operation, 22.7 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
Do you want to continue? [Y/n] y
Get:1 http://ftp.jaist.ac.jp/debian/ jessie/main libmpdec2 amd64 2.4.1-1 [85.7 kB]
Get:2 http://ftp.jaist.ac.jp/debian/ jessie/main libpython3.4-minimal amd64 3.4.2-1 [492 kB]
Get:3 http://ftp.jaist.ac.jp/debian/ jessie/main libpython3.4-stdlib amd64 3.4.2-1 [2,088 kB] 
Get:4 http://ftp.jaist.ac.jp/debian/ jessie/main libseccomp2 amd64 2.1.1-1 [27.6 kB]
Get:5 http://security.debian.org/ jessie/updates/main lxc amd64 1:1.0.6-6+deb8u2 [626 kB]
Get:6 http://ftp.jaist.ac.jp/debian/ jessie/main libapparmor1 amd64 2.9.0-3 [60.3 kB]
Get:7 http://ftp.jaist.ac.jp/debian/ jessie/main python3.4-minimal amd64 3.4.2-1 [1,646 kB]
Get:8 http://ftp.jaist.ac.jp/debian/ jessie/main python3.4 amd64 3.4.2-1 [204 kB]
Get:9 http://ftp.jaist.ac.jp/debian/ jessie/main python3-minimal amd64 3.4.2-2 [34.7 kB]
Get:10 http://ftp.jaist.ac.jp/debian/ jessie/main libpython3-stdlib amd64 3.4.2-2 [18.1 kB]
Get:11 http://ftp.jaist.ac.jp/debian/ jessie/main dh-python all 1.20141111-2 [66.4 kB]
Get:12 http://ftp.jaist.ac.jp/debian/ jessie/main python3 amd64 3.4.2-2 [21.2 kB]
Get:13 http://ftp.jaist.ac.jp/debian/ jessie/main rsync amd64 3.1.1-3 [390 kB]
Get:14 http://ftp.jaist.ac.jp/debian/ jessie/main debootstrap all 1.0.67 [62.7 kB]
Fetched 5,822 kB in 2s (2,785 kB/s)                          
Selecting previously unselected package libmpdec2:amd64.
(Reading database ... 29812 files and directories currently installed.)
Preparing to unpack .../libmpdec2_2.4.1-1_amd64.deb ...
〜〜〜 中略 〜〜〜
Setting up python3 (3.4.2-2) ...
running python rtupdate hooks for python3.4...
running python post-rtupdate hooks for python3.4...
Setting up lxc (1:1.0.6-6+deb8u2) ...
Setting up dh-python (1.20141111-2) ...
Processing triggers for libc-bin (2.19-18+deb8u1) ...
Processing triggers for systemd (215-17+deb8u2) ...
root@jessie:~# 


コンテナの作成

lxc-create コマンドで、Ubuntu テンプレートを使用して Ubuntu のコンテナを作成します。

root@jessie:~# MIRROR=https://mirrors.kernel.org/ubuntu lxc-create -n trusty -t ubuntu -- -r trusty
Checking cache download in /var/cache/lxc/trusty/rootfs-amd64 ... 
Installing packages in template: ssh,vim,language-pack-en
Downloading ubuntu trusty minimal ...
W: Cannot check Release signature; keyring file not available /usr/share/keyrings/ubuntu-archive-keyring.gpg
I: Retrieving Release 
I: Retrieving Packages 
I: Validating Packages 
I: Retrieving Packages 
I: Validating Packages 
I: Resolving dependencies of required packages...
I: Resolving dependencies of base packages...
...
Generating locales...
  en_US.UTF-8... up-to-date
Generation complete.
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
Creating SSH2 ED25519 key; this may take some time ...
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match ssh Default-Stop values (none)
initctl: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused
invoke-rc.d: policy-rc.d denied execution of start.

Current default time zone: 'Asia/Tokyo'
Local time is now:      Fri Jan  8 11:38:36 JST 2016.
Universal Time is now:  Fri Jan  8 02:38:36 UTC 2016.


##
# The default user is 'ubuntu' with password 'ubuntu'!
# Use the 'sudo' command to run tasks as root in the container.
##

root@jessie:~# 


設定

Debian の lxc-create では細かい設定をしてくれないので、手動で設定を変更します。

/var/lib/lxc/trusty/config の編集

root@jessie:~# nano -w /var/lib/lxc/trusty/config 
# Template used to create this container: /usr/share/lxc/templates/lxc-ubuntu
# Parameters passed to the template: -r trusty
# For additional config options, please look at lxc.container.conf(5)

# Common configuration
lxc.include = /usr/share/lxc/config/ubuntu.common.conf

# Container specific configuration
lxc.rootfs = /var/lib/lxc/trusty/rootfs
lxc.mount = /var/lib/lxc/trusty/fstab
lxc.utsname = trusty
lxc.arch = amd64

# Network configuration
lxc.network.type = veth
lxc.network.link = lxcbr0
lxc.network.flags = up

lxc.devttydir = lxc


lxcbr0 I/F の作成

bridge-utils パッケージを導入します。

root@jessie:~# apt-get install bridge-utils

/etc/network/interfaces を編集し、lxcbr0 の設定を追加します。

root@jessie:~# nano -w /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet dhcp

auto lxcbr0
iface lxcbr0 inet static
    address 192.168.8.1
    netmask 255.255.255.0
    pre-up brctl addbr lxcbr0


dnsmasq の導入

DHCPサーバ/DNSキャッシュ用 dnsmasq パッケージを導入します。

root@jessie:~# apt-get install dnsmasq

/etc/dnsmasq.conf を編集します。

root@jessie:~# nano -w /etc/dnsmasq.conf 

下記エントリを追加します。

interface=lxcbr0
bind-interfaces
dhcp-range=192.168.8.50,192.168.8.150,12h


iptables の設定

iptables の設定を自動化するため、iptables-persistent パッケージを導入します。
導入時に現在のルールを保存するか聞かれるため、eth0 への MASQUERADE 設定(コンテナから外へ抜けるため) を追加しておきます。

root@jessie:~# iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE


root@jessie:~# apt-get install iptables-persistent
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  netfilter-persistent
The following NEW packages will be installed:
  iptables-persistent netfilter-persistent
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.9 kB of archives.
After this operation, 147 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://ftp.jaist.ac.jp/debian/ jessie/main netfilter-persistent all 1.0.3 [8,036 B]
Get:2 http://ftp.jaist.ac.jp/debian/ jessie/main iptables-persistent all 1.0.3 [10.8 kB]
Fetched 18.9 kB in 0s (198 kB/s)          
Preconfiguring packages ...
Selecting previously unselected package netfilter-persistent.
(Reading database ... 31310 files and directories currently installed.)
Preparing to unpack .../netfilter-persistent_1.0.3_all.deb ...
Unpacking netfilter-persistent (1.0.3) ...
Selecting previously unselected package iptables-persistent.
Preparing to unpack .../iptables-persistent_1.0.3_all.deb ...
Unpacking iptables-persistent (1.0.3) ...
Processing triggers for man-db (2.7.0.2-5) ...
Processing triggers for systemd (215-17+deb8u2) ...
Setting up netfilter-persistent (1.0.3) ...
update-rc.d: warning: start and stop actions are no longer supported; falling back to defaults
Setting up iptables-persistent (1.0.3) ...
Processing triggers for systemd (215-17+deb8u2) ...


Ubuntu コンテナの起動

lxc-start コマンドで、作成した Ubuntu コンテナを起動します。

root@jessie:~# lxc-start -n trusty
lxc-start: No such file or directory - Mount of '/sys/fs/fuse/connections' onto '/usr/lib/x86_64-linux-gnu/lxc/rootfs/sys/fs/fuse/connections' failed
<4>init: plymouth-upstart-bridge main process (5) terminated with status 1
<4>init: plymouth-upstart-bridge main process ended, respawning
<4>init: hwclock main process (7) terminated with status 77
<4>init: ureadahead main process (8) terminated with status 5
<4>init: plymouth-upstart-bridge main process (15) terminated with status 1
<4>init: plymouth-upstart-bridge main process ended, respawning
 * Starting Mount filesystems on boot   ...done.
 * Starting Signal sysvinit that the rootfs is mounted   ...done.
 * Stopping Send an event to indicate plymouth is up   ...done.
 * Starting Clean /tmp directory   ...done.
 * Starting Populate and link to /run filesystem   ...done.
 * Stopping Populate and link to /run filesystem   ...done.
 * Stopping Clean /tmp directory   ...done.
 * Starting Track if upstart is running in a container   ...done.
 * Starting load fallback graphics devices   ...done.
 * Starting workaround for missing events in container   ...done.
 * Starting Initialize or finalize resolvconf   ...done.
 * Stopping load fallback graphics devices   ...done.
<4>init: console-font main process (114) terminated with status 1
 * Stopping workaround for missing events in container   ...done.
 * Starting userspace bootsplash   ...done.
Ubuntu 14.04.3 LTS trusty console

trusty login: <4>init: setvtrgb main process (417) terminated with status 1
<4>init: plymouth-upstart-bridge main process (21) killed by TERM signal


Ubuntu 14.04.3 LTS trusty console

trusty login: 

ubuntu/ubuntu でログインできます。

Ubuntu 14.04.3 LTS trusty console

trusty login: ubuntu
Password: 
Last login: Fri Jan  8 13:09:57 JST 2016 on lxc/console
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.16.0-4-amd64 x86_64)

 * Documentation:  https://help.ubuntu.com/
ubuntu@trusty:~$ ifconfig 
eth0      Link encap:Ethernet  HWaddr 4e:0a:d2:c2:7a:ff  
          inet addr:192.168.8.79  Bcast:192.168.8.255  Mask:255.255.255.0
          inet6 addr: fe80::4c0a:d2ff:fec2:7aff/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:20 errors:0 dropped:0 overruns:0 frame:0
          TX packets:35 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:2020 (2.0 KB)  TX bytes:4178 (4.1 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

ubuntu@trusty:~$ 


ssh でログインすることもできます。

root@jessie:~# lxc-ls --fancy
NAME    STATE    IPV4          IPV6  AUTOSTART  
----------------------------------------------
trusty  RUNNING  192.168.8.79  -     NO         
root@jessie:~# ssh -l ubuntu 192.168.8.79
The authenticity of host '192.168.8.79 (192.168.8.79)' can't be established.
ECDSA key fingerprint is 10:86:e7:23:d0:6d:98:da:ef:14:d6:8b:45:7c:09:fc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.8.79' (ECDSA) to the list of known hosts.
ubuntu@192.168.8.79's password: 
Welcome to Ubuntu 14.04.3 LTS (GNU/Linux 3.16.0-4-amd64 x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Fri Jan  8 13:23:18 2016
ubuntu@trusty:~$ 


Ubuntu 環境の利用

あとは、apt-get でクロスコンパイラパッケージを導入するなり、Ubuntu のマシンを用意したのと同じことが可能です。

ubuntu@trusty:~$ sudo apt-get update
[sudo] password for ubuntu: 
Hit http://security.ubuntu.com trusty-security InRelease
Hit http://security.ubuntu.com trusty-security/main amd64 Packages
Ign https://mirrors.kernel.org trusty InRelease
Hit http://security.ubuntu.com trusty-security/restricted amd64 Packages
Hit https://mirrors.kernel.org trusty-updates InRelease
Hit http://security.ubuntu.com trusty-security/universe amd64 Packages
Hit https://mirrors.kernel.org trusty Release.gpg
Hit https://mirrors.kernel.org trusty-updates/main amd64 Packages
Hit http://security.ubuntu.com trusty-security/multiverse amd64 Packages
Hit https://mirrors.kernel.org trusty-updates/restricted amd64 Packages
Hit http://security.ubuntu.com trusty-security/main Translation-en
Hit https://mirrors.kernel.org trusty-updates/universe amd64 Packages
Hit http://security.ubuntu.com trusty-security/multiverse Translation-en
Hit https://mirrors.kernel.org trusty-updates/multiverse amd64 Packages
Hit https://mirrors.kernel.org trusty-updates/main Translation-en
Hit http://security.ubuntu.com trusty-security/restricted Translation-en
Hit https://mirrors.kernel.org trusty-updates/multiverse Translation-en
Hit http://security.ubuntu.com trusty-security/universe Translation-en
Hit https://mirrors.kernel.org trusty-updates/restricted Translation-en
Hit https://mirrors.kernel.org trusty-updates/universe Translation-en
Hit https://mirrors.kernel.org trusty Release
Hit https://mirrors.kernel.org trusty/main amd64 Packages
Hit https://mirrors.kernel.org trusty/restricted amd64 Packages
Hit https://mirrors.kernel.org trusty/universe amd64 Packages
Hit https://mirrors.kernel.org trusty/multiverse amd64 Packages
Get:1 https://mirrors.kernel.org trusty/main Translation-en_US
Hit https://mirrors.kernel.org trusty/main Translation-en
Get:2 https://mirrors.kernel.org trusty/multiverse Translation-en_US
Hit https://mirrors.kernel.org trusty/multiverse Translation-en
Get:3 https://mirrors.kernel.org trusty/restricted Translation-en_US
Hit https://mirrors.kernel.org trusty/restricted Translation-en
Get:4 https://mirrors.kernel.org trusty/universe Translation-en_US
Hit https://mirrors.kernel.org trusty/universe Translation-en
Get:5 https://mirrors.kernel.org trusty/main Translation-en_US
Ign https://mirrors.kernel.org trusty/main Translation-en_US
Ign https://mirrors.kernel.org trusty/multiverse Translation-en_US
Ign https://mirrors.kernel.org trusty/restricted Translation-en_US
Ign https://mirrors.kernel.org trusty/universe Translation-en_US
Reading package lists... Done
ubuntu@trusty:~$ 

ARMhf 開発ツールの導入

ubuntu@trusty:~$ sudo apt-cache search gcc|grep armhf
cpp-arm-linux-gnueabihf - The GNU C preprocessor (cpp) for armhf architecture
gcc-arm-linux-gnueabihf - The GNU C compiler for armhf architecture
libatomic1-armhf-cross - support library providing __atomic built-in functions
libgcc-4.8-dev-armhf-cross - GCC support library (development files)
libgcc1-armhf-cross - GCC support library
libgomp1-armhf-cross - GCC OpenMP (GOMP) support library
libsfatomic1-armhf-cross - support library providing __atomic built-in functions (soft float ABI)
libsfgcc-4.8-dev-armhf-cross - GCC support library (soft float ABI development files)
libsfgcc1-armhf-cross - GCC support library (soft float ABI)
libsfgomp1-armhf-cross - GCC OpenMP (GOMP) support library (soft float ABI)
gfortran-arm-linux-gnueabihf - The GNU Fortran 95 compiler for armhf architecture
gobjc++-arm-linux-gnueabihf - The GNU Objective-C++ compiler for armhf architecture
gobjc-arm-linux-gnueabihf - The GNU Objective-C compiler for armhf architecture
libatomic1-dbg-armhf-cross - support library providing __atomic built-in functions (debug symbols)
libgcc-4.7-dev-armhf-cross - GCC support library (development files)
libgcc1-dbg-armhf-cross - GCC support library (debug symbols)
libgomp1-dbg-armhf-cross - GCC OpenMP (GOMP) support library (debug symbols)
libmudflap0-4.7-dev-armhf-cross - GCC mudflap support libraries (development files)
libmudflap0-4.8-dev-armhf-cross - GCC mudflap support libraries (development files)
libmudflap0-armhf-cross - GCC mudflap shared support libraries
libmudflap0-dbg-armhf-cross - GCC mudflap shared support libraries (debug symbols)
libsfatomic1-dbg-armhf-cross - support library providing __atomic built-in functions (soft float ABI debug symbols)
libsfgcc-4.7-dev-armhf-cross - GCC support library (soft float ABI development files)
libsfgcc1-dbg-armhf-cross - GCC support library (debug symbols)
libsfgomp1-dbg-armhf-cross - GCC OpenMP (GOMP) support library (soft float ABI debug symbols)
libsfmudflap0-armhf-cross - GCC mudflap shared support libraries (soft float)
libsfmudflap0-dbg-armhf-cross - GCC mudflap shared support libraries (soft float debug symbols)
ubuntu@trusty:~$ sudo apt-get install gcc-arm-linux-gnueabihf
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  binutils binutils-arm-linux-gnueabihf cpp-4.8-arm-linux-gnueabihf
  cpp-arm-linux-gnueabihf gcc-4.8-arm-linux-gnueabihf
  gcc-4.8-arm-linux-gnueabihf-base gcc-4.8-multilib-arm-linux-gnueabihf
  libasan0-armhf-cross libatomic1-armhf-cross libc6-armel-armhf-cross
  libc6-armel-cross libc6-armhf-cross libc6-dev-armel-armhf-cross
  libc6-dev-armel-cross libc6-dev-armhf-cross libcloog-isl4
  libgcc-4.8-dev-armhf-cross libgcc1-armhf-cross libgmp10 libgomp1-armhf-cross
  libisl10 libmpc3 libmpfr4 libsfasan0-armhf-cross libsfatomic1-armhf-cross
  libsfgcc-4.8-dev-armhf-cross libsfgcc1-armhf-cross libsfgomp1-armhf-cross
  linux-libc-dev-armel-cross linux-libc-dev-armhf-cross
Suggested packages:
  binutils-doc gcc-4.8-locales cpp-doc gcc-4.8-doc libgcc1-dbg-armhf-cross
  libgomp1-dbg-armhf-cross libitm1-dbg-armhf-cross libatomic1-dbg-armhf-cross
  libasan0-dbg-armhf-cross libtsan0-dbg-armhf-cross
  libbacktrace1-dbg-armhf-cross libquadmath-dbg-armhf-cross binutils-gold make
  manpages-dev autoconf automake1.9 libtool flex bison gdb-arm-linux-gnueabihf
  gcc-doc
The following NEW packages will be installed:
  binutils binutils-arm-linux-gnueabihf cpp-4.8-arm-linux-gnueabihf
  cpp-arm-linux-gnueabihf gcc-4.8-arm-linux-gnueabihf
  gcc-4.8-arm-linux-gnueabihf-base gcc-4.8-multilib-arm-linux-gnueabihf
  gcc-arm-linux-gnueabihf libasan0-armhf-cross libatomic1-armhf-cross
  libc6-armel-armhf-cross libc6-armel-cross libc6-armhf-cross
  libc6-dev-armel-armhf-cross libc6-dev-armel-cross libc6-dev-armhf-cross
  libcloog-isl4 libgcc-4.8-dev-armhf-cross libgcc1-armhf-cross libgmp10
  libgomp1-armhf-cross libisl10 libmpc3 libmpfr4 libsfasan0-armhf-cross
  libsfatomic1-armhf-cross libsfgcc-4.8-dev-armhf-cross libsfgcc1-armhf-cross
  libsfgomp1-armhf-cross linux-libc-dev-armel-cross linux-libc-dev-armhf-cross
0 upgraded, 31 newly installed, 0 to remove and 0 not upgraded.
Need to get 23.6 MB of archives.
After this operation, 73.2 MB of additional disk space will be used.
Do you want to continue? [Y/n]