LoginSignup
2
2

More than 3 years have passed since last update.

LXC 向け base image を作る

Last updated at Posted at 2019-04-28

Fedora29 上で CentOS7 の LXC 向け base image を作成する

Docker でもいいんだけど、systemctl が使えないとか、OS 周りの確認に制約があるので、LXC でコンテナしたいなと。

参考

https://www.hiroom2.com/2018/12/14/fedora-29-lxd-en/
https://wp.metainfo.jp/?p=58
https://knowledge.sakura.ad.jp/2226/

base image のベースを作る

基本的には Docker の base image と同じ

yum.conf 準備

以下のような感じに CentOS7 インストール用の yum.conf を作る

[main]
keepcache=0
exactarch=1
obsoletes=1
gpgcheck=0
plugins=1

[base]
name=CentOS-7.6 - Base
baseurl=http://ftp.jaist.ac.jp/pub/Linux/CentOS/7.6.1810/os/x86_64/

[update]
name=CentOS-7.6 - Update
baseurl=http://ftp.jaist.ac.jp/pub/Linux/CentOS/7.6.1810/updates/x86_64/

update は必要に応じて。OS をデフォルトでインストールしたければ不要

base 作成

/tmp/c7 配下に base image に必要なファイルを作成(ここは任意のディレクトリで OK)

/tmp/c7/rootfs 配下に base image の root を作る
rootfs ディレクトリは lxc で必須らしいので名前もこのままで作成する

mkdir -p /tmp/c7/rootfs
sudo yum -c /tmp/c7.conf --disablerepo="*" --enablerepo="base" --installroot=/tmp/c7/rootfs -y groupinstall Core

disablerepo, enablerepo 周りは、Fedora のレポジトリを見に行かないようにするための設定

以下の感じで /tmp/c7/metadata.yaml を作成
OS 名などは適宜変更

metadata.yaml
{
    "architecture": "x86_64",
    "creation_date": 1556402782,
    "properties": {
        "architecture": "x86_64",
        "description": "Centos 7.6(x86_64)",
        "name": "centos-7.6-x86_64",
        "os": "centos",
        "release": "7.6",
        "variant": "default"
    }
}

以下コマンドで作ったファイルを固める
/tmp/c7 は適宜読み替え

cd /tmp/c7
sudo chroot rootfs
--- ここから base image
echo "export LANG=C >> /root/.bashrc"
echo "export LANG=C >> /etc/locale.conf
exit
--- ここまで
sudo tar czf /tmp/c7.tgz metadata.yaml  rootfs

ポイント
tar は必ず metadata.yaml rootfs を指定して固める。sudo tar czf /tmp/c7.tgz . はだめ。
LXC にインポートするときに内部で tar -O -xzf ~~~ metadata.yaml を実行して metadata.yaml の内容を取り出しているらしく、. で固めちゃうと取り出せずエラーになるので。。

base image インポート

$ lxc image list
+-------+-------------+--------+-------------+------+------+-------------+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
+-------+-------------+--------+-------------+------+------+-------------+
$ lxc image import /tmp/c7.tgz
Image imported with fingerprint: e0946c177810db3d0dad64750fc110ec91463a965f85521
$ lxc image list
+-------+--------------+--------+--------------------+--------+----------+-------------------------------+
| ALIAS | FINGERPRINT  | PUBLIC |    DESCRIPTION     |  ARCH  |   SIZE   |          UPLOAD DATE          |
+-------+--------------+--------+--------------------+--------+----------+-------------------------------+
|       | e0946c177810 | no     | Centos 7.6(x86_64) | x86_64 | 492.40MB | Apr 28, 2019 at 12:46am (UTC) |
+-------+--------------+--------+--------------------+--------+----------+-------------------------------+

ここまででインポートは完了。あとは起動するだけ

いざ、起動

$ lxc launch e0946c177810 centos7
$ lxc list
+---------+---------+------+-----------------------------------------------+------------+-----------+
|  NAME   |  STATE  | IPV4 |                     IPV6                      |    TYPE    | SNAPSHOTS |
+---------+---------+------+-----------------------------------------------+------------+-----------+
| centos7 | RUNNING |      | fd42:8879:24d8:33f6:216:3eff:fe04:f97d (eth0) | PERSISTENT |           |
+---------+---------+------+-----------------------------------------------+------------+-----------+
$ lxc exec centos7 -- /bin/bash

これで完了。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2