mmdebstrapはdebootstrapライクなインターフェースを持つ、
debootstrapとmmdebstrap
Linux環境を構築する上で
世界に遍く存在する多種多様なソフトウェアをすべてひとつのルートファイルシステムに含めることは事実上不可能であり、
よって大半のLinuxディストリビューションは、
しかしながらコンテナ仮想化の普及によって、
コンテナ時代の初期から、
debootstrapは何もない状態から、
もともとdebootstrapは名前の候補に
debootstrapは
前置きが長くなりましたが、
mmdebstrapのインストール
mmdebstrapは構築時にaptコマンドを活用することで、
結果的にdebootstrapよりも数倍速くなったり、
まずはmmdebstrapをインストールしましょう。mmdebstrapは比較的新しいパッケージのため、
- Ubuntu 19.
04、 19. 10、 Debian 10:0. 4.1 - Ubuntu focal、
Debian testing:0. 5.1
今回はUbuntu 19.
Ubuntu 19.
$ sudo apt install mmdebstrap
LXD上でmmdebstrapを動かすには
ただ普段使っている環境はUbuntu 18.
LXDの初期設定については第521回~/rootfs/
」
$ mkdir $HOME/rootfs $ lxc launch ubuntu:19.10 mmdebstrap Creating mmdebstrap Starting mmdebstrap $ echo -e "both 0 0\nboth 1000 1000" | lxc config set mmdebstrap raw.idmap - $ lxc config device add mmdebstrap share disk source=$HOME/rootfs path=/home/ubuntu/rootfs Device share added to mmdebstrap
さらにdebootstram/
$ lxc config set mmdebstrap security.privileged true $ lxc config set mmdebstrap security.nesting true $ lxc restart mmdebstrap
このような設定だと、/etc/
」sudo
コマンドを実行できるようになっています。コンテナの扱いには十分に注意してください。
cloud-initなどを使わない場合、
LaunchpadにSSHの公開鍵を登録しているなら、
$ lxc exec mmdebstrap -- sudo -i -u ubuntu ssh-import-id lp:(LaunchpadのID)
GitHubに登録した公開鍵を利用したいのであれば、lp:
」lxc list
」
基本的な使い方
基本的な使い方を見ていきましょう。Ubuntu 19.
$ mmdebstrap --components="main restricted universe multiverse" \ eoan eoan-rootfs http://jp.archive.ubuntu.com/ubuntu I: automatically chosen mode: unshare I: chroot architecture amd64 is equal to the host's architecture I: running apt-get update... done I: downloading packages with apt... done I: extracting archives... done I: installing packages... done I: downloading apt... done I: installing apt... done I: installing remaining packages inside the chroot... done I: cleaning package lists and apt cache... done done
debootstrapと異なり管理者権限でsudo
コマンドを付けて)unshare
コマンドを用いて隔離空間を作成します。この挙動は--mode
」
コマンドオプションにはコンポーネントを指定しています。Debianの場合は原則としてmainだけで良く、
残りのコマンドライン引数はそれぞれ順番に、/etc/
を構築する際に利用されます。
2つ目の引数である-
」
最後の引数がミラーサーバーのURIです。0.
ミラーサーバーは複数指定できます。たとえば通常のリポジトリに加えて、
ミラーサーバーで指定できるURIは次の3種類です。
- 「
-
」:標準入力の内容がそのままsources.listに追加されます - 「
deb
」もしくは 「 deb-src
」で始まる文字列:その行がそのままsources.listに追加されます - 「
://
」が含まれる文字列: 「 deb URI リリース名 コンポーネント
」の形でsources. listに追加されます - 上記以外でファイル名の場合:ファイル名の内容がそのままsources.
listに追加されます
特に-
」
$ cat /etc/apt/sources.list /etc/apt/source.list.d/*.list | mmdebstrap eoan eoan-rootfs -
Debianのルートファイルシステムを作る際の注意点
Ubuntu上でDebianのルートファイルシステムを作ろうとするとリポジトリの鍵がないため、
$ mmdebstrap unstable sid I: automatically chosen mode: unshare I: chroot architecture amd64 is equal to the host's architecture I: running apt-get update... done Get:1 http://deb.debian.org/debian unstable InRelease [139 kB] Err:1 http://deb.debian.org/debian unstable InRelease The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 04EE7237B7D453EC NO_PUBKEY 648ACFD622F3D138 Reading package lists... W: GPG error: http://deb.debian.org/debian unstable InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 04EE7237B7D453EC NO_PUBKEY 648ACFD622F3D138 E: The repository 'http://deb.debian.org/debian unstable InRelease' is not signed. E: apt-get update -oAPT::Status-Fd=<$fd> -oDpkg::Use-Pty=false failed
これはUbuntuには、
mmdebstrapは最初の段階では実行ホスト上のapt
コマンドを利用します。その際、apt
コマンドは実行ホスト上に登録されているリポジトリ鍵です。Ubuntuマシンなら、apt-key
コマンドで参照できます。
$ apt-key list /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-archive.gpg ------------------------------------------------------ pub rsa4096 2012-05-11 [SC] 790B C727 7767 219C 42C8 6F93 3B4F E6AC C0B2 1F32 uid [ unknown] Ubuntu Archive Automatic Signing Key (2012) <ftpmaster@ubuntu.com> /etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg ------------------------------------------------------ pub rsa4096 2012-05-11 [SC] 8439 38DF 228D 22F7 B374 2BC0 D94A A3F0 EFE2 1092 uid [ unknown] Ubuntu CD Image Automatic Signing Key (2012) <cdimage@ubuntu.com> /etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg ------------------------------------------------------ pub rsa4096 2018-09-17 [SC] F6EC B376 2474 EDA9 D21B 7022 8719 20D1 991B C93C uid [ unknown] Ubuntu Archive Automatic Signing Key (2018) <ftpmaster@ubuntu.com>
しかしながらDebianのルートファイルシステムを作る際に参照するリポジトリはUbuntuではなく、
Debian系のリポジトリには、
$ sudo apt install debian-archive-keyring
これにより/usr/
以下に、
$ mmdebstrap unstable sid \ --aptopt='Dir::Etc::Trusted "/usr/share/keyrings/debian-archive-keyring.gpg"'
ちなみにmmdebstrapの最新のコードにはdebootstrap
コマンドと同じように、--keyring
」
ルートファイルシステムのクロスビルド
もちろんホストとは異なるCPUアーキテクチャーのルートファイルシステムの構築も可能です。あらかじめqemu-user-staticとbinfmt-supportをインストールしておきます。これによりホストと異なるアーキテクチャーのバイナリを実行する際に、
$ sudo apt install qemu-user-static binfmt-support
あとは次のコマンドを実行するだけです。
$ mmdebstrap --components="main restricted universe multiverse" \ --architecture=arm64 \ eoan eoan-arm64-rootfs http://ports.ubuntu.com/ubuntu-ports
「--architecture=arm64
」
注意しなければならないのはミラーサーバーの部分です。Ubuntuの公式リポジトリサーバーでサポートしているアーキテクチャーはamd64とi386だけです
残念ながら、
$ mmdebstrap --components="main restricted universe multiverse" \ --architecture=arm64 \ eoan eoan-arm64-rootfs http://ports.ubuntu.com/ubuntu-ports I: automatically chosen mode: unshare I: arm64 cannot be executed, falling back to qemu-user update-binfmts: warning: qemu-aarch64 not in database of installed binary formats. update-binfmts: exiting due to previous errors E: qemu-aarch64 is not a supported binfmt name
これは権限や名前空間の都合上、
ちなみに--architectures
」--add-architecture
オプションが指定された状態でルートファイルシステムが作られます。MultiArchな環境を作りたい場合に便利でしょう。
ルートファイルシステムのサイズを減らす方法
実はこの時点で、
$ sudo du -hs eoan-*rootfs/ | sort -nr 334M eoan-debootstrap-rootfs/ 269M eoan-rootfs/
サイズとしてはそこそこ差が出ていますが、--variant
」
debootstrapの--variant
」
もっとも小さくなるのはcustomを指定して、--include
」--variant
」
$ mmdebstrap --components="main restricted universe multiverse" \ --variant=essential \ eoan eoan-essential-rootfs http://jp.archive.ubuntu.com/ubuntu
ちなみにessentialはaptコマンドすらインストールしないので、
$ sudo du -hs eoan-*rootfs/ | sort -nr 334M eoan-debootstrap-rootfs/ 269M eoan-rootfs/ 119M eoan-debootstrap-minbase-rootfs/ 65M eoan-essential-rootfs/
かなり小さくなりましたね。
dpkgにはパッケージのインストール時に
$ mmdebstrap --components="main restricted universe multiverse" \ --variant=essential \ --dpkgopt='path-exclude=/usr/share/man/*' \ --dpkgopt='path-exclude=/usr/share/locale/*/LC_MESSAGES/*.mo' \ --dpkgopt='path-exclude=/usr/share/doc/*' \ --dpkgopt='path-include=/usr/share/doc/*/copyright' \ eoan eoan-essential-min-rootfs http://jp.archive.ubuntu.com/ubuntu
「--dpkgopt
」dpkg
コマンドのオプションから先頭のハイフンを取り除いたものです。
上記の例だと
$ sudo du -hs eoan-*rootfs/ | sort -nr 334M eoan-debootstrap-rootfs/ 269M eoan-rootfs/ 119M eoan-debootstrap-minbase-rootfs/ 74M eoan-apt-min-rootfs/ 65M eoan-essential-rootfs/ 57M eoan-essential-min-rootfs/
さらに小さくなりました。--variant=essential
」--variant=apt
」
これ以上はもう個別にファイルを削除したり、
Dockerイメージとして活用する
作ったルートファイルシステムをDockerイメージにしてみましょう。
$ cd eoan-apt-min-rootfs/ $ sudo tar -c . | docker import - eoan
公式のUbuntu 19.
$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE eoan latest 603b264ef4ea 7 seconds ago 68.7MB ubuntu eoan 09604a62a001 3 weeks ago 72.9MB
Ubuntu BaseやUbuntu Minimalとの比較
Ubuntuには組み込み・
- http://
cdimage. ubuntu. com/ ubuntu-base/ releases/ 19. 10/ release/ - http://
cloud-images. ubuntu. com/ minimal/ releases/ eoan/ release/
$ sudo du -hs * | sort -nr 404M minimal 77M base
Ubuntu Baseのほうはeoan-apt-min-rootfsと大差ありませんね。当たり前と言えば当たり前なのですが、/etc/
」
# Drop all man pages
path-exclude=/usr/share/man/*
# Drop all translations
path-exclude=/usr/share/locale/*/LC_MESSAGES/*.mo
# Drop all documentation ...
path-exclude=/usr/share/doc/*
# ... except copyright files ...
path-include=/usr/share/doc/*/copyright
# ... and Debian changelogs
path-include=/usr/share/doc/*/changelog.Debian.*
http://cdimage.ubuntu.com/ubuntu-base/releases/
まさにmmdebstrapで指定した内容です。その結果、
なおUbuntu Baseには/usr/
」
ちなみにUbuntu Minimalは、