mkinitcpio (日本語)
| 概要 |
|---|
| Arch の initramfs 作成ユーティリティの詳細ガイド。 |
| 概括 |
| Arch Linux を起動するためには、GRUB(2), Syslinux, LILO, GRUB Legacy などの Linux 対応のブートローダを、Master Boot Record もしくは GUID Partition Table にインストールする必要があります。ブートローダは、ブートプロセスが始まる前にカーネルやイニシャルラムディスクをロードする仕事を行います。 |
mkinitcpio は次世代の initramfs 作成ツールです。
Contents |
概要
mkinitcpio は initial ramdisk 環境を作成するために使われる Bash スクリプトです。mkinitcpio man page より:
- initial ramdisk は非常に小さい環境(初期ユーザー空間)であり、様々なカーネルモジュールをロードして init にコントロールを移す前に必要なことをセットアップします。これにより、root ファイルシステムを暗号化したりソフトウェア RAID アレイに root を載せることが可能になります。mkinitcpio はカスタムフックによって簡単に拡張することができ、自動時の自動検知など様々な機能を持っています。
伝統的には、ブートプロセスの初めにハードウェアを検知したりタスクの初期化を行うのはカーネルで、それから root ファイルシステムがマウントされ init にコントロールが移されます。しかしながら、先端技術が生まれるにつれ、こういった作業は複雑になりつつあります。
今日、root ファイルシステムが載るハードウェアは SCSI から SATA、USB ドライブまで様々にわたり、それらハードウェアはそれぞれのマニュファクチャーによって作られたいろんなドライブコントローラによってコントロールされています。しかも、root ファイルシステムは暗号化されたり圧縮されたりする可能性もあります; ソフトウェア RAID アレイや論理ボリュームグループに含まれるかもしれません。複雑になったものをシンプルに扱う方法はユーザースペースへ管理を譲渡することです: initial ramdisk。
参照: /dev/brain0 » Blog Archive » Early Userspace in Arch Linux.
mkinitcpio は initramfs CPIO イメージを作成するためのモジュール式のツールで、他の方法に比べて多くのアドバンテージを有します。例えば:
- 初期ユーザ空間の小さくて軽量な基盤として BusyBox を使用。
- 実行時のハードウェア自動検知のための udev サポート、そして不必要なモジュールのロードの回避。
- 拡張性のあるフックベースの init スクリプト、カスタムフックを簡単に pacman (日本語) パッケージに含めることが可能。
- LUKS ボリュームのための LVM2, dm-crypt サポート。USB マスストレージデバイスからのレジューム・ブートのための mdadm, swsusp, suspend2 サポート。
- イメージをリビルドする必要なくカーネルコマンドラインから多くの機能を設定可能。
- カーネル内にイメージを含めることをサポート、自己完結型のカーネルイメージを作れます。
mkinitcpio は Arch Linux デベロッパーとコミュニティの貢献によって開発されています。Git リポジトリを見て下さい。
インストール
mkinitcpio パッケージは公式リポジトリで利用可能で base グループの一部としてデフォルトでインストールされています。
上級ユーザーのために Git から mkinitcpio の最新開発バージョンをインストールできます:
$ git clone git://projects.archlinux.org/mkinitcpio.git
イメージ作成とアクティベーション
デフォルトで、カーネルのインストール・アップグレードが行われた後 mkinitcpio スクリプトは2つのイメージを生成します: /boot/initramfs-linux.img と /boot/initramfs-linux-fallback.img。fallback イメージは default イメージと同じ設定ファイルを利用しますが、作成中に autodetect フックだけはスキップされ、全てのモジュールが入ります。autodetect フックは必要なモジュールを検出して、イメージを固有のハードウェアにあわせて、initramfs を小さくします。
様々な設定ファイルを使って initramfs イメージを沢山作りたいと思っているユーザーもいるかもしれません。ブートローダに使いたいイメージを指定する必要があります、主としてブートローダの設定ファイルで指定します。設定ファイルに変更を加えた後は、イメージを再生成しなければなりません。標準の Arch Linux カーネル linux で再生成するには次のコマンドを root 権限で実行してください:
# mkinitcpio -p linux
-p スイッチはプリセット (preset) を使うことを示します; ほとんどのカーネルパッケージは /etc/mkinitcpio.d の中に関連する mkinitcpio プリセットファイルを提供しています (例: linux は /etc/mkinitcpio.d/linux.preset)。毎回設定ファイルと出力ファイルを指定する代わりに initramfs イメージを作成する方法がプリセットに定義されています。
ユーザーは他の設定ファイルを使ってイメージを手動で作成することもできます。例えば、以下のコマンドは /etc/mkinitcpio-custom.conf の指示に従って initramfs イメージを生成し /boot/linux-custom.img にイメージを保存します。
# mkinitcpio -c /etc/mkinitcpio-custom.conf -g /boot/linux-custom.img
現在動作しているカーネル以外のカーネル用のイメージを作成するときは、コマンドラインにカーネルのバージョンを加えて下さい。/usr/lib/modules 内でカーネルバージョンを見ることができます。
# mkinitcpio -g /boot/linux.img -k 3.3.0-ARCH
設定
mkinitcpio の基本の設定ファイルは /etc/mkinitcpio.conf です。さらに、カーネルパッケージによって提供されるプリセット定義が /etc/mkinitcpio.d ディレクトリにあります (例: /etc/mkinitcpio.d/linux.preset)。
ユーザーは設定ファイルの中にある6つの変数を修正することが可能です:
-
MODULES - boot フックが実行される前にロードするべきカーネルモジュール。
-
BINARIES - initramfs イメージに含める追加のバイナリ。
-
FILES - initramfs イメージに含める追加のファイル。
-
HOOKS - フックは initial ramdisk で実行するスクリプトです。
-
COMPRESSION - initramfs イメージを圧縮するのに使われます。
-
COMPRESSION_OPTIONS -
COMPRESSIONプログラムに渡すコマンドラインオプション。
MODULES
MODULES 行では何か他のことが行われる前にロードするモジュールを指定します。
モジュールの後ろに ? を付けると、モジュールが見つからなかったときでもエラーを投げません。これは、フックや設定ファイルにリストアップされたモジュールを含んでコンパイルするカスタムカーネルで有益かもしれません。
BINARIES と FILES
ユーザーはオプションを使ってイメージにファイルを追加することができます。BINARIES と FILES はフックが実行される前に追加され、フックによって使用されるファイルを上書きすることがあります。BINARIES は標準の PATH に自動で配置され、依存関係がパースされます。つまり必要なライブラリも全て追加されます。FILES はそのままの状態で (as-is) 追加されます。例:
FILES="/etc/modprobe.d/modprobe.conf"
BINARIES="kexec"
BINARIES と FILES のどちらでも、スペースで区切ることで複数のエントリを追加することができます。
HOOKS
HOOKS 設定は設定ファイルの中で一番重要な設定です。フックはイメージに追加されるものを記述する小さなスクリプトです。フックによっては、デーモンを起動したりスタックしたブロックデバイスを集めたりするためのランタイムコンポーネントを含んでいるものもあります。フックは名前によって参照され、設定ファイルの HOOKS にある順番通りに実行されます。
ほとんどのシンプルな、シングルディスクセットアップではデフォルトの HOOKS 設定で十分なはずです。LVM, mdadm, LUKS などスタックした root デバイスやマルチブロックデバイスを使う時は、関連する wiki ページを見て必要な設定を調べて下さい。
ビルドフック
ビルドフックは /usr/lib/initcpio/install にあります。これらのファイルは mkinitcpio の実行時に bash シェルによって実行されます。ビルドフックには2つの関数が含まれています: build と help。build 関数にはイメージに追加するモジュール・ファイル・バイナリを記述します。これらのアイテムの追加を容易にする mkinitcpio(8) による API が用意されています。help 関数はフックの役割を出力します。
すべてのフックを一覧するには:
$ mkinitcpio -L
特定のフックのヘルプを出力するには mkinitcpio の -H オプションを使って下さい、例えば:
$ mkinitcpio -H udev
ランタイムフック
ランタイムフックは /usr/lib/initcpio/hooks にあります。全てのランタイムフックには同じ名前のビルドフックが存在している必要があり、そのビルドフックは add_runscript をコールしてランタイムフックをイメージに追加します。これらのファイルは初期ユーザ空間で busybox の ash シェルによって実行されます。cleanup フックを除いて全てのフックは HOOKS 設定の順番で実行されます。ランタイムフックには複数の関数を含めることができます:
run_earlyhook: API ファイルシステムがマウントされカーネルコマンドラインがパースされたときに、この名前の関数が実行されます。一般的に udev など初期ユーザ空間が始まったときに必要になる追加のデーモンはこの関数に置きます。
run_hook: 初期のフックの後すぐに、この名前の関数が実行されます。一番普通のフックポイントで、スタックブロックデバイスのアセンブリなどのオペレーションはここで行なって下さい。
run_latehook: root デバイスがマウントされた後、この名前の関数が実行されます。root デバイスの後のセットアップや /usr など他のファイルシステムのマウントなどに控えめに使われます。
run_cleanuphook: 出来る限り最後に、この名前の関数が実行されます。設定ファイルの HOOKS の順番とは逆に実行されます。これらのフックは初期のフックによって起動したデーモンのシャットダウンなど、最後のクリーンアップに使われます。
通常のフック
通常のフックとイメージ生成への影響と関連するランタイムを示した表です。パッケージにカスタムフックを入れることができるので、この表は完全ではないことに注意してください。
| フック | インストール | ランタイム |
|---|---|---|
| base | 全ての initial directory をセットアップし基本のユーティリティとライブラリをインストールする。何をしようとしているのかわからない限りはこのフックを一番最初のフックにしておいてください。 | -- |
| udev | イメージに udevd, udevadm, そして udev ルールの小さなサブセットを追加します。 | Starts the udev daemon and processes uevents from the kernel; creating device nodes. As it simplifies the boot process by not requiring the user to explicitly specify necessary modules, using the udev hook is recommended. |
| autodetect | sysfs のスキャンによってモジュールのホワイトリストを作成して initramfs を小さくします。含まれているモジュールが正しく、欠けているものがないか確認してください。自動検知の利点を活かすためにこのフックは他の subsystem フックより前に実行してください。'autodetect' より前に置かれたフックは完全にインストールされます。 | -- |
| modconf | -- | /etc/modprobe.d と /usr/lib/modprobe.d から modprobe 設定ファイルをロードします。
|
| block | 全てのブロックデバイスモジュールを追加します。昔は fw, mmc, pata, sata, scsi , usb, virtio フックに分かれていました。 | -- |
| pcmcia | PCMCIA デバイスに必要なモジュールを追加します。これを使うには pcmciautils をインストールする必要があります。 | -- |
| net | ネットワークデバイスに必要なモジュールを追加します。PCMCIA net デバイスには pcmcia フックも追加してください。 | Provides handling for an NFS based root filesystem. |
| dmraid | fakeRAID root デバイスのサポートを提供します。これを使うには dmraid をインストールする必要があります。 | Locates and assembles fakeRAID block devices using mdassemble.
|
| mdadm | Provides support for assembling RAID arrays from /etc/mdadm.conf, or autodetection during boot. You must have mdadm installed to use this. The mdadm_udev hook is preferred over this hook. |
Locates and assembles software RAID block devices using mdassemble.
|
| mdadm_udev | Provides support for assembling RAID arrays via udev. You must have mdadm installed to use this. | Locates and assembles software RAID block devices using udev and mdadm incremental assembly. This is the preferred method of mdadm assembly (rather than using the above mdadm hook).
|
| keyboard | キーボードデバイスに必要なモジュールを追加します。あなたが USB キーボードを使っていて、それを初期ユーザ空間で使う必要があるとき (暗号化のパスフレーズを入力したりインタラクティブシェルを使う場合) はこのフックを使って下さい。副作用としてキーボード以外の入力デバイス用のモジュールが追加されることがありますが、困ることはないはずです。 | -- |
| keymap | /etc/vconsole.conf からキーマップとコンソールフォントを追加します。 |
初期ユーザ空間で /etc/vconsole.conf から指定されたキーマップとコンソールフォントをロードします。
|
| encrypt | dm_crypt カーネルモジュールと cryptsetup ツールをイメージに追加します。これを使うには cryptsetup をインストールする必要があります。 |
暗号化された root パーティションを検知してアンロックします。設定については #ランタイムのカスタマイズ を見て下さい。 |
| lvm2 | Adds the device mapper kernel module and the lvm tool to the image. You must have lvm2 installed to use this. |
Enables all LVM2 volume groups. This is necessary if you have your root filesystem on LVM. |
| fsck | Adds the fsck binary and filesystem-specific helpers. If added after the autodetect hook, only the helper specific to your root filesystem will be added. Usage of this hook is strongly recommended, and it is required with a separate /usr partition. |
Runs fsck against your root device (and /usr if separate) prior to mounting.
|
| resume | -- | "ハイバネート" (suspend to disk) 状態からの復帰を試みます。swsusp と suspend2 の両方と一緒に動作します。設定は #ランタイムのカスタマイズ を見て下さい。 |
| filesystems | イメージに必要なファイルシステムモジュールを含めます。ファイルシステムモジュールを MODULES で指定していない限りこのフックが必要になります。 | -- |
| shutdown | シャットダウン initramfs サポートを追加します。/usr パーティションを分割していたり root を暗号化しているときはこのフックを使うことを強く推奨します。 |
Unmounts and disassembles devices on shutdown. |
| usr | /usr の分割パーティションのサポートを追加。 |
Mounts the /usr partition after the real root has been mounted.
|
| timestamp | systemd-timestamp バイナリをイメージに追加します。初期ユーザ空間での RD_TIMESTAMP のサポートを提供します。RD_TIMESTAMP は起動時間を測定するのに systemd-analyze に読み込まれたりします。 |
-- |
廃止されたフック
mkinitcpio 0.13.0 では、usbinput フックは廃止され keyboard フックを使うようになりました。
mkinitcpio 0.12.0 では、以下のフックが廃止されました。以下のフックのどれかを使っているときは、block フックに置き換える必要があります。
fwmmcpatasatascsiusbvirtio
詳しくは、Git コミット 97368c0e78 や arch-projects mailing list を見て下さい。
COMPRESSION
カーネルは initramfs の圧縮に複数のフォーマットをサポートしています - gzip, bzip2, lzma, xz (別名 lzma2), lzo。ほとんどの場合、gzip か lzop で圧縮イメージサイズと展開速度のバランスが一番良くなります。
COMPRESSION="gzip" COMPRESSION="bzip2" COMPRESSION="lzma" COMPRESSION="lzop" COMPRESSION="xz"
COMPRESSION を指定しないときは initramfs ファイルは gzip で圧縮されます。イメージを圧縮したくないときは、COMPRESSION=cat を設定するかコマンドラインで -z cat を使って下さい。
COMPRESSION_OPTIONS
COMPRESSION で指定したプログラムに渡す追加のフラグを記述します、例:
COMPRESSION_OPTIONS='-9'
mkinitcpio はイメージを作成するときサポートされている圧縮方法に必要なフラグを渡すので、通常はこのオプションは必要ありません。
ランタイムのカスタマイズ
ランタイム設定オプションはカーネルコマンドラインを通して init や特定のフックに渡すことができます。カーネルコマンドラインパラメータはブートローダによって与えられることがほとんどです。下で書かれているオプションをカーネルコマンドラインに追加することでデフォルトの挙動を変えることができます。詳しくは Kernel parameters (日本語) や Arch Boot Process (日本語) を見て下さい。
init
-
root - This is the most important parameter specified on the kernel command line, as it determines what device will be mounted as your proper root device. mkinitcpio is flexible enough to allow a wide variety of formats, for example:
root=/dev/sda1 # /dev node root=LABEL=CorsairF80 # label root=UUID=ea1c4959-406c-45d0-a144-912f4e86b207 # UUID root=/dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0-part1 # udev symlink (requires the udev hook) root=801 # hex-encoded major/minor number
-
break - If
breakorbreak=premountis specified,initpauses the boot process (after loading hooks, but before mounting the root filesystem) and launches an interactive shell which can be used for troubleshooting purposes. This shell can be launched after the root has been mounted by specifyingbreak=postmount. Normal boot continues after exiting from the shell.
-
disablehooks - Disable hooks at runtime by adding
disablehooks=hook1{,hook2,...}. For example:disablehooks=resume
-
earlymodules - Alter the order in which modules are loaded by specifying modules to load early via
earlymodules=mod1{,mod2,...}. (This may be used, for example, to ensure the correct ordering of multiple network interfaces.)
-
rootdelay=N - Pause for
Nseconds before mounting the root file system by appendingrootdelay. (This may be used, for example, if booting from a USB hard drive that takes longer to initialize.)
参照: Debugging with GRUB and init
RAID を使う
まず、/etc/mkinitcpio.conf 内の HOOKS に mdadm フックを追加し、必要な RAID モジュール全て (raid456, ext4) を MODULES に追加して下さい。
カーネルパラメータ:
Using the mdadm hook, you no longer need to configure your RAID array in the GRUB parameters. The mdadm hook will either use your /etc/mdadm.conf file or automatically detect the array(s) during the init phase of boot.
Assembly via udev is also possible using the mdadm_udev hook. Upstream prefers this method of assembly. /etc/mdadm.conf will still be read for purposes of naming the assembled devices if it exists.
net を使う
必要なパッケージ:
net は公式リポジトリの mkinitcpio-nfs-utils パッケージを必要とします。
カーネルパラメータ:
ip=
An interface spec can be either short form, which is just the name of an interface (eth0 or whatever), or long form. ( Kernel Documentation )
The long form consists of up to seven elements, separated by colons:
ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf> nfsaddrs= is an alias to ip= and can be used too.
Parameter explanation:
<client-ip> IP address of the client. If empty, the address will
either be determined by RARP/BOOTP/DHCP. What protocol
is used depends on the <autoconf> parameter. If this
parameter is not empty, autoconf will be used.
<server-ip> IP address of the NFS server. If RARP is used to
determine the client address and this parameter is NOT
empty only replies from the specified server are
accepted. To use different RARP and NFS server,
specify your RARP server here (or leave it blank), and
specify your NFS server in the `nfsroot' parameter
(see above). If this entry is blank the address of the
server is used which answe
You can also get a more human-friendly listing of the important parts in the imagered the RARP/BOOTP/DHCP
request.
<gw-ip> IP address of a gateway if the server is on a different
subnet. If this entry is empty no gateway is used and the
server is assumed to be on the local network, unless a
value has been received by BOOTP/DHCP.
<netmask> Netmask for local network interface. If this is empty,
the netmask is derived from the client IP address assuming
classful addressing, unless overridden in BOOTP/DHCP reply.
<hostname> Name of the client. If empty, the client IP address is
used in ASCII notation, or the value received by
BOOTP/DHCP.
<device> Name of network device to use. If this is empty, all
devices are used for RARP/BOOTP/DHCP requests, and the
first one we receive a reply on is configured. If you
have only one device, you can safely leave this blank.
<autoconf> Method to use for autoconfiguration. If this is either
'rarp', 'bootp', or 'dhcp' the specified protocol is
used. If the value is 'both', 'all' or empty, all
protocols are used. 'off', 'static' or 'none' means
no autoconfiguration.
Examples:
ip=127.0.0.1:::::lo:none --> Enable the loopback interface. ip=192.168.1.1:::::eth2:none --> Enable static eth2 interface. ip=:::::eth0:dhcp --> Enable dhcp protocol for eth0 configuration.
BOOTIF=
If you have multiple network cards, this parameter can include the MAC address of the interface you are booting from. This is often useful as interface numbering may change, or in conjunction with pxelinux IPAPPEND 2 or IPAPPEND 3 option. If not given, eth0 will be used.
Example:
BOOTIF=01-A1-B2-C3-D4-E5-F6 # Note the prepended "01-" and capital letters.
nfsroot=
If the nfsroot parameter is NOT given on the command line, the default /tftpboot/%s will be used.
nfsroot=[<server-ip>:]<root-dir>[,<nfs-options>]
Parameter explanation:
<server-ip> Specifies the IP address of the NFS server. If this field
is not given, the default address as determined by the
`ip' variable (see below) is used. One use of this
parameter is for example to allow using different servers
for RARP and NFS. Usually you can leave this blank.
<root-dir> Name of the directory on the server to mount as root. If
there is a "%s" token in the string, the token will be
replaced by the ASCII-representation of the client's IP
address.
<nfs-options> Standard NFS options. All options are separated by commas.
If the options field is not given, the following defaults
will be used:
port = as given by server portmap daemon
rsize = 1024
wsize = 1024
timeo = 7
retrans = 3
acregmin = 3
acregmax = 60
acdirmin = 30
acdirmax = 60
flags = hard, nointr, noposix, cto, ac
root=/dev/nfs
If you do not use the nfsroot parameter, you need to set root=/dev/nfs to boot from an NFS root via automatic configuration.
LVM を使う
root デバイスを LVM 上に置いている場合は、lvm2 フックを追加する必要があります。カーネルコマンドラインで以下の形式を使って root デバイスのパスを通して下さい:
root=/dev/mapper/<volume group name>-<logical volume name>
例:
root=/dev/mapper/myvg-root
In addition, if your root device might initialize slowly (e.g. a USB device) and/or you receive a "volume group not found" error during boot, you might need to add the following to the kernel command line:
lvmwait=/dev/mapper/<volume group name><logical volume name>
for example:
lvmwait=/dev/mapper/myvg-root
This lets the boot process wait until LVM manages to make the device available.
暗号化された root を使う
あなたの root ボリュームが暗号化されている場合、encrypt フックを追加する必要があります。
For an encrypted root, use something similar to:
root=/dev/mapper/root cryptdevice=/dev/sda5:root
In this case, /dev/sda5 is the encrypted device, and we give it an arbitrary name of root, which means our root device, once unlocked, is mounted as /dev/mapper/root. On bootup, you will be prompted for the passphrase to unlock it.See LUKS#Configuration_of_initcpio for more details about using encrypted root.
/usr パーティションを分割する
If you keep /usr as a separate partition, you must adhere to the following requirements:
- Add the
shutdownhook. The shutdown process will pivot to a saved copy of the initramfs and allow for/usr(and root) to be properly unmounted from the VFS. - Add the
fsckhook, mark/usrwith apassnoof0in/etc/fstab. While recommended for everyone, it is mandatory if you want your/usrpartition to be fsck'ed at boot-up. Without this hook,/usrwill never be fsck'd. - Add the
usrhook. This will mount the/usrpartition after root is mounted. Prior to 0.9.0, mounting of/usrwould be automatic if it was found in the real root's/etc/fstab.
トラブルシューティング
イメージの展開
initrd イメージの中に何が入っているか気になるときは、イメージを展開して中に入っているファイルを調べることができます。
initrd イメージは SVR4 CPIO アーカイブであり、find と bsdcpio コマンドによって生成されています。また、任意でカーネルが対応した圧縮方法によって圧縮されています。圧縮方法について詳しくは #COMPRESSION を見て下さい。
mkinitcpio には lsinitcpio というユーティリティが含まれており initramfs イメージの中身を一覧・展開することができます。
イメージ内のファイルを一覧するには:
$ lsinitcpio /boot/initramfs-linux.img
作業ディレクトリにファイルを全て展開するには:
$ lsinitcpio -x /boot/initramfs-linux.img
イメージの重要なパーツについて読みやすいリストを取得することもできます:
$ lsinitcpio -a /boot/initramfs-linux.img
関連項目
- Boot Debugging - Debugging with GRUB