Gentoo をブータブル USB メモリにインストールする

From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Install Gentoo on a bootable USB stick and the translation is 80% complete.

USB メモリから Gentoo をブートするのは実に単純です。主な難関は、初期 ram ディスクをセットアップすることと、起動したいすべてのマシンのためのドライバを含めることです。この記事では、USB メモリに Gentoo をインストールして、どんなコンピュータでも起動できるようにする方法を解説します。このインストール作業は、通常のデスクトップへのインストール作業と似たものになり、変更点は変わらないでしょう。

注意
この記事は、USB ドライブ上に完全な機能する Gentoo システムをインストールすることついて取り扱っています。Gentoo のインストールなどに利用できるシンプルなブータブルドライブを作成する方法については、LiveUSB の記事をお読みください。

準備

注意
このガイドは、作業を行うプラットフォームが Gentoo Linux であることを仮定しています。しかし、作業の大部分は、どんなディストリビューションでも行えます。ソフトウェアをインストールする部分など、Gentoo 固有のやり方の部分は、そのディストリビューションのプラクティスに読み替えてください。

いくつか必要なパッケージをインストールするところから始めてください。それらのうち最重要なのは、カーネルと、sys-kernel/genkernel パッケージです:

root #emerge --ask sys-kernel/gentoo-sources sys-kernel/genkernel

カーネルをコンパイルする

カーネルに必要な変更を加えて、コンパイルしてください。このステップのさらなる詳細についてはカーネルの設定カーネルコンフィグガイドを確認してください。重要なこととして、汎用的であればあるほど、多くのマシンでブートできるので、ここでは汎用的なカーネルに設定するのがより適していることを覚えておいてください。

もちろん、カーネルの設定では、initramfs 中に含めてブート時にロードできるように、モジュールをコンパイルすることができます。

root #mkdir /tmp/boot
root #genkernel --firmware --bootdir=/tmp/boot --no-symlink --all-ramdisk-modules --install all

上のコマンドは、デフォルト設定のカーネルを構築し、ramdisk を作成し、すべてのファイルを /tmp/boot にコピーするため、しばらく時間がかかります。

root #ls /tmp/boot/
initramfs-genkernel-x86_64-3.5.2-gentoo
kernel-genkernel-x86_64-3.5.2-gentoo
System.map-genkernel-x86_64-3.5.2-gentoo

USB メモリを準備する

ドライブに 2 個のパーティションを作成してください (インストール後に /boot/ (ルート) パーティションができるようにすると仮定しています)。USB メモリは /dev/sdb にあると仮定しています (どのデバイスが使用されているかを確認するには、USB メモリを挿した直後に dmesg を起動してください)。

root #fdisk /dev/sdb
Command (m for help): d
Selected partition 1
 
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4001759, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4001759, default 4001759): +100M
 
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2):  
Using default value 2
First sector (206848-4001759, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-4001759, default 4001759): 
Using default value 4001759
 
Command (m for help): a
Partition number (1-4): 1
 
Command (m for help): p
 
Disk /dev/sdb: 2048 MB, 2048901120 bytes
255 heads, 63 sectors/track, 249 cylinders, total 4001760 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x001663df
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048      206847      102400   83  Linux
/dev/sdb2          206848     4001759     1897456   83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.

1 番目のパーティションがブート可能になっている (a コマンドは boot フラグを切り替えます) ことを確認してください。

次に新しいパーティションをフォーマットします。例では / パーティションに ext2 ファイルシステムを利用していますが、カーネルが対応していれば他のファイルシステムを利用することもできます。フォーマット操作の途中で、パーティションにラベルを適用していることに注意してください。これが重要なのは、ドライブレターの割り当てがまったく異なるシステムでも USB メモリをブートできるようにするために、後でルートファイルシステムを検出するための方法としてラベルを使うからです。

root #mkfs.ext2 -L GENTOO_USB_BOOT /dev/sdb1
root #mkfs.ext2 -L GENTOO_USB_ROOT /dev/sdb2

Gentoo stage 3 のインストール

カーネル、ramdisk、そしてファイルシステムの準備ができたら、最小構成のインストールを行います。これには最新の stage3 と Portage ツリーのスナップショットが含まれます。

この例では stage3-amd64-20120621.tar.bz2portage-latest.tar.xz を使用します。次の部分は Gentoo をインストールするためのハンドブックの内容と同じです。

root #mount /dev/sdb2 /mnt/gentoo
root #cd /mnt/gentoo
root #tar -xpf ~/Download/stage3-amd64-20120621.tar.bz2
root #cd usr
root #tar -xpf ~/Download/portage-latest.tar.xz
root #mount /dev/sdb1 /mnt/gentoo/boot
root #cp /tmp/boot/* /mnt/gentoo/boot

以降のセクションでは、Gentoo Linux インストール作業のうち、USB メモリへのインストールに固有の事項を扱っています。この記事の内容にも注意を払いつつ、Gentoo ハンドブックの手順を使ってインストールするのがおすすめです。

ブートローダ

新しいシステムをブートするために、USB メモリ上にブートローダをインストールしてください。

以下のセクションでは、GRUB を使う設定例と、lilosyslinux と組み合わせて使う設定例を提供します。

GRUB

警告
この方法は UEFI ブートにのみ適用できます!

GRUB を emerge してください (chroot の中で):

(chroot) #emerge sys-boot/grub

対象プラットフォームごとの対応する場所に GRUB をインストールしてください。実行中のマシンが、EFI パーティション/boot/efi にマウントされた x86_64 マシンであると仮定すると、次のコマンドで USB スティックに GRUB をインストールできるでしょう:

(chroot) #grub-install --efi-directory=/boot/efi --target=x86_64-efi --removable
警告
ここで --removable オプションはとても重要です。これはターゲットの場所がリムーバブルデバイスであることを示しています。これを忘れると自身のシステムがめちゃくちゃになるかもしれません!

問題がなければ、grub-mkconfig を実行して GRUB 設定を生成してください:

(chroot) #grub-mkconfig -o /boot/grub/grub.cfg

異なる GRUB 設定ディレクトリを使いたい場合は、それに応じてターゲットディレクトリを変更してください。

lilo

lilo を emerge してください (chroot の中で):

(chroot) #emerge sys-boot/lilo

Edit etc/lilo.conf so that it has the correct information in it.

警告
These settings are very important and will be discussed line by line.
ファイル /etc/lilo.confConfigure lilo.conf
boot=/dev/sdb              # The location of the USB Stick (currently)
lba32                      # use lba32 addressing (ignore)
compact                    # boot quickly by loading lots of blocks
                           # remove when there are problems with booting
prompt                     # Prompt for user input
timeout=20                 # Time to wait before default selection
default="Gentoo-352"       # Default selection after timeout
  
image=/boot/vmlinuz-5.10.76-gentoo-r1-x86_64
	label="Gentoo-352"
	read-only
	root=/dev/ram0
	append="root=LABEL=GENTOO_USB_ROOT scandelay=5"
	initrd=/boot/initramfs-5.10.76-gentoo-r1-x86_64.img

The lines after image are all easy but the append line contains some interesting options.

  • root=LABEL=GENTOO_USB_ROOT will use the label of the disk instead of /dev/sdb2 which is important because these device numbers move around depending on the number of hard disks in the computer or number of USB disk drives.
  • Also important for USB booting is the scandelay option, as USB devices need a little time to be detected by the kernel; that is what this option is for. When there are lots of modules booting takes a long time so it probably does not matter, but if there are only a few modules loaded then it is important, because the system could have booted before the kernel detects the USB device. Also, on newer machines with USB3 controllers it is necessary to add the xhci_hcd driver (built into the kernel or as a module).

Now install LILO (with the configuration) on the USB stick:

(chroot) #lilo
Warning: /dev/sdb is not on the first disk
Warning: The initial RAM disk is too big to fit between the kernel and
   the 15M-16M memory hole.  It will be loaded in the highest memory as
   though the configuration file specified "large-memory" and it will
   be assumed that the BIOS supports memory moves above 16M.
Added Gentoo-352 ? *
2 warnings were issued.

The first warning is to be expected as we are not installing the boot loader onto the hard disk in the computer. The second warning will only cause a problem on machines that are old; if it is a problem try cutting down the kernel by removing modules.

syslinux

警告
syslinux currently only works with ext{2,3,4}, btrfs, ntfs and fat filesystems.
(chroot) #emerge syslinux

Put the boot sector onto the USB stick.

(chroot) #dd bs=440 count=1 conv=notrunc if=/usr/share/syslinux/mbr.bin of=/dev/sdb

Now to configure the bootloader.

(chroot) #mkdir /boot/syslinux
(chroot) #nano -w /boot/syslinux/syslinux.cfg
ファイル /boot/syslinux/syslinux.cfg
PROMPT 1
TIMEOUT 50
DEFAULT gentoo
 
LABEL gentoo
        LINUX ../vmlinuz-5.10.76-gentoo-r1-x86_64
        APPEND root=LABEL=GENTOO_USB_ROOT scandelay=3 ro
        INITRD ../initramfs-5.10.76-gentoo-r1-x86_64.img
注意
The ro in the above APPEND line will cause the root partition to be mounted read-only (which is usually preferred on USB sticks). Replace ro with rw to allow the system to be modified from the boot onwards (instead of having the user mount the file system as read-write later).

Next install syslinux onto the USB stick. The --device /dev/sdb1 option is not absolutely necessary though.

(chroot) #extlinux --device /dev/sdb1 --install /boot/syslinux

fstab

ファイル /etc/fstabラベルを使って fstab を設定する
LABEL=GENTOO_USB_BOOT   /boot           ext2            noauto,noatime  1 2
LABEL=GENTOO_USB_ROOT   /               ext2            noatime         0 1
/dev/SWAP               none            swap            sw              0 0
/dev/cdrom              /mnt/cdrom      auto            noauto,ro       0 0
/dev/fd0                /mnt/floppy     auto            noauto          0 0

ヒントと小技

Although it is possible to use the system just made as a standard Gentoo system, it might be worthwhile to compile binary packages on a host and then installing them on the USB stick. Or if the system has enough memory just mount /var/tmp to a tmpfs, as compilation will be much quicker that way anyway!

関連項目

  • Installation — an overview of the principles and the practice of installing Gentoo on a running system.
  • LiveUSB — explains how to create a Gentoo LiveUSB or, in other words, how to emulate a x86 or amd64 Gentoo LiveCD using a USB drive.